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