Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationServerLLGS.cpp --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <errno.h> |
| 11 | |
| 12 | #include "lldb/Host/Config.h" |
| 13 | |
| 14 | #include "GDBRemoteCommunicationServerLLGS.h" |
| 15 | #include "lldb/Core/StreamGDBRemote.h" |
| 16 | |
| 17 | // C Includes |
| 18 | // C++ Includes |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 19 | #include <chrono> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include <cstring> |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
| 23 | // Other libraries and framework includes |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame] | 24 | #include "lldb/Core/DataBuffer.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Log.h" |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 26 | #include "lldb/Core/RegisterValue.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 27 | #include "lldb/Core/State.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 28 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 29 | #include "lldb/Host/Debug.h" |
| 30 | #include "lldb/Host/Endian.h" |
| 31 | #include "lldb/Host/File.h" |
| 32 | #include "lldb/Host/FileSystem.h" |
| 33 | #include "lldb/Host/Host.h" |
| 34 | #include "lldb/Host/HostInfo.h" |
| 35 | #include "lldb/Host/StringConvert.h" |
Pavel Labath | 1eb0d42 | 2016-08-08 12:54:36 +0000 | [diff] [blame] | 36 | #include "lldb/Host/common/NativeProcessProtocol.h" |
| 37 | #include "lldb/Host/common/NativeRegisterContext.h" |
| 38 | #include "lldb/Host/common/NativeThreadProtocol.h" |
| 39 | #include "lldb/Interpreter/Args.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 40 | #include "lldb/Target/FileAction.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 41 | #include "lldb/Target/MemoryRegionInfo.h" |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/JSON.h" |
Pavel Labath | abadc22 | 2015-11-27 13:33:29 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | 1eb0d42 | 2016-08-08 12:54:36 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Triple.h" |
| 46 | #include "llvm/Support/ScopedPrinter.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 47 | |
| 48 | // Project includes |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 49 | #include "ProcessGDBRemote.h" |
| 50 | #include "ProcessGDBRemoteLog.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | #include "Utility/StringExtractorGDBRemote.h" |
| 52 | #include "Utility/UriParser.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace lldb; |
| 55 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 56 | using namespace lldb_private::process_gdb_remote; |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 57 | using namespace llvm; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 58 | |
| 59 | //---------------------------------------------------------------------- |
| 60 | // GDBRemote Errors |
| 61 | //---------------------------------------------------------------------- |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | namespace { |
| 64 | enum GDBRemoteServerError { |
| 65 | // Set to the first unused error number in literal form below |
| 66 | eErrorFirst = 29, |
| 67 | eErrorNoProcess = eErrorFirst, |
| 68 | eErrorResume, |
| 69 | eErrorExitStatus |
| 70 | }; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | //---------------------------------------------------------------------- |
| 74 | // GDBRemoteCommunicationServerLLGS constructor |
| 75 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS( |
| 77 | MainLoop &mainloop) |
| 78 | : GDBRemoteCommunicationServerCommon("gdb-remote.server", |
| 79 | "gdb-remote.server.rx_packet"), |
| 80 | m_mainloop(mainloop), m_current_tid(LLDB_INVALID_THREAD_ID), |
| 81 | m_continue_tid(LLDB_INVALID_THREAD_ID), m_debugged_process_mutex(), |
| 82 | m_debugged_process_sp(), m_stdio_communication("process.stdio"), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 83 | m_inferior_prev_state(StateType::eStateInvalid), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | m_active_auxv_buffer_sp(), m_saved_registers_mutex(), |
| 85 | m_saved_registers_map(), m_next_saved_registers_id(1), |
| 86 | m_handshake_completed(false) { |
| 87 | RegisterPacketHandlers(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | void GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() { |
| 91 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C, |
| 92 | &GDBRemoteCommunicationServerLLGS::Handle_C); |
| 93 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c, |
| 94 | &GDBRemoteCommunicationServerLLGS::Handle_c); |
| 95 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D, |
| 96 | &GDBRemoteCommunicationServerLLGS::Handle_D); |
| 97 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H, |
| 98 | &GDBRemoteCommunicationServerLLGS::Handle_H); |
| 99 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I, |
| 100 | &GDBRemoteCommunicationServerLLGS::Handle_I); |
| 101 | RegisterMemberFunctionHandler( |
| 102 | StringExtractorGDBRemote::eServerPacketType_interrupt, |
| 103 | &GDBRemoteCommunicationServerLLGS::Handle_interrupt); |
| 104 | RegisterMemberFunctionHandler( |
| 105 | StringExtractorGDBRemote::eServerPacketType_m, |
| 106 | &GDBRemoteCommunicationServerLLGS::Handle_memory_read); |
| 107 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M, |
| 108 | &GDBRemoteCommunicationServerLLGS::Handle_M); |
| 109 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p, |
| 110 | &GDBRemoteCommunicationServerLLGS::Handle_p); |
| 111 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P, |
| 112 | &GDBRemoteCommunicationServerLLGS::Handle_P); |
| 113 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC, |
| 114 | &GDBRemoteCommunicationServerLLGS::Handle_qC); |
| 115 | RegisterMemberFunctionHandler( |
| 116 | StringExtractorGDBRemote::eServerPacketType_qfThreadInfo, |
| 117 | &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo); |
| 118 | RegisterMemberFunctionHandler( |
| 119 | StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress, |
| 120 | &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress); |
| 121 | RegisterMemberFunctionHandler( |
| 122 | StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir, |
| 123 | &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir); |
| 124 | RegisterMemberFunctionHandler( |
| 125 | StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo, |
| 126 | &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo); |
| 127 | RegisterMemberFunctionHandler( |
| 128 | StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported, |
| 129 | &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported); |
| 130 | RegisterMemberFunctionHandler( |
| 131 | StringExtractorGDBRemote::eServerPacketType_qProcessInfo, |
| 132 | &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo); |
| 133 | RegisterMemberFunctionHandler( |
| 134 | StringExtractorGDBRemote::eServerPacketType_qRegisterInfo, |
| 135 | &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo); |
| 136 | RegisterMemberFunctionHandler( |
| 137 | StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState, |
| 138 | &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState); |
| 139 | RegisterMemberFunctionHandler( |
| 140 | StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState, |
| 141 | &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState); |
| 142 | RegisterMemberFunctionHandler( |
| 143 | StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR, |
| 144 | &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR); |
| 145 | RegisterMemberFunctionHandler( |
| 146 | StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir, |
| 147 | &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir); |
| 148 | RegisterMemberFunctionHandler( |
| 149 | StringExtractorGDBRemote::eServerPacketType_qsThreadInfo, |
| 150 | &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo); |
| 151 | RegisterMemberFunctionHandler( |
| 152 | StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo, |
| 153 | &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo); |
| 154 | RegisterMemberFunctionHandler( |
| 155 | StringExtractorGDBRemote::eServerPacketType_jThreadsInfo, |
| 156 | &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo); |
| 157 | RegisterMemberFunctionHandler( |
| 158 | StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo, |
| 159 | &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo); |
| 160 | RegisterMemberFunctionHandler( |
| 161 | StringExtractorGDBRemote::eServerPacketType_qXfer_auxv_read, |
| 162 | &GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read); |
| 163 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s, |
| 164 | &GDBRemoteCommunicationServerLLGS::Handle_s); |
| 165 | RegisterMemberFunctionHandler( |
| 166 | StringExtractorGDBRemote::eServerPacketType_stop_reason, |
| 167 | &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ? |
| 168 | RegisterMemberFunctionHandler( |
| 169 | StringExtractorGDBRemote::eServerPacketType_vAttach, |
| 170 | &GDBRemoteCommunicationServerLLGS::Handle_vAttach); |
| 171 | RegisterMemberFunctionHandler( |
| 172 | StringExtractorGDBRemote::eServerPacketType_vCont, |
| 173 | &GDBRemoteCommunicationServerLLGS::Handle_vCont); |
| 174 | RegisterMemberFunctionHandler( |
| 175 | StringExtractorGDBRemote::eServerPacketType_vCont_actions, |
| 176 | &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions); |
| 177 | RegisterMemberFunctionHandler( |
| 178 | StringExtractorGDBRemote::eServerPacketType_x, |
| 179 | &GDBRemoteCommunicationServerLLGS::Handle_memory_read); |
| 180 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z, |
| 181 | &GDBRemoteCommunicationServerLLGS::Handle_Z); |
| 182 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z, |
| 183 | &GDBRemoteCommunicationServerLLGS::Handle_z); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k, |
| 186 | [this](StringExtractorGDBRemote packet, Error &error, |
| 187 | bool &interrupt, bool &quit) { |
| 188 | quit = true; |
| 189 | return this->Handle_k(packet); |
| 190 | }); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | Error GDBRemoteCommunicationServerLLGS::SetLaunchArguments( |
| 194 | const char *const args[], int argc) { |
| 195 | if ((argc < 1) || !args || !args[0] || !args[0][0]) |
| 196 | return Error("%s: no process command line specified to launch", |
| 197 | __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | m_process_launch_info.SetArguments(const_cast<const char **>(args), true); |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 200 | return Error(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | Error GDBRemoteCommunicationServerLLGS::SetLaunchFlags( |
| 204 | unsigned int launch_flags) { |
| 205 | m_process_launch_info.GetFlags().Set(launch_flags); |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 206 | return Error(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | Error GDBRemoteCommunicationServerLLGS::LaunchProcess() { |
| 210 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | if (!m_process_launch_info.GetArguments().GetArgumentCount()) |
| 213 | return Error("%s: no process command line specified to launch", |
| 214 | __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | const bool should_forward_stdio = |
| 217 | m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr || |
| 218 | m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr || |
| 219 | m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr; |
| 220 | m_process_launch_info.SetLaunchInSeparateProcessGroup(true); |
| 221 | m_process_launch_info.GetFlags().Set(eLaunchFlagDebug); |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | const bool default_to_use_pty = true; |
| 224 | m_process_launch_info.FinalizeFileActions(nullptr, default_to_use_pty); |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | Error error; |
| 227 | { |
| 228 | std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex); |
| 229 | assert(!m_debugged_process_sp && "lldb-server creating debugged " |
| 230 | "process but one already exists"); |
| 231 | error = NativeProcessProtocol::Launch(m_process_launch_info, *this, |
| 232 | m_mainloop, m_debugged_process_sp); |
| 233 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | if (!error.Success()) { |
| 236 | fprintf(stderr, "%s: failed to launch executable %s", __FUNCTION__, |
| 237 | m_process_launch_info.GetArguments().GetArgumentAtIndex(0)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 238 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol |
| 242 | // as needed. |
| 243 | // llgs local-process debugging may specify PTY paths, which will make these |
| 244 | // file actions non-null |
| 245 | // process launch -i/e/o will also make these file actions non-null |
| 246 | // nullptr means that the traffic is expected to flow over gdb-remote protocol |
| 247 | if (should_forward_stdio) { |
| 248 | // nullptr means it's not redirected to file or pty (in case of LLGS local) |
| 249 | // at least one of stdio will be transferred pty<->gdb-remote |
| 250 | // we need to give the pty master handle to this object to read and/or write |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 251 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | log->Printf( |
| 253 | "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 254 | " setting up stdout/stderr redirection via $O gdb-remote commands", |
| 255 | __FUNCTION__, m_debugged_process_sp->GetID()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | // Setup stdout/stderr mapping from inferior to $O |
| 258 | auto terminal_fd = m_debugged_process_sp->GetTerminalFileDescriptor(); |
| 259 | if (terminal_fd >= 0) { |
| 260 | if (log) |
| 261 | log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting " |
| 262 | "inferior STDIO fd to %d", |
| 263 | __FUNCTION__, terminal_fd); |
| 264 | error = SetSTDIOFileDescriptor(terminal_fd); |
| 265 | if (error.Fail()) |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 266 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | } else { |
| 268 | if (log) |
| 269 | log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring " |
| 270 | "inferior STDIO since terminal fd reported as %d", |
| 271 | __FUNCTION__, terminal_fd); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 272 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | } else { |
| 274 | if (log) |
| 275 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 276 | " skipping stdout/stderr redirection via $O: inferior will " |
| 277 | "communicate over client-provided file descriptors", |
| 278 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 279 | } |
Pavel Labath | 6e4f19d | 2015-07-29 12:33:31 +0000 | [diff] [blame] | 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | printf("Launched '%s' as process %" PRIu64 "...\n", |
| 282 | m_process_launch_info.GetArguments().GetArgumentAtIndex(0), |
| 283 | m_process_launch_info.GetProcessID()); |
Pavel Labath | 6e4f19d | 2015-07-29 12:33:31 +0000 | [diff] [blame] | 284 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 285 | return error; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | Error GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) { |
| 289 | Error error; |
| 290 | |
| 291 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 292 | if (log) |
| 293 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64, |
| 294 | __FUNCTION__, pid); |
| 295 | |
| 296 | // Before we try to attach, make sure we aren't already monitoring something |
| 297 | // else. |
| 298 | if (m_debugged_process_sp && |
| 299 | m_debugged_process_sp->GetID() != LLDB_INVALID_PROCESS_ID) |
| 300 | return Error("cannot attach to a process %" PRIu64 |
| 301 | " when another process with pid %" PRIu64 |
| 302 | " is being debugged.", |
| 303 | pid, m_debugged_process_sp->GetID()); |
| 304 | |
| 305 | // Try to attach. |
| 306 | error = NativeProcessProtocol::Attach(pid, *this, m_mainloop, |
| 307 | m_debugged_process_sp); |
| 308 | if (!error.Success()) { |
| 309 | fprintf(stderr, "%s: failed to attach to process %" PRIu64 ": %s", |
| 310 | __FUNCTION__, pid, error.AsCString()); |
| 311 | return error; |
| 312 | } |
| 313 | |
| 314 | // Setup stdout/stderr mapping from inferior. |
| 315 | auto terminal_fd = m_debugged_process_sp->GetTerminalFileDescriptor(); |
| 316 | if (terminal_fd >= 0) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 317 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting " |
| 319 | "inferior STDIO fd to %d", |
| 320 | __FUNCTION__, terminal_fd); |
| 321 | error = SetSTDIOFileDescriptor(terminal_fd); |
| 322 | if (error.Fail()) |
| 323 | return error; |
| 324 | } else { |
| 325 | if (log) |
| 326 | log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring " |
| 327 | "inferior STDIO since terminal fd reported as %d", |
| 328 | __FUNCTION__, terminal_fd); |
| 329 | } |
| 330 | |
| 331 | printf("Attached to process %" PRIu64 "...\n", pid); |
| 332 | |
| 333 | return error; |
| 334 | } |
| 335 | |
| 336 | void GDBRemoteCommunicationServerLLGS::InitializeDelegate( |
| 337 | NativeProcessProtocol *process) { |
| 338 | assert(process && "process cannot be NULL"); |
| 339 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 340 | if (log) { |
| 341 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called with " |
| 342 | "NativeProcessProtocol pid %" PRIu64 ", current state: %s", |
| 343 | __FUNCTION__, process->GetID(), |
| 344 | StateAsCString(process->GetState())); |
| 345 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | GDBRemoteCommunicationServerLLGS::SendWResponse( |
| 350 | NativeProcessProtocol *process) { |
| 351 | assert(process && "process cannot be NULL"); |
| 352 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | // send W notification |
| 355 | ExitType exit_type = ExitType::eExitTypeInvalid; |
| 356 | int return_code = 0; |
| 357 | std::string exit_description; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | const bool got_exit_info = |
| 360 | process->GetExitStatus(&exit_type, &return_code, exit_description); |
| 361 | if (!got_exit_info) { |
| 362 | if (log) |
| 363 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 364 | ", failed to retrieve process exit status", |
| 365 | __FUNCTION__, process->GetID()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | StreamGDBRemote response; |
| 368 | response.PutChar('E'); |
| 369 | response.PutHex8(GDBRemoteServerError::eErrorExitStatus); |
| 370 | return SendPacketNoLock(response.GetString()); |
| 371 | } else { |
| 372 | if (log) |
| 373 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 374 | ", returning exit type %d, return code %d [%s]", |
| 375 | __FUNCTION__, process->GetID(), exit_type, return_code, |
| 376 | exit_description.c_str()); |
| 377 | |
| 378 | StreamGDBRemote response; |
| 379 | |
| 380 | char return_type_code; |
| 381 | switch (exit_type) { |
| 382 | case ExitType::eExitTypeExit: |
| 383 | return_type_code = 'W'; |
| 384 | break; |
| 385 | case ExitType::eExitTypeSignal: |
| 386 | return_type_code = 'X'; |
| 387 | break; |
| 388 | case ExitType::eExitTypeStop: |
| 389 | return_type_code = 'S'; |
| 390 | break; |
| 391 | case ExitType::eExitTypeInvalid: |
| 392 | return_type_code = 'E'; |
| 393 | break; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 394 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | response.PutChar(return_type_code); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | // POSIX exit status limited to unsigned 8 bits. |
| 398 | response.PutHex8(return_code); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | return SendPacketNoLock(response.GetString()); |
| 401 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | static void AppendHexValue(StreamString &response, const uint8_t *buf, |
| 405 | uint32_t buf_size, bool swap) { |
| 406 | int64_t i; |
| 407 | if (swap) { |
| 408 | for (i = buf_size - 1; i >= 0; i--) |
| 409 | response.PutHex8(buf[i]); |
| 410 | } else { |
| 411 | for (i = 0; i < buf_size; i++) |
| 412 | response.PutHex8(buf[i]); |
| 413 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | static void WriteRegisterValueInHexFixedWidth( |
| 417 | StreamString &response, NativeRegisterContextSP ®_ctx_sp, |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 418 | const RegisterInfo ®_info, const RegisterValue *reg_value_p, |
| 419 | lldb::ByteOrder byte_order) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | RegisterValue reg_value; |
| 421 | if (!reg_value_p) { |
| 422 | Error error = reg_ctx_sp->ReadRegister(®_info, reg_value); |
| 423 | if (error.Success()) |
| 424 | reg_value_p = ®_value; |
| 425 | // else log. |
| 426 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | if (reg_value_p) { |
| 429 | AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(), |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 430 | reg_value_p->GetByteSize(), |
| 431 | byte_order == lldb::eByteOrderLittle); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | } else { |
| 433 | // Zero-out any unreadable values. |
| 434 | if (reg_info.byte_size > 0) { |
| 435 | std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0'); |
| 436 | AppendHexValue(response, zeros.data(), zeros.size(), false); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 437 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 441 | static JSONObject::SP GetRegistersAsJSON(NativeThreadProtocol &thread) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | NativeRegisterContextSP reg_ctx_sp = thread.GetRegisterContext(); |
| 445 | if (!reg_ctx_sp) |
| 446 | return nullptr; |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 447 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 448 | JSONObject::SP register_object_sp = std::make_shared<JSONObject>(); |
Pavel Labath | e4fc4ef | 2015-07-17 10:27:42 +0000 | [diff] [blame] | 449 | |
| 450 | #ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | // Expedite all registers in the first register set (i.e. should be GPRs) that |
| 452 | // are not contained in other registers. |
| 453 | const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0); |
| 454 | if (!reg_set_p) |
| 455 | return nullptr; |
| 456 | for (const uint32_t *reg_num_p = reg_set_p->registers; |
| 457 | *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) { |
| 458 | uint32_t reg_num = *reg_num_p; |
Pavel Labath | e4fc4ef | 2015-07-17 10:27:42 +0000 | [diff] [blame] | 459 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | // Expedite only a couple of registers until we figure out why sending |
| 461 | // registers is |
| 462 | // expensive. |
| 463 | static const uint32_t k_expedited_registers[] = { |
| 464 | LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP, |
| 465 | LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM}; |
Pavel Labath | c4645bb | 2015-10-26 16:25:28 +0000 | [diff] [blame] | 466 | |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 467 | for (const uint32_t *generic_reg_p = k_expedited_registers; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 468 | *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) { |
| 469 | uint32_t reg_num = reg_ctx_sp->ConvertRegisterKindToRegisterNumber( |
| 470 | eRegisterKindGeneric, *generic_reg_p); |
| 471 | if (reg_num == LLDB_INVALID_REGNUM) |
| 472 | continue; // Target does not support the given register. |
Pavel Labath | e4fc4ef | 2015-07-17 10:27:42 +0000 | [diff] [blame] | 473 | #endif |
| 474 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 475 | const RegisterInfo *const reg_info_p = |
| 476 | reg_ctx_sp->GetRegisterInfoAtIndex(reg_num); |
| 477 | if (reg_info_p == nullptr) { |
| 478 | if (log) |
| 479 | log->Printf( |
| 480 | "%s failed to get register info for register index %" PRIu32, |
| 481 | __FUNCTION__, reg_num); |
| 482 | continue; |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | if (reg_info_p->value_regs != nullptr) |
| 486 | continue; // Only expedite registers that are not contained in other |
| 487 | // registers. |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | RegisterValue reg_value; |
| 490 | Error error = reg_ctx_sp->ReadRegister(reg_info_p, reg_value); |
| 491 | if (error.Fail()) { |
| 492 | if (log) |
| 493 | log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s", |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame] | 494 | __FUNCTION__, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | reg_info_p->name ? reg_info_p->name : "<unnamed-register>", |
| 496 | reg_num, error.AsCString()); |
| 497 | continue; |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | StreamString stream; |
| 501 | WriteRegisterValueInHexFixedWidth(stream, reg_ctx_sp, *reg_info_p, |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 502 | ®_value, lldb::eByteOrderBig); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | |
| 504 | register_object_sp->SetObject( |
| 505 | llvm::to_string(reg_num), |
| 506 | std::make_shared<JSONString>(stream.GetString())); |
| 507 | } |
| 508 | |
| 509 | return register_object_sp; |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 512 | static const char *GetStopReasonString(StopReason stop_reason) { |
| 513 | switch (stop_reason) { |
| 514 | case eStopReasonTrace: |
| 515 | return "trace"; |
| 516 | case eStopReasonBreakpoint: |
| 517 | return "breakpoint"; |
| 518 | case eStopReasonWatchpoint: |
| 519 | return "watchpoint"; |
| 520 | case eStopReasonSignal: |
| 521 | return "signal"; |
| 522 | case eStopReasonException: |
| 523 | return "exception"; |
| 524 | case eStopReasonExec: |
| 525 | return "exec"; |
| 526 | case eStopReasonInstrumentation: |
| 527 | case eStopReasonInvalid: |
| 528 | case eStopReasonPlanComplete: |
| 529 | case eStopReasonThreadExiting: |
| 530 | case eStopReasonNone: |
| 531 | break; // ignored |
| 532 | } |
| 533 | return nullptr; |
| 534 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 535 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | static JSONArray::SP GetJSONThreadsInfo(NativeProcessProtocol &process, |
| 537 | bool abridged) { |
| 538 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | JSONArray::SP threads_array_sp = std::make_shared<JSONArray>(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 541 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | // Ensure we can get info on the given thread. |
| 543 | uint32_t thread_idx = 0; |
| 544 | for (NativeThreadProtocolSP thread_sp; |
| 545 | (thread_sp = process.GetThreadAtIndex(thread_idx)) != nullptr; |
| 546 | ++thread_idx) { |
| 547 | |
| 548 | lldb::tid_t tid = thread_sp->GetID(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 549 | |
| 550 | // Grab the reason this thread stopped. |
| 551 | struct ThreadStopInfo tid_stop_info; |
| 552 | std::string description; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 553 | if (!thread_sp->GetStopReason(tid_stop_info, description)) |
| 554 | return nullptr; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | const int signum = tid_stop_info.details.signal.signo; |
| 557 | if (log) { |
| 558 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 559 | " tid %" PRIu64 |
| 560 | " got signal signo = %d, reason = %d, exc_type = %" PRIu64, |
| 561 | __FUNCTION__, process.GetID(), tid, signum, |
| 562 | tid_stop_info.reason, tid_stop_info.details.exception.type); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>(); |
| 566 | threads_array_sp->AppendObject(thread_obj_sp); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 567 | |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 568 | if (!abridged) { |
| 569 | if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread_sp)) |
| 570 | thread_obj_sp->SetObject("registers", registers_sp); |
| 571 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 572 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | thread_obj_sp->SetObject("tid", std::make_shared<JSONNumber>(tid)); |
| 574 | if (signum != 0) |
| 575 | thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(signum)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 576 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | const std::string thread_name = thread_sp->GetName(); |
| 578 | if (!thread_name.empty()) |
| 579 | thread_obj_sp->SetObject("name", |
| 580 | std::make_shared<JSONString>(thread_name)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 581 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 582 | if (const char *stop_reason_str = GetStopReasonString(tid_stop_info.reason)) |
| 583 | thread_obj_sp->SetObject("reason", |
| 584 | std::make_shared<JSONString>(stop_reason_str)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 585 | |
| 586 | if (!description.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 587 | thread_obj_sp->SetObject("description", |
| 588 | std::make_shared<JSONString>(description)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 589 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | if ((tid_stop_info.reason == eStopReasonException) && |
| 591 | tid_stop_info.details.exception.type) { |
| 592 | thread_obj_sp->SetObject( |
| 593 | "metype", |
| 594 | std::make_shared<JSONNumber>(tid_stop_info.details.exception.type)); |
| 595 | |
| 596 | JSONArray::SP medata_array_sp = std::make_shared<JSONArray>(); |
| 597 | for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; |
| 598 | ++i) { |
| 599 | medata_array_sp->AppendObject(std::make_shared<JSONNumber>( |
| 600 | tid_stop_info.details.exception.data[i])); |
| 601 | } |
| 602 | thread_obj_sp->SetObject("medata", medata_array_sp); |
| 603 | } |
| 604 | |
| 605 | // TODO: Expedite interesting regions of inferior memory |
| 606 | } |
| 607 | |
| 608 | return threads_array_sp; |
| 609 | } |
| 610 | |
| 611 | GDBRemoteCommunication::PacketResult |
| 612 | GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread( |
| 613 | lldb::tid_t tid) { |
| 614 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 615 | |
| 616 | // Ensure we have a debugged process. |
| 617 | if (!m_debugged_process_sp || |
| 618 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) |
| 619 | return SendErrorResponse(50); |
| 620 | |
| 621 | if (log) |
| 622 | log->Printf( |
| 623 | "GDBRemoteCommunicationServerLLGS::%s preparing packet for pid %" PRIu64 |
| 624 | " tid %" PRIu64, |
| 625 | __FUNCTION__, m_debugged_process_sp->GetID(), tid); |
| 626 | |
| 627 | // Ensure we can get info on the given thread. |
| 628 | NativeThreadProtocolSP thread_sp(m_debugged_process_sp->GetThreadByID(tid)); |
| 629 | if (!thread_sp) |
| 630 | return SendErrorResponse(51); |
| 631 | |
| 632 | // Grab the reason this thread stopped. |
| 633 | struct ThreadStopInfo tid_stop_info; |
| 634 | std::string description; |
| 635 | if (!thread_sp->GetStopReason(tid_stop_info, description)) |
| 636 | return SendErrorResponse(52); |
| 637 | |
| 638 | // FIXME implement register handling for exec'd inferiors. |
| 639 | // if (tid_stop_info.reason == eStopReasonExec) |
| 640 | // { |
| 641 | // const bool force = true; |
| 642 | // InitializeRegisters(force); |
| 643 | // } |
| 644 | |
| 645 | StreamString response; |
| 646 | // Output the T packet with the thread |
| 647 | response.PutChar('T'); |
| 648 | int signum = tid_stop_info.details.signal.signo; |
| 649 | if (log) { |
| 650 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 651 | " tid %" PRIu64 |
| 652 | " got signal signo = %d, reason = %d, exc_type = %" PRIu64, |
| 653 | __FUNCTION__, m_debugged_process_sp->GetID(), tid, signum, |
| 654 | tid_stop_info.reason, tid_stop_info.details.exception.type); |
| 655 | } |
| 656 | |
| 657 | // Print the signal number. |
| 658 | response.PutHex8(signum & 0xff); |
| 659 | |
| 660 | // Include the tid. |
| 661 | response.Printf("thread:%" PRIx64 ";", tid); |
| 662 | |
| 663 | // Include the thread name if there is one. |
| 664 | const std::string thread_name = thread_sp->GetName(); |
| 665 | if (!thread_name.empty()) { |
| 666 | size_t thread_name_len = thread_name.length(); |
| 667 | |
| 668 | if (::strcspn(thread_name.c_str(), "$#+-;:") == thread_name_len) { |
| 669 | response.PutCString("name:"); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 670 | response.PutCString(thread_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 671 | } else { |
| 672 | // The thread name contains special chars, send as hex bytes. |
| 673 | response.PutCString("hexname:"); |
| 674 | response.PutCStringAsRawHex8(thread_name.c_str()); |
| 675 | } |
| 676 | response.PutChar(';'); |
| 677 | } |
| 678 | |
| 679 | // If a 'QListThreadsInStopReply' was sent to enable this feature, we |
| 680 | // will send all thread IDs back in the "threads" key whose value is |
| 681 | // a list of hex thread IDs separated by commas: |
| 682 | // "threads:10a,10b,10c;" |
| 683 | // This will save the debugger from having to send a pair of qfThreadInfo |
| 684 | // and qsThreadInfo packets, but it also might take a lot of room in the |
| 685 | // stop reply packet, so it must be enabled only on systems where there |
| 686 | // are no limits on packet lengths. |
| 687 | if (m_list_threads_in_stop_reply) { |
| 688 | response.PutCString("threads:"); |
| 689 | |
| 690 | uint32_t thread_index = 0; |
| 691 | NativeThreadProtocolSP listed_thread_sp; |
| 692 | for (listed_thread_sp = |
| 693 | m_debugged_process_sp->GetThreadAtIndex(thread_index); |
| 694 | listed_thread_sp; ++thread_index, |
| 695 | listed_thread_sp = m_debugged_process_sp->GetThreadAtIndex( |
| 696 | thread_index)) { |
| 697 | if (thread_index > 0) |
| 698 | response.PutChar(','); |
| 699 | response.Printf("%" PRIx64, listed_thread_sp->GetID()); |
| 700 | } |
| 701 | response.PutChar(';'); |
| 702 | |
| 703 | // Include JSON info that describes the stop reason for any threads |
| 704 | // that actually have stop reasons. We use the new "jstopinfo" key |
| 705 | // whose values is hex ascii JSON that contains the thread IDs |
| 706 | // thread stop info only for threads that have stop reasons. Only send |
| 707 | // this if we have more than one thread otherwise this packet has all |
| 708 | // the info it needs. |
| 709 | if (thread_index > 0) { |
| 710 | const bool threads_with_valid_stop_info_only = true; |
| 711 | JSONArray::SP threads_info_sp = GetJSONThreadsInfo( |
| 712 | *m_debugged_process_sp, threads_with_valid_stop_info_only); |
| 713 | if (threads_info_sp) { |
| 714 | response.PutCString("jstopinfo:"); |
| 715 | StreamString unescaped_response; |
| 716 | threads_info_sp->Write(unescaped_response); |
| 717 | response.PutCStringAsRawHex8(unescaped_response.GetData()); |
| 718 | response.PutChar(';'); |
| 719 | } else if (log) |
| 720 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to prepare a " |
| 721 | "jstopinfo field for pid %" PRIu64, |
| 722 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 723 | } |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 724 | |
| 725 | uint32_t i = 0; |
| 726 | response.PutCString("thread-pcs"); |
| 727 | char delimiter = ':'; |
| 728 | for (NativeThreadProtocolSP thread_sp; |
| 729 | (thread_sp = m_debugged_process_sp->GetThreadAtIndex(i)) != nullptr; |
| 730 | ++i) { |
| 731 | NativeRegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext(); |
| 732 | if (!reg_ctx_sp) |
| 733 | continue; |
| 734 | |
| 735 | uint32_t reg_to_read = reg_ctx_sp->ConvertRegisterKindToRegisterNumber( |
| 736 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 737 | const RegisterInfo *const reg_info_p = |
| 738 | reg_ctx_sp->GetRegisterInfoAtIndex(reg_to_read); |
| 739 | |
| 740 | RegisterValue reg_value; |
| 741 | Error error = reg_ctx_sp->ReadRegister(reg_info_p, reg_value); |
| 742 | if (error.Fail()) { |
| 743 | if (log) |
| 744 | log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s", |
| 745 | __FUNCTION__, |
| 746 | reg_info_p->name ? reg_info_p->name |
| 747 | : "<unnamed-register>", |
| 748 | reg_to_read, error.AsCString()); |
| 749 | continue; |
| 750 | } |
| 751 | |
| 752 | response.PutChar(delimiter); |
| 753 | delimiter = ','; |
| 754 | WriteRegisterValueInHexFixedWidth(response, reg_ctx_sp, *reg_info_p, |
| 755 | ®_value, endian::InlHostByteOrder()); |
| 756 | } |
| 757 | |
| 758 | response.PutChar(';'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | // |
| 762 | // Expedite registers. |
| 763 | // |
| 764 | |
| 765 | // Grab the register context. |
| 766 | NativeRegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext(); |
| 767 | if (reg_ctx_sp) { |
| 768 | // Expedite all registers in the first register set (i.e. should be GPRs) |
| 769 | // that are not contained in other registers. |
| 770 | const RegisterSet *reg_set_p; |
| 771 | if (reg_ctx_sp->GetRegisterSetCount() > 0 && |
| 772 | ((reg_set_p = reg_ctx_sp->GetRegisterSet(0)) != nullptr)) { |
| 773 | if (log) |
| 774 | log->Printf("GDBRemoteCommunicationServerLLGS::%s expediting registers " |
| 775 | "from set '%s' (registers set count: %zu)", |
| 776 | __FUNCTION__, |
| 777 | reg_set_p->name ? reg_set_p->name : "<unnamed-set>", |
| 778 | reg_set_p->num_registers); |
| 779 | |
| 780 | for (const uint32_t *reg_num_p = reg_set_p->registers; |
| 781 | *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) { |
| 782 | const RegisterInfo *const reg_info_p = |
| 783 | reg_ctx_sp->GetRegisterInfoAtIndex(*reg_num_p); |
| 784 | if (reg_info_p == nullptr) { |
| 785 | if (log) |
| 786 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get " |
| 787 | "register info for register set '%s', register index " |
| 788 | "%" PRIu32, |
| 789 | __FUNCTION__, |
| 790 | reg_set_p->name ? reg_set_p->name : "<unnamed-set>", |
| 791 | *reg_num_p); |
| 792 | } else if (reg_info_p->value_regs == nullptr) { |
| 793 | // Only expediate registers that are not contained in other registers. |
| 794 | RegisterValue reg_value; |
| 795 | Error error = reg_ctx_sp->ReadRegister(reg_info_p, reg_value); |
| 796 | if (error.Success()) { |
| 797 | response.Printf("%.02x:", *reg_num_p); |
| 798 | WriteRegisterValueInHexFixedWidth(response, reg_ctx_sp, *reg_info_p, |
Pavel Labath | e0a5b57 | 2017-01-20 14:17:16 +0000 | [diff] [blame] | 799 | ®_value, lldb::eByteOrderBig); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 800 | response.PutChar(';'); |
| 801 | } else { |
| 802 | if (log) |
| 803 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to read " |
| 804 | "register '%s' index %" PRIu32 ": %s", |
| 805 | __FUNCTION__, reg_info_p->name ? reg_info_p->name |
| 806 | : "<unnamed-register>", |
| 807 | *reg_num_p, error.AsCString()); |
| 808 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 809 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 810 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 811 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 812 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | const char *reason_str = GetStopReasonString(tid_stop_info.reason); |
| 815 | if (reason_str != nullptr) { |
| 816 | response.Printf("reason:%s;", reason_str); |
| 817 | } |
| 818 | |
| 819 | if (!description.empty()) { |
| 820 | // Description may contains special chars, send as hex bytes. |
| 821 | response.PutCString("description:"); |
| 822 | response.PutCStringAsRawHex8(description.c_str()); |
| 823 | response.PutChar(';'); |
| 824 | } else if ((tid_stop_info.reason == eStopReasonException) && |
| 825 | tid_stop_info.details.exception.type) { |
| 826 | response.PutCString("metype:"); |
| 827 | response.PutHex64(tid_stop_info.details.exception.type); |
| 828 | response.PutCString(";mecount:"); |
| 829 | response.PutHex32(tid_stop_info.details.exception.data_count); |
| 830 | response.PutChar(';'); |
| 831 | |
| 832 | for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) { |
| 833 | response.PutCString("medata:"); |
| 834 | response.PutHex64(tid_stop_info.details.exception.data[i]); |
| 835 | response.PutChar(';'); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited( |
| 843 | NativeProcessProtocol *process) { |
| 844 | assert(process && "process cannot be NULL"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 845 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 846 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 847 | if (log) |
| 848 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); |
| 849 | |
| 850 | PacketResult result = SendStopReasonForState(StateType::eStateExited); |
| 851 | if (result != PacketResult::Success) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 852 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 853 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop " |
| 854 | "notification for PID %" PRIu64 ", state: eStateExited", |
| 855 | __FUNCTION__, process->GetID()); |
| 856 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | // Close the pipe to the inferior terminal i/o if we launched it |
| 859 | // and set one up. |
| 860 | MaybeCloseInferiorTerminalConnection(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 861 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 862 | // We are ready to exit the debug monitor. |
| 863 | m_exit_now = true; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 866 | void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped( |
| 867 | NativeProcessProtocol *process) { |
| 868 | assert(process && "process cannot be NULL"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 869 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 870 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 871 | if (log) |
| 872 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | // Send the stop reason unless this is the stop after the |
| 875 | // launch or attach. |
| 876 | switch (m_inferior_prev_state) { |
| 877 | case eStateLaunching: |
| 878 | case eStateAttaching: |
| 879 | // Don't send anything per debugserver behavior. |
| 880 | break; |
| 881 | default: |
| 882 | // In all other cases, send the stop reason. |
| 883 | PacketResult result = SendStopReasonForState(StateType::eStateStopped); |
| 884 | if (result != PacketResult::Success) { |
| 885 | if (log) |
| 886 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop " |
| 887 | "notification for PID %" PRIu64 ", state: eStateExited", |
| 888 | __FUNCTION__, process->GetID()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 889 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 894 | void GDBRemoteCommunicationServerLLGS::ProcessStateChanged( |
| 895 | NativeProcessProtocol *process, lldb::StateType state) { |
| 896 | assert(process && "process cannot be NULL"); |
| 897 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 898 | if (log) { |
| 899 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called with " |
| 900 | "NativeProcessProtocol pid %" PRIu64 ", state: %s", |
| 901 | __FUNCTION__, process->GetID(), StateAsCString(state)); |
| 902 | } |
| 903 | |
| 904 | switch (state) { |
| 905 | case StateType::eStateRunning: |
| 906 | StartSTDIOForwarding(); |
| 907 | break; |
| 908 | |
| 909 | case StateType::eStateStopped: |
| 910 | // Make sure we get all of the pending stdout/stderr from the inferior |
| 911 | // and send it to the lldb host before we send the state change |
| 912 | // notification |
| 913 | SendProcessOutput(); |
| 914 | // Then stop the forwarding, so that any late output (see llvm.org/pr25652) |
| 915 | // does not |
| 916 | // interfere with our protocol. |
| 917 | StopSTDIOForwarding(); |
| 918 | HandleInferiorState_Stopped(process); |
| 919 | break; |
| 920 | |
| 921 | case StateType::eStateExited: |
| 922 | // Same as above |
| 923 | SendProcessOutput(); |
| 924 | StopSTDIOForwarding(); |
| 925 | HandleInferiorState_Exited(process); |
| 926 | break; |
| 927 | |
| 928 | default: |
| 929 | if (log) { |
| 930 | log->Printf("GDBRemoteCommunicationServerLLGS::%s didn't handle state " |
| 931 | "change for pid %" PRIu64 ", new state: %s", |
| 932 | __FUNCTION__, process->GetID(), StateAsCString(state)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 933 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 934 | break; |
| 935 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 936 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 937 | // Remember the previous state reported to us. |
| 938 | m_inferior_prev_state = state; |
| 939 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 940 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 941 | void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) { |
| 942 | ClearProcessSpecificData(); |
| 943 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 945 | void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() { |
| 946 | Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_COMM)); |
| 947 | |
| 948 | if (!m_handshake_completed) { |
| 949 | if (!HandshakeWithClient()) { |
| 950 | if (log) |
| 951 | log->Printf("GDBRemoteCommunicationServerLLGS::%s handshake with " |
| 952 | "client failed, exiting", |
| 953 | __FUNCTION__); |
| 954 | m_mainloop.RequestTermination(); |
| 955 | return; |
| 956 | } |
| 957 | m_handshake_completed = true; |
| 958 | } |
| 959 | |
| 960 | bool interrupt = false; |
| 961 | bool done = false; |
| 962 | Error error; |
| 963 | while (true) { |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 964 | const PacketResult result = GetPacketAndSendResponse( |
| 965 | std::chrono::microseconds(0), error, interrupt, done); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 966 | if (result == PacketResult::ErrorReplyTimeout) |
| 967 | break; // No more packets in the queue |
| 968 | |
| 969 | if ((result != PacketResult::Success)) { |
| 970 | if (log) |
| 971 | log->Printf("GDBRemoteCommunicationServerLLGS::%s processing a packet " |
| 972 | "failed: %s", |
| 973 | __FUNCTION__, error.AsCString()); |
| 974 | m_mainloop.RequestTermination(); |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | Error GDBRemoteCommunicationServerLLGS::InitializeConnection( |
| 981 | std::unique_ptr<Connection> &&connection) { |
| 982 | IOObjectSP read_object_sp = connection->GetReadObject(); |
| 983 | GDBRemoteCommunicationServer::SetConnection(connection.release()); |
| 984 | |
| 985 | Error error; |
| 986 | m_network_handle_up = m_mainloop.RegisterReadObject( |
| 987 | read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); }, |
| 988 | error); |
| 989 | return error; |
| 990 | } |
| 991 | |
| 992 | GDBRemoteCommunication::PacketResult |
| 993 | GDBRemoteCommunicationServerLLGS::SendONotification(const char *buffer, |
| 994 | uint32_t len) { |
| 995 | if ((buffer == nullptr) || (len == 0)) { |
| 996 | // Nothing to send. |
| 997 | return PacketResult::Success; |
| 998 | } |
| 999 | |
| 1000 | StreamString response; |
| 1001 | response.PutChar('O'); |
| 1002 | response.PutBytesAsRawHex8(buffer, len); |
| 1003 | |
| 1004 | return SendPacketNoLock(response.GetString()); |
| 1005 | } |
| 1006 | |
| 1007 | Error GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) { |
| 1008 | Error error; |
| 1009 | |
| 1010 | // Set up the reading/handling of process I/O |
| 1011 | std::unique_ptr<ConnectionFileDescriptor> conn_up( |
| 1012 | new ConnectionFileDescriptor(fd, true)); |
| 1013 | if (!conn_up) { |
| 1014 | error.SetErrorString("failed to create ConnectionFileDescriptor"); |
| 1015 | return error; |
| 1016 | } |
| 1017 | |
| 1018 | m_stdio_communication.SetCloseOnEOF(false); |
| 1019 | m_stdio_communication.SetConnection(conn_up.release()); |
| 1020 | if (!m_stdio_communication.IsConnected()) { |
| 1021 | error.SetErrorString( |
| 1022 | "failed to set connection for inferior I/O communication"); |
| 1023 | return error; |
| 1024 | } |
| 1025 | |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 1026 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() { |
| 1030 | // Don't forward if not connected (e.g. when attaching). |
| 1031 | if (!m_stdio_communication.IsConnected()) |
| 1032 | return; |
| 1033 | |
| 1034 | Error error; |
| 1035 | lldbassert(!m_stdio_handle_up); |
| 1036 | m_stdio_handle_up = m_mainloop.RegisterReadObject( |
| 1037 | m_stdio_communication.GetConnection()->GetReadObject(), |
| 1038 | [this](MainLoopBase &) { SendProcessOutput(); }, error); |
| 1039 | |
| 1040 | if (!m_stdio_handle_up) { |
| 1041 | // Not much we can do about the failure. Log it and continue without |
| 1042 | // forwarding. |
| 1043 | if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)) |
| 1044 | log->Printf("GDBRemoteCommunicationServerLLGS::%s Failed to set up stdio " |
| 1045 | "forwarding: %s", |
| 1046 | __FUNCTION__, error.AsCString()); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() { |
| 1051 | m_stdio_handle_up.reset(); |
| 1052 | } |
| 1053 | |
| 1054 | void GDBRemoteCommunicationServerLLGS::SendProcessOutput() { |
| 1055 | char buffer[1024]; |
| 1056 | ConnectionStatus status; |
| 1057 | Error error; |
| 1058 | while (true) { |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 1059 | size_t bytes_read = m_stdio_communication.Read( |
| 1060 | buffer, sizeof buffer, std::chrono::microseconds(0), status, &error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1061 | switch (status) { |
| 1062 | case eConnectionStatusSuccess: |
| 1063 | SendONotification(buffer, bytes_read); |
| 1064 | break; |
| 1065 | case eConnectionStatusLostConnection: |
| 1066 | case eConnectionStatusEndOfFile: |
| 1067 | case eConnectionStatusError: |
| 1068 | case eConnectionStatusNoConnection: |
| 1069 | if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)) |
| 1070 | log->Printf("GDBRemoteCommunicationServerLLGS::%s Stopping stdio " |
| 1071 | "forwarding as communication returned status %d (error: " |
| 1072 | "%s)", |
| 1073 | __FUNCTION__, status, error.AsCString()); |
| 1074 | m_stdio_handle_up.reset(); |
| 1075 | return; |
| 1076 | |
| 1077 | case eConnectionStatusInterrupted: |
| 1078 | case eConnectionStatusTimedOut: |
| 1079 | return; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | GDBRemoteCommunication::PacketResult |
| 1085 | GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo( |
| 1086 | StringExtractorGDBRemote &packet) { |
| 1087 | // Fail if we don't have a current process. |
| 1088 | if (!m_debugged_process_sp || |
| 1089 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) |
| 1090 | return SendErrorResponse(68); |
| 1091 | |
| 1092 | lldb::pid_t pid = m_debugged_process_sp->GetID(); |
| 1093 | |
| 1094 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 1095 | return SendErrorResponse(1); |
| 1096 | |
| 1097 | ProcessInstanceInfo proc_info; |
| 1098 | if (!Host::GetProcessInfo(pid, proc_info)) |
| 1099 | return SendErrorResponse(1); |
| 1100 | |
| 1101 | StreamString response; |
| 1102 | CreateProcessInfoResponse_DebugServerStyle(proc_info, response); |
| 1103 | return SendPacketNoLock(response.GetString()); |
| 1104 | } |
| 1105 | |
| 1106 | GDBRemoteCommunication::PacketResult |
| 1107 | GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) { |
| 1108 | // Fail if we don't have a current process. |
| 1109 | if (!m_debugged_process_sp || |
| 1110 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) |
| 1111 | return SendErrorResponse(68); |
| 1112 | |
| 1113 | // Make sure we set the current thread so g and p packets return |
| 1114 | // the data the gdb will expect. |
| 1115 | lldb::tid_t tid = m_debugged_process_sp->GetCurrentThreadID(); |
| 1116 | SetCurrentThreadID(tid); |
| 1117 | |
| 1118 | NativeThreadProtocolSP thread_sp = m_debugged_process_sp->GetCurrentThread(); |
| 1119 | if (!thread_sp) |
| 1120 | return SendErrorResponse(69); |
| 1121 | |
| 1122 | StreamString response; |
| 1123 | response.Printf("QC%" PRIx64, thread_sp->GetID()); |
| 1124 | |
| 1125 | return SendPacketNoLock(response.GetString()); |
| 1126 | } |
| 1127 | |
| 1128 | GDBRemoteCommunication::PacketResult |
| 1129 | GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) { |
| 1130 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1131 | |
| 1132 | StopSTDIOForwarding(); |
| 1133 | |
| 1134 | if (!m_debugged_process_sp) { |
| 1135 | if (log) |
| 1136 | log->Printf( |
| 1137 | "GDBRemoteCommunicationServerLLGS::%s No debugged process found.", |
| 1138 | __FUNCTION__); |
| 1139 | return PacketResult::Success; |
| 1140 | } |
| 1141 | |
| 1142 | Error error = m_debugged_process_sp->Kill(); |
| 1143 | if (error.Fail() && log) |
| 1144 | log->Printf("GDBRemoteCommunicationServerLLGS::%s Failed to kill debugged " |
| 1145 | "process %" PRIu64 ": %s", |
| 1146 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 1147 | error.AsCString()); |
| 1148 | |
| 1149 | // No OK response for kill packet. |
| 1150 | // return SendOKResponse (); |
| 1151 | return PacketResult::Success; |
| 1152 | } |
| 1153 | |
| 1154 | GDBRemoteCommunication::PacketResult |
| 1155 | GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR( |
| 1156 | StringExtractorGDBRemote &packet) { |
| 1157 | packet.SetFilePos(::strlen("QSetDisableASLR:")); |
| 1158 | if (packet.GetU32(0)) |
| 1159 | m_process_launch_info.GetFlags().Set(eLaunchFlagDisableASLR); |
| 1160 | else |
| 1161 | m_process_launch_info.GetFlags().Clear(eLaunchFlagDisableASLR); |
| 1162 | return SendOKResponse(); |
| 1163 | } |
| 1164 | |
| 1165 | GDBRemoteCommunication::PacketResult |
| 1166 | GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir( |
| 1167 | StringExtractorGDBRemote &packet) { |
| 1168 | packet.SetFilePos(::strlen("QSetWorkingDir:")); |
| 1169 | std::string path; |
| 1170 | packet.GetHexByteString(path); |
| 1171 | m_process_launch_info.SetWorkingDirectory(FileSpec{path, true}); |
| 1172 | return SendOKResponse(); |
| 1173 | } |
| 1174 | |
| 1175 | GDBRemoteCommunication::PacketResult |
| 1176 | GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir( |
| 1177 | StringExtractorGDBRemote &packet) { |
| 1178 | FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()}; |
| 1179 | if (working_dir) { |
| 1180 | StreamString response; |
| 1181 | response.PutCStringAsRawHex8(working_dir.GetCString()); |
| 1182 | return SendPacketNoLock(response.GetString()); |
| 1183 | } |
| 1184 | |
| 1185 | return SendErrorResponse(14); |
| 1186 | } |
| 1187 | |
| 1188 | GDBRemoteCommunication::PacketResult |
| 1189 | GDBRemoteCommunicationServerLLGS::Handle_C(StringExtractorGDBRemote &packet) { |
| 1190 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 1191 | if (log) |
| 1192 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); |
| 1193 | |
| 1194 | // Ensure we have a native process. |
| 1195 | if (!m_debugged_process_sp) { |
| 1196 | if (log) |
| 1197 | log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process " |
| 1198 | "shared pointer", |
| 1199 | __FUNCTION__); |
| 1200 | return SendErrorResponse(0x36); |
| 1201 | } |
| 1202 | |
| 1203 | // Pull out the signal number. |
| 1204 | packet.SetFilePos(::strlen("C")); |
| 1205 | if (packet.GetBytesLeft() < 1) { |
| 1206 | // Shouldn't be using a C without a signal. |
| 1207 | return SendIllFormedResponse(packet, "C packet specified without signal."); |
| 1208 | } |
| 1209 | const uint32_t signo = |
| 1210 | packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); |
| 1211 | if (signo == std::numeric_limits<uint32_t>::max()) |
| 1212 | return SendIllFormedResponse(packet, "failed to parse signal number"); |
| 1213 | |
| 1214 | // Handle optional continue address. |
| 1215 | if (packet.GetBytesLeft() > 0) { |
| 1216 | // FIXME add continue at address support for $C{signo}[;{continue-address}]. |
| 1217 | if (*packet.Peek() == ';') |
| 1218 | return SendUnimplementedResponse(packet.GetStringRef().c_str()); |
| 1219 | else |
| 1220 | return SendIllFormedResponse( |
| 1221 | packet, "unexpected content after $C{signal-number}"); |
| 1222 | } |
| 1223 | |
| 1224 | ResumeActionList resume_actions(StateType::eStateRunning, 0); |
| 1225 | Error error; |
| 1226 | |
| 1227 | // We have two branches: what to do if a continue thread is specified (in |
| 1228 | // which case we target |
| 1229 | // sending the signal to that thread), or when we don't have a continue thread |
| 1230 | // set (in which |
| 1231 | // case we send a signal to the process). |
| 1232 | |
| 1233 | // TODO discuss with Greg Clayton, make sure this makes sense. |
| 1234 | |
| 1235 | lldb::tid_t signal_tid = GetContinueThreadID(); |
| 1236 | if (signal_tid != LLDB_INVALID_THREAD_ID) { |
| 1237 | // The resume action for the continue thread (or all threads if a continue |
| 1238 | // thread is not set). |
| 1239 | ResumeAction action = {GetContinueThreadID(), StateType::eStateRunning, |
| 1240 | static_cast<int>(signo)}; |
| 1241 | |
| 1242 | // Add the action for the continue thread (or all threads when the continue |
| 1243 | // thread isn't present). |
| 1244 | resume_actions.Append(action); |
| 1245 | } else { |
| 1246 | // Send the signal to the process since we weren't targeting a specific |
| 1247 | // continue thread with the signal. |
| 1248 | error = m_debugged_process_sp->Signal(signo); |
| 1249 | if (error.Fail()) { |
| 1250 | if (log) |
| 1251 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send " |
| 1252 | "signal for process %" PRIu64 ": %s", |
| 1253 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 1254 | error.AsCString()); |
| 1255 | |
| 1256 | return SendErrorResponse(0x52); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // Resume the threads. |
| 1261 | error = m_debugged_process_sp->Resume(resume_actions); |
| 1262 | if (error.Fail()) { |
| 1263 | if (log) |
| 1264 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to resume " |
| 1265 | "threads for process %" PRIu64 ": %s", |
| 1266 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 1267 | error.AsCString()); |
| 1268 | |
| 1269 | return SendErrorResponse(0x38); |
| 1270 | } |
| 1271 | |
| 1272 | // Don't send an "OK" packet; response is the stopped/exited message. |
| 1273 | return PacketResult::Success; |
| 1274 | } |
| 1275 | |
| 1276 | GDBRemoteCommunication::PacketResult |
| 1277 | GDBRemoteCommunicationServerLLGS::Handle_c(StringExtractorGDBRemote &packet) { |
| 1278 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 1279 | if (log) |
| 1280 | log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); |
| 1281 | |
| 1282 | packet.SetFilePos(packet.GetFilePos() + ::strlen("c")); |
| 1283 | |
| 1284 | // For now just support all continue. |
| 1285 | const bool has_continue_address = (packet.GetBytesLeft() > 0); |
| 1286 | if (has_continue_address) { |
| 1287 | if (log) |
| 1288 | log->Printf("GDBRemoteCommunicationServerLLGS::%s not implemented for " |
| 1289 | "c{address} variant [%s remains]", |
| 1290 | __FUNCTION__, packet.Peek()); |
| 1291 | return SendUnimplementedResponse(packet.GetStringRef().c_str()); |
| 1292 | } |
| 1293 | |
| 1294 | // Ensure we have a native process. |
| 1295 | if (!m_debugged_process_sp) { |
| 1296 | if (log) |
| 1297 | log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process " |
| 1298 | "shared pointer", |
| 1299 | __FUNCTION__); |
| 1300 | return SendErrorResponse(0x36); |
| 1301 | } |
| 1302 | |
| 1303 | // Build the ResumeActionList |
| 1304 | ResumeActionList actions(StateType::eStateRunning, 0); |
| 1305 | |
| 1306 | Error error = m_debugged_process_sp->Resume(actions); |
| 1307 | if (error.Fail()) { |
| 1308 | if (log) { |
| 1309 | log->Printf( |
| 1310 | "GDBRemoteCommunicationServerLLGS::%s c failed for process %" PRIu64 |
| 1311 | ": %s", |
| 1312 | __FUNCTION__, m_debugged_process_sp->GetID(), error.AsCString()); |
| 1313 | } |
| 1314 | return SendErrorResponse(GDBRemoteServerError::eErrorResume); |
| 1315 | } |
| 1316 | |
| 1317 | if (log) |
| 1318 | log->Printf( |
| 1319 | "GDBRemoteCommunicationServerLLGS::%s continued process %" PRIu64, |
| 1320 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 1321 | |
| 1322 | // No response required from continue. |
| 1323 | return PacketResult::Success; |
| 1324 | } |
| 1325 | |
| 1326 | GDBRemoteCommunication::PacketResult |
| 1327 | GDBRemoteCommunicationServerLLGS::Handle_vCont_actions( |
| 1328 | StringExtractorGDBRemote &packet) { |
| 1329 | StreamString response; |
| 1330 | response.Printf("vCont;c;C;s;S"); |
| 1331 | |
| 1332 | return SendPacketNoLock(response.GetString()); |
| 1333 | } |
| 1334 | |
| 1335 | GDBRemoteCommunication::PacketResult |
| 1336 | GDBRemoteCommunicationServerLLGS::Handle_vCont( |
| 1337 | StringExtractorGDBRemote &packet) { |
| 1338 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1339 | if (log) |
| 1340 | log->Printf("GDBRemoteCommunicationServerLLGS::%s handling vCont packet", |
| 1341 | __FUNCTION__); |
| 1342 | |
| 1343 | packet.SetFilePos(::strlen("vCont")); |
| 1344 | |
| 1345 | if (packet.GetBytesLeft() == 0) { |
| 1346 | if (log) |
| 1347 | log->Printf("GDBRemoteCommunicationServerLLGS::%s missing action from " |
| 1348 | "vCont package", |
| 1349 | __FUNCTION__); |
| 1350 | return SendIllFormedResponse(packet, "Missing action from vCont package"); |
| 1351 | } |
| 1352 | |
| 1353 | // Check if this is all continue (no options or ";c"). |
| 1354 | if (::strcmp(packet.Peek(), ";c") == 0) { |
| 1355 | // Move past the ';', then do a simple 'c'. |
| 1356 | packet.SetFilePos(packet.GetFilePos() + 1); |
| 1357 | return Handle_c(packet); |
| 1358 | } else if (::strcmp(packet.Peek(), ";s") == 0) { |
| 1359 | // Move past the ';', then do a simple 's'. |
| 1360 | packet.SetFilePos(packet.GetFilePos() + 1); |
| 1361 | return Handle_s(packet); |
| 1362 | } |
| 1363 | |
| 1364 | // Ensure we have a native process. |
| 1365 | if (!m_debugged_process_sp) { |
| 1366 | if (log) |
| 1367 | log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process " |
| 1368 | "shared pointer", |
| 1369 | __FUNCTION__); |
| 1370 | return SendErrorResponse(0x36); |
| 1371 | } |
| 1372 | |
| 1373 | ResumeActionList thread_actions; |
| 1374 | |
| 1375 | while (packet.GetBytesLeft() && *packet.Peek() == ';') { |
| 1376 | // Skip the semi-colon. |
| 1377 | packet.GetChar(); |
| 1378 | |
| 1379 | // Build up the thread action. |
| 1380 | ResumeAction thread_action; |
| 1381 | thread_action.tid = LLDB_INVALID_THREAD_ID; |
| 1382 | thread_action.state = eStateInvalid; |
| 1383 | thread_action.signal = 0; |
| 1384 | |
| 1385 | const char action = packet.GetChar(); |
| 1386 | switch (action) { |
| 1387 | case 'C': |
| 1388 | thread_action.signal = packet.GetHexMaxU32(false, 0); |
| 1389 | if (thread_action.signal == 0) |
| 1390 | return SendIllFormedResponse( |
| 1391 | packet, "Could not parse signal in vCont packet C action"); |
| 1392 | LLVM_FALLTHROUGH; |
| 1393 | |
| 1394 | case 'c': |
| 1395 | // Continue |
| 1396 | thread_action.state = eStateRunning; |
| 1397 | break; |
| 1398 | |
| 1399 | case 'S': |
| 1400 | thread_action.signal = packet.GetHexMaxU32(false, 0); |
| 1401 | if (thread_action.signal == 0) |
| 1402 | return SendIllFormedResponse( |
| 1403 | packet, "Could not parse signal in vCont packet S action"); |
| 1404 | LLVM_FALLTHROUGH; |
| 1405 | |
| 1406 | case 's': |
| 1407 | // Step |
| 1408 | thread_action.state = eStateStepping; |
| 1409 | break; |
Pavel Labath | abadc22 | 2015-11-27 13:33:29 +0000 | [diff] [blame] | 1410 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1411 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1412 | return SendIllFormedResponse(packet, "Unsupported vCont action"); |
| 1413 | break; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | // Parse out optional :{thread-id} value. |
| 1417 | if (packet.GetBytesLeft() && (*packet.Peek() == ':')) { |
| 1418 | // Consume the separator. |
| 1419 | packet.GetChar(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1421 | thread_action.tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); |
| 1422 | if (thread_action.tid == LLDB_INVALID_THREAD_ID) |
| 1423 | return SendIllFormedResponse( |
| 1424 | packet, "Could not parse thread number in vCont packet"); |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1427 | thread_actions.Append(thread_action); |
| 1428 | } |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1430 | Error error = m_debugged_process_sp->Resume(thread_actions); |
| 1431 | if (error.Fail()) { |
| 1432 | if (log) { |
| 1433 | log->Printf("GDBRemoteCommunicationServerLLGS::%s vCont failed for " |
| 1434 | "process %" PRIu64 ": %s", |
| 1435 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 1436 | error.AsCString()); |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1437 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1438 | return SendErrorResponse(GDBRemoteServerError::eErrorResume); |
| 1439 | } |
| 1440 | |
| 1441 | if (log) |
| 1442 | log->Printf( |
| 1443 | "GDBRemoteCommunicationServerLLGS::%s continued process %" PRIu64, |
| 1444 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 1445 | |
| 1446 | // No response required from vCont. |
| 1447 | return PacketResult::Success; |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1450 | void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) { |
| 1451 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1452 | if (log) |
| 1453 | log->Printf("GDBRemoteCommunicationServerLLGS::%s setting current thread " |
| 1454 | "id to %" PRIu64, |
| 1455 | __FUNCTION__, tid); |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1456 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1457 | m_current_tid = tid; |
| 1458 | if (m_debugged_process_sp) |
| 1459 | m_debugged_process_sp->SetCurrentThreadID(m_current_tid); |
| 1460 | } |
| 1461 | |
| 1462 | void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) { |
| 1463 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1464 | if (log) |
| 1465 | log->Printf("GDBRemoteCommunicationServerLLGS::%s setting continue thread " |
| 1466 | "id to %" PRIu64, |
| 1467 | __FUNCTION__, tid); |
| 1468 | |
| 1469 | m_continue_tid = tid; |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1472 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | GDBRemoteCommunicationServerLLGS::Handle_stop_reason( |
| 1474 | StringExtractorGDBRemote &packet) { |
| 1475 | // Handle the $? gdbremote command. |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1477 | // If no process, indicate error |
| 1478 | if (!m_debugged_process_sp) |
| 1479 | return SendErrorResponse(02); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1480 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1481 | return SendStopReasonForState(m_debugged_process_sp->GetState()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1485 | GDBRemoteCommunicationServerLLGS::SendStopReasonForState( |
| 1486 | lldb::StateType process_state) { |
| 1487 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1489 | switch (process_state) { |
| 1490 | case eStateAttaching: |
| 1491 | case eStateLaunching: |
| 1492 | case eStateRunning: |
| 1493 | case eStateStepping: |
| 1494 | case eStateDetached: |
| 1495 | // NOTE: gdb protocol doc looks like it should return $OK |
| 1496 | // when everything is running (i.e. no stopped result). |
| 1497 | return PacketResult::Success; // Ignore |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1499 | case eStateSuspended: |
| 1500 | case eStateStopped: |
| 1501 | case eStateCrashed: { |
| 1502 | lldb::tid_t tid = m_debugged_process_sp->GetCurrentThreadID(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1503 | // Make sure we set the current thread so g and p packets return |
| 1504 | // the data the gdb will expect. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1505 | SetCurrentThreadID(tid); |
| 1506 | return SendStopReplyPacketForThread(tid); |
| 1507 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1509 | case eStateInvalid: |
| 1510 | case eStateUnloaded: |
| 1511 | case eStateExited: |
| 1512 | return SendWResponse(m_debugged_process_sp.get()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1513 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1514 | default: |
| 1515 | if (log) { |
| 1516 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 1517 | ", current state reporting not handled: %s", |
| 1518 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 1519 | StateAsCString(process_state)); |
Pavel Labath | 424b201 | 2015-07-28 09:06:56 +0000 | [diff] [blame] | 1520 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | } |
Pavel Labath | 424b201 | 2015-07-28 09:06:56 +0000 | [diff] [blame] | 1523 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1524 | return SendErrorResponse(0); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1528 | GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo( |
| 1529 | StringExtractorGDBRemote &packet) { |
| 1530 | // Fail if we don't have a current process. |
| 1531 | if (!m_debugged_process_sp || |
| 1532 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) |
| 1533 | return SendErrorResponse(68); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1535 | // Ensure we have a thread. |
| 1536 | NativeThreadProtocolSP thread_sp(m_debugged_process_sp->GetThreadAtIndex(0)); |
| 1537 | if (!thread_sp) |
| 1538 | return SendErrorResponse(69); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1540 | // Get the register context for the first thread. |
| 1541 | NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext()); |
| 1542 | if (!reg_context_sp) |
| 1543 | return SendErrorResponse(69); |
| 1544 | |
| 1545 | // Parse out the register number from the request. |
| 1546 | packet.SetFilePos(strlen("qRegisterInfo")); |
| 1547 | const uint32_t reg_index = |
| 1548 | packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); |
| 1549 | if (reg_index == std::numeric_limits<uint32_t>::max()) |
| 1550 | return SendErrorResponse(69); |
| 1551 | |
| 1552 | // Return the end of registers response if we've iterated one past the end of |
| 1553 | // the register set. |
| 1554 | if (reg_index >= reg_context_sp->GetUserRegisterCount()) |
| 1555 | return SendErrorResponse(69); |
| 1556 | |
| 1557 | const RegisterInfo *reg_info = |
| 1558 | reg_context_sp->GetRegisterInfoAtIndex(reg_index); |
| 1559 | if (!reg_info) |
| 1560 | return SendErrorResponse(69); |
| 1561 | |
| 1562 | // Build the reginfos response. |
| 1563 | StreamGDBRemote response; |
| 1564 | |
| 1565 | response.PutCString("name:"); |
| 1566 | response.PutCString(reg_info->name); |
| 1567 | response.PutChar(';'); |
| 1568 | |
| 1569 | if (reg_info->alt_name && reg_info->alt_name[0]) { |
| 1570 | response.PutCString("alt-name:"); |
| 1571 | response.PutCString(reg_info->alt_name); |
| 1572 | response.PutChar(';'); |
| 1573 | } |
| 1574 | |
| 1575 | response.Printf("bitsize:%" PRIu32 ";offset:%" PRIu32 ";", |
| 1576 | reg_info->byte_size * 8, reg_info->byte_offset); |
| 1577 | |
| 1578 | switch (reg_info->encoding) { |
| 1579 | case eEncodingUint: |
| 1580 | response.PutCString("encoding:uint;"); |
| 1581 | break; |
| 1582 | case eEncodingSint: |
| 1583 | response.PutCString("encoding:sint;"); |
| 1584 | break; |
| 1585 | case eEncodingIEEE754: |
| 1586 | response.PutCString("encoding:ieee754;"); |
| 1587 | break; |
| 1588 | case eEncodingVector: |
| 1589 | response.PutCString("encoding:vector;"); |
| 1590 | break; |
| 1591 | default: |
| 1592 | break; |
| 1593 | } |
| 1594 | |
| 1595 | switch (reg_info->format) { |
| 1596 | case eFormatBinary: |
| 1597 | response.PutCString("format:binary;"); |
| 1598 | break; |
| 1599 | case eFormatDecimal: |
| 1600 | response.PutCString("format:decimal;"); |
| 1601 | break; |
| 1602 | case eFormatHex: |
| 1603 | response.PutCString("format:hex;"); |
| 1604 | break; |
| 1605 | case eFormatFloat: |
| 1606 | response.PutCString("format:float;"); |
| 1607 | break; |
| 1608 | case eFormatVectorOfSInt8: |
| 1609 | response.PutCString("format:vector-sint8;"); |
| 1610 | break; |
| 1611 | case eFormatVectorOfUInt8: |
| 1612 | response.PutCString("format:vector-uint8;"); |
| 1613 | break; |
| 1614 | case eFormatVectorOfSInt16: |
| 1615 | response.PutCString("format:vector-sint16;"); |
| 1616 | break; |
| 1617 | case eFormatVectorOfUInt16: |
| 1618 | response.PutCString("format:vector-uint16;"); |
| 1619 | break; |
| 1620 | case eFormatVectorOfSInt32: |
| 1621 | response.PutCString("format:vector-sint32;"); |
| 1622 | break; |
| 1623 | case eFormatVectorOfUInt32: |
| 1624 | response.PutCString("format:vector-uint32;"); |
| 1625 | break; |
| 1626 | case eFormatVectorOfFloat32: |
| 1627 | response.PutCString("format:vector-float32;"); |
| 1628 | break; |
Valentina Giusti | cda0ae4 | 2016-09-08 14:16:45 +0000 | [diff] [blame] | 1629 | case eFormatVectorOfUInt64: |
| 1630 | response.PutCString("format:vector-uint64;"); |
| 1631 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1632 | case eFormatVectorOfUInt128: |
| 1633 | response.PutCString("format:vector-uint128;"); |
| 1634 | break; |
| 1635 | default: |
| 1636 | break; |
| 1637 | }; |
| 1638 | |
| 1639 | const char *const register_set_name = |
| 1640 | reg_context_sp->GetRegisterSetNameForRegisterAtIndex(reg_index); |
| 1641 | if (register_set_name) { |
| 1642 | response.PutCString("set:"); |
| 1643 | response.PutCString(register_set_name); |
| 1644 | response.PutChar(';'); |
| 1645 | } |
| 1646 | |
| 1647 | if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] != |
| 1648 | LLDB_INVALID_REGNUM) |
| 1649 | response.Printf("ehframe:%" PRIu32 ";", |
| 1650 | reg_info->kinds[RegisterKind::eRegisterKindEHFrame]); |
| 1651 | |
| 1652 | if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM) |
| 1653 | response.Printf("dwarf:%" PRIu32 ";", |
| 1654 | reg_info->kinds[RegisterKind::eRegisterKindDWARF]); |
| 1655 | |
| 1656 | switch (reg_info->kinds[RegisterKind::eRegisterKindGeneric]) { |
| 1657 | case LLDB_REGNUM_GENERIC_PC: |
| 1658 | response.PutCString("generic:pc;"); |
| 1659 | break; |
| 1660 | case LLDB_REGNUM_GENERIC_SP: |
| 1661 | response.PutCString("generic:sp;"); |
| 1662 | break; |
| 1663 | case LLDB_REGNUM_GENERIC_FP: |
| 1664 | response.PutCString("generic:fp;"); |
| 1665 | break; |
| 1666 | case LLDB_REGNUM_GENERIC_RA: |
| 1667 | response.PutCString("generic:ra;"); |
| 1668 | break; |
| 1669 | case LLDB_REGNUM_GENERIC_FLAGS: |
| 1670 | response.PutCString("generic:flags;"); |
| 1671 | break; |
| 1672 | case LLDB_REGNUM_GENERIC_ARG1: |
| 1673 | response.PutCString("generic:arg1;"); |
| 1674 | break; |
| 1675 | case LLDB_REGNUM_GENERIC_ARG2: |
| 1676 | response.PutCString("generic:arg2;"); |
| 1677 | break; |
| 1678 | case LLDB_REGNUM_GENERIC_ARG3: |
| 1679 | response.PutCString("generic:arg3;"); |
| 1680 | break; |
| 1681 | case LLDB_REGNUM_GENERIC_ARG4: |
| 1682 | response.PutCString("generic:arg4;"); |
| 1683 | break; |
| 1684 | case LLDB_REGNUM_GENERIC_ARG5: |
| 1685 | response.PutCString("generic:arg5;"); |
| 1686 | break; |
| 1687 | case LLDB_REGNUM_GENERIC_ARG6: |
| 1688 | response.PutCString("generic:arg6;"); |
| 1689 | break; |
| 1690 | case LLDB_REGNUM_GENERIC_ARG7: |
| 1691 | response.PutCString("generic:arg7;"); |
| 1692 | break; |
| 1693 | case LLDB_REGNUM_GENERIC_ARG8: |
| 1694 | response.PutCString("generic:arg8;"); |
| 1695 | break; |
| 1696 | default: |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) { |
| 1701 | response.PutCString("container-regs:"); |
| 1702 | int i = 0; |
| 1703 | for (const uint32_t *reg_num = reg_info->value_regs; |
| 1704 | *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) { |
| 1705 | if (i > 0) |
| 1706 | response.PutChar(','); |
| 1707 | response.Printf("%" PRIx32, *reg_num); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1708 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1709 | response.PutChar(';'); |
| 1710 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1712 | if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) { |
| 1713 | response.PutCString("invalidate-regs:"); |
| 1714 | int i = 0; |
| 1715 | for (const uint32_t *reg_num = reg_info->invalidate_regs; |
| 1716 | *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) { |
| 1717 | if (i > 0) |
| 1718 | response.PutChar(','); |
| 1719 | response.Printf("%" PRIx32, *reg_num); |
| 1720 | } |
| 1721 | response.PutChar(';'); |
| 1722 | } |
| 1723 | |
| 1724 | if (reg_info->dynamic_size_dwarf_expr_bytes) { |
| 1725 | const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len; |
| 1726 | response.PutCString("dynamic_size_dwarf_expr_bytes:"); |
| 1727 | for (uint32_t i = 0; i < dwarf_opcode_len; ++i) |
| 1728 | response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]); |
| 1729 | response.PutChar(';'); |
| 1730 | } |
| 1731 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1735 | GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo( |
| 1736 | StringExtractorGDBRemote &packet) { |
| 1737 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1738 | |
| 1739 | // Fail if we don't have a current process. |
| 1740 | if (!m_debugged_process_sp || |
| 1741 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1742 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1743 | log->Printf("GDBRemoteCommunicationServerLLGS::%s() no process (%s), " |
| 1744 | "returning OK", |
| 1745 | __FUNCTION__, |
| 1746 | m_debugged_process_sp ? "invalid process id" |
| 1747 | : "null m_debugged_process_sp"); |
| 1748 | return SendOKResponse(); |
| 1749 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1751 | StreamGDBRemote response; |
| 1752 | response.PutChar('m'); |
| 1753 | |
| 1754 | if (log) |
| 1755 | log->Printf( |
| 1756 | "GDBRemoteCommunicationServerLLGS::%s() starting thread iteration", |
| 1757 | __FUNCTION__); |
| 1758 | |
| 1759 | NativeThreadProtocolSP thread_sp; |
| 1760 | uint32_t thread_index; |
| 1761 | for (thread_index = 0, |
| 1762 | thread_sp = m_debugged_process_sp->GetThreadAtIndex(thread_index); |
| 1763 | thread_sp; ++thread_index, |
| 1764 | thread_sp = m_debugged_process_sp->GetThreadAtIndex(thread_index)) { |
| 1765 | if (log) |
| 1766 | log->Printf( |
| 1767 | "GDBRemoteCommunicationServerLLGS::%s() iterated thread %" PRIu32 |
| 1768 | "(%s, tid=0x%" PRIx64 ")", |
| 1769 | __FUNCTION__, thread_index, thread_sp ? "is not null" : "null", |
| 1770 | thread_sp ? thread_sp->GetID() : LLDB_INVALID_THREAD_ID); |
| 1771 | if (thread_index > 0) |
| 1772 | response.PutChar(','); |
| 1773 | response.Printf("%" PRIx64, thread_sp->GetID()); |
| 1774 | } |
| 1775 | |
| 1776 | if (log) |
| 1777 | log->Printf( |
| 1778 | "GDBRemoteCommunicationServerLLGS::%s() finished thread iteration", |
| 1779 | __FUNCTION__); |
| 1780 | |
| 1781 | return SendPacketNoLock(response.GetString()); |
| 1782 | } |
| 1783 | |
| 1784 | GDBRemoteCommunication::PacketResult |
| 1785 | GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo( |
| 1786 | StringExtractorGDBRemote &packet) { |
| 1787 | // FIXME for now we return the full thread list in the initial packet and |
| 1788 | // always do nothing here. |
| 1789 | return SendPacketNoLock("l"); |
| 1790 | } |
| 1791 | |
| 1792 | GDBRemoteCommunication::PacketResult |
| 1793 | GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) { |
| 1794 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1795 | |
| 1796 | // Parse out the register number from the request. |
| 1797 | packet.SetFilePos(strlen("p")); |
| 1798 | const uint32_t reg_index = |
| 1799 | packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); |
| 1800 | if (reg_index == std::numeric_limits<uint32_t>::max()) { |
| 1801 | if (log) |
| 1802 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not " |
| 1803 | "parse register number from request \"%s\"", |
| 1804 | __FUNCTION__, packet.GetStringRef().c_str()); |
| 1805 | return SendErrorResponse(0x15); |
| 1806 | } |
| 1807 | |
| 1808 | // Get the thread to use. |
| 1809 | NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet); |
| 1810 | if (!thread_sp) { |
| 1811 | if (log) |
| 1812 | log->Printf( |
| 1813 | "GDBRemoteCommunicationServerLLGS::%s failed, no thread available", |
| 1814 | __FUNCTION__); |
| 1815 | return SendErrorResponse(0x15); |
| 1816 | } |
| 1817 | |
| 1818 | // Get the thread's register context. |
| 1819 | NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext()); |
| 1820 | if (!reg_context_sp) { |
| 1821 | if (log) |
| 1822 | log->Printf( |
| 1823 | "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 |
| 1824 | " failed, no register context available for the thread", |
| 1825 | __FUNCTION__, m_debugged_process_sp->GetID(), thread_sp->GetID()); |
| 1826 | return SendErrorResponse(0x15); |
| 1827 | } |
| 1828 | |
| 1829 | // Return the end of registers response if we've iterated one past the end of |
| 1830 | // the register set. |
| 1831 | if (reg_index >= reg_context_sp->GetUserRegisterCount()) { |
| 1832 | if (log) |
| 1833 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested " |
| 1834 | "register %" PRIu32 " beyond register count %" PRIu32, |
| 1835 | __FUNCTION__, reg_index, |
| 1836 | reg_context_sp->GetUserRegisterCount()); |
| 1837 | return SendErrorResponse(0x15); |
| 1838 | } |
| 1839 | |
| 1840 | const RegisterInfo *reg_info = |
| 1841 | reg_context_sp->GetRegisterInfoAtIndex(reg_index); |
| 1842 | if (!reg_info) { |
| 1843 | if (log) |
| 1844 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested " |
| 1845 | "register %" PRIu32 " returned NULL", |
| 1846 | __FUNCTION__, reg_index); |
| 1847 | return SendErrorResponse(0x15); |
| 1848 | } |
| 1849 | |
| 1850 | // Build the reginfos response. |
| 1851 | StreamGDBRemote response; |
| 1852 | |
| 1853 | // Retrieve the value |
| 1854 | RegisterValue reg_value; |
| 1855 | Error error = reg_context_sp->ReadRegister(reg_info, reg_value); |
| 1856 | if (error.Fail()) { |
| 1857 | if (log) |
| 1858 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, read of " |
| 1859 | "requested register %" PRIu32 " (%s) failed: %s", |
| 1860 | __FUNCTION__, reg_index, reg_info->name, error.AsCString()); |
| 1861 | return SendErrorResponse(0x15); |
| 1862 | } |
| 1863 | |
| 1864 | const uint8_t *const data = |
| 1865 | reinterpret_cast<const uint8_t *>(reg_value.GetBytes()); |
| 1866 | if (!data) { |
| 1867 | if (log) |
| 1868 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get data " |
| 1869 | "bytes from requested register %" PRIu32, |
| 1870 | __FUNCTION__, reg_index); |
| 1871 | return SendErrorResponse(0x15); |
| 1872 | } |
| 1873 | |
| 1874 | // FIXME flip as needed to get data in big/little endian format for this host. |
| 1875 | for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i) |
| 1876 | response.PutHex8(data[i]); |
| 1877 | |
| 1878 | return SendPacketNoLock(response.GetString()); |
| 1879 | } |
| 1880 | |
| 1881 | GDBRemoteCommunication::PacketResult |
| 1882 | GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) { |
| 1883 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1884 | |
| 1885 | // Ensure there is more content. |
| 1886 | if (packet.GetBytesLeft() < 1) |
| 1887 | return SendIllFormedResponse(packet, "Empty P packet"); |
| 1888 | |
| 1889 | // Parse out the register number from the request. |
| 1890 | packet.SetFilePos(strlen("P")); |
| 1891 | const uint32_t reg_index = |
| 1892 | packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); |
| 1893 | if (reg_index == std::numeric_limits<uint32_t>::max()) { |
| 1894 | if (log) |
| 1895 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not " |
| 1896 | "parse register number from request \"%s\"", |
| 1897 | __FUNCTION__, packet.GetStringRef().c_str()); |
| 1898 | return SendErrorResponse(0x29); |
| 1899 | } |
| 1900 | |
| 1901 | // Note debugserver would send an E30 here. |
| 1902 | if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '=')) |
| 1903 | return SendIllFormedResponse( |
| 1904 | packet, "P packet missing '=' char after register number"); |
| 1905 | |
| 1906 | // Get process architecture. |
| 1907 | ArchSpec process_arch; |
| 1908 | if (!m_debugged_process_sp || |
| 1909 | !m_debugged_process_sp->GetArchitecture(process_arch)) { |
| 1910 | if (log) |
| 1911 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to retrieve " |
| 1912 | "inferior architecture", |
| 1913 | __FUNCTION__); |
| 1914 | return SendErrorResponse(0x49); |
| 1915 | } |
| 1916 | |
| 1917 | // Parse out the value. |
| 1918 | uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register |
| 1919 | size_t reg_size = packet.GetHexBytesAvail(reg_bytes); |
| 1920 | |
| 1921 | // Get the thread to use. |
| 1922 | NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet); |
| 1923 | if (!thread_sp) { |
| 1924 | if (log) |
| 1925 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread " |
| 1926 | "available (thread index 0)", |
| 1927 | __FUNCTION__); |
| 1928 | return SendErrorResponse(0x28); |
| 1929 | } |
| 1930 | |
| 1931 | // Get the thread's register context. |
| 1932 | NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext()); |
| 1933 | if (!reg_context_sp) { |
| 1934 | if (log) |
| 1935 | log->Printf( |
| 1936 | "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 |
| 1937 | " failed, no register context available for the thread", |
| 1938 | __FUNCTION__, m_debugged_process_sp->GetID(), thread_sp->GetID()); |
| 1939 | return SendErrorResponse(0x15); |
| 1940 | } |
| 1941 | |
| 1942 | const RegisterInfo *reg_info = |
| 1943 | reg_context_sp->GetRegisterInfoAtIndex(reg_index); |
| 1944 | if (!reg_info) { |
| 1945 | if (log) |
| 1946 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested " |
| 1947 | "register %" PRIu32 " returned NULL", |
| 1948 | __FUNCTION__, reg_index); |
| 1949 | return SendErrorResponse(0x48); |
| 1950 | } |
| 1951 | |
| 1952 | // Return the end of registers response if we've iterated one past the end of |
| 1953 | // the register set. |
| 1954 | if (reg_index >= reg_context_sp->GetUserRegisterCount()) { |
| 1955 | if (log) |
| 1956 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested " |
| 1957 | "register %" PRIu32 " beyond register count %" PRIu32, |
| 1958 | __FUNCTION__, reg_index, |
| 1959 | reg_context_sp->GetUserRegisterCount()); |
| 1960 | return SendErrorResponse(0x47); |
| 1961 | } |
| 1962 | |
| 1963 | // The dwarf expression are evaluate on host site |
| 1964 | // which may cause register size to change |
| 1965 | // Hence the reg_size may not be same as reg_info->bytes_size |
| 1966 | if ((reg_size != reg_info->byte_size) && |
| 1967 | !(reg_info->dynamic_size_dwarf_expr_bytes)) { |
| 1968 | return SendIllFormedResponse(packet, "P packet register size is incorrect"); |
| 1969 | } |
| 1970 | |
| 1971 | // Build the reginfos response. |
| 1972 | StreamGDBRemote response; |
| 1973 | |
| 1974 | RegisterValue reg_value(reg_bytes, reg_size, process_arch.GetByteOrder()); |
| 1975 | Error error = reg_context_sp->WriteRegister(reg_info, reg_value); |
| 1976 | if (error.Fail()) { |
| 1977 | if (log) |
| 1978 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, write of " |
| 1979 | "requested register %" PRIu32 " (%s) failed: %s", |
| 1980 | __FUNCTION__, reg_index, reg_info->name, error.AsCString()); |
| 1981 | return SendErrorResponse(0x32); |
| 1982 | } |
| 1983 | |
| 1984 | return SendOKResponse(); |
| 1985 | } |
| 1986 | |
| 1987 | GDBRemoteCommunication::PacketResult |
| 1988 | GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) { |
| 1989 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 1990 | |
| 1991 | // Fail if we don't have a current process. |
| 1992 | if (!m_debugged_process_sp || |
| 1993 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 1994 | if (log) |
| 1995 | log->Printf( |
| 1996 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 1997 | __FUNCTION__); |
| 1998 | return SendErrorResponse(0x15); |
| 1999 | } |
| 2000 | |
| 2001 | // Parse out which variant of $H is requested. |
| 2002 | packet.SetFilePos(strlen("H")); |
| 2003 | if (packet.GetBytesLeft() < 1) { |
| 2004 | if (log) |
| 2005 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, H command " |
| 2006 | "missing {g,c} variant", |
| 2007 | __FUNCTION__); |
| 2008 | return SendIllFormedResponse(packet, "H command missing {g,c} variant"); |
| 2009 | } |
| 2010 | |
| 2011 | const char h_variant = packet.GetChar(); |
| 2012 | switch (h_variant) { |
| 2013 | case 'g': |
| 2014 | break; |
| 2015 | |
| 2016 | case 'c': |
| 2017 | break; |
| 2018 | |
| 2019 | default: |
| 2020 | if (log) |
| 2021 | log->Printf( |
| 2022 | "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c", |
| 2023 | __FUNCTION__, h_variant); |
| 2024 | return SendIllFormedResponse(packet, |
| 2025 | "H variant unsupported, should be c or g"); |
| 2026 | } |
| 2027 | |
| 2028 | // Parse out the thread number. |
| 2029 | // FIXME return a parse success/fail value. All values are valid here. |
| 2030 | const lldb::tid_t tid = |
| 2031 | packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max()); |
| 2032 | |
| 2033 | // Ensure we have the given thread when not specifying -1 (all threads) or 0 |
| 2034 | // (any thread). |
| 2035 | if (tid != LLDB_INVALID_THREAD_ID && tid != 0) { |
| 2036 | NativeThreadProtocolSP thread_sp(m_debugged_process_sp->GetThreadByID(tid)); |
| 2037 | if (!thread_sp) { |
| 2038 | if (log) |
| 2039 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64 |
| 2040 | " not found", |
| 2041 | __FUNCTION__, tid); |
| 2042 | return SendErrorResponse(0x15); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2043 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2044 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2046 | // Now switch the given thread type. |
| 2047 | switch (h_variant) { |
| 2048 | case 'g': |
| 2049 | SetCurrentThreadID(tid); |
| 2050 | break; |
| 2051 | |
| 2052 | case 'c': |
| 2053 | SetContinueThreadID(tid); |
| 2054 | break; |
| 2055 | |
| 2056 | default: |
| 2057 | assert(false && "unsupported $H variant - shouldn't get here"); |
| 2058 | return SendIllFormedResponse(packet, |
| 2059 | "H variant unsupported, should be c or g"); |
| 2060 | } |
| 2061 | |
| 2062 | return SendOKResponse(); |
| 2063 | } |
| 2064 | |
| 2065 | GDBRemoteCommunication::PacketResult |
| 2066 | GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) { |
| 2067 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
| 2068 | |
| 2069 | // Fail if we don't have a current process. |
| 2070 | if (!m_debugged_process_sp || |
| 2071 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2072 | if (log) |
| 2073 | log->Printf( |
| 2074 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2075 | __FUNCTION__); |
| 2076 | return SendErrorResponse(0x15); |
| 2077 | } |
| 2078 | |
| 2079 | packet.SetFilePos(::strlen("I")); |
| 2080 | uint8_t tmp[4096]; |
| 2081 | for (;;) { |
| 2082 | size_t read = packet.GetHexBytesAvail(tmp); |
| 2083 | if (read == 0) { |
| 2084 | break; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2085 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2086 | // write directly to stdin *this might block if stdin buffer is full* |
| 2087 | // TODO: enqueue this block in circular buffer and send window size to |
| 2088 | // remote host |
| 2089 | ConnectionStatus status; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2090 | Error error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2091 | m_stdio_communication.Write(tmp, read, status, &error); |
| 2092 | if (error.Fail()) { |
| 2093 | return SendErrorResponse(0x15); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2094 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2095 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2096 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2097 | return SendOKResponse(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2101 | GDBRemoteCommunicationServerLLGS::Handle_interrupt( |
| 2102 | StringExtractorGDBRemote &packet) { |
| 2103 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 2104 | |
| 2105 | // Fail if we don't have a current process. |
| 2106 | if (!m_debugged_process_sp || |
| 2107 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2108 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2109 | log->Printf( |
| 2110 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2111 | __FUNCTION__); |
| 2112 | return SendErrorResponse(0x15); |
| 2113 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2115 | // Interrupt the process. |
| 2116 | Error error = m_debugged_process_sp->Interrupt(); |
| 2117 | if (error.Fail()) { |
| 2118 | if (log) { |
| 2119 | log->Printf( |
| 2120 | "GDBRemoteCommunicationServerLLGS::%s failed for process %" PRIu64 |
| 2121 | ": %s", |
| 2122 | __FUNCTION__, m_debugged_process_sp->GetID(), error.AsCString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2123 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2124 | return SendErrorResponse(GDBRemoteServerError::eErrorResume); |
| 2125 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2127 | if (log) |
| 2128 | log->Printf("GDBRemoteCommunicationServerLLGS::%s stopped process %" PRIu64, |
| 2129 | __FUNCTION__, m_debugged_process_sp->GetID()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2131 | // No response required from stop all. |
| 2132 | return PacketResult::Success; |
| 2133 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2135 | GDBRemoteCommunication::PacketResult |
| 2136 | GDBRemoteCommunicationServerLLGS::Handle_memory_read( |
| 2137 | StringExtractorGDBRemote &packet) { |
| 2138 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2140 | if (!m_debugged_process_sp || |
| 2141 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2142 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2143 | log->Printf( |
| 2144 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2145 | __FUNCTION__); |
| 2146 | return SendErrorResponse(0x15); |
| 2147 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2149 | // Parse out the memory address. |
| 2150 | packet.SetFilePos(strlen("m")); |
| 2151 | if (packet.GetBytesLeft() < 1) |
| 2152 | return SendIllFormedResponse(packet, "Too short m packet"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2154 | // Read the address. Punting on validation. |
| 2155 | // FIXME replace with Hex U64 read with no default value that fails on failed |
| 2156 | // read. |
| 2157 | const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2159 | // Validate comma. |
| 2160 | if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) |
| 2161 | return SendIllFormedResponse(packet, "Comma sep missing in m packet"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2163 | // Get # bytes to read. |
| 2164 | if (packet.GetBytesLeft() < 1) |
| 2165 | return SendIllFormedResponse(packet, "Length missing in m packet"); |
| 2166 | |
| 2167 | const uint64_t byte_count = packet.GetHexMaxU64(false, 0); |
| 2168 | if (byte_count == 0) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2169 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2170 | log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to read: " |
| 2171 | "zero-length packet", |
| 2172 | __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2173 | return SendOKResponse(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2174 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2175 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2176 | // Allocate the response buffer. |
| 2177 | std::string buf(byte_count, '\0'); |
| 2178 | if (buf.empty()) |
| 2179 | return SendErrorResponse(0x78); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2181 | // Retrieve the process memory. |
| 2182 | size_t bytes_read = 0; |
| 2183 | Error error = m_debugged_process_sp->ReadMemoryWithoutTrap( |
| 2184 | read_addr, &buf[0], byte_count, bytes_read); |
| 2185 | if (error.Fail()) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2186 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2187 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2188 | " mem 0x%" PRIx64 ": failed to read. Error: %s", |
| 2189 | __FUNCTION__, m_debugged_process_sp->GetID(), read_addr, |
| 2190 | error.AsCString()); |
| 2191 | return SendErrorResponse(0x08); |
| 2192 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2194 | if (bytes_read == 0) { |
| 2195 | if (log) |
| 2196 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2197 | " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes", |
| 2198 | __FUNCTION__, m_debugged_process_sp->GetID(), read_addr, |
| 2199 | byte_count); |
| 2200 | return SendErrorResponse(0x08); |
| 2201 | } |
| 2202 | |
| 2203 | StreamGDBRemote response; |
| 2204 | packet.SetFilePos(0); |
| 2205 | char kind = packet.GetChar('?'); |
| 2206 | if (kind == 'x') |
| 2207 | response.PutEscapedBytes(buf.data(), byte_count); |
| 2208 | else { |
| 2209 | assert(kind == 'm'); |
| 2210 | for (size_t i = 0; i < bytes_read; ++i) |
| 2211 | response.PutHex8(buf[i]); |
| 2212 | } |
| 2213 | |
| 2214 | return SendPacketNoLock(response.GetString()); |
| 2215 | } |
| 2216 | |
| 2217 | GDBRemoteCommunication::PacketResult |
| 2218 | GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) { |
| 2219 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2220 | |
| 2221 | if (!m_debugged_process_sp || |
| 2222 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2223 | if (log) |
| 2224 | log->Printf( |
| 2225 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2226 | __FUNCTION__); |
| 2227 | return SendErrorResponse(0x15); |
| 2228 | } |
| 2229 | |
| 2230 | // Parse out the memory address. |
| 2231 | packet.SetFilePos(strlen("M")); |
| 2232 | if (packet.GetBytesLeft() < 1) |
| 2233 | return SendIllFormedResponse(packet, "Too short M packet"); |
| 2234 | |
| 2235 | // Read the address. Punting on validation. |
| 2236 | // FIXME replace with Hex U64 read with no default value that fails on failed |
| 2237 | // read. |
| 2238 | const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0); |
| 2239 | |
| 2240 | // Validate comma. |
| 2241 | if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) |
| 2242 | return SendIllFormedResponse(packet, "Comma sep missing in M packet"); |
| 2243 | |
| 2244 | // Get # bytes to read. |
| 2245 | if (packet.GetBytesLeft() < 1) |
| 2246 | return SendIllFormedResponse(packet, "Length missing in M packet"); |
| 2247 | |
| 2248 | const uint64_t byte_count = packet.GetHexMaxU64(false, 0); |
| 2249 | if (byte_count == 0) { |
| 2250 | if (log) |
| 2251 | log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to write: " |
| 2252 | "zero-length packet", |
| 2253 | __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2254 | return PacketResult::Success; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | // Validate colon. |
| 2258 | if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':')) |
| 2259 | return SendIllFormedResponse( |
| 2260 | packet, "Comma sep missing in M packet after byte length"); |
| 2261 | |
| 2262 | // Allocate the conversion buffer. |
| 2263 | std::vector<uint8_t> buf(byte_count, 0); |
| 2264 | if (buf.empty()) |
| 2265 | return SendErrorResponse(0x78); |
| 2266 | |
| 2267 | // Convert the hex memory write contents to bytes. |
| 2268 | StreamGDBRemote response; |
| 2269 | const uint64_t convert_count = packet.GetHexBytes(buf, 0); |
| 2270 | if (convert_count != byte_count) { |
| 2271 | if (log) |
| 2272 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2273 | " mem 0x%" PRIx64 ": asked to write %" PRIu64 |
| 2274 | " bytes, but only found %" PRIu64 " to convert.", |
| 2275 | __FUNCTION__, m_debugged_process_sp->GetID(), write_addr, |
| 2276 | byte_count, convert_count); |
| 2277 | return SendIllFormedResponse(packet, "M content byte length specified did " |
| 2278 | "not match hex-encoded content " |
| 2279 | "length"); |
| 2280 | } |
| 2281 | |
| 2282 | // Write the process memory. |
| 2283 | size_t bytes_written = 0; |
| 2284 | Error error = m_debugged_process_sp->WriteMemory(write_addr, &buf[0], |
| 2285 | byte_count, bytes_written); |
| 2286 | if (error.Fail()) { |
| 2287 | if (log) |
| 2288 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2289 | " mem 0x%" PRIx64 ": failed to write. Error: %s", |
| 2290 | __FUNCTION__, m_debugged_process_sp->GetID(), write_addr, |
| 2291 | error.AsCString()); |
| 2292 | return SendErrorResponse(0x09); |
| 2293 | } |
| 2294 | |
| 2295 | if (bytes_written == 0) { |
| 2296 | if (log) |
| 2297 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2298 | " mem 0x%" PRIx64 ": wrote 0 of %" PRIu64 " requested bytes", |
| 2299 | __FUNCTION__, m_debugged_process_sp->GetID(), write_addr, |
| 2300 | byte_count); |
| 2301 | return SendErrorResponse(0x09); |
| 2302 | } |
| 2303 | |
| 2304 | return SendOKResponse(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2308 | GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported( |
| 2309 | StringExtractorGDBRemote &packet) { |
| 2310 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2312 | // Currently only the NativeProcessProtocol knows if it can handle a |
| 2313 | // qMemoryRegionInfoSupported |
| 2314 | // request, but we're not guaranteed to be attached to a process. For now |
| 2315 | // we'll assume the |
| 2316 | // client only asks this when a process is being debugged. |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2318 | // Ensure we have a process running; otherwise, we can't figure this out |
| 2319 | // since we won't have a NativeProcessProtocol. |
| 2320 | if (!m_debugged_process_sp || |
| 2321 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2322 | if (log) |
| 2323 | log->Printf( |
| 2324 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2325 | __FUNCTION__); |
| 2326 | return SendErrorResponse(0x15); |
| 2327 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2329 | // Test if we can get any region back when asking for the region around NULL. |
| 2330 | MemoryRegionInfo region_info; |
| 2331 | const Error error = |
| 2332 | m_debugged_process_sp->GetMemoryRegionInfo(0, region_info); |
| 2333 | if (error.Fail()) { |
| 2334 | // We don't support memory region info collection for this |
| 2335 | // NativeProcessProtocol. |
| 2336 | return SendUnimplementedResponse(""); |
| 2337 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2339 | return SendOKResponse(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2343 | GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo( |
| 2344 | StringExtractorGDBRemote &packet) { |
| 2345 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2347 | // Ensure we have a process. |
| 2348 | if (!m_debugged_process_sp || |
| 2349 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2350 | if (log) |
| 2351 | log->Printf( |
| 2352 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2353 | __FUNCTION__); |
| 2354 | return SendErrorResponse(0x15); |
| 2355 | } |
| 2356 | |
| 2357 | // Parse out the memory address. |
| 2358 | packet.SetFilePos(strlen("qMemoryRegionInfo:")); |
| 2359 | if (packet.GetBytesLeft() < 1) |
| 2360 | return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet"); |
| 2361 | |
| 2362 | // Read the address. Punting on validation. |
| 2363 | const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); |
| 2364 | |
| 2365 | StreamGDBRemote response; |
| 2366 | |
| 2367 | // Get the memory region info for the target address. |
| 2368 | MemoryRegionInfo region_info; |
| 2369 | const Error error = |
| 2370 | m_debugged_process_sp->GetMemoryRegionInfo(read_addr, region_info); |
| 2371 | if (error.Fail()) { |
| 2372 | // Return the error message. |
| 2373 | |
| 2374 | response.PutCString("error:"); |
| 2375 | response.PutCStringAsRawHex8(error.AsCString()); |
| 2376 | response.PutChar(';'); |
| 2377 | } else { |
| 2378 | // Range start and size. |
| 2379 | response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";", |
| 2380 | region_info.GetRange().GetRangeBase(), |
| 2381 | region_info.GetRange().GetByteSize()); |
| 2382 | |
| 2383 | // Permissions. |
| 2384 | if (region_info.GetReadable() || region_info.GetWritable() || |
| 2385 | region_info.GetExecutable()) { |
| 2386 | // Write permissions info. |
| 2387 | response.PutCString("permissions:"); |
| 2388 | |
| 2389 | if (region_info.GetReadable()) |
| 2390 | response.PutChar('r'); |
| 2391 | if (region_info.GetWritable()) |
| 2392 | response.PutChar('w'); |
| 2393 | if (region_info.GetExecutable()) |
| 2394 | response.PutChar('x'); |
| 2395 | |
| 2396 | response.PutChar(';'); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2399 | // Name |
| 2400 | ConstString name = region_info.GetName(); |
| 2401 | if (name) { |
| 2402 | response.PutCString("name:"); |
| 2403 | response.PutCStringAsRawHex8(name.AsCString()); |
| 2404 | response.PutChar(';'); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2405 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2406 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2407 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2408 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2412 | GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) { |
| 2413 | // Ensure we have a process. |
| 2414 | if (!m_debugged_process_sp || |
| 2415 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2416 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2417 | if (log) |
| 2418 | log->Printf( |
| 2419 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2420 | __FUNCTION__); |
| 2421 | return SendErrorResponse(0x15); |
| 2422 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2424 | // Parse out software or hardware breakpoint or watchpoint requested. |
| 2425 | packet.SetFilePos(strlen("Z")); |
| 2426 | if (packet.GetBytesLeft() < 1) |
| 2427 | return SendIllFormedResponse( |
| 2428 | packet, "Too short Z packet, missing software/hardware specifier"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2430 | bool want_breakpoint = true; |
| 2431 | bool want_hardware = false; |
| 2432 | uint32_t watch_flags = 0; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2433 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2434 | const GDBStoppointType stoppoint_type = |
| 2435 | GDBStoppointType(packet.GetS32(eStoppointInvalid)); |
| 2436 | switch (stoppoint_type) { |
| 2437 | case eBreakpointSoftware: |
| 2438 | want_hardware = false; |
| 2439 | want_breakpoint = true; |
| 2440 | break; |
| 2441 | case eBreakpointHardware: |
| 2442 | want_hardware = true; |
| 2443 | want_breakpoint = true; |
| 2444 | break; |
| 2445 | case eWatchpointWrite: |
| 2446 | watch_flags = 1; |
| 2447 | want_hardware = true; |
| 2448 | want_breakpoint = false; |
| 2449 | break; |
| 2450 | case eWatchpointRead: |
| 2451 | watch_flags = 2; |
| 2452 | want_hardware = true; |
| 2453 | want_breakpoint = false; |
| 2454 | break; |
| 2455 | case eWatchpointReadWrite: |
| 2456 | watch_flags = 3; |
| 2457 | want_hardware = true; |
| 2458 | want_breakpoint = false; |
| 2459 | break; |
| 2460 | case eStoppointInvalid: |
| 2461 | return SendIllFormedResponse( |
| 2462 | packet, "Z packet had invalid software/hardware specifier"); |
| 2463 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2465 | if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') |
| 2466 | return SendIllFormedResponse( |
| 2467 | packet, "Malformed Z packet, expecting comma after stoppoint type"); |
| 2468 | |
| 2469 | // Parse out the stoppoint address. |
| 2470 | if (packet.GetBytesLeft() < 1) |
| 2471 | return SendIllFormedResponse(packet, "Too short Z packet, missing address"); |
| 2472 | const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); |
| 2473 | |
| 2474 | if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') |
| 2475 | return SendIllFormedResponse( |
| 2476 | packet, "Malformed Z packet, expecting comma after address"); |
| 2477 | |
| 2478 | // Parse out the stoppoint size (i.e. size hint for opcode size). |
| 2479 | const uint32_t size = |
| 2480 | packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); |
| 2481 | if (size == std::numeric_limits<uint32_t>::max()) |
| 2482 | return SendIllFormedResponse( |
| 2483 | packet, "Malformed Z packet, failed to parse size argument"); |
| 2484 | |
| 2485 | if (want_breakpoint) { |
| 2486 | // Try to set the breakpoint. |
| 2487 | const Error error = |
| 2488 | m_debugged_process_sp->SetBreakpoint(addr, size, want_hardware); |
| 2489 | if (error.Success()) |
| 2490 | return SendOKResponse(); |
| 2491 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 2492 | if (log) |
| 2493 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2494 | " failed to set breakpoint: %s", |
| 2495 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2496 | error.AsCString()); |
| 2497 | return SendErrorResponse(0x09); |
| 2498 | } else { |
| 2499 | // Try to set the watchpoint. |
| 2500 | const Error error = m_debugged_process_sp->SetWatchpoint( |
| 2501 | addr, size, watch_flags, want_hardware); |
| 2502 | if (error.Success()) |
| 2503 | return SendOKResponse(); |
| 2504 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); |
| 2505 | if (log) |
| 2506 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2507 | " failed to set watchpoint: %s", |
| 2508 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2509 | error.AsCString()); |
| 2510 | return SendErrorResponse(0x09); |
| 2511 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2515 | GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) { |
| 2516 | // Ensure we have a process. |
| 2517 | if (!m_debugged_process_sp || |
| 2518 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2519 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2520 | if (log) |
| 2521 | log->Printf( |
| 2522 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2523 | __FUNCTION__); |
| 2524 | return SendErrorResponse(0x15); |
| 2525 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2527 | // Parse out software or hardware breakpoint or watchpoint requested. |
| 2528 | packet.SetFilePos(strlen("z")); |
| 2529 | if (packet.GetBytesLeft() < 1) |
| 2530 | return SendIllFormedResponse( |
| 2531 | packet, "Too short z packet, missing software/hardware specifier"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2533 | bool want_breakpoint = true; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2535 | const GDBStoppointType stoppoint_type = |
| 2536 | GDBStoppointType(packet.GetS32(eStoppointInvalid)); |
| 2537 | switch (stoppoint_type) { |
| 2538 | case eBreakpointHardware: |
| 2539 | want_breakpoint = true; |
| 2540 | break; |
| 2541 | case eBreakpointSoftware: |
| 2542 | want_breakpoint = true; |
| 2543 | break; |
| 2544 | case eWatchpointWrite: |
| 2545 | want_breakpoint = false; |
| 2546 | break; |
| 2547 | case eWatchpointRead: |
| 2548 | want_breakpoint = false; |
| 2549 | break; |
| 2550 | case eWatchpointReadWrite: |
| 2551 | want_breakpoint = false; |
| 2552 | break; |
| 2553 | default: |
| 2554 | return SendIllFormedResponse( |
| 2555 | packet, "z packet had invalid software/hardware specifier"); |
| 2556 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2557 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2558 | if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') |
| 2559 | return SendIllFormedResponse( |
| 2560 | packet, "Malformed z packet, expecting comma after stoppoint type"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2562 | // Parse out the stoppoint address. |
| 2563 | if (packet.GetBytesLeft() < 1) |
| 2564 | return SendIllFormedResponse(packet, "Too short z packet, missing address"); |
| 2565 | const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2567 | if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') |
| 2568 | return SendIllFormedResponse( |
| 2569 | packet, "Malformed z packet, expecting comma after address"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2571 | /* |
| 2572 | // Parse out the stoppoint size (i.e. size hint for opcode size). |
| 2573 | const uint32_t size = packet.GetHexMaxU32 (false, |
| 2574 | std::numeric_limits<uint32_t>::max ()); |
| 2575 | if (size == std::numeric_limits<uint32_t>::max ()) |
| 2576 | return SendIllFormedResponse(packet, "Malformed z packet, failed to parse |
| 2577 | size argument"); |
| 2578 | */ |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2579 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2580 | if (want_breakpoint) { |
| 2581 | // Try to clear the breakpoint. |
| 2582 | const Error error = m_debugged_process_sp->RemoveBreakpoint(addr); |
| 2583 | if (error.Success()) |
| 2584 | return SendOKResponse(); |
| 2585 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 2586 | if (log) |
| 2587 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2588 | " failed to remove breakpoint: %s", |
| 2589 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2590 | error.AsCString()); |
| 2591 | return SendErrorResponse(0x09); |
| 2592 | } else { |
| 2593 | // Try to clear the watchpoint. |
| 2594 | const Error error = m_debugged_process_sp->RemoveWatchpoint(addr); |
| 2595 | if (error.Success()) |
| 2596 | return SendOKResponse(); |
| 2597 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); |
| 2598 | if (log) |
| 2599 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2600 | " failed to remove watchpoint: %s", |
| 2601 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2602 | error.AsCString()); |
| 2603 | return SendErrorResponse(0x09); |
| 2604 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2608 | GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) { |
| 2609 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2610 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2611 | // Ensure we have a process. |
| 2612 | if (!m_debugged_process_sp || |
| 2613 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2614 | if (log) |
| 2615 | log->Printf( |
| 2616 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2617 | __FUNCTION__); |
| 2618 | return SendErrorResponse(0x32); |
| 2619 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2620 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2621 | // We first try to use a continue thread id. If any one or any all set, use |
| 2622 | // the current thread. |
| 2623 | // Bail out if we don't have a thread id. |
| 2624 | lldb::tid_t tid = GetContinueThreadID(); |
| 2625 | if (tid == 0 || tid == LLDB_INVALID_THREAD_ID) |
| 2626 | tid = GetCurrentThreadID(); |
| 2627 | if (tid == LLDB_INVALID_THREAD_ID) |
| 2628 | return SendErrorResponse(0x33); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2629 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2630 | // Double check that we have such a thread. |
| 2631 | // TODO investigate: on MacOSX we might need to do an UpdateThreads () here. |
| 2632 | NativeThreadProtocolSP thread_sp = m_debugged_process_sp->GetThreadByID(tid); |
| 2633 | if (!thread_sp || thread_sp->GetID() != tid) |
| 2634 | return SendErrorResponse(0x33); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2635 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2636 | // Create the step action for the given thread. |
| 2637 | ResumeAction action = {tid, eStateStepping, 0}; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2638 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2639 | // Setup the actions list. |
| 2640 | ResumeActionList actions; |
| 2641 | actions.Append(action); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2643 | // All other threads stop while we're single stepping a thread. |
| 2644 | actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0); |
| 2645 | Error error = m_debugged_process_sp->Resume(actions); |
| 2646 | if (error.Fail()) { |
| 2647 | if (log) |
| 2648 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2649 | " tid %" PRIu64 " Resume() failed with error: %s", |
| 2650 | __FUNCTION__, m_debugged_process_sp->GetID(), tid, |
| 2651 | error.AsCString()); |
| 2652 | return SendErrorResponse(0x49); |
| 2653 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2654 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2655 | // No response here - the stop or exit will come from the resulting action. |
| 2656 | return PacketResult::Success; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2660 | GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read( |
| 2661 | StringExtractorGDBRemote &packet) { |
| 2662 | // *BSD impls should be able to do this too. |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2663 | #if defined(__linux__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2664 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2666 | // Parse out the offset. |
| 2667 | packet.SetFilePos(strlen("qXfer:auxv:read::")); |
| 2668 | if (packet.GetBytesLeft() < 1) |
| 2669 | return SendIllFormedResponse(packet, |
| 2670 | "qXfer:auxv:read:: packet missing offset"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2671 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2672 | const uint64_t auxv_offset = |
| 2673 | packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max()); |
| 2674 | if (auxv_offset == std::numeric_limits<uint64_t>::max()) |
| 2675 | return SendIllFormedResponse(packet, |
| 2676 | "qXfer:auxv:read:: packet missing offset"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2677 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2678 | // Parse out comma. |
| 2679 | if (packet.GetBytesLeft() < 1 || packet.GetChar() != ',') |
| 2680 | return SendIllFormedResponse( |
| 2681 | packet, "qXfer:auxv:read:: packet missing comma after offset"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2682 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2683 | // Parse out the length. |
| 2684 | const uint64_t auxv_length = |
| 2685 | packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max()); |
| 2686 | if (auxv_length == std::numeric_limits<uint64_t>::max()) |
| 2687 | return SendIllFormedResponse(packet, |
| 2688 | "qXfer:auxv:read:: packet missing length"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2689 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2690 | // Grab the auxv data if we need it. |
| 2691 | if (!m_active_auxv_buffer_sp) { |
| 2692 | // Make sure we have a valid process. |
| 2693 | if (!m_debugged_process_sp || |
| 2694 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2695 | if (log) |
| 2696 | log->Printf( |
| 2697 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2698 | __FUNCTION__); |
| 2699 | return SendErrorResponse(0x10); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2702 | // Grab the auxv data. |
| 2703 | m_active_auxv_buffer_sp = Host::GetAuxvData(m_debugged_process_sp->GetID()); |
| 2704 | if (!m_active_auxv_buffer_sp || |
| 2705 | m_active_auxv_buffer_sp->GetByteSize() == 0) { |
| 2706 | // Hmm, no auxv data, call that an error. |
| 2707 | if (log) |
| 2708 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no auxv data " |
| 2709 | "retrieved", |
| 2710 | __FUNCTION__); |
| 2711 | m_active_auxv_buffer_sp.reset(); |
| 2712 | return SendErrorResponse(0x11); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2713 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2714 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2715 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2716 | // FIXME find out if/how I lock the stream here. |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2717 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2718 | StreamGDBRemote response; |
| 2719 | bool done_with_buffer = false; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2720 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2721 | if (auxv_offset >= m_active_auxv_buffer_sp->GetByteSize()) { |
| 2722 | // We have nothing left to send. Mark the buffer as complete. |
| 2723 | response.PutChar('l'); |
| 2724 | done_with_buffer = true; |
| 2725 | } else { |
| 2726 | // Figure out how many bytes are available starting at the given offset. |
| 2727 | const uint64_t bytes_remaining = |
| 2728 | m_active_auxv_buffer_sp->GetByteSize() - auxv_offset; |
| 2729 | |
| 2730 | // Figure out how many bytes we're going to read. |
| 2731 | const uint64_t bytes_to_read = |
| 2732 | (auxv_length > bytes_remaining) ? bytes_remaining : auxv_length; |
| 2733 | |
| 2734 | // Mark the response type according to whether we're reading the remainder |
| 2735 | // of the auxv data. |
| 2736 | if (bytes_to_read >= bytes_remaining) { |
| 2737 | // There will be nothing left to read after this |
| 2738 | response.PutChar('l'); |
| 2739 | done_with_buffer = true; |
| 2740 | } else { |
| 2741 | // There will still be bytes to read after this request. |
| 2742 | response.PutChar('m'); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2745 | // Now write the data in encoded binary form. |
| 2746 | response.PutEscapedBytes(m_active_auxv_buffer_sp->GetBytes() + auxv_offset, |
| 2747 | bytes_to_read); |
| 2748 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2750 | if (done_with_buffer) |
| 2751 | m_active_auxv_buffer_sp.reset(); |
| 2752 | |
| 2753 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2754 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2755 | return SendUnimplementedResponse("not implemented on this platform"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2756 | #endif |
| 2757 | } |
| 2758 | |
| 2759 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2760 | GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState( |
| 2761 | StringExtractorGDBRemote &packet) { |
| 2762 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2763 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2764 | // Move past packet name. |
| 2765 | packet.SetFilePos(strlen("QSaveRegisterState")); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2766 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2767 | // Get the thread to use. |
| 2768 | NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet); |
| 2769 | if (!thread_sp) { |
| 2770 | if (m_thread_suffix_supported) |
| 2771 | return SendIllFormedResponse( |
| 2772 | packet, "No thread specified in QSaveRegisterState packet"); |
| 2773 | else |
| 2774 | return SendIllFormedResponse(packet, |
| 2775 | "No thread was is set with the Hg packet"); |
| 2776 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2777 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2778 | // Grab the register context for the thread. |
| 2779 | NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext()); |
| 2780 | if (!reg_context_sp) { |
| 2781 | if (log) |
| 2782 | log->Printf( |
| 2783 | "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 |
| 2784 | " failed, no register context available for the thread", |
| 2785 | __FUNCTION__, m_debugged_process_sp->GetID(), thread_sp->GetID()); |
| 2786 | return SendErrorResponse(0x15); |
| 2787 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2789 | // Save registers to a buffer. |
| 2790 | DataBufferSP register_data_sp; |
| 2791 | Error error = reg_context_sp->ReadAllRegisterValues(register_data_sp); |
| 2792 | if (error.Fail()) { |
| 2793 | if (log) |
| 2794 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2795 | " failed to save all register values: %s", |
| 2796 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2797 | error.AsCString()); |
| 2798 | return SendErrorResponse(0x75); |
| 2799 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2800 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2801 | // Allocate a new save id. |
| 2802 | const uint32_t save_id = GetNextSavedRegistersID(); |
| 2803 | assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) && |
| 2804 | "GetNextRegisterSaveID() returned an existing register save id"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2806 | // Save the register data buffer under the save id. |
| 2807 | { |
| 2808 | std::lock_guard<std::mutex> guard(m_saved_registers_mutex); |
| 2809 | m_saved_registers_map[save_id] = register_data_sp; |
| 2810 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2811 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2812 | // Write the response. |
| 2813 | StreamGDBRemote response; |
| 2814 | response.Printf("%" PRIu32, save_id); |
| 2815 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2819 | GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState( |
| 2820 | StringExtractorGDBRemote &packet) { |
| 2821 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2823 | // Parse out save id. |
| 2824 | packet.SetFilePos(strlen("QRestoreRegisterState:")); |
| 2825 | if (packet.GetBytesLeft() < 1) |
| 2826 | return SendIllFormedResponse( |
| 2827 | packet, "QRestoreRegisterState packet missing register save id"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2828 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2829 | const uint32_t save_id = packet.GetU32(0); |
| 2830 | if (save_id == 0) { |
| 2831 | if (log) |
| 2832 | log->Printf("GDBRemoteCommunicationServerLLGS::%s QRestoreRegisterState " |
| 2833 | "packet has malformed save id, expecting decimal uint32_t", |
| 2834 | __FUNCTION__); |
| 2835 | return SendErrorResponse(0x76); |
| 2836 | } |
| 2837 | |
| 2838 | // Get the thread to use. |
| 2839 | NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet); |
| 2840 | if (!thread_sp) { |
| 2841 | if (m_thread_suffix_supported) |
| 2842 | return SendIllFormedResponse( |
| 2843 | packet, "No thread specified in QRestoreRegisterState packet"); |
| 2844 | else |
| 2845 | return SendIllFormedResponse(packet, |
| 2846 | "No thread was is set with the Hg packet"); |
| 2847 | } |
| 2848 | |
| 2849 | // Grab the register context for the thread. |
| 2850 | NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext()); |
| 2851 | if (!reg_context_sp) { |
| 2852 | if (log) |
| 2853 | log->Printf( |
| 2854 | "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 |
| 2855 | " failed, no register context available for the thread", |
| 2856 | __FUNCTION__, m_debugged_process_sp->GetID(), thread_sp->GetID()); |
| 2857 | return SendErrorResponse(0x15); |
| 2858 | } |
| 2859 | |
| 2860 | // Retrieve register state buffer, then remove from the list. |
| 2861 | DataBufferSP register_data_sp; |
| 2862 | { |
| 2863 | std::lock_guard<std::mutex> guard(m_saved_registers_mutex); |
| 2864 | |
| 2865 | // Find the register set buffer for the given save id. |
| 2866 | auto it = m_saved_registers_map.find(save_id); |
| 2867 | if (it == m_saved_registers_map.end()) { |
| 2868 | if (log) |
| 2869 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2870 | " does not have a register set save buffer for id %" PRIu32, |
| 2871 | __FUNCTION__, m_debugged_process_sp->GetID(), save_id); |
| 2872 | return SendErrorResponse(0x77); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2873 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2874 | register_data_sp = it->second; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2875 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2876 | // Remove it from the map. |
| 2877 | m_saved_registers_map.erase(it); |
| 2878 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2879 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2880 | Error error = reg_context_sp->WriteAllRegisterValues(register_data_sp); |
| 2881 | if (error.Fail()) { |
| 2882 | if (log) |
| 2883 | log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 |
| 2884 | " failed to restore all register values: %s", |
| 2885 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2886 | error.AsCString()); |
| 2887 | return SendErrorResponse(0x77); |
| 2888 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2890 | return SendOKResponse(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2894 | GDBRemoteCommunicationServerLLGS::Handle_vAttach( |
| 2895 | StringExtractorGDBRemote &packet) { |
| 2896 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2897 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2898 | // Consume the ';' after vAttach. |
| 2899 | packet.SetFilePos(strlen("vAttach")); |
| 2900 | if (!packet.GetBytesLeft() || packet.GetChar() != ';') |
| 2901 | return SendIllFormedResponse(packet, "vAttach missing expected ';'"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2903 | // Grab the PID to which we will attach (assume hex encoding). |
| 2904 | lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16); |
| 2905 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 2906 | return SendIllFormedResponse(packet, |
| 2907 | "vAttach failed to parse the process id"); |
| 2908 | |
| 2909 | // Attempt to attach. |
| 2910 | if (log) |
| 2911 | log->Printf("GDBRemoteCommunicationServerLLGS::%s attempting to attach to " |
| 2912 | "pid %" PRIu64, |
| 2913 | __FUNCTION__, pid); |
| 2914 | |
| 2915 | Error error = AttachToProcess(pid); |
| 2916 | |
| 2917 | if (error.Fail()) { |
| 2918 | if (log) |
| 2919 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to attach to " |
| 2920 | "pid %" PRIu64 ": %s\n", |
| 2921 | __FUNCTION__, pid, error.AsCString()); |
| 2922 | return SendErrorResponse(0x01); |
| 2923 | } |
| 2924 | |
| 2925 | // Notify we attached by sending a stop packet. |
| 2926 | return SendStopReasonForState(m_debugged_process_sp->GetState()); |
| 2927 | } |
| 2928 | |
| 2929 | GDBRemoteCommunication::PacketResult |
| 2930 | GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) { |
| 2931 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2932 | |
| 2933 | StopSTDIOForwarding(); |
| 2934 | |
| 2935 | // Fail if we don't have a current process. |
| 2936 | if (!m_debugged_process_sp || |
| 2937 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) { |
| 2938 | if (log) |
| 2939 | log->Printf( |
| 2940 | "GDBRemoteCommunicationServerLLGS::%s failed, no process available", |
| 2941 | __FUNCTION__); |
| 2942 | return SendErrorResponse(0x15); |
| 2943 | } |
| 2944 | |
| 2945 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 2946 | |
| 2947 | // Consume the ';' after D. |
| 2948 | packet.SetFilePos(1); |
| 2949 | if (packet.GetBytesLeft()) { |
| 2950 | if (packet.GetChar() != ';') |
| 2951 | return SendIllFormedResponse(packet, "D missing expected ';'"); |
| 2952 | |
| 2953 | // Grab the PID from which we will detach (assume hex encoding). |
| 2954 | pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2955 | if (pid == LLDB_INVALID_PROCESS_ID) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2956 | return SendIllFormedResponse(packet, "D failed to parse the process id"); |
| 2957 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2958 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2959 | if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_sp->GetID() != pid) { |
| 2960 | return SendIllFormedResponse(packet, "Invalid pid"); |
| 2961 | } |
| 2962 | |
| 2963 | const Error error = m_debugged_process_sp->Detach(); |
| 2964 | if (error.Fail()) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2965 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2966 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to detach from " |
| 2967 | "pid %" PRIu64 ": %s\n", |
| 2968 | __FUNCTION__, m_debugged_process_sp->GetID(), |
| 2969 | error.AsCString()); |
| 2970 | return SendErrorResponse(0x01); |
| 2971 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2972 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2973 | return SendOKResponse(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2977 | GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo( |
| 2978 | StringExtractorGDBRemote &packet) { |
| 2979 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 2980 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2981 | packet.SetFilePos(strlen("qThreadStopInfo")); |
| 2982 | const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); |
| 2983 | if (tid == LLDB_INVALID_THREAD_ID) { |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 2984 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2985 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not " |
| 2986 | "parse thread id from request \"%s\"", |
| 2987 | __FUNCTION__, packet.GetStringRef().c_str()); |
| 2988 | return SendErrorResponse(0x15); |
| 2989 | } |
| 2990 | return SendStopReplyPacketForThread(tid); |
| 2991 | } |
| 2992 | |
| 2993 | GDBRemoteCommunication::PacketResult |
| 2994 | GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo( |
| 2995 | StringExtractorGDBRemote &) { |
| 2996 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 2997 | |
| 2998 | // Ensure we have a debugged process. |
| 2999 | if (!m_debugged_process_sp || |
| 3000 | (m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID)) |
| 3001 | return SendErrorResponse(50); |
| 3002 | |
| 3003 | if (log) |
| 3004 | log->Printf("GDBRemoteCommunicationServerLLGS::%s preparing packet for pid " |
| 3005 | "%" PRIu64, |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 3006 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 3007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3008 | StreamString response; |
| 3009 | const bool threads_with_valid_stop_info_only = false; |
| 3010 | JSONArray::SP threads_array_sp = GetJSONThreadsInfo( |
| 3011 | *m_debugged_process_sp, threads_with_valid_stop_info_only); |
| 3012 | if (!threads_array_sp) { |
| 3013 | if (log) |
| 3014 | log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to prepare a " |
| 3015 | "packet for pid %" PRIu64, |
| 3016 | __FUNCTION__, m_debugged_process_sp->GetID()); |
| 3017 | return SendErrorResponse(52); |
| 3018 | } |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 3019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3020 | threads_array_sp->Write(response); |
| 3021 | StreamGDBRemote escaped_response; |
| 3022 | escaped_response.PutEscapedBytes(response.GetData(), response.GetSize()); |
| 3023 | return SendPacketNoLock(escaped_response.GetString()); |
Pavel Labath | 4a4bb12 | 2015-07-16 14:14:35 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3027 | GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo( |
| 3028 | StringExtractorGDBRemote &packet) { |
| 3029 | // Fail if we don't have a current process. |
| 3030 | if (!m_debugged_process_sp || |
| 3031 | m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID) |
| 3032 | return SendErrorResponse(68); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3033 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3034 | packet.SetFilePos(strlen("qWatchpointSupportInfo")); |
| 3035 | if (packet.GetBytesLeft() == 0) |
| 3036 | return SendOKResponse(); |
| 3037 | if (packet.GetChar() != ':') |
| 3038 | return SendErrorResponse(67); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3039 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3040 | uint32_t num = m_debugged_process_sp->GetMaxWatchpoints(); |
| 3041 | StreamGDBRemote response; |
| 3042 | response.Printf("num:%d;", num); |
| 3043 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3046 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3047 | GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress( |
| 3048 | StringExtractorGDBRemote &packet) { |
| 3049 | // Fail if we don't have a current process. |
| 3050 | if (!m_debugged_process_sp || |
| 3051 | m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID) |
| 3052 | return SendErrorResponse(67); |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3053 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3054 | packet.SetFilePos(strlen("qFileLoadAddress:")); |
| 3055 | if (packet.GetBytesLeft() == 0) |
| 3056 | return SendErrorResponse(68); |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3057 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3058 | std::string file_name; |
| 3059 | packet.GetHexByteString(file_name); |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3060 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3061 | lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS; |
| 3062 | Error error = |
| 3063 | m_debugged_process_sp->GetFileLoadAddress(file_name, file_load_address); |
| 3064 | if (error.Fail()) |
| 3065 | return SendErrorResponse(69); |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3066 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3067 | if (file_load_address == LLDB_INVALID_ADDRESS) |
| 3068 | return SendErrorResponse(1); // File not loaded |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3069 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3070 | StreamGDBRemote response; |
| 3071 | response.PutHex64(file_load_address); |
| 3072 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3075 | void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() { |
| 3076 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3077 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3078 | // Tell the stdio connection to shut down. |
| 3079 | if (m_stdio_communication.IsConnected()) { |
| 3080 | auto connection = m_stdio_communication.GetConnection(); |
| 3081 | if (connection) { |
| 3082 | Error error; |
| 3083 | connection->Disconnect(&error); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3084 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3085 | if (error.Success()) { |
| 3086 | if (log) |
| 3087 | log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process " |
| 3088 | "terminal stdio - SUCCESS", |
| 3089 | __FUNCTION__); |
| 3090 | } else { |
| 3091 | if (log) |
| 3092 | log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process " |
| 3093 | "terminal stdio - FAIL: %s", |
| 3094 | __FUNCTION__, error.AsCString()); |
| 3095 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3096 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3097 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3100 | NativeThreadProtocolSP GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix( |
| 3101 | StringExtractorGDBRemote &packet) { |
| 3102 | NativeThreadProtocolSP thread_sp; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3104 | // We have no thread if we don't have a process. |
| 3105 | if (!m_debugged_process_sp || |
| 3106 | m_debugged_process_sp->GetID() == LLDB_INVALID_PROCESS_ID) |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3107 | return thread_sp; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3109 | // If the client hasn't asked for thread suffix support, there will not be a |
| 3110 | // thread suffix. |
| 3111 | // Use the current thread in that case. |
| 3112 | if (!m_thread_suffix_supported) { |
| 3113 | const lldb::tid_t current_tid = GetCurrentThreadID(); |
| 3114 | if (current_tid == LLDB_INVALID_THREAD_ID) |
| 3115 | return thread_sp; |
| 3116 | else if (current_tid == 0) { |
| 3117 | // Pick a thread. |
| 3118 | return m_debugged_process_sp->GetThreadAtIndex(0); |
| 3119 | } else |
| 3120 | return m_debugged_process_sp->GetThreadByID(current_tid); |
| 3121 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3123 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3125 | // Parse out the ';'. |
| 3126 | if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3127 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3128 | log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse " |
| 3129 | "error: expected ';' prior to start of thread suffix: packet " |
| 3130 | "contents = '%s'", |
| 3131 | __FUNCTION__, packet.GetStringRef().c_str()); |
| 3132 | return thread_sp; |
| 3133 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3135 | if (!packet.GetBytesLeft()) |
| 3136 | return thread_sp; |
| 3137 | |
| 3138 | // Parse out thread: portion. |
| 3139 | if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) { |
| 3140 | if (log) |
| 3141 | log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse " |
| 3142 | "error: expected 'thread:' but not found, packet contents = " |
| 3143 | "'%s'", |
| 3144 | __FUNCTION__, packet.GetStringRef().c_str()); |
| 3145 | return thread_sp; |
| 3146 | } |
| 3147 | packet.SetFilePos(packet.GetFilePos() + strlen("thread:")); |
| 3148 | const lldb::tid_t tid = packet.GetHexMaxU64(false, 0); |
| 3149 | if (tid != 0) |
| 3150 | return m_debugged_process_sp->GetThreadByID(tid); |
| 3151 | |
| 3152 | return thread_sp; |
| 3153 | } |
| 3154 | |
| 3155 | lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const { |
| 3156 | if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) { |
| 3157 | // Use whatever the debug process says is the current thread id |
| 3158 | // since the protocol either didn't specify or specified we want |
| 3159 | // any/all threads marked as the current thread. |
| 3160 | if (!m_debugged_process_sp) |
| 3161 | return LLDB_INVALID_THREAD_ID; |
| 3162 | return m_debugged_process_sp->GetCurrentThreadID(); |
| 3163 | } |
| 3164 | // Use the specific current thread id set by the gdb remote protocol. |
| 3165 | return m_current_tid; |
| 3166 | } |
| 3167 | |
| 3168 | uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() { |
| 3169 | std::lock_guard<std::mutex> guard(m_saved_registers_mutex); |
| 3170 | return m_next_saved_registers_id++; |
| 3171 | } |
| 3172 | |
| 3173 | void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() { |
| 3174 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | GDBR_LOG_PROCESS)); |
| 3175 | if (log) |
| 3176 | log->Printf("GDBRemoteCommunicationServerLLGS::%s()", __FUNCTION__); |
| 3177 | |
| 3178 | // Clear any auxv cached data. |
| 3179 | // *BSD impls should be able to do this too. |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3180 | #if defined(__linux__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3181 | if (log) |
| 3182 | log->Printf("GDBRemoteCommunicationServerLLGS::%s clearing auxv buffer " |
| 3183 | "(previously %s)", |
| 3184 | __FUNCTION__, |
| 3185 | m_active_auxv_buffer_sp ? "was set" : "was not set"); |
| 3186 | m_active_auxv_buffer_sp.reset(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 3187 | #endif |
| 3188 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3189 | |
| 3190 | FileSpec |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3191 | GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path, |
| 3192 | const ArchSpec &arch) { |
| 3193 | if (m_debugged_process_sp) { |
| 3194 | FileSpec file_spec; |
| 3195 | if (m_debugged_process_sp |
| 3196 | ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec) |
| 3197 | .Success()) { |
| 3198 | if (file_spec.Exists()) |
| 3199 | return file_spec; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3200 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3201 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3203 | return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3204 | } |