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