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