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