blob: d15f54dcc2a3305b41dcef38abce1cb3eb1b392f [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerLLGS.cpp --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <errno.h>
11
12#include "lldb/Host/Config.h"
13
14#include "GDBRemoteCommunicationServerLLGS.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000015#include "lldb/Utility/StreamGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000016
Tamas Berghammere13c2732015-02-11 10:29:30 +000017#include <chrono>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <cstring>
Tamas Berghammere13c2732015-02-11 10:29:30 +000019#include <thread>
20
Tamas Berghammere13c2732015-02-11 10:29:30 +000021#include "lldb/Host/ConnectionFileDescriptor.h"
22#include "lldb/Host/Debug.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000023#include "lldb/Host/File.h"
24#include "lldb/Host/FileSystem.h"
25#include "lldb/Host/Host.h"
26#include "lldb/Host/HostInfo.h"
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000027#include "lldb/Host/PosixApi.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000028#include "lldb/Host/common/NativeProcessProtocol.h"
29#include "lldb/Host/common/NativeRegisterContext.h"
30#include "lldb/Host/common/NativeThreadProtocol.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000031#include "lldb/Target/FileAction.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000032#include "lldb/Target/MemoryRegionInfo.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000033#include "lldb/Utility/Args.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000034#include "lldb/Utility/DataBuffer.h"
Zachary Turner01c32432017-02-14 19:06:07 +000035#include "lldb/Utility/Endian.h"
Pavel Labath4a4bb122015-07-16 14:14:35 +000036#include "lldb/Utility/JSON.h"
Pavel Labathabadc222015-11-27 13:33:29 +000037#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000038#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000039#include "lldb/Utility/RegisterValue.h"
40#include "lldb/Utility/State.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000041#include "lldb/Utility/StreamString.h"
Pavel Labath5f7e5832017-02-10 12:21:22 +000042#include "lldb/Utility/UriParser.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000043#include "llvm/ADT/Triple.h"
44#include "llvm/Support/ScopedPrinter.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000045
Tamas Berghammere13c2732015-02-11 10:29:30 +000046#include "ProcessGDBRemote.h"
47#include "ProcessGDBRemoteLog.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000048#include "lldb/Utility/StringExtractorGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000049
50using namespace lldb;
51using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000052using namespace lldb_private::process_gdb_remote;
Tamas Berghammer783bfc82015-06-18 20:43:56 +000053using namespace llvm;
Tamas Berghammere13c2732015-02-11 10:29:30 +000054
55//----------------------------------------------------------------------
56// GDBRemote Errors
57//----------------------------------------------------------------------
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059namespace {
60enum GDBRemoteServerError {
61 // Set to the first unused error number in literal form below
62 eErrorFirst = 29,
63 eErrorNoProcess = eErrorFirst,
64 eErrorResume,
65 eErrorExitStatus
66};
Tamas Berghammere13c2732015-02-11 10:29:30 +000067}
68
69//----------------------------------------------------------------------
70// GDBRemoteCommunicationServerLLGS constructor
71//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000072GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
Pavel Labath96e600f2017-07-07 11:02:19 +000073 MainLoop &mainloop, const NativeProcessProtocol::Factory &process_factory)
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 : GDBRemoteCommunicationServerCommon("gdb-remote.server",
75 "gdb-remote.server.rx_packet"),
Pavel Labath96e600f2017-07-07 11:02:19 +000076 m_mainloop(mainloop), m_process_factory(process_factory),
77 m_stdio_communication("process.stdio") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 RegisterPacketHandlers();
Tamas Berghammere13c2732015-02-11 10:29:30 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081void GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() {
82 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C,
83 &GDBRemoteCommunicationServerLLGS::Handle_C);
84 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c,
85 &GDBRemoteCommunicationServerLLGS::Handle_c);
86 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D,
87 &GDBRemoteCommunicationServerLLGS::Handle_D);
88 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H,
89 &GDBRemoteCommunicationServerLLGS::Handle_H);
90 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I,
91 &GDBRemoteCommunicationServerLLGS::Handle_I);
92 RegisterMemberFunctionHandler(
93 StringExtractorGDBRemote::eServerPacketType_interrupt,
94 &GDBRemoteCommunicationServerLLGS::Handle_interrupt);
95 RegisterMemberFunctionHandler(
96 StringExtractorGDBRemote::eServerPacketType_m,
97 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
98 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M,
99 &GDBRemoteCommunicationServerLLGS::Handle_M);
100 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p,
101 &GDBRemoteCommunicationServerLLGS::Handle_p);
102 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P,
103 &GDBRemoteCommunicationServerLLGS::Handle_P);
104 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC,
105 &GDBRemoteCommunicationServerLLGS::Handle_qC);
106 RegisterMemberFunctionHandler(
107 StringExtractorGDBRemote::eServerPacketType_qfThreadInfo,
108 &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo);
109 RegisterMemberFunctionHandler(
110 StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress,
111 &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress);
112 RegisterMemberFunctionHandler(
113 StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir,
114 &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir);
115 RegisterMemberFunctionHandler(
116 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo,
117 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo);
118 RegisterMemberFunctionHandler(
119 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported,
120 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported);
121 RegisterMemberFunctionHandler(
122 StringExtractorGDBRemote::eServerPacketType_qProcessInfo,
123 &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo);
124 RegisterMemberFunctionHandler(
125 StringExtractorGDBRemote::eServerPacketType_qRegisterInfo,
126 &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo);
127 RegisterMemberFunctionHandler(
128 StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState,
129 &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState);
130 RegisterMemberFunctionHandler(
131 StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState,
132 &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState);
133 RegisterMemberFunctionHandler(
134 StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR,
135 &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR);
136 RegisterMemberFunctionHandler(
137 StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir,
138 &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir);
139 RegisterMemberFunctionHandler(
140 StringExtractorGDBRemote::eServerPacketType_qsThreadInfo,
141 &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo);
142 RegisterMemberFunctionHandler(
143 StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo,
144 &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo);
145 RegisterMemberFunctionHandler(
146 StringExtractorGDBRemote::eServerPacketType_jThreadsInfo,
147 &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo);
148 RegisterMemberFunctionHandler(
149 StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo,
150 &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo);
151 RegisterMemberFunctionHandler(
152 StringExtractorGDBRemote::eServerPacketType_qXfer_auxv_read,
153 &GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read);
154 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s,
155 &GDBRemoteCommunicationServerLLGS::Handle_s);
156 RegisterMemberFunctionHandler(
157 StringExtractorGDBRemote::eServerPacketType_stop_reason,
158 &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ?
159 RegisterMemberFunctionHandler(
160 StringExtractorGDBRemote::eServerPacketType_vAttach,
161 &GDBRemoteCommunicationServerLLGS::Handle_vAttach);
162 RegisterMemberFunctionHandler(
163 StringExtractorGDBRemote::eServerPacketType_vCont,
164 &GDBRemoteCommunicationServerLLGS::Handle_vCont);
165 RegisterMemberFunctionHandler(
166 StringExtractorGDBRemote::eServerPacketType_vCont_actions,
167 &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions);
168 RegisterMemberFunctionHandler(
169 StringExtractorGDBRemote::eServerPacketType_x,
170 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
171 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z,
172 &GDBRemoteCommunicationServerLLGS::Handle_Z);
173 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z,
174 &GDBRemoteCommunicationServerLLGS::Handle_z);
Pavel Labath4a705e72017-02-24 09:29:14 +0000175 RegisterMemberFunctionHandler(
176 StringExtractorGDBRemote::eServerPacketType_QPassSignals,
177 &GDBRemoteCommunicationServerLLGS::Handle_QPassSignals);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000178
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +0000179 RegisterMemberFunctionHandler(
180 StringExtractorGDBRemote::eServerPacketType_jTraceStart,
181 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStart);
182 RegisterMemberFunctionHandler(
183 StringExtractorGDBRemote::eServerPacketType_jTraceBufferRead,
184 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
185 RegisterMemberFunctionHandler(
186 StringExtractorGDBRemote::eServerPacketType_jTraceMetaRead,
187 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
188 RegisterMemberFunctionHandler(
189 StringExtractorGDBRemote::eServerPacketType_jTraceStop,
190 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStop);
191 RegisterMemberFunctionHandler(
192 StringExtractorGDBRemote::eServerPacketType_jTraceConfigRead,
193 &GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead);
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k,
Zachary Turner97206d52017-05-12 04:51:55 +0000196 [this](StringExtractorGDBRemote packet, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 bool &interrupt, bool &quit) {
198 quit = true;
199 return this->Handle_k(packet);
200 });
Tamas Berghammere13c2732015-02-11 10:29:30 +0000201}
202
Pavel Labath11e59172017-12-18 14:31:39 +0000203void GDBRemoteCommunicationServerLLGS::SetLaunchInfo(const ProcessLaunchInfo &info) {
204 m_process_launch_info = info;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000205}
206
Zachary Turner97206d52017-05-12 04:51:55 +0000207Status GDBRemoteCommunicationServerLLGS::LaunchProcess() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 if (!m_process_launch_info.GetArguments().GetArgumentCount())
Zachary Turner97206d52017-05-12 04:51:55 +0000211 return Status("%s: no process command line specified to launch",
212 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 const bool should_forward_stdio =
215 m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
216 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
217 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr;
218 m_process_launch_info.SetLaunchInSeparateProcessGroup(true);
219 m_process_launch_info.GetFlags().Set(eLaunchFlagDebug);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 const bool default_to_use_pty = true;
222 m_process_launch_info.FinalizeFileActions(nullptr, default_to_use_pty);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 {
225 std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex);
Pavel Labath82abefa2017-07-18 09:24:48 +0000226 assert(!m_debugged_process_up && "lldb-server creating debugged "
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 "process but one already exists");
Pavel Labath96e600f2017-07-07 11:02:19 +0000228 auto process_or =
229 m_process_factory.Launch(m_process_launch_info, *this, m_mainloop);
Pavel Labath8630d382017-12-14 14:56:29 +0000230 if (!process_or)
231 return Status(process_or.takeError());
Pavel Labath82abefa2017-07-18 09:24:48 +0000232 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000234
Adrian Prantl05097242018-04-30 16:49:04 +0000235 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000240 if (should_forward_stdio) {
241 // nullptr means it's not redirected to file or pty (in case of LLGS local)
Adrian Prantl05097242018-04-30 16:49:04 +0000242 // 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 Labath82abefa2017-07-18 09:24:48 +0000244 LLDB_LOG(log,
245 "pid = {0}: setting up stdout/stderr redirection via $O "
246 "gdb-remote commands",
247 m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 // Setup stdout/stderr mapping from inferior to $O
Pavel Labath82abefa2017-07-18 09:24:48 +0000250 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (terminal_fd >= 0) {
252 if (log)
253 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
254 "inferior STDIO fd to %d",
255 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000256 Status status = SetSTDIOFileDescriptor(terminal_fd);
257 if (status.Fail())
258 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 } else {
260 if (log)
261 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
262 "inferior STDIO since terminal fd reported as %d",
263 __FUNCTION__, terminal_fd);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 } else {
Pavel Labath82abefa2017-07-18 09:24:48 +0000266 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 Stoneb9c1b512016-09-06 20:57:50 +0000270 }
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 printf("Launched '%s' as process %" PRIu64 "...\n",
273 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
Pavel Labath82abefa2017-07-18 09:24:48 +0000274 m_debugged_process_up->GetID());
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000275
Pavel Labath96e600f2017-07-07 11:02:19 +0000276 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000277}
278
Zachary Turner97206d52017-05-12 04:51:55 +0000279Status GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 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 Labath82abefa2017-07-18 09:24:48 +0000287 if (m_debugged_process_up &&
288 m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID)
Pavel Labatha70512a2018-04-11 13:30:54 +0000289 return Status("cannot attach to process %" PRIu64
Zachary Turner97206d52017-05-12 04:51:55 +0000290 " when another process with pid %" PRIu64
291 " is being debugged.",
Pavel Labath82abefa2017-07-18 09:24:48 +0000292 pid, m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293
294 // Try to attach.
Pavel Labath96e600f2017-07-07 11:02:19 +0000295 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 Stoneb9c1b512016-09-06 20:57:50 +0000301 }
Pavel Labath82abefa2017-07-18 09:24:48 +0000302 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303
304 // Setup stdout/stderr mapping from inferior.
Pavel Labath82abefa2017-07-18 09:24:48 +0000305 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 if (terminal_fd >= 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000307 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
309 "inferior STDIO fd to %d",
310 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000311 Status status = SetSTDIOFileDescriptor(terminal_fd);
312 if (status.Fail())
313 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 } 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 Labath96e600f2017-07-07 11:02:19 +0000322 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323}
324
325void 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 Berghammere13c2732015-02-11 10:29:30 +0000335}
336
337GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338GDBRemoteCommunicationServerLLGS::SendWResponse(
339 NativeProcessProtocol *process) {
340 assert(process && "process cannot be NULL");
341 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 // send W notification
Pavel Labath3508fc82017-06-19 12:47:50 +0000344 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 Berghammere13c2732015-02-11 10:29:30 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 StreamGDBRemote response;
350 response.PutChar('E');
351 response.PutHex8(GDBRemoteServerError::eErrorExitStatus);
352 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
Pavel Labath3508fc82017-06-19 12:47:50 +0000354
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 Berghammere13c2732015-02-11 10:29:30 +0000361}
362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363static 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 Berghammere13c2732015-02-11 10:29:30 +0000373}
374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375static void WriteRegisterValueInHexFixedWidth(
Pavel Labathd37349f2017-11-10 11:05:49 +0000376 StreamString &response, NativeRegisterContext &reg_ctx,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000377 const RegisterInfo &reg_info, const RegisterValue *reg_value_p,
378 lldb::ByteOrder byte_order) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 RegisterValue reg_value;
380 if (!reg_value_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000381 Status error = reg_ctx.ReadRegister(&reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 if (error.Success())
383 reg_value_p = &reg_value;
384 // else log.
385 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 if (reg_value_p) {
388 AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(),
Pavel Labathe0a5b572017-01-20 14:17:16 +0000389 reg_value_p->GetByteSize(),
390 byte_order == lldb::eByteOrderLittle);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 } 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 Berghammere13c2732015-02-11 10:29:30 +0000396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000398}
399
Pavel Labathe0a5b572017-01-20 14:17:16 +0000400static JSONObject::SP GetRegistersAsJSON(NativeThreadProtocol &thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath4a4bb122015-07-16 14:14:35 +0000402
Pavel Labathd37349f2017-11-10 11:05:49 +0000403 NativeRegisterContext& reg_ctx = thread.GetRegisterContext();
Pavel Labath4a4bb122015-07-16 14:14:35 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 JSONObject::SP register_object_sp = std::make_shared<JSONObject>();
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000406
407#ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET
Adrian Prantl05097242018-04-30 16:49:04 +0000408 // Expedite all registers in the first register set (i.e. should be GPRs)
409 // that are not contained in other registers.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 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 Labathe4fc4ef2015-07-17 10:27:42 +0000416#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 // Expedite only a couple of registers until we figure out why sending
Adrian Prantl05097242018-04-30 16:49:04 +0000418 // registers is expensive.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 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 Labathc4645bb2015-10-26 16:25:28 +0000422
Pavel Labathe0a5b572017-01-20 14:17:16 +0000423 for (const uint32_t *generic_reg_p = k_expedited_registers;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000425 uint32_t reg_num = reg_ctx.ConvertRegisterKindToRegisterNumber(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 eRegisterKindGeneric, *generic_reg_p);
427 if (reg_num == LLDB_INVALID_REGNUM)
428 continue; // Target does not support the given register.
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000429#endif
430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000432 reg_ctx.GetRegisterInfoAtIndex(reg_num);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 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 Labath4a4bb122015-07-16 14:14:35 +0000439 }
440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 if (reg_info_p->value_regs != nullptr)
442 continue; // Only expedite registers that are not contained in other
443 // registers.
Pavel Labath4a4bb122015-07-16 14:14:35 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000446 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (error.Fail()) {
448 if (log)
449 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
Pavel Labath05569f62015-07-23 09:09:29 +0000450 __FUNCTION__,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 reg_info_p->name ? reg_info_p->name : "<unnamed-register>",
452 reg_num, error.AsCString());
453 continue;
Pavel Labath05569f62015-07-23 09:09:29 +0000454 }
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 StreamString stream;
Pavel Labathd37349f2017-11-10 11:05:49 +0000457 WriteRegisterValueInHexFixedWidth(stream, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000458 &reg_value, lldb::eByteOrderBig);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459
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 Labath05569f62015-07-23 09:09:29 +0000466}
467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468static 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 Berghammere13c2732015-02-11 10:29:30 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492static JSONArray::SP GetJSONThreadsInfo(NativeProcessProtocol &process,
493 bool abridged) {
494 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 JSONArray::SP threads_array_sp = std::make_shared<JSONArray>();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 // Ensure we can get info on the given thread.
499 uint32_t thread_idx = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000500 for (NativeThreadProtocol *thread;
501 (thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 ++thread_idx) {
503
Pavel Labatha5be48b2017-10-17 15:52:16 +0000504 lldb::tid_t tid = thread->GetID();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000505
506 // Grab the reason this thread stopped.
507 struct ThreadStopInfo tid_stop_info;
508 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000509 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 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 Berghammere13c2732015-02-11 10:29:30 +0000519 }
520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>();
522 threads_array_sp->AppendObject(thread_obj_sp);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000523
Pavel Labathe0a5b572017-01-20 14:17:16 +0000524 if (!abridged) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000525 if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread))
Pavel Labathe0a5b572017-01-20 14:17:16 +0000526 thread_obj_sp->SetObject("registers", registers_sp);
527 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 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 Berghammere13c2732015-02-11 10:29:30 +0000532
Pavel Labatha5be48b2017-10-17 15:52:16 +0000533 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 if (!thread_name.empty())
535 thread_obj_sp->SetObject("name",
536 std::make_shared<JSONString>(thread_name));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 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 Berghammere13c2732015-02-11 10:29:30 +0000541
542 if (!description.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 thread_obj_sp->SetObject("description",
544 std::make_shared<JSONString>(description));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 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
567GDBRemoteCommunication::PacketResult
568GDBRemoteCommunicationServerLLGS::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 Labath82abefa2017-07-18 09:24:48 +0000573 if (!m_debugged_process_up ||
574 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 return SendErrorResponse(50);
576
Pavel Labath82abefa2017-07-18 09:24:48 +0000577 LLDB_LOG(log, "preparing packet for pid {0} tid {1}",
578 m_debugged_process_up->GetID(), tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579
580 // Ensure we can get info on the given thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000581 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
582 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 return SendErrorResponse(51);
584
585 // Grab the reason this thread stopped.
586 struct ThreadStopInfo tid_stop_info;
587 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000588 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 return SendErrorResponse(52);
590
591 // FIXME implement register handling for exec'd inferiors.
Adrian Prantl05097242018-04-30 16:49:04 +0000592 // if (tid_stop_info.reason == eStopReasonExec) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 // 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 Labath82abefa2017-07-18 09:24:48 +0000601 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 Stoneb9c1b512016-09-06 20:57:50 +0000606
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 Labatha5be48b2017-10-17 15:52:16 +0000614 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 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 Parsons771ef6d2016-11-02 20:34:10 +0000620 response.PutCString(thread_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 } else {
622 // The thread name contains special chars, send as hex bytes.
623 response.PutCString("hexname:");
624 response.PutCStringAsRawHex8(thread_name.c_str());
625 }
626 response.PutChar(';');
627 }
628
Adrian Prantl05097242018-04-30 16:49:04 +0000629 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000632 // "threads:10a,10b,10c;"
Adrian Prantl05097242018-04-30 16:49:04 +0000633 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000637 if (m_list_threads_in_stop_reply) {
638 response.PutCString("threads:");
639
640 uint32_t thread_index = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000641 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 Stoneb9c1b512016-09-06 20:57:50 +0000645 if (thread_index > 0)
646 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +0000647 response.Printf("%" PRIx64, listed_thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 }
649 response.PutChar(';');
650
Adrian Prantl05097242018-04-30 16:49:04 +0000651 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000656 if (thread_index > 0) {
657 const bool threads_with_valid_stop_info_only = true;
658 JSONArray::SP threads_info_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +0000659 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 if (threads_info_sp) {
661 response.PutCString("jstopinfo:");
662 StreamString unescaped_response;
663 threads_info_sp->Write(unescaped_response);
664 response.PutCStringAsRawHex8(unescaped_response.GetData());
665 response.PutChar(';');
Pavel Labath82abefa2017-07-18 09:24:48 +0000666 } else
667 LLDB_LOG(log, "failed to prepare a jstopinfo field for pid {0}",
668 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
Pavel Labathe0a5b572017-01-20 14:17:16 +0000670
671 uint32_t i = 0;
672 response.PutCString("thread-pcs");
673 char delimiter = ':';
Pavel Labatha5be48b2017-10-17 15:52:16 +0000674 for (NativeThreadProtocol *thread;
675 (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
Pavel Labathe0a5b572017-01-20 14:17:16 +0000676 ++i) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000677 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
Pavel Labathe0a5b572017-01-20 14:17:16 +0000678
Pavel Labathd37349f2017-11-10 11:05:49 +0000679 uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber(
Pavel Labathe0a5b572017-01-20 14:17:16 +0000680 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
681 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000682 reg_ctx.GetRegisterInfoAtIndex(reg_to_read);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000683
684 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000685 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000686 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 Labathd37349f2017-11-10 11:05:49 +0000698 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000699 &reg_value, endian::InlHostByteOrder());
700 }
701
702 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 }
704
705 //
706 // Expedite registers.
707 //
708
709 // Grab the register context.
Pavel Labathd37349f2017-11-10 11:05:49 +0000710 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 Stoneb9c1b512016-09-06 20:57:50 +0000722
Pavel Labathd37349f2017-11-10 11:05:49 +0000723 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 &reg_value, lldb::eByteOrderBig);
743 response.PutChar(';');
744 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 if (log)
Pavel Labathd37349f2017-11-10 11:05:49 +0000746 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to read "
747 "register '%s' index %" PRIu32 ": %s",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 __FUNCTION__,
Pavel Labathd37349f2017-11-10 11:05:49 +0000749 reg_info_p->name ? reg_info_p->name
750 : "<unnamed-register>",
751 *reg_num_p, error.AsCString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000752 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 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:");
765 response.PutCStringAsRawHex8(description.c_str());
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 Berghammere13c2732015-02-11 10:29:30 +0000783}
784
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited(
786 NativeProcessProtocol *process) {
787 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 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 Berghammere13c2732015-02-11 10:29:30 +0000795 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
797 "notification for PID %" PRIu64 ", state: eStateExited",
798 __FUNCTION__, process->GetID());
799 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000800
Adrian Prantl05097242018-04-30 16:49:04 +0000801 // Close the pipe to the inferior terminal i/o if we launched it and set one
802 // up.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 MaybeCloseInferiorTerminalConnection();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 // We are ready to exit the debug monitor.
806 m_exit_now = true;
Pavel Labathd8b3c1a2017-12-18 09:44:29 +0000807 m_mainloop.RequestTermination();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000808}
809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped(
811 NativeProcessProtocol *process) {
812 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
815 if (log)
816 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000817
Adrian Prantl05097242018-04-30 16:49:04 +0000818 // Send the stop reason unless this is the stop after the launch or attach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 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 Berghammere13c2732015-02-11 10:29:30 +0000832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 break;
834 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000835}
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837void 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 Prantl05097242018-04-30 16:49:04 +0000853 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000855 SendProcessOutput();
856 // Then stop the forwarding, so that any late output (see llvm.org/pr25652)
Adrian Prantl05097242018-04-30 16:49:04 +0000857 // does not interfere with our protocol.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 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 Berghammere13c2732015-02-11 10:29:30 +0000874 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 break;
876 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 // Remember the previous state reported to us.
879 m_inferior_prev_state = state;
880}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000881
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) {
883 ClearProcessSpecificData();
884}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000885
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886void 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 Turner97206d52017-05-12 04:51:55 +0000903 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 while (true) {
Pavel Labath1eff73c2016-11-24 10:54:49 +0000905 const PacketResult result = GetPacketAndSendResponse(
906 std::chrono::microseconds(0), error, interrupt, done);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 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 Turner97206d52017-05-12 04:51:55 +0000921Status GDBRemoteCommunicationServerLLGS::InitializeConnection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 std::unique_ptr<Connection> &&connection) {
923 IOObjectSP read_object_sp = connection->GetReadObject();
924 GDBRemoteCommunicationServer::SetConnection(connection.release());
925
Zachary Turner97206d52017-05-12 04:51:55 +0000926 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 m_network_handle_up = m_mainloop.RegisterReadObject(
928 read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); },
929 error);
930 return error;
931}
932
933GDBRemoteCommunication::PacketResult
934GDBRemoteCommunicationServerLLGS::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 Turner97206d52017-05-12 04:51:55 +0000948Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) {
949 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950
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 Turner97206d52017-05-12 04:51:55 +0000967 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968}
969
970void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() {
971 // Don't forward if not connected (e.g. when attaching).
972 if (!m_stdio_communication.IsConnected())
973 return;
974
Zachary Turner97206d52017-05-12 04:51:55 +0000975 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 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
991void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() {
992 m_stdio_handle_up.reset();
993}
994
995void GDBRemoteCommunicationServerLLGS::SendProcessOutput() {
996 char buffer[1024];
997 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +0000998 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 while (true) {
Pavel Labathc4063ee2016-11-25 11:58:44 +00001000 size_t bytes_read = m_stdio_communication.Read(
1001 buffer, sizeof buffer, std::chrono::microseconds(0), status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 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
1025GDBRemoteCommunication::PacketResult
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001026GDBRemoteCommunicationServerLLGS::Handle_jTraceStart(
1027 StringExtractorGDBRemote &packet) {
1028 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1029 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001030 if (!m_debugged_process_up ||
1031 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001032 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 Bergmanncf4321a2017-05-29 08:51:58 +00001046 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001047 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1048
1049 auto json_dict = json_object->GetAsDictionary();
1050
Pavel Labath4c950232017-05-26 13:53:39 +00001051 json_dict->GetValueForKeyAsInteger("metabuffersize", metabuffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001052 options.setMetaDataBufferSize(metabuffersize);
1053
Pavel Labath4c950232017-05-26 13:53:39 +00001054 json_dict->GetValueForKeyAsInteger("buffersize", buffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001055 options.setTraceBufferSize(buffersize);
1056
Pavel Labath4c950232017-05-26 13:53:39 +00001057 json_dict->GetValueForKeyAsInteger("type", type);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001058 options.setType(static_cast<lldb::TraceType>(type));
1059
Pavel Labath4c950232017-05-26 13:53:39 +00001060 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001061 options.setThreadID(tid);
1062
1063 StructuredData::ObjectSP custom_params_sp =
1064 json_dict->GetValueForKey("params");
1065 if (custom_params_sp &&
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001066 custom_params_sp->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001067 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 Labath82abefa2017-07-18 09:24:48 +00001081 uid = m_debugged_process_up->StartTrace(options, error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001082 LLDB_LOG(log, "uid is {0} , error is {1}", uid, error.GetError());
1083 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001084 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001085
1086 StreamGDBRemote response;
1087 response.Printf("%" PRIx64, uid);
1088 return SendPacketNoLock(response.GetString());
1089}
1090
1091GDBRemoteCommunication::PacketResult
1092GDBRemoteCommunicationServerLLGS::Handle_jTraceStop(
1093 StringExtractorGDBRemote &packet) {
1094 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001095 if (!m_debugged_process_up ||
1096 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001097 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 Bergmanncf4321a2017-05-29 08:51:58 +00001108 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001109 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1110
1111 auto json_dict = json_object->GetAsDictionary();
1112
Pavel Labath4c950232017-05-26 13:53:39 +00001113 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001114 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1115
Pavel Labath4c950232017-05-26 13:53:39 +00001116 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001117
Pavel Labath82abefa2017-07-18 09:24:48 +00001118 Status error = m_debugged_process_up->StopTrace(uid, tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001119
1120 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001121 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001122
1123 return SendOKResponse();
1124}
1125
1126GDBRemoteCommunication::PacketResult
1127GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead(
1128 StringExtractorGDBRemote &packet) {
1129
1130 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001131 if (!m_debugged_process_up ||
1132 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001133 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 Bergmanncf4321a2017-05-29 08:51:58 +00001145 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001146 return SendIllFormedResponse(packet,
1147 "jTraceConfigRead: Ill formed packet ");
1148
1149 auto json_dict = json_object->GetAsDictionary();
1150
Pavel Labath4c950232017-05-26 13:53:39 +00001151 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001152 return SendIllFormedResponse(packet,
1153 "jTraceConfigRead: Ill formed packet ");
1154
Pavel Labath4c950232017-05-26 13:53:39 +00001155 json_dict->GetValueForKeyAsInteger("threadid", threadid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001156
1157 TraceOptions options;
1158 StreamGDBRemote response;
1159
1160 options.setThreadID(threadid);
Pavel Labath82abefa2017-07-18 09:24:48 +00001161 Status error = m_debugged_process_up->GetTraceConfig(uid, options);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001162
1163 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001164 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001165
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
1184GDBRemoteCommunication::PacketResult
1185GDBRemoteCommunicationServerLLGS::Handle_jTraceRead(
1186 StringExtractorGDBRemote &packet) {
1187
1188 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001189 if (!m_debugged_process_up ||
1190 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001191 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 Addepally99e37692017-06-28 07:58:31 +00001206 uint64_t byte_count = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001207 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001208 uint64_t offset = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001209
1210 auto json_object = StructuredData::ParseJSON(packet.Peek());
1211
1212 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001213 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001214 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1215
1216 auto json_dict = json_object->GetAsDictionary();
1217
Pavel Labath4c950232017-05-26 13:53:39 +00001218 if (!json_dict->GetValueForKeyAsInteger("traceid", uid) ||
1219 !json_dict->GetValueForKeyAsInteger("offset", offset) ||
1220 !json_dict->GetValueForKeyAsInteger("buffersize", byte_count))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001221 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1222
Pavel Labath4c950232017-05-26 13:53:39 +00001223 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001224
1225 // Allocate the response buffer.
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001226 std::unique_ptr<uint8_t[]> buffer (new (std::nothrow) uint8_t[byte_count]);
1227 if (!buffer)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001228 return SendErrorResponse(0x78);
1229
1230 StreamGDBRemote response;
1231 Status error;
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001232 llvm::MutableArrayRef<uint8_t> buf(buffer.get(), byte_count);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001233
1234 if (tracetype == BufferData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001235 error = m_debugged_process_up->GetData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001236 else if (tracetype == MetaData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001237 error = m_debugged_process_up->GetMetaData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001238
1239 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001240 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001241
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001242 for (auto i : buf)
1243 response.PutHex8(i);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001244
1245 StreamGDBRemote escaped_response;
1246 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
1247 return SendPacketNoLock(escaped_response.GetString());
1248}
1249
1250GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo(
1252 StringExtractorGDBRemote &packet) {
1253 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001254 if (!m_debugged_process_up ||
1255 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 return SendErrorResponse(68);
1257
Pavel Labath82abefa2017-07-18 09:24:48 +00001258 lldb::pid_t pid = m_debugged_process_up->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259
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
1272GDBRemoteCommunication::PacketResult
1273GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) {
1274 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001275 if (!m_debugged_process_up ||
1276 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 return SendErrorResponse(68);
1278
Adrian Prantl05097242018-04-30 16:49:04 +00001279 // Make sure we set the current thread so g and p packets return the data the
1280 // gdb will expect.
Pavel Labath82abefa2017-07-18 09:24:48 +00001281 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 SetCurrentThreadID(tid);
1283
Pavel Labatha5be48b2017-10-17 15:52:16 +00001284 NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread();
1285 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 return SendErrorResponse(69);
1287
1288 StreamString response;
Pavel Labatha5be48b2017-10-17 15:52:16 +00001289 response.Printf("QC%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290
1291 return SendPacketNoLock(response.GetString());
1292}
1293
1294GDBRemoteCommunication::PacketResult
1295GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) {
1296 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1297
1298 StopSTDIOForwarding();
1299
Pavel Labath82abefa2017-07-18 09:24:48 +00001300 if (!m_debugged_process_up) {
1301 LLDB_LOG(log, "No debugged process found.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 return PacketResult::Success;
1303 }
1304
Pavel Labath82abefa2017-07-18 09:24:48 +00001305 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 Stoneb9c1b512016-09-06 20:57:50 +00001309
1310 // No OK response for kill packet.
1311 // return SendOKResponse ();
1312 return PacketResult::Success;
1313}
1314
1315GDBRemoteCommunication::PacketResult
1316GDBRemoteCommunicationServerLLGS::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
1326GDBRemoteCommunication::PacketResult
1327GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir(
1328 StringExtractorGDBRemote &packet) {
1329 packet.SetFilePos(::strlen("QSetWorkingDir:"));
1330 std::string path;
1331 packet.GetHexByteString(path);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001332 m_process_launch_info.SetWorkingDirectory(FileSpec(path));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 return SendOKResponse();
1334}
1335
1336GDBRemoteCommunication::PacketResult
1337GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir(
1338 StringExtractorGDBRemote &packet) {
1339 FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()};
1340 if (working_dir) {
1341 StreamString response;
1342 response.PutCStringAsRawHex8(working_dir.GetCString());
1343 return SendPacketNoLock(response.GetString());
1344 }
1345
1346 return SendErrorResponse(14);
1347}
1348
1349GDBRemoteCommunication::PacketResult
1350GDBRemoteCommunicationServerLLGS::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 Labath82abefa2017-07-18 09:24:48 +00001356 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 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 Turner97206d52017-05-12 04:51:55 +00001386 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387
1388 // We have two branches: what to do if a continue thread is specified (in
Adrian Prantl05097242018-04-30 16:49:04 +00001389 // 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 Stoneb9c1b512016-09-06 20:57:50 +00001392
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 Labath82abefa2017-07-18 09:24:48 +00001408 error = m_debugged_process_up->Signal(signo);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001410 LLDB_LOG(log, "failed to send signal for process {0}: {1}",
1411 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412
1413 return SendErrorResponse(0x52);
1414 }
1415 }
1416
1417 // Resume the threads.
Pavel Labath82abefa2017-07-18 09:24:48 +00001418 error = m_debugged_process_up->Resume(resume_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001420 LLDB_LOG(log, "failed to resume threads for process {0}: {1}",
1421 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422
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
1430GDBRemoteCommunication::PacketResult
1431GDBRemoteCommunicationServerLLGS::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 Labath82abefa2017-07-18 09:24:48 +00001441 LLDB_LOG(log, "not implemented for c[address] variant [{0} remains]",
1442 packet.Peek());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1444 }
1445
1446 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001447 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 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 Labath82abefa2017-07-18 09:24:48 +00001458 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001460 LLDB_LOG(log, "c failed for process {0}: {1}",
1461 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1463 }
1464
Pavel Labath82abefa2017-07-18 09:24:48 +00001465 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 // No response required from continue.
1467 return PacketResult::Success;
1468}
1469
1470GDBRemoteCommunication::PacketResult
1471GDBRemoteCommunicationServerLLGS::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
1479GDBRemoteCommunication::PacketResult
1480GDBRemoteCommunicationServerLLGS::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 Labath82abefa2017-07-18 09:24:48 +00001509 if (!m_debugged_process_up) {
1510 LLDB_LOG(log, "no debugged process");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 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 Labathabadc222015-11-27 13:33:29 +00001551
Tamas Berghammere13c2732015-02-11 10:29:30 +00001552 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 return SendIllFormedResponse(packet, "Unsupported vCont action");
1554 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00001555 }
1556
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 // Parse out optional :{thread-id} value.
1558 if (packet.GetBytesLeft() && (*packet.Peek() == ':')) {
1559 // Consume the separator.
1560 packet.GetChar();
Tamas Berghammere13c2732015-02-11 10:29:30 +00001561
Kate Stoneb9c1b512016-09-06 20:57:50 +00001562 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 Labath77dc9562015-07-13 10:44:55 +00001566 }
1567
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 thread_actions.Append(thread_action);
1569 }
Pavel Labath77dc9562015-07-13 10:44:55 +00001570
Pavel Labath82abefa2017-07-18 09:24:48 +00001571 Status error = m_debugged_process_up->Resume(thread_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001573 LLDB_LOG(log, "vCont failed for process {0}: {1}",
1574 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1576 }
1577
Pavel Labath82abefa2017-07-18 09:24:48 +00001578 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 // No response required from vCont.
1580 return PacketResult::Success;
Pavel Labath77dc9562015-07-13 10:44:55 +00001581}
1582
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) {
1584 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001585 LLDB_LOG(log, "setting current thread id to {0}", tid);
Pavel Labath77dc9562015-07-13 10:44:55 +00001586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 m_current_tid = tid;
Pavel Labath82abefa2017-07-18 09:24:48 +00001588 if (m_debugged_process_up)
1589 m_debugged_process_up->SetCurrentThreadID(m_current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590}
1591
1592void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) {
1593 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001594 LLDB_LOG(log, "setting continue thread id to {0}", tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595
1596 m_continue_tid = tid;
Pavel Labath77dc9562015-07-13 10:44:55 +00001597}
1598
Tamas Berghammere13c2732015-02-11 10:29:30 +00001599GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600GDBRemoteCommunicationServerLLGS::Handle_stop_reason(
1601 StringExtractorGDBRemote &packet) {
1602 // Handle the $? gdbremote command.
Tamas Berghammere13c2732015-02-11 10:29:30 +00001603
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604 // If no process, indicate error
Pavel Labath82abefa2017-07-18 09:24:48 +00001605 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 return SendErrorResponse(02);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001607
Pavel Labath82abefa2017-07-18 09:24:48 +00001608 return SendStopReasonForState(m_debugged_process_up->GetState());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001609}
1610
1611GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612GDBRemoteCommunicationServerLLGS::SendStopReasonForState(
1613 lldb::StateType process_state) {
1614 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 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 Berghammere13c2732015-02-11 10:29:30 +00001625
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 case eStateSuspended:
1627 case eStateStopped:
1628 case eStateCrashed: {
Pavel Labath82abefa2017-07-18 09:24:48 +00001629 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Adrian Prantl05097242018-04-30 16:49:04 +00001630 // Make sure we set the current thread so g and p packets return the data
1631 // the gdb will expect.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 SetCurrentThreadID(tid);
1633 return SendStopReplyPacketForThread(tid);
1634 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001635
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 case eStateInvalid:
1637 case eStateUnloaded:
1638 case eStateExited:
Pavel Labath82abefa2017-07-18 09:24:48 +00001639 return SendWResponse(m_debugged_process_up.get());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 default:
Pavel Labath82abefa2017-07-18 09:24:48 +00001642 LLDB_LOG(log, "pid {0}, current state reporting not handled: {1}",
1643 m_debugged_process_up->GetID(), process_state);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 break;
1645 }
Pavel Labath424b2012015-07-28 09:06:56 +00001646
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647 return SendErrorResponse(0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001648}
1649
1650GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo(
1652 StringExtractorGDBRemote &packet) {
1653 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001654 if (!m_debugged_process_up ||
1655 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001657
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 // Ensure we have a thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001659 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0);
1660 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 return SendErrorResponse(69);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001662
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 // Get the register context for the first thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00001664 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665
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 Labathd37349f2017-11-10 11:05:49 +00001675 if (reg_index >= reg_context.GetUserRegisterCount())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676 return SendErrorResponse(69);
1677
Pavel Labathd37349f2017-11-10 11:05:49 +00001678 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679 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 Giusticda0ae42016-09-08 14:16:45 +00001749 case eFormatVectorOfUInt64:
1750 response.PutCString("format:vector-uint64;");
1751 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 case eFormatVectorOfUInt128:
1753 response.PutCString("format:vector-uint128;");
1754 break;
1755 default:
1756 break;
1757 };
1758
1759 const char *const register_set_name =
Pavel Labathd37349f2017-11-10 11:05:49 +00001760 reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 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 Berghammere13c2732015-02-11 10:29:30 +00001828 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001829 response.PutChar(';');
1830 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001831
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 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 Berghammere13c2732015-02-11 10:29:30 +00001852}
1853
1854GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo(
1856 StringExtractorGDBRemote &packet) {
1857 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1858
1859 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001860 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 Stoneb9c1b512016-09-06 20:57:50 +00001865 return SendOKResponse();
1866 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001867
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 StreamGDBRemote response;
1869 response.PutChar('m');
1870
Pavel Labath82abefa2017-07-18 09:24:48 +00001871 LLDB_LOG(log, "starting thread iteration");
Pavel Labatha5be48b2017-10-17 15:52:16 +00001872 NativeThreadProtocol *thread;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873 uint32_t thread_index;
1874 for (thread_index = 0,
Pavel Labatha5be48b2017-10-17 15:52:16 +00001875 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 Stoneb9c1b512016-09-06 20:57:50 +00001880 if (thread_index > 0)
1881 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +00001882 response.Printf("%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001883 }
1884
Pavel Labath82abefa2017-07-18 09:24:48 +00001885 LLDB_LOG(log, "finished thread iteration");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 return SendPacketNoLock(response.GetString());
1887}
1888
1889GDBRemoteCommunication::PacketResult
1890GDBRemoteCommunicationServerLLGS::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
1897GDBRemoteCommunication::PacketResult
1898GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) {
1899 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1900
1901 // Parse out the register number from the request.
1902 packet.SetFilePos(strlen("p"));
1903 const uint32_t reg_index =
1904 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1905 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1906 if (log)
1907 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1908 "parse register number from request \"%s\"",
1909 __FUNCTION__, packet.GetStringRef().c_str());
1910 return SendErrorResponse(0x15);
1911 }
1912
1913 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001914 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
1915 if (!thread) {
1916 LLDB_LOG(log, "failed, no thread available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001917 return SendErrorResponse(0x15);
1918 }
1919
1920 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00001921 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001922
1923 // Return the end of registers response if we've iterated one past the end of
1924 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001925 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926 if (log)
1927 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1928 "register %" PRIu32 " beyond register count %" PRIu32,
1929 __FUNCTION__, reg_index,
Pavel Labathd37349f2017-11-10 11:05:49 +00001930 reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001931 return SendErrorResponse(0x15);
1932 }
1933
Pavel Labathd37349f2017-11-10 11:05:49 +00001934 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001935 if (!reg_info) {
1936 if (log)
1937 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1938 "register %" PRIu32 " returned NULL",
1939 __FUNCTION__, reg_index);
1940 return SendErrorResponse(0x15);
1941 }
1942
1943 // Build the reginfos response.
1944 StreamGDBRemote response;
1945
1946 // Retrieve the value
1947 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +00001948 Status error = reg_context.ReadRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 if (error.Fail()) {
1950 if (log)
1951 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, read of "
1952 "requested register %" PRIu32 " (%s) failed: %s",
1953 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
1954 return SendErrorResponse(0x15);
1955 }
1956
1957 const uint8_t *const data =
1958 reinterpret_cast<const uint8_t *>(reg_value.GetBytes());
1959 if (!data) {
1960 if (log)
1961 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get data "
1962 "bytes from requested register %" PRIu32,
1963 __FUNCTION__, reg_index);
1964 return SendErrorResponse(0x15);
1965 }
1966
1967 // FIXME flip as needed to get data in big/little endian format for this host.
1968 for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i)
1969 response.PutHex8(data[i]);
1970
1971 return SendPacketNoLock(response.GetString());
1972}
1973
1974GDBRemoteCommunication::PacketResult
1975GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) {
1976 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1977
1978 // Ensure there is more content.
1979 if (packet.GetBytesLeft() < 1)
1980 return SendIllFormedResponse(packet, "Empty P packet");
1981
1982 // Parse out the register number from the request.
1983 packet.SetFilePos(strlen("P"));
1984 const uint32_t reg_index =
1985 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1986 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1987 if (log)
1988 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1989 "parse register number from request \"%s\"",
1990 __FUNCTION__, packet.GetStringRef().c_str());
1991 return SendErrorResponse(0x29);
1992 }
1993
1994 // Note debugserver would send an E30 here.
1995 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '='))
1996 return SendIllFormedResponse(
1997 packet, "P packet missing '=' char after register number");
1998
Kate Stoneb9c1b512016-09-06 20:57:50 +00001999 // Parse out the value.
2000 uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register
2001 size_t reg_size = packet.GetHexBytesAvail(reg_bytes);
2002
2003 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002004 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2005 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 if (log)
2007 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread "
2008 "available (thread index 0)",
2009 __FUNCTION__);
2010 return SendErrorResponse(0x28);
2011 }
2012
2013 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00002014 NativeRegisterContext &reg_context = thread->GetRegisterContext();
2015 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002016 if (!reg_info) {
2017 if (log)
2018 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2019 "register %" PRIu32 " returned NULL",
2020 __FUNCTION__, reg_index);
2021 return SendErrorResponse(0x48);
2022 }
2023
2024 // Return the end of registers response if we've iterated one past the end of
2025 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00002026 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002027 if (log)
2028 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2029 "register %" PRIu32 " beyond register count %" PRIu32,
Pavel Labathd37349f2017-11-10 11:05:49 +00002030 __FUNCTION__, reg_index, reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 return SendErrorResponse(0x47);
2032 }
2033
Adrian Prantl05097242018-04-30 16:49:04 +00002034 // The dwarf expression are evaluate on host site which may cause register
2035 // size to change Hence the reg_size may not be same as reg_info->bytes_size
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 if ((reg_size != reg_info->byte_size) &&
2037 !(reg_info->dynamic_size_dwarf_expr_bytes)) {
2038 return SendIllFormedResponse(packet, "P packet register size is incorrect");
2039 }
2040
2041 // Build the reginfos response.
2042 StreamGDBRemote response;
2043
Pavel Labath578a4252017-11-09 10:43:16 +00002044 RegisterValue reg_value(
2045 reg_bytes, reg_size,
2046 m_debugged_process_up->GetArchitecture().GetByteOrder());
Pavel Labathd37349f2017-11-10 11:05:49 +00002047 Status error = reg_context.WriteRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002048 if (error.Fail()) {
2049 if (log)
2050 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, write of "
2051 "requested register %" PRIu32 " (%s) failed: %s",
2052 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
2053 return SendErrorResponse(0x32);
2054 }
2055
2056 return SendOKResponse();
2057}
2058
2059GDBRemoteCommunication::PacketResult
2060GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) {
2061 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2062
2063 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002064 if (!m_debugged_process_up ||
2065 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002066 if (log)
2067 log->Printf(
2068 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2069 __FUNCTION__);
2070 return SendErrorResponse(0x15);
2071 }
2072
2073 // Parse out which variant of $H is requested.
2074 packet.SetFilePos(strlen("H"));
2075 if (packet.GetBytesLeft() < 1) {
2076 if (log)
2077 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, H command "
2078 "missing {g,c} variant",
2079 __FUNCTION__);
2080 return SendIllFormedResponse(packet, "H command missing {g,c} variant");
2081 }
2082
2083 const char h_variant = packet.GetChar();
2084 switch (h_variant) {
2085 case 'g':
2086 break;
2087
2088 case 'c':
2089 break;
2090
2091 default:
2092 if (log)
2093 log->Printf(
2094 "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c",
2095 __FUNCTION__, h_variant);
2096 return SendIllFormedResponse(packet,
2097 "H variant unsupported, should be c or g");
2098 }
2099
2100 // Parse out the thread number.
2101 // FIXME return a parse success/fail value. All values are valid here.
2102 const lldb::tid_t tid =
2103 packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max());
2104
2105 // Ensure we have the given thread when not specifying -1 (all threads) or 0
2106 // (any thread).
2107 if (tid != LLDB_INVALID_THREAD_ID && tid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +00002108 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2109 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002110 if (log)
2111 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
2112 " not found",
2113 __FUNCTION__, tid);
2114 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002117
Kate Stoneb9c1b512016-09-06 20:57:50 +00002118 // Now switch the given thread type.
2119 switch (h_variant) {
2120 case 'g':
2121 SetCurrentThreadID(tid);
2122 break;
2123
2124 case 'c':
2125 SetContinueThreadID(tid);
2126 break;
2127
2128 default:
2129 assert(false && "unsupported $H variant - shouldn't get here");
2130 return SendIllFormedResponse(packet,
2131 "H variant unsupported, should be c or g");
2132 }
2133
2134 return SendOKResponse();
2135}
2136
2137GDBRemoteCommunication::PacketResult
2138GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) {
2139 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2140
2141 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002142 if (!m_debugged_process_up ||
2143 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002144 if (log)
2145 log->Printf(
2146 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2147 __FUNCTION__);
2148 return SendErrorResponse(0x15);
2149 }
2150
2151 packet.SetFilePos(::strlen("I"));
2152 uint8_t tmp[4096];
2153 for (;;) {
2154 size_t read = packet.GetHexBytesAvail(tmp);
2155 if (read == 0) {
2156 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158 // write directly to stdin *this might block if stdin buffer is full*
2159 // TODO: enqueue this block in circular buffer and send window size to
2160 // remote host
2161 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00002162 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 m_stdio_communication.Write(tmp, read, status, &error);
2164 if (error.Fail()) {
2165 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002166 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002168
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002170}
2171
2172GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002173GDBRemoteCommunicationServerLLGS::Handle_interrupt(
2174 StringExtractorGDBRemote &packet) {
2175 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2176
2177 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002178 if (!m_debugged_process_up ||
2179 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
2180 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181 return SendErrorResponse(0x15);
2182 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002183
Kate Stoneb9c1b512016-09-06 20:57:50 +00002184 // Interrupt the process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002185 Status error = m_debugged_process_up->Interrupt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002186 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002187 LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(),
2188 error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002189 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
2190 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002191
Pavel Labath82abefa2017-07-18 09:24:48 +00002192 LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002193
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194 // No response required from stop all.
2195 return PacketResult::Success;
2196}
Tamas Berghammere13c2732015-02-11 10:29:30 +00002197
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198GDBRemoteCommunication::PacketResult
2199GDBRemoteCommunicationServerLLGS::Handle_memory_read(
2200 StringExtractorGDBRemote &packet) {
2201 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002202
Pavel Labath82abefa2017-07-18 09:24:48 +00002203 if (!m_debugged_process_up ||
2204 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002205 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 log->Printf(
2207 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2208 __FUNCTION__);
2209 return SendErrorResponse(0x15);
2210 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002211
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 // Parse out the memory address.
2213 packet.SetFilePos(strlen("m"));
2214 if (packet.GetBytesLeft() < 1)
2215 return SendIllFormedResponse(packet, "Too short m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002216
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217 // Read the address. Punting on validation.
2218 // FIXME replace with Hex U64 read with no default value that fails on failed
2219 // read.
2220 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002221
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 // Validate comma.
2223 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2224 return SendIllFormedResponse(packet, "Comma sep missing in m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002225
Kate Stoneb9c1b512016-09-06 20:57:50 +00002226 // Get # bytes to read.
2227 if (packet.GetBytesLeft() < 1)
2228 return SendIllFormedResponse(packet, "Length missing in m packet");
2229
2230 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2231 if (byte_count == 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002232 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002233 log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to read: "
2234 "zero-length packet",
2235 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002236 return SendOKResponse();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002238
Kate Stoneb9c1b512016-09-06 20:57:50 +00002239 // Allocate the response buffer.
2240 std::string buf(byte_count, '\0');
2241 if (buf.empty())
2242 return SendErrorResponse(0x78);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002243
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 // Retrieve the process memory.
2245 size_t bytes_read = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002246 Status error = m_debugged_process_up->ReadMemoryWithoutTrap(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 read_addr, &buf[0], byte_count, bytes_read);
2248 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002249 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2251 " mem 0x%" PRIx64 ": failed to read. Error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002252 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002253 error.AsCString());
2254 return SendErrorResponse(0x08);
2255 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002256
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 if (bytes_read == 0) {
2258 if (log)
2259 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2260 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes",
Pavel Labath82abefa2017-07-18 09:24:48 +00002261 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262 byte_count);
2263 return SendErrorResponse(0x08);
2264 }
2265
2266 StreamGDBRemote response;
2267 packet.SetFilePos(0);
2268 char kind = packet.GetChar('?');
2269 if (kind == 'x')
2270 response.PutEscapedBytes(buf.data(), byte_count);
2271 else {
2272 assert(kind == 'm');
2273 for (size_t i = 0; i < bytes_read; ++i)
2274 response.PutHex8(buf[i]);
2275 }
2276
2277 return SendPacketNoLock(response.GetString());
2278}
2279
2280GDBRemoteCommunication::PacketResult
2281GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) {
2282 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2283
Pavel Labath82abefa2017-07-18 09:24:48 +00002284 if (!m_debugged_process_up ||
2285 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286 if (log)
2287 log->Printf(
2288 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2289 __FUNCTION__);
2290 return SendErrorResponse(0x15);
2291 }
2292
2293 // Parse out the memory address.
2294 packet.SetFilePos(strlen("M"));
2295 if (packet.GetBytesLeft() < 1)
2296 return SendIllFormedResponse(packet, "Too short M packet");
2297
2298 // Read the address. Punting on validation.
2299 // FIXME replace with Hex U64 read with no default value that fails on failed
2300 // read.
2301 const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0);
2302
2303 // Validate comma.
2304 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2305 return SendIllFormedResponse(packet, "Comma sep missing in M packet");
2306
2307 // Get # bytes to read.
2308 if (packet.GetBytesLeft() < 1)
2309 return SendIllFormedResponse(packet, "Length missing in M packet");
2310
2311 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2312 if (byte_count == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002313 LLDB_LOG(log, "nothing to write: zero-length packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002314 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002315 }
2316
2317 // Validate colon.
2318 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':'))
2319 return SendIllFormedResponse(
2320 packet, "Comma sep missing in M packet after byte length");
2321
2322 // Allocate the conversion buffer.
2323 std::vector<uint8_t> buf(byte_count, 0);
2324 if (buf.empty())
2325 return SendErrorResponse(0x78);
2326
2327 // Convert the hex memory write contents to bytes.
2328 StreamGDBRemote response;
2329 const uint64_t convert_count = packet.GetHexBytes(buf, 0);
2330 if (convert_count != byte_count) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002331 LLDB_LOG(log,
2332 "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} "
2333 "to convert.",
2334 m_debugged_process_up->GetID(), write_addr, byte_count,
2335 convert_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 return SendIllFormedResponse(packet, "M content byte length specified did "
2337 "not match hex-encoded content "
2338 "length");
2339 }
2340
2341 // Write the process memory.
2342 size_t bytes_written = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002343 Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0],
Zachary Turner97206d52017-05-12 04:51:55 +00002344 byte_count, bytes_written);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002346 LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}",
2347 m_debugged_process_up->GetID(), write_addr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002348 return SendErrorResponse(0x09);
2349 }
2350
2351 if (bytes_written == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002352 LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes",
2353 m_debugged_process_up->GetID(), write_addr, byte_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002354 return SendErrorResponse(0x09);
2355 }
2356
2357 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002358}
2359
2360GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported(
2362 StringExtractorGDBRemote &packet) {
2363 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002364
Kate Stoneb9c1b512016-09-06 20:57:50 +00002365 // Currently only the NativeProcessProtocol knows if it can handle a
Adrian Prantl05097242018-04-30 16:49:04 +00002366 // qMemoryRegionInfoSupported request, but we're not guaranteed to be
2367 // attached to a process. For now we'll assume the client only asks this
2368 // when a process is being debugged.
Tamas Berghammere13c2732015-02-11 10:29:30 +00002369
Kate Stoneb9c1b512016-09-06 20:57:50 +00002370 // Ensure we have a process running; otherwise, we can't figure this out
2371 // since we won't have a NativeProcessProtocol.
Pavel Labath82abefa2017-07-18 09:24:48 +00002372 if (!m_debugged_process_up ||
2373 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002374 if (log)
2375 log->Printf(
2376 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2377 __FUNCTION__);
2378 return SendErrorResponse(0x15);
2379 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002380
Kate Stoneb9c1b512016-09-06 20:57:50 +00002381 // Test if we can get any region back when asking for the region around NULL.
2382 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002383 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002384 m_debugged_process_up->GetMemoryRegionInfo(0, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002385 if (error.Fail()) {
2386 // We don't support memory region info collection for this
2387 // NativeProcessProtocol.
2388 return SendUnimplementedResponse("");
2389 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002390
Kate Stoneb9c1b512016-09-06 20:57:50 +00002391 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002392}
2393
2394GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002395GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
2396 StringExtractorGDBRemote &packet) {
2397 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002398
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002400 if (!m_debugged_process_up ||
2401 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 if (log)
2403 log->Printf(
2404 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2405 __FUNCTION__);
2406 return SendErrorResponse(0x15);
2407 }
2408
2409 // Parse out the memory address.
2410 packet.SetFilePos(strlen("qMemoryRegionInfo:"));
2411 if (packet.GetBytesLeft() < 1)
2412 return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet");
2413
2414 // Read the address. Punting on validation.
2415 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
2416
2417 StreamGDBRemote response;
2418
2419 // Get the memory region info for the target address.
2420 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002421 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002422 m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002423 if (error.Fail()) {
2424 // Return the error message.
2425
2426 response.PutCString("error:");
2427 response.PutCStringAsRawHex8(error.AsCString());
2428 response.PutChar(';');
2429 } else {
2430 // Range start and size.
2431 response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";",
2432 region_info.GetRange().GetRangeBase(),
2433 region_info.GetRange().GetByteSize());
2434
2435 // Permissions.
2436 if (region_info.GetReadable() || region_info.GetWritable() ||
2437 region_info.GetExecutable()) {
2438 // Write permissions info.
2439 response.PutCString("permissions:");
2440
2441 if (region_info.GetReadable())
2442 response.PutChar('r');
2443 if (region_info.GetWritable())
2444 response.PutChar('w');
2445 if (region_info.GetExecutable())
2446 response.PutChar('x');
2447
2448 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002449 }
2450
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451 // Name
2452 ConstString name = region_info.GetName();
2453 if (name) {
2454 response.PutCString("name:");
2455 response.PutCStringAsRawHex8(name.AsCString());
2456 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002457 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002458 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002459
Kate Stoneb9c1b512016-09-06 20:57:50 +00002460 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002461}
2462
2463GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) {
2465 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002466 if (!m_debugged_process_up ||
2467 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002469 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002470 return SendErrorResponse(0x15);
2471 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002472
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473 // Parse out software or hardware breakpoint or watchpoint requested.
2474 packet.SetFilePos(strlen("Z"));
2475 if (packet.GetBytesLeft() < 1)
2476 return SendIllFormedResponse(
2477 packet, "Too short Z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002478
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479 bool want_breakpoint = true;
2480 bool want_hardware = false;
2481 uint32_t watch_flags = 0;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002482
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483 const GDBStoppointType stoppoint_type =
2484 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2485 switch (stoppoint_type) {
2486 case eBreakpointSoftware:
2487 want_hardware = false;
2488 want_breakpoint = true;
2489 break;
2490 case eBreakpointHardware:
2491 want_hardware = true;
2492 want_breakpoint = true;
2493 break;
2494 case eWatchpointWrite:
2495 watch_flags = 1;
2496 want_hardware = true;
2497 want_breakpoint = false;
2498 break;
2499 case eWatchpointRead:
2500 watch_flags = 2;
2501 want_hardware = true;
2502 want_breakpoint = false;
2503 break;
2504 case eWatchpointReadWrite:
2505 watch_flags = 3;
2506 want_hardware = true;
2507 want_breakpoint = false;
2508 break;
2509 case eStoppointInvalid:
2510 return SendIllFormedResponse(
2511 packet, "Z packet had invalid software/hardware specifier");
2512 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002513
Kate Stoneb9c1b512016-09-06 20:57:50 +00002514 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2515 return SendIllFormedResponse(
2516 packet, "Malformed Z packet, expecting comma after stoppoint type");
2517
2518 // Parse out the stoppoint address.
2519 if (packet.GetBytesLeft() < 1)
2520 return SendIllFormedResponse(packet, "Too short Z packet, missing address");
2521 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
2522
2523 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2524 return SendIllFormedResponse(
2525 packet, "Malformed Z packet, expecting comma after address");
2526
2527 // Parse out the stoppoint size (i.e. size hint for opcode size).
2528 const uint32_t size =
2529 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
2530 if (size == std::numeric_limits<uint32_t>::max())
2531 return SendIllFormedResponse(
2532 packet, "Malformed Z packet, failed to parse size argument");
2533
2534 if (want_breakpoint) {
2535 // Try to set the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002536 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002537 m_debugged_process_up->SetBreakpoint(addr, size, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002538 if (error.Success())
2539 return SendOKResponse();
2540 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002541 LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}",
2542 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002543 return SendErrorResponse(0x09);
2544 } else {
2545 // Try to set the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002546 const Status error = m_debugged_process_up->SetWatchpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002547 addr, size, watch_flags, want_hardware);
2548 if (error.Success())
2549 return SendOKResponse();
2550 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002551 LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}",
2552 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002553 return SendErrorResponse(0x09);
2554 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002555}
2556
2557GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002558GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) {
2559 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002560 if (!m_debugged_process_up ||
2561 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002562 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002563 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002564 return SendErrorResponse(0x15);
2565 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002566
Kate Stoneb9c1b512016-09-06 20:57:50 +00002567 // Parse out software or hardware breakpoint or watchpoint requested.
2568 packet.SetFilePos(strlen("z"));
2569 if (packet.GetBytesLeft() < 1)
2570 return SendIllFormedResponse(
2571 packet, "Too short z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002572
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573 bool want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002574 bool want_hardware = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002575
Kate Stoneb9c1b512016-09-06 20:57:50 +00002576 const GDBStoppointType stoppoint_type =
2577 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2578 switch (stoppoint_type) {
2579 case eBreakpointHardware:
2580 want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002581 want_hardware = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002582 break;
2583 case eBreakpointSoftware:
2584 want_breakpoint = true;
2585 break;
2586 case eWatchpointWrite:
2587 want_breakpoint = false;
2588 break;
2589 case eWatchpointRead:
2590 want_breakpoint = false;
2591 break;
2592 case eWatchpointReadWrite:
2593 want_breakpoint = false;
2594 break;
2595 default:
2596 return SendIllFormedResponse(
2597 packet, "z packet had invalid software/hardware specifier");
2598 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002599
Kate Stoneb9c1b512016-09-06 20:57:50 +00002600 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2601 return SendIllFormedResponse(
2602 packet, "Malformed z packet, expecting comma after stoppoint type");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002603
Kate Stoneb9c1b512016-09-06 20:57:50 +00002604 // Parse out the stoppoint address.
2605 if (packet.GetBytesLeft() < 1)
2606 return SendIllFormedResponse(packet, "Too short z packet, missing address");
2607 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002608
Kate Stoneb9c1b512016-09-06 20:57:50 +00002609 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2610 return SendIllFormedResponse(
2611 packet, "Malformed z packet, expecting comma after address");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002612
Kate Stoneb9c1b512016-09-06 20:57:50 +00002613 /*
2614 // Parse out the stoppoint size (i.e. size hint for opcode size).
2615 const uint32_t size = packet.GetHexMaxU32 (false,
2616 std::numeric_limits<uint32_t>::max ());
2617 if (size == std::numeric_limits<uint32_t>::max ())
2618 return SendIllFormedResponse(packet, "Malformed z packet, failed to parse
2619 size argument");
2620 */
Tamas Berghammere13c2732015-02-11 10:29:30 +00002621
Kate Stoneb9c1b512016-09-06 20:57:50 +00002622 if (want_breakpoint) {
2623 // Try to clear the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002624 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002625 m_debugged_process_up->RemoveBreakpoint(addr, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 if (error.Success())
2627 return SendOKResponse();
2628 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002629 LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}",
2630 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002631 return SendErrorResponse(0x09);
2632 } else {
2633 // Try to clear the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002634 const Status error = m_debugged_process_up->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 if (error.Success())
2636 return SendOKResponse();
2637 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002638 LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}",
2639 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640 return SendErrorResponse(0x09);
2641 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002642}
2643
2644GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002645GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) {
2646 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002647
Kate Stoneb9c1b512016-09-06 20:57:50 +00002648 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002649 if (!m_debugged_process_up ||
2650 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002651 if (log)
2652 log->Printf(
2653 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2654 __FUNCTION__);
2655 return SendErrorResponse(0x32);
2656 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002657
Kate Stoneb9c1b512016-09-06 20:57:50 +00002658 // We first try to use a continue thread id. If any one or any all set, use
Adrian Prantl05097242018-04-30 16:49:04 +00002659 // the current thread. Bail out if we don't have a thread id.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002660 lldb::tid_t tid = GetContinueThreadID();
2661 if (tid == 0 || tid == LLDB_INVALID_THREAD_ID)
2662 tid = GetCurrentThreadID();
2663 if (tid == LLDB_INVALID_THREAD_ID)
2664 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002665
Kate Stoneb9c1b512016-09-06 20:57:50 +00002666 // Double check that we have such a thread.
2667 // TODO investigate: on MacOSX we might need to do an UpdateThreads () here.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002668 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2669 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002670 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002671
Kate Stoneb9c1b512016-09-06 20:57:50 +00002672 // Create the step action for the given thread.
2673 ResumeAction action = {tid, eStateStepping, 0};
Tamas Berghammere13c2732015-02-11 10:29:30 +00002674
Kate Stoneb9c1b512016-09-06 20:57:50 +00002675 // Setup the actions list.
2676 ResumeActionList actions;
2677 actions.Append(action);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002678
Kate Stoneb9c1b512016-09-06 20:57:50 +00002679 // All other threads stop while we're single stepping a thread.
2680 actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0);
Pavel Labath82abefa2017-07-18 09:24:48 +00002681 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002682 if (error.Fail()) {
2683 if (log)
2684 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2685 " tid %" PRIu64 " Resume() failed with error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002686 __FUNCTION__, m_debugged_process_up->GetID(), tid,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687 error.AsCString());
2688 return SendErrorResponse(0x49);
2689 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002690
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 // No response here - the stop or exit will come from the resulting action.
2692 return PacketResult::Success;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002693}
2694
2695GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002696GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read(
2697 StringExtractorGDBRemote &packet) {
2698// *BSD impls should be able to do this too.
Kamil Rytarowskic93408a2017-03-21 17:27:59 +00002699#if defined(__linux__) || defined(__NetBSD__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002701
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 // Parse out the offset.
2703 packet.SetFilePos(strlen("qXfer:auxv:read::"));
2704 if (packet.GetBytesLeft() < 1)
2705 return SendIllFormedResponse(packet,
2706 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002707
Kate Stoneb9c1b512016-09-06 20:57:50 +00002708 const uint64_t auxv_offset =
2709 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2710 if (auxv_offset == std::numeric_limits<uint64_t>::max())
2711 return SendIllFormedResponse(packet,
2712 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002713
Kate Stoneb9c1b512016-09-06 20:57:50 +00002714 // Parse out comma.
2715 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ',')
2716 return SendIllFormedResponse(
2717 packet, "qXfer:auxv:read:: packet missing comma after offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002718
Kate Stoneb9c1b512016-09-06 20:57:50 +00002719 // Parse out the length.
2720 const uint64_t auxv_length =
2721 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2722 if (auxv_length == std::numeric_limits<uint64_t>::max())
2723 return SendIllFormedResponse(packet,
2724 "qXfer:auxv:read:: packet missing length");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002725
Kate Stoneb9c1b512016-09-06 20:57:50 +00002726 // Grab the auxv data if we need it.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002727 if (!m_active_auxv_buffer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002728 // Make sure we have a valid process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002729 if (!m_debugged_process_up ||
2730 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002731 if (log)
2732 log->Printf(
2733 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2734 __FUNCTION__);
2735 return SendErrorResponse(0x10);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002736 }
2737
Kate Stoneb9c1b512016-09-06 20:57:50 +00002738 // Grab the auxv data.
Pavel Labath82abefa2017-07-18 09:24:48 +00002739 auto buffer_or_error = m_debugged_process_up->GetAuxvData();
Pavel Labathb7f0f452017-03-17 11:08:40 +00002740 if (!buffer_or_error) {
2741 std::error_code ec = buffer_or_error.getError();
2742 LLDB_LOG(log, "no auxv data retrieved: {0}", ec.message());
2743 return SendErrorResponse(ec.value());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002744 }
Pavel Labathb7f0f452017-03-17 11:08:40 +00002745 m_active_auxv_buffer_up = std::move(*buffer_or_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002746 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002747
Kate Stoneb9c1b512016-09-06 20:57:50 +00002748 StreamGDBRemote response;
2749 bool done_with_buffer = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002750
Pavel Labathb7f0f452017-03-17 11:08:40 +00002751 llvm::StringRef buffer = m_active_auxv_buffer_up->getBuffer();
2752 if (auxv_offset >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002753 // We have nothing left to send. Mark the buffer as complete.
2754 response.PutChar('l');
2755 done_with_buffer = true;
2756 } else {
2757 // Figure out how many bytes are available starting at the given offset.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002758 buffer = buffer.drop_front(auxv_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002759
2760 // Mark the response type according to whether we're reading the remainder
2761 // of the auxv data.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002762 if (auxv_length >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002763 // There will be nothing left to read after this
2764 response.PutChar('l');
2765 done_with_buffer = true;
2766 } else {
2767 // There will still be bytes to read after this request.
2768 response.PutChar('m');
Pavel Labathb7f0f452017-03-17 11:08:40 +00002769 buffer = buffer.take_front(auxv_length);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002770 }
2771
Kate Stoneb9c1b512016-09-06 20:57:50 +00002772 // Now write the data in encoded binary form.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002773 response.PutEscapedBytes(buffer.data(), buffer.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002774 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002775
Kate Stoneb9c1b512016-09-06 20:57:50 +00002776 if (done_with_buffer)
Pavel Labathb7f0f452017-03-17 11:08:40 +00002777 m_active_auxv_buffer_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002778
2779 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002780#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002781 return SendUnimplementedResponse("not implemented on this platform");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002782#endif
2783}
2784
2785GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002786GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState(
2787 StringExtractorGDBRemote &packet) {
2788 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002789
Kate Stoneb9c1b512016-09-06 20:57:50 +00002790 // Move past packet name.
2791 packet.SetFilePos(strlen("QSaveRegisterState"));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002792
Kate Stoneb9c1b512016-09-06 20:57:50 +00002793 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002794 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2795 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 if (m_thread_suffix_supported)
2797 return SendIllFormedResponse(
2798 packet, "No thread specified in QSaveRegisterState packet");
2799 else
2800 return SendIllFormedResponse(packet,
2801 "No thread was is set with the Hg packet");
2802 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002803
Kate Stoneb9c1b512016-09-06 20:57:50 +00002804 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002805 NativeRegisterContext& reg_context = thread->GetRegisterContext();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002806
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 // Save registers to a buffer.
2808 DataBufferSP register_data_sp;
Pavel Labathd37349f2017-11-10 11:05:49 +00002809 Status error = reg_context.ReadAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002810 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002811 LLDB_LOG(log, "pid {0} failed to save all register values: {1}",
2812 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 return SendErrorResponse(0x75);
2814 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002815
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816 // Allocate a new save id.
2817 const uint32_t save_id = GetNextSavedRegistersID();
2818 assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) &&
2819 "GetNextRegisterSaveID() returned an existing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002820
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 // Save the register data buffer under the save id.
2822 {
2823 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2824 m_saved_registers_map[save_id] = register_data_sp;
2825 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002826
Kate Stoneb9c1b512016-09-06 20:57:50 +00002827 // Write the response.
2828 StreamGDBRemote response;
2829 response.Printf("%" PRIu32, save_id);
2830 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002831}
2832
2833GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState(
2835 StringExtractorGDBRemote &packet) {
2836 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002837
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 // Parse out save id.
2839 packet.SetFilePos(strlen("QRestoreRegisterState:"));
2840 if (packet.GetBytesLeft() < 1)
2841 return SendIllFormedResponse(
2842 packet, "QRestoreRegisterState packet missing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002843
Kate Stoneb9c1b512016-09-06 20:57:50 +00002844 const uint32_t save_id = packet.GetU32(0);
2845 if (save_id == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002846 LLDB_LOG(log, "QRestoreRegisterState packet has malformed save id, "
2847 "expecting decimal uint32_t");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002848 return SendErrorResponse(0x76);
2849 }
2850
2851 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002852 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2853 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 if (m_thread_suffix_supported)
2855 return SendIllFormedResponse(
2856 packet, "No thread specified in QRestoreRegisterState packet");
2857 else
2858 return SendIllFormedResponse(packet,
2859 "No thread was is set with the Hg packet");
2860 }
2861
2862 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002863 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002864
2865 // Retrieve register state buffer, then remove from the list.
2866 DataBufferSP register_data_sp;
2867 {
2868 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2869
2870 // Find the register set buffer for the given save id.
2871 auto it = m_saved_registers_map.find(save_id);
2872 if (it == m_saved_registers_map.end()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002873 LLDB_LOG(log,
2874 "pid {0} does not have a register set save buffer for id {1}",
2875 m_debugged_process_up->GetID(), save_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876 return SendErrorResponse(0x77);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002877 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002878 register_data_sp = it->second;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002879
Kate Stoneb9c1b512016-09-06 20:57:50 +00002880 // Remove it from the map.
2881 m_saved_registers_map.erase(it);
2882 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002883
Pavel Labathd37349f2017-11-10 11:05:49 +00002884 Status error = reg_context.WriteAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002886 LLDB_LOG(log, "pid {0} failed to restore all register values: {1}",
2887 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002888 return SendErrorResponse(0x77);
2889 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002890
Kate Stoneb9c1b512016-09-06 20:57:50 +00002891 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002892}
2893
2894GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002895GDBRemoteCommunicationServerLLGS::Handle_vAttach(
2896 StringExtractorGDBRemote &packet) {
2897 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002898
Kate Stoneb9c1b512016-09-06 20:57:50 +00002899 // Consume the ';' after vAttach.
2900 packet.SetFilePos(strlen("vAttach"));
2901 if (!packet.GetBytesLeft() || packet.GetChar() != ';')
2902 return SendIllFormedResponse(packet, "vAttach missing expected ';'");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002903
Kate Stoneb9c1b512016-09-06 20:57:50 +00002904 // Grab the PID to which we will attach (assume hex encoding).
2905 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
2906 if (pid == LLDB_INVALID_PROCESS_ID)
2907 return SendIllFormedResponse(packet,
2908 "vAttach failed to parse the process id");
2909
2910 // Attempt to attach.
2911 if (log)
2912 log->Printf("GDBRemoteCommunicationServerLLGS::%s attempting to attach to "
2913 "pid %" PRIu64,
2914 __FUNCTION__, pid);
2915
Zachary Turner97206d52017-05-12 04:51:55 +00002916 Status error = AttachToProcess(pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917
2918 if (error.Fail()) {
2919 if (log)
2920 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to attach to "
2921 "pid %" PRIu64 ": %s\n",
2922 __FUNCTION__, pid, error.AsCString());
Pavel Labatha70512a2018-04-11 13:30:54 +00002923 return SendErrorResponse(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 }
2925
2926 // Notify we attached by sending a stop packet.
Pavel Labath82abefa2017-07-18 09:24:48 +00002927 return SendStopReasonForState(m_debugged_process_up->GetState());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928}
2929
2930GDBRemoteCommunication::PacketResult
2931GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) {
2932 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2933
2934 StopSTDIOForwarding();
2935
2936 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002937 if (!m_debugged_process_up ||
2938 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002939 if (log)
2940 log->Printf(
2941 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2942 __FUNCTION__);
2943 return SendErrorResponse(0x15);
2944 }
2945
2946 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2947
2948 // Consume the ';' after D.
2949 packet.SetFilePos(1);
2950 if (packet.GetBytesLeft()) {
2951 if (packet.GetChar() != ';')
2952 return SendIllFormedResponse(packet, "D missing expected ';'");
2953
2954 // Grab the PID from which we will detach (assume hex encoding).
2955 pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002956 if (pid == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002957 return SendIllFormedResponse(packet, "D failed to parse the process id");
2958 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002959
Pavel Labath82abefa2017-07-18 09:24:48 +00002960 if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_up->GetID() != pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002961 return SendIllFormedResponse(packet, "Invalid pid");
2962 }
2963
Pavel Labath82abefa2017-07-18 09:24:48 +00002964 const Status error = m_debugged_process_up->Detach();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002965 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002966 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002967 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to detach from "
2968 "pid %" PRIu64 ": %s\n",
Pavel Labath82abefa2017-07-18 09:24:48 +00002969 __FUNCTION__, m_debugged_process_up->GetID(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002970 error.AsCString());
2971 return SendErrorResponse(0x01);
2972 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002973
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002975}
2976
2977GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002978GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo(
2979 StringExtractorGDBRemote &packet) {
2980 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002981
Kate Stoneb9c1b512016-09-06 20:57:50 +00002982 packet.SetFilePos(strlen("qThreadStopInfo"));
2983 const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
2984 if (tid == LLDB_INVALID_THREAD_ID) {
Pavel Labath4a4bb122015-07-16 14:14:35 +00002985 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002986 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
2987 "parse thread id from request \"%s\"",
2988 __FUNCTION__, packet.GetStringRef().c_str());
2989 return SendErrorResponse(0x15);
2990 }
2991 return SendStopReplyPacketForThread(tid);
2992}
2993
2994GDBRemoteCommunication::PacketResult
2995GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo(
2996 StringExtractorGDBRemote &) {
2997 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2998
2999 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003000 if (!m_debugged_process_up ||
3001 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002 return SendErrorResponse(50);
Pavel Labath82abefa2017-07-18 09:24:48 +00003003 LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003004
Kate Stoneb9c1b512016-09-06 20:57:50 +00003005 StreamString response;
3006 const bool threads_with_valid_stop_info_only = false;
3007 JSONArray::SP threads_array_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +00003008 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009 if (!threads_array_sp) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003010 LLDB_LOG(log, "failed to prepare a packet for pid {0}",
3011 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003012 return SendErrorResponse(52);
3013 }
Pavel Labath4a4bb122015-07-16 14:14:35 +00003014
Kate Stoneb9c1b512016-09-06 20:57:50 +00003015 threads_array_sp->Write(response);
3016 StreamGDBRemote escaped_response;
3017 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
3018 return SendPacketNoLock(escaped_response.GetString());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003019}
3020
3021GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003022GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo(
3023 StringExtractorGDBRemote &packet) {
3024 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003025 if (!m_debugged_process_up ||
3026 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003028
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 packet.SetFilePos(strlen("qWatchpointSupportInfo"));
3030 if (packet.GetBytesLeft() == 0)
3031 return SendOKResponse();
3032 if (packet.GetChar() != ':')
3033 return SendErrorResponse(67);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003034
Pavel Labath82abefa2017-07-18 09:24:48 +00003035 auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo();
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003036
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037 StreamGDBRemote response;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003038 if (hw_debug_cap == llvm::None)
3039 response.Printf("num:0;");
3040 else
3041 response.Printf("num:%d;", hw_debug_cap->second);
3042
Kate Stoneb9c1b512016-09-06 20:57:50 +00003043 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00003044}
3045
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003046GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress(
3048 StringExtractorGDBRemote &packet) {
3049 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003050 if (!m_debugged_process_up ||
3051 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052 return SendErrorResponse(67);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003053
Kate Stoneb9c1b512016-09-06 20:57:50 +00003054 packet.SetFilePos(strlen("qFileLoadAddress:"));
3055 if (packet.GetBytesLeft() == 0)
3056 return SendErrorResponse(68);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003057
Kate Stoneb9c1b512016-09-06 20:57:50 +00003058 std::string file_name;
3059 packet.GetHexByteString(file_name);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003060
Kate Stoneb9c1b512016-09-06 20:57:50 +00003061 lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +00003062 Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00003063 m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003064 if (error.Fail())
3065 return SendErrorResponse(69);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003066
Kate Stoneb9c1b512016-09-06 20:57:50 +00003067 if (file_load_address == LLDB_INVALID_ADDRESS)
3068 return SendErrorResponse(1); // File not loaded
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003069
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070 StreamGDBRemote response;
3071 response.PutHex64(file_load_address);
3072 return SendPacketNoLock(response.GetString());
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003073}
3074
Pavel Labath4a705e72017-02-24 09:29:14 +00003075GDBRemoteCommunication::PacketResult
3076GDBRemoteCommunicationServerLLGS::Handle_QPassSignals(
3077 StringExtractorGDBRemote &packet) {
3078 std::vector<int> signals;
3079 packet.SetFilePos(strlen("QPassSignals:"));
3080
Adrian Prantl05097242018-04-30 16:49:04 +00003081 // Read sequence of hex signal numbers divided by a semicolon and optionally
3082 // spaces.
Pavel Labath4a705e72017-02-24 09:29:14 +00003083 while (packet.GetBytesLeft() > 0) {
3084 int signal = packet.GetS32(-1, 16);
3085 if (signal < 0)
3086 return SendIllFormedResponse(packet, "Failed to parse signal number.");
3087 signals.push_back(signal);
3088
3089 packet.SkipSpaces();
3090 char separator = packet.GetChar();
3091 if (separator == '\0')
3092 break; // End of string
3093 if (separator != ';')
3094 return SendIllFormedResponse(packet, "Invalid separator,"
3095 " expected semicolon.");
3096 }
3097
3098 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003099 if (!m_debugged_process_up)
Pavel Labath4a705e72017-02-24 09:29:14 +00003100 return SendErrorResponse(68);
3101
Pavel Labath82abefa2017-07-18 09:24:48 +00003102 Status error = m_debugged_process_up->IgnoreSignals(signals);
Pavel Labath4a705e72017-02-24 09:29:14 +00003103 if (error.Fail())
3104 return SendErrorResponse(69);
3105
3106 return SendOKResponse();
3107}
3108
Kate Stoneb9c1b512016-09-06 20:57:50 +00003109void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() {
3110 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003111
Kate Stoneb9c1b512016-09-06 20:57:50 +00003112 // Tell the stdio connection to shut down.
3113 if (m_stdio_communication.IsConnected()) {
3114 auto connection = m_stdio_communication.GetConnection();
3115 if (connection) {
Zachary Turner97206d52017-05-12 04:51:55 +00003116 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003117 connection->Disconnect(&error);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003118
Kate Stoneb9c1b512016-09-06 20:57:50 +00003119 if (error.Success()) {
3120 if (log)
3121 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3122 "terminal stdio - SUCCESS",
3123 __FUNCTION__);
3124 } else {
3125 if (log)
3126 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3127 "terminal stdio - FAIL: %s",
3128 __FUNCTION__, error.AsCString());
3129 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003130 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003131 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003132}
3133
Pavel Labatha5be48b2017-10-17 15:52:16 +00003134NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
Kate Stoneb9c1b512016-09-06 20:57:50 +00003135 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003136 // We have no thread if we don't have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003137 if (!m_debugged_process_up ||
3138 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003139 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +00003140
Kate Stoneb9c1b512016-09-06 20:57:50 +00003141 // If the client hasn't asked for thread suffix support, there will not be a
Adrian Prantl05097242018-04-30 16:49:04 +00003142 // thread suffix. Use the current thread in that case.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003143 if (!m_thread_suffix_supported) {
3144 const lldb::tid_t current_tid = GetCurrentThreadID();
3145 if (current_tid == LLDB_INVALID_THREAD_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003146 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147 else if (current_tid == 0) {
3148 // Pick a thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003149 return m_debugged_process_up->GetThreadAtIndex(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003150 } else
Pavel Labath82abefa2017-07-18 09:24:48 +00003151 return m_debugged_process_up->GetThreadByID(current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003153
Kate Stoneb9c1b512016-09-06 20:57:50 +00003154 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003155
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156 // Parse out the ';'.
3157 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') {
Tamas Berghammere13c2732015-02-11 10:29:30 +00003158 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003159 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3160 "error: expected ';' prior to start of thread suffix: packet "
3161 "contents = '%s'",
3162 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003163 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003165
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166 if (!packet.GetBytesLeft())
Pavel Labatha5be48b2017-10-17 15:52:16 +00003167 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003168
3169 // Parse out thread: portion.
3170 if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) {
3171 if (log)
3172 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3173 "error: expected 'thread:' but not found, packet contents = "
3174 "'%s'",
3175 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003176 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003177 }
3178 packet.SetFilePos(packet.GetFilePos() + strlen("thread:"));
3179 const lldb::tid_t tid = packet.GetHexMaxU64(false, 0);
3180 if (tid != 0)
Pavel Labath82abefa2017-07-18 09:24:48 +00003181 return m_debugged_process_up->GetThreadByID(tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003182
Pavel Labatha5be48b2017-10-17 15:52:16 +00003183 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184}
3185
3186lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const {
3187 if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) {
Adrian Prantl05097242018-04-30 16:49:04 +00003188 // Use whatever the debug process says is the current thread id since the
3189 // protocol either didn't specify or specified we want any/all threads
3190 // marked as the current thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003191 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003192 return LLDB_INVALID_THREAD_ID;
Pavel Labath82abefa2017-07-18 09:24:48 +00003193 return m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194 }
3195 // Use the specific current thread id set by the gdb remote protocol.
3196 return m_current_tid;
3197}
3198
3199uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() {
3200 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
3201 return m_next_saved_registers_id++;
3202}
3203
3204void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() {
Pavel Labathb7f0f452017-03-17 11:08:40 +00003205 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206
Pavel Labathb7f0f452017-03-17 11:08:40 +00003207 LLDB_LOG(log, "clearing auxv buffer: {0}", m_active_auxv_buffer_up.get());
3208 m_active_auxv_buffer_up.reset();
Tamas Berghammere13c2732015-02-11 10:29:30 +00003209}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003210
3211FileSpec
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path,
3213 const ArchSpec &arch) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003214 if (m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 FileSpec file_spec;
Pavel Labath82abefa2017-07-18 09:24:48 +00003216 if (m_debugged_process_up
Kate Stoneb9c1b512016-09-06 20:57:50 +00003217 ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec)
3218 .Success()) {
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003219 if (FileSystem::Instance().Exists(file_spec))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220 return file_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003223
Kate Stoneb9c1b512016-09-06 20:57:50 +00003224 return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003225}