blob: 857f428d7b765a320c6e338429435585205822ff [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerLLGS.cpp --------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tamas Berghammere13c2732015-02-11 10:29:30 +00006//
7//===----------------------------------------------------------------------===//
8
9#include <errno.h>
10
11#include "lldb/Host/Config.h"
12
13#include "GDBRemoteCommunicationServerLLGS.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000014#include "lldb/Utility/StreamGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000015
Tamas Berghammere13c2732015-02-11 10:29:30 +000016#include <chrono>
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include <cstring>
Tamas Berghammere13c2732015-02-11 10:29:30 +000018#include <thread>
19
Tamas Berghammere13c2732015-02-11 10:29:30 +000020#include "lldb/Host/ConnectionFileDescriptor.h"
21#include "lldb/Host/Debug.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000022#include "lldb/Host/File.h"
Pavel Labatheef758e2019-02-04 14:28:08 +000023#include "lldb/Host/FileAction.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000024#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"
Zachary Turner93749ab2015-03-03 21:51:25 +000031#include "lldb/Target/MemoryRegionInfo.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000032#include "lldb/Utility/Args.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000033#include "lldb/Utility/DataBuffer.h"
Zachary Turner01c32432017-02-14 19:06:07 +000034#include "lldb/Utility/Endian.h"
Pavel Labath4a4bb122015-07-16 14:14:35 +000035#include "lldb/Utility/JSON.h"
Pavel Labathabadc222015-11-27 13:33:29 +000036#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000037#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000038#include "lldb/Utility/RegisterValue.h"
39#include "lldb/Utility/State.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000040#include "lldb/Utility/StreamString.h"
Pavel Labath5f7e5832017-02-10 12:21:22 +000041#include "lldb/Utility/UriParser.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000042#include "llvm/ADT/Triple.h"
43#include "llvm/Support/ScopedPrinter.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000044
Tamas Berghammere13c2732015-02-11 10:29:30 +000045#include "ProcessGDBRemote.h"
46#include "ProcessGDBRemoteLog.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000047#include "lldb/Utility/StringExtractorGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000048
49using namespace lldb;
50using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000051using namespace lldb_private::process_gdb_remote;
Tamas Berghammer783bfc82015-06-18 20:43:56 +000052using namespace llvm;
Tamas Berghammere13c2732015-02-11 10:29:30 +000053
54//----------------------------------------------------------------------
55// GDBRemote Errors
56//----------------------------------------------------------------------
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058namespace {
59enum GDBRemoteServerError {
60 // Set to the first unused error number in literal form below
61 eErrorFirst = 29,
62 eErrorNoProcess = eErrorFirst,
63 eErrorResume,
64 eErrorExitStatus
65};
Tamas Berghammere13c2732015-02-11 10:29:30 +000066}
67
68//----------------------------------------------------------------------
69// GDBRemoteCommunicationServerLLGS constructor
70//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000071GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
Pavel Labath96e600f2017-07-07 11:02:19 +000072 MainLoop &mainloop, const NativeProcessProtocol::Factory &process_factory)
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 : GDBRemoteCommunicationServerCommon("gdb-remote.server",
74 "gdb-remote.server.rx_packet"),
Pavel Labath96e600f2017-07-07 11:02:19 +000075 m_mainloop(mainloop), m_process_factory(process_factory),
76 m_stdio_communication("process.stdio") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 RegisterPacketHandlers();
Tamas Berghammere13c2732015-02-11 10:29:30 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() {
81 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C,
82 &GDBRemoteCommunicationServerLLGS::Handle_C);
83 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c,
84 &GDBRemoteCommunicationServerLLGS::Handle_c);
85 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D,
86 &GDBRemoteCommunicationServerLLGS::Handle_D);
87 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H,
88 &GDBRemoteCommunicationServerLLGS::Handle_H);
89 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I,
90 &GDBRemoteCommunicationServerLLGS::Handle_I);
91 RegisterMemberFunctionHandler(
92 StringExtractorGDBRemote::eServerPacketType_interrupt,
93 &GDBRemoteCommunicationServerLLGS::Handle_interrupt);
94 RegisterMemberFunctionHandler(
95 StringExtractorGDBRemote::eServerPacketType_m,
96 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
97 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M,
98 &GDBRemoteCommunicationServerLLGS::Handle_M);
99 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p,
100 &GDBRemoteCommunicationServerLLGS::Handle_p);
101 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P,
102 &GDBRemoteCommunicationServerLLGS::Handle_P);
103 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC,
104 &GDBRemoteCommunicationServerLLGS::Handle_qC);
105 RegisterMemberFunctionHandler(
106 StringExtractorGDBRemote::eServerPacketType_qfThreadInfo,
107 &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo);
108 RegisterMemberFunctionHandler(
109 StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress,
110 &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress);
111 RegisterMemberFunctionHandler(
112 StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir,
113 &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir);
114 RegisterMemberFunctionHandler(
115 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo,
116 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo);
117 RegisterMemberFunctionHandler(
118 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported,
119 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported);
120 RegisterMemberFunctionHandler(
121 StringExtractorGDBRemote::eServerPacketType_qProcessInfo,
122 &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo);
123 RegisterMemberFunctionHandler(
124 StringExtractorGDBRemote::eServerPacketType_qRegisterInfo,
125 &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo);
126 RegisterMemberFunctionHandler(
127 StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState,
128 &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState);
129 RegisterMemberFunctionHandler(
130 StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState,
131 &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState);
132 RegisterMemberFunctionHandler(
133 StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR,
134 &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR);
135 RegisterMemberFunctionHandler(
136 StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir,
137 &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir);
138 RegisterMemberFunctionHandler(
139 StringExtractorGDBRemote::eServerPacketType_qsThreadInfo,
140 &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo);
141 RegisterMemberFunctionHandler(
142 StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo,
143 &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo);
144 RegisterMemberFunctionHandler(
145 StringExtractorGDBRemote::eServerPacketType_jThreadsInfo,
146 &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo);
147 RegisterMemberFunctionHandler(
148 StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo,
149 &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo);
150 RegisterMemberFunctionHandler(
151 StringExtractorGDBRemote::eServerPacketType_qXfer_auxv_read,
152 &GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read);
153 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s,
154 &GDBRemoteCommunicationServerLLGS::Handle_s);
155 RegisterMemberFunctionHandler(
156 StringExtractorGDBRemote::eServerPacketType_stop_reason,
157 &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ?
158 RegisterMemberFunctionHandler(
159 StringExtractorGDBRemote::eServerPacketType_vAttach,
160 &GDBRemoteCommunicationServerLLGS::Handle_vAttach);
161 RegisterMemberFunctionHandler(
162 StringExtractorGDBRemote::eServerPacketType_vCont,
163 &GDBRemoteCommunicationServerLLGS::Handle_vCont);
164 RegisterMemberFunctionHandler(
165 StringExtractorGDBRemote::eServerPacketType_vCont_actions,
166 &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions);
167 RegisterMemberFunctionHandler(
168 StringExtractorGDBRemote::eServerPacketType_x,
169 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
170 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z,
171 &GDBRemoteCommunicationServerLLGS::Handle_Z);
172 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z,
173 &GDBRemoteCommunicationServerLLGS::Handle_z);
Pavel Labath4a705e72017-02-24 09:29:14 +0000174 RegisterMemberFunctionHandler(
175 StringExtractorGDBRemote::eServerPacketType_QPassSignals,
176 &GDBRemoteCommunicationServerLLGS::Handle_QPassSignals);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000177
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +0000178 RegisterMemberFunctionHandler(
179 StringExtractorGDBRemote::eServerPacketType_jTraceStart,
180 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStart);
181 RegisterMemberFunctionHandler(
182 StringExtractorGDBRemote::eServerPacketType_jTraceBufferRead,
183 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
184 RegisterMemberFunctionHandler(
185 StringExtractorGDBRemote::eServerPacketType_jTraceMetaRead,
186 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
187 RegisterMemberFunctionHandler(
188 StringExtractorGDBRemote::eServerPacketType_jTraceStop,
189 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStop);
190 RegisterMemberFunctionHandler(
191 StringExtractorGDBRemote::eServerPacketType_jTraceConfigRead,
192 &GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead);
193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k,
Zachary Turner97206d52017-05-12 04:51:55 +0000195 [this](StringExtractorGDBRemote packet, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 bool &interrupt, bool &quit) {
197 quit = true;
198 return this->Handle_k(packet);
199 });
Tamas Berghammere13c2732015-02-11 10:29:30 +0000200}
201
Pavel Labath11e59172017-12-18 14:31:39 +0000202void GDBRemoteCommunicationServerLLGS::SetLaunchInfo(const ProcessLaunchInfo &info) {
203 m_process_launch_info = info;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000204}
205
Zachary Turner97206d52017-05-12 04:51:55 +0000206Status GDBRemoteCommunicationServerLLGS::LaunchProcess() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 if (!m_process_launch_info.GetArguments().GetArgumentCount())
Zachary Turner97206d52017-05-12 04:51:55 +0000210 return Status("%s: no process command line specified to launch",
211 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 const bool should_forward_stdio =
214 m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
215 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
216 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr;
217 m_process_launch_info.SetLaunchInSeparateProcessGroup(true);
218 m_process_launch_info.GetFlags().Set(eLaunchFlagDebug);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000219
Pavel Labath8e55dde2019-01-08 11:55:19 +0000220 if (should_forward_stdio) {
221 if (llvm::Error Err = m_process_launch_info.SetUpPtyRedirection())
222 return Status(std::move(Err));
223 }
Pavel Labath5ad891f2016-07-21 14:54:03 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 {
226 std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex);
Pavel Labath82abefa2017-07-18 09:24:48 +0000227 assert(!m_debugged_process_up && "lldb-server creating debugged "
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 "process but one already exists");
Pavel Labath96e600f2017-07-07 11:02:19 +0000229 auto process_or =
230 m_process_factory.Launch(m_process_launch_info, *this, m_mainloop);
Pavel Labath8630d382017-12-14 14:56:29 +0000231 if (!process_or)
232 return Status(process_or.takeError());
Pavel Labath82abefa2017-07-18 09:24:48 +0000233 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000235
Adrian Prantl05097242018-04-30 16:49:04 +0000236 // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol as
237 // needed. llgs local-process debugging may specify PTY paths, which will
238 // make these file actions non-null process launch -i/e/o will also make
239 // these file actions non-null nullptr means that the traffic is expected to
240 // flow over gdb-remote protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 if (should_forward_stdio) {
242 // nullptr means it's not redirected to file or pty (in case of LLGS local)
Adrian Prantl05097242018-04-30 16:49:04 +0000243 // at least one of stdio will be transferred pty<->gdb-remote we need to
244 // give the pty master handle to this object to read and/or write
Pavel Labath82abefa2017-07-18 09:24:48 +0000245 LLDB_LOG(log,
246 "pid = {0}: setting up stdout/stderr redirection via $O "
247 "gdb-remote commands",
248 m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 // Setup stdout/stderr mapping from inferior to $O
Pavel Labath82abefa2017-07-18 09:24:48 +0000251 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 if (terminal_fd >= 0) {
253 if (log)
254 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
255 "inferior STDIO fd to %d",
256 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000257 Status status = SetSTDIOFileDescriptor(terminal_fd);
258 if (status.Fail())
259 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 } else {
261 if (log)
262 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
263 "inferior STDIO since terminal fd reported as %d",
264 __FUNCTION__, terminal_fd);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 } else {
Pavel Labath82abefa2017-07-18 09:24:48 +0000267 LLDB_LOG(log,
268 "pid = {0} skipping stdout/stderr redirection via $O: inferior "
269 "will communicate over client-provided file descriptors",
270 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 }
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 printf("Launched '%s' as process %" PRIu64 "...\n",
274 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
Pavel Labath82abefa2017-07-18 09:24:48 +0000275 m_debugged_process_up->GetID());
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000276
Pavel Labath96e600f2017-07-07 11:02:19 +0000277 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000278}
279
Zachary Turner97206d52017-05-12 04:51:55 +0000280Status GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
282 if (log)
283 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64,
284 __FUNCTION__, pid);
285
286 // Before we try to attach, make sure we aren't already monitoring something
287 // else.
Pavel Labath82abefa2017-07-18 09:24:48 +0000288 if (m_debugged_process_up &&
289 m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID)
Pavel Labatha70512a2018-04-11 13:30:54 +0000290 return Status("cannot attach to process %" PRIu64
Zachary Turner97206d52017-05-12 04:51:55 +0000291 " when another process with pid %" PRIu64
292 " is being debugged.",
Pavel Labath82abefa2017-07-18 09:24:48 +0000293 pid, m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294
295 // Try to attach.
Pavel Labath96e600f2017-07-07 11:02:19 +0000296 auto process_or = m_process_factory.Attach(pid, *this, m_mainloop);
297 if (!process_or) {
298 Status status(process_or.takeError());
299 llvm::errs() << llvm::formatv("failed to attach to process {0}: {1}", pid,
300 status);
301 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 }
Pavel Labath82abefa2017-07-18 09:24:48 +0000303 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304
305 // Setup stdout/stderr mapping from inferior.
Pavel Labath82abefa2017-07-18 09:24:48 +0000306 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 if (terminal_fd >= 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000308 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
310 "inferior STDIO fd to %d",
311 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000312 Status status = SetSTDIOFileDescriptor(terminal_fd);
313 if (status.Fail())
314 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 } else {
316 if (log)
317 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
318 "inferior STDIO since terminal fd reported as %d",
319 __FUNCTION__, terminal_fd);
320 }
321
322 printf("Attached to process %" PRIu64 "...\n", pid);
Pavel Labath96e600f2017-07-07 11:02:19 +0000323 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324}
325
326void GDBRemoteCommunicationServerLLGS::InitializeDelegate(
327 NativeProcessProtocol *process) {
328 assert(process && "process cannot be NULL");
329 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
330 if (log) {
331 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
332 "NativeProcessProtocol pid %" PRIu64 ", current state: %s",
333 __FUNCTION__, process->GetID(),
334 StateAsCString(process->GetState()));
335 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000336}
337
338GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339GDBRemoteCommunicationServerLLGS::SendWResponse(
340 NativeProcessProtocol *process) {
341 assert(process && "process cannot be NULL");
342 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 // send W notification
Pavel Labath3508fc82017-06-19 12:47:50 +0000345 auto wait_status = process->GetExitStatus();
346 if (!wait_status) {
347 LLDB_LOG(log, "pid = {0}, failed to retrieve process exit status",
348 process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 StreamGDBRemote response;
351 response.PutChar('E');
352 response.PutHex8(GDBRemoteServerError::eErrorExitStatus);
353 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 }
Pavel Labath3508fc82017-06-19 12:47:50 +0000355
356 LLDB_LOG(log, "pid = {0}, returning exit type {1}", process->GetID(),
357 *wait_status);
358
359 StreamGDBRemote response;
360 response.Format("{0:g}", *wait_status);
361 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000362}
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364static void AppendHexValue(StreamString &response, const uint8_t *buf,
365 uint32_t buf_size, bool swap) {
366 int64_t i;
367 if (swap) {
368 for (i = buf_size - 1; i >= 0; i--)
369 response.PutHex8(buf[i]);
370 } else {
371 for (i = 0; i < buf_size; i++)
372 response.PutHex8(buf[i]);
373 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000374}
375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376static void WriteRegisterValueInHexFixedWidth(
Pavel Labathd37349f2017-11-10 11:05:49 +0000377 StreamString &response, NativeRegisterContext &reg_ctx,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000378 const RegisterInfo &reg_info, const RegisterValue *reg_value_p,
379 lldb::ByteOrder byte_order) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 RegisterValue reg_value;
381 if (!reg_value_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000382 Status error = reg_ctx.ReadRegister(&reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 if (error.Success())
384 reg_value_p = &reg_value;
385 // else log.
386 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 if (reg_value_p) {
389 AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(),
Pavel Labathe0a5b572017-01-20 14:17:16 +0000390 reg_value_p->GetByteSize(),
391 byte_order == lldb::eByteOrderLittle);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 } else {
393 // Zero-out any unreadable values.
394 if (reg_info.byte_size > 0) {
395 std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0');
396 AppendHexValue(response, zeros.data(), zeros.size(), false);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000397 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000399}
400
Pavel Labathe0a5b572017-01-20 14:17:16 +0000401static JSONObject::SP GetRegistersAsJSON(NativeThreadProtocol &thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath4a4bb122015-07-16 14:14:35 +0000403
Pavel Labathd37349f2017-11-10 11:05:49 +0000404 NativeRegisterContext& reg_ctx = thread.GetRegisterContext();
Pavel Labath4a4bb122015-07-16 14:14:35 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 JSONObject::SP register_object_sp = std::make_shared<JSONObject>();
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000407
408#ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET
Adrian Prantl05097242018-04-30 16:49:04 +0000409 // Expedite all registers in the first register set (i.e. should be GPRs)
410 // that are not contained in other registers.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0);
412 if (!reg_set_p)
413 return nullptr;
414 for (const uint32_t *reg_num_p = reg_set_p->registers;
415 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
416 uint32_t reg_num = *reg_num_p;
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000417#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 // Expedite only a couple of registers until we figure out why sending
Adrian Prantl05097242018-04-30 16:49:04 +0000419 // registers is expensive.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 static const uint32_t k_expedited_registers[] = {
421 LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP,
422 LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM};
Pavel Labathc4645bb2015-10-26 16:25:28 +0000423
Pavel Labathe0a5b572017-01-20 14:17:16 +0000424 for (const uint32_t *generic_reg_p = k_expedited_registers;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000426 uint32_t reg_num = reg_ctx.ConvertRegisterKindToRegisterNumber(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 eRegisterKindGeneric, *generic_reg_p);
428 if (reg_num == LLDB_INVALID_REGNUM)
429 continue; // Target does not support the given register.
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000430#endif
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000433 reg_ctx.GetRegisterInfoAtIndex(reg_num);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 if (reg_info_p == nullptr) {
435 if (log)
436 log->Printf(
437 "%s failed to get register info for register index %" PRIu32,
438 __FUNCTION__, reg_num);
439 continue;
Pavel Labath4a4bb122015-07-16 14:14:35 +0000440 }
441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 if (reg_info_p->value_regs != nullptr)
443 continue; // Only expedite registers that are not contained in other
444 // registers.
Pavel Labath4a4bb122015-07-16 14:14:35 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000447 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 if (error.Fail()) {
449 if (log)
450 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
Pavel Labath05569f62015-07-23 09:09:29 +0000451 __FUNCTION__,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 reg_info_p->name ? reg_info_p->name : "<unnamed-register>",
453 reg_num, error.AsCString());
454 continue;
Pavel Labath05569f62015-07-23 09:09:29 +0000455 }
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 StreamString stream;
Pavel Labathd37349f2017-11-10 11:05:49 +0000458 WriteRegisterValueInHexFixedWidth(stream, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000459 &reg_value, lldb::eByteOrderBig);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460
461 register_object_sp->SetObject(
462 llvm::to_string(reg_num),
463 std::make_shared<JSONString>(stream.GetString()));
464 }
465
466 return register_object_sp;
Pavel Labath05569f62015-07-23 09:09:29 +0000467}
468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469static const char *GetStopReasonString(StopReason stop_reason) {
470 switch (stop_reason) {
471 case eStopReasonTrace:
472 return "trace";
473 case eStopReasonBreakpoint:
474 return "breakpoint";
475 case eStopReasonWatchpoint:
476 return "watchpoint";
477 case eStopReasonSignal:
478 return "signal";
479 case eStopReasonException:
480 return "exception";
481 case eStopReasonExec:
482 return "exec";
483 case eStopReasonInstrumentation:
484 case eStopReasonInvalid:
485 case eStopReasonPlanComplete:
486 case eStopReasonThreadExiting:
487 case eStopReasonNone:
488 break; // ignored
489 }
490 return nullptr;
491}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493static JSONArray::SP GetJSONThreadsInfo(NativeProcessProtocol &process,
494 bool abridged) {
495 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 JSONArray::SP threads_array_sp = std::make_shared<JSONArray>();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 // Ensure we can get info on the given thread.
500 uint32_t thread_idx = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000501 for (NativeThreadProtocol *thread;
502 (thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 ++thread_idx) {
504
Pavel Labatha5be48b2017-10-17 15:52:16 +0000505 lldb::tid_t tid = thread->GetID();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000506
507 // Grab the reason this thread stopped.
508 struct ThreadStopInfo tid_stop_info;
509 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000510 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 const int signum = tid_stop_info.details.signal.signo;
514 if (log) {
515 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
516 " tid %" PRIu64
517 " got signal signo = %d, reason = %d, exc_type = %" PRIu64,
518 __FUNCTION__, process.GetID(), tid, signum,
519 tid_stop_info.reason, tid_stop_info.details.exception.type);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000520 }
521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>();
523 threads_array_sp->AppendObject(thread_obj_sp);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000524
Pavel Labathe0a5b572017-01-20 14:17:16 +0000525 if (!abridged) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000526 if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread))
Pavel Labathe0a5b572017-01-20 14:17:16 +0000527 thread_obj_sp->SetObject("registers", registers_sp);
528 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 thread_obj_sp->SetObject("tid", std::make_shared<JSONNumber>(tid));
531 if (signum != 0)
532 thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(signum));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000533
Pavel Labatha5be48b2017-10-17 15:52:16 +0000534 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 if (!thread_name.empty())
536 thread_obj_sp->SetObject("name",
537 std::make_shared<JSONString>(thread_name));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 if (const char *stop_reason_str = GetStopReasonString(tid_stop_info.reason))
540 thread_obj_sp->SetObject("reason",
541 std::make_shared<JSONString>(stop_reason_str));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000542
543 if (!description.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 thread_obj_sp->SetObject("description",
545 std::make_shared<JSONString>(description));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 if ((tid_stop_info.reason == eStopReasonException) &&
548 tid_stop_info.details.exception.type) {
549 thread_obj_sp->SetObject(
550 "metype",
551 std::make_shared<JSONNumber>(tid_stop_info.details.exception.type));
552
553 JSONArray::SP medata_array_sp = std::make_shared<JSONArray>();
554 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count;
555 ++i) {
556 medata_array_sp->AppendObject(std::make_shared<JSONNumber>(
557 tid_stop_info.details.exception.data[i]));
558 }
559 thread_obj_sp->SetObject("medata", medata_array_sp);
560 }
561
562 // TODO: Expedite interesting regions of inferior memory
563 }
564
565 return threads_array_sp;
566}
567
568GDBRemoteCommunication::PacketResult
569GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread(
570 lldb::tid_t tid) {
571 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
572
573 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +0000574 if (!m_debugged_process_up ||
575 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 return SendErrorResponse(50);
577
Pavel Labath82abefa2017-07-18 09:24:48 +0000578 LLDB_LOG(log, "preparing packet for pid {0} tid {1}",
579 m_debugged_process_up->GetID(), tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580
581 // Ensure we can get info on the given thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000582 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
583 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 return SendErrorResponse(51);
585
586 // Grab the reason this thread stopped.
587 struct ThreadStopInfo tid_stop_info;
588 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000589 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 return SendErrorResponse(52);
591
592 // FIXME implement register handling for exec'd inferiors.
Adrian Prantl05097242018-04-30 16:49:04 +0000593 // if (tid_stop_info.reason == eStopReasonExec) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 // const bool force = true;
595 // InitializeRegisters(force);
596 // }
597
598 StreamString response;
599 // Output the T packet with the thread
600 response.PutChar('T');
601 int signum = tid_stop_info.details.signal.signo;
Pavel Labath82abefa2017-07-18 09:24:48 +0000602 LLDB_LOG(
603 log,
604 "pid {0}, tid {1}, got signal signo = {2}, reason = {3}, exc_type = {4}",
605 m_debugged_process_up->GetID(), tid, signum, int(tid_stop_info.reason),
606 tid_stop_info.details.exception.type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607
608 // Print the signal number.
609 response.PutHex8(signum & 0xff);
610
611 // Include the tid.
612 response.Printf("thread:%" PRIx64 ";", tid);
613
614 // Include the thread name if there is one.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000615 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 if (!thread_name.empty()) {
617 size_t thread_name_len = thread_name.length();
618
619 if (::strcspn(thread_name.c_str(), "$#+-;:") == thread_name_len) {
620 response.PutCString("name:");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000621 response.PutCString(thread_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 } else {
623 // The thread name contains special chars, send as hex bytes.
624 response.PutCString("hexname:");
625 response.PutCStringAsRawHex8(thread_name.c_str());
626 }
627 response.PutChar(';');
628 }
629
Adrian Prantl05097242018-04-30 16:49:04 +0000630 // If a 'QListThreadsInStopReply' was sent to enable this feature, we will
631 // send all thread IDs back in the "threads" key whose value is a list of hex
632 // thread IDs separated by commas:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 // "threads:10a,10b,10c;"
Adrian Prantl05097242018-04-30 16:49:04 +0000634 // This will save the debugger from having to send a pair of qfThreadInfo and
635 // qsThreadInfo packets, but it also might take a lot of room in the stop
636 // reply packet, so it must be enabled only on systems where there are no
637 // limits on packet lengths.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 if (m_list_threads_in_stop_reply) {
639 response.PutCString("threads:");
640
641 uint32_t thread_index = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000642 NativeThreadProtocol *listed_thread;
643 for (listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
644 listed_thread; ++thread_index,
645 listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 if (thread_index > 0)
647 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +0000648 response.Printf("%" PRIx64, listed_thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 }
650 response.PutChar(';');
651
Adrian Prantl05097242018-04-30 16:49:04 +0000652 // Include JSON info that describes the stop reason for any threads that
653 // actually have stop reasons. We use the new "jstopinfo" key whose values
654 // is hex ascii JSON that contains the thread IDs thread stop info only for
655 // threads that have stop reasons. Only send this if we have more than one
656 // thread otherwise this packet has all the info it needs.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 if (thread_index > 0) {
658 const bool threads_with_valid_stop_info_only = true;
659 JSONArray::SP threads_info_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +0000660 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 if (threads_info_sp) {
662 response.PutCString("jstopinfo:");
663 StreamString unescaped_response;
664 threads_info_sp->Write(unescaped_response);
665 response.PutCStringAsRawHex8(unescaped_response.GetData());
666 response.PutChar(';');
Pavel Labath82abefa2017-07-18 09:24:48 +0000667 } else
668 LLDB_LOG(log, "failed to prepare a jstopinfo field for pid {0}",
669 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670 }
Pavel Labathe0a5b572017-01-20 14:17:16 +0000671
672 uint32_t i = 0;
673 response.PutCString("thread-pcs");
674 char delimiter = ':';
Pavel Labatha5be48b2017-10-17 15:52:16 +0000675 for (NativeThreadProtocol *thread;
676 (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
Pavel Labathe0a5b572017-01-20 14:17:16 +0000677 ++i) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000678 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
Pavel Labathe0a5b572017-01-20 14:17:16 +0000679
Pavel Labathd37349f2017-11-10 11:05:49 +0000680 uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber(
Pavel Labathe0a5b572017-01-20 14:17:16 +0000681 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
682 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000683 reg_ctx.GetRegisterInfoAtIndex(reg_to_read);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000684
685 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000686 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000687 if (error.Fail()) {
688 if (log)
689 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
690 __FUNCTION__,
691 reg_info_p->name ? reg_info_p->name
692 : "<unnamed-register>",
693 reg_to_read, error.AsCString());
694 continue;
695 }
696
697 response.PutChar(delimiter);
698 delimiter = ',';
Pavel Labathd37349f2017-11-10 11:05:49 +0000699 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000700 &reg_value, endian::InlHostByteOrder());
701 }
702
703 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 }
705
706 //
707 // Expedite registers.
708 //
709
710 // Grab the register context.
Pavel Labathd37349f2017-11-10 11:05:49 +0000711 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
712 // Expedite all registers in the first register set (i.e. should be GPRs)
713 // that are not contained in other registers.
714 const RegisterSet *reg_set_p;
715 if (reg_ctx.GetRegisterSetCount() > 0 &&
716 ((reg_set_p = reg_ctx.GetRegisterSet(0)) != nullptr)) {
717 if (log)
718 log->Printf("GDBRemoteCommunicationServerLLGS::%s expediting registers "
719 "from set '%s' (registers set count: %zu)",
720 __FUNCTION__,
721 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
722 reg_set_p->num_registers);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723
Pavel Labathd37349f2017-11-10 11:05:49 +0000724 for (const uint32_t *reg_num_p = reg_set_p->registers;
725 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
726 const RegisterInfo *const reg_info_p =
727 reg_ctx.GetRegisterInfoAtIndex(*reg_num_p);
728 if (reg_info_p == nullptr) {
729 if (log)
730 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get "
731 "register info for register set '%s', register index "
732 "%" PRIu32,
733 __FUNCTION__,
734 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
735 *reg_num_p);
736 } else if (reg_info_p->value_regs == nullptr) {
737 // Only expediate registers that are not contained in other registers.
738 RegisterValue reg_value;
739 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
740 if (error.Success()) {
741 response.Printf("%.02x:", *reg_num_p);
742 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
743 &reg_value, lldb::eByteOrderBig);
744 response.PutChar(';');
745 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 if (log)
Pavel Labathd37349f2017-11-10 11:05:49 +0000747 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to read "
748 "register '%s' index %" PRIu32 ": %s",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 __FUNCTION__,
Pavel Labathd37349f2017-11-10 11:05:49 +0000750 reg_info_p->name ? reg_info_p->name
751 : "<unnamed-register>",
752 *reg_num_p, error.AsCString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000757
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 const char *reason_str = GetStopReasonString(tid_stop_info.reason);
759 if (reason_str != nullptr) {
760 response.Printf("reason:%s;", reason_str);
761 }
762
763 if (!description.empty()) {
764 // Description may contains special chars, send as hex bytes.
765 response.PutCString("description:");
766 response.PutCStringAsRawHex8(description.c_str());
767 response.PutChar(';');
768 } else if ((tid_stop_info.reason == eStopReasonException) &&
769 tid_stop_info.details.exception.type) {
770 response.PutCString("metype:");
771 response.PutHex64(tid_stop_info.details.exception.type);
772 response.PutCString(";mecount:");
773 response.PutHex32(tid_stop_info.details.exception.data_count);
774 response.PutChar(';');
775
776 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) {
777 response.PutCString("medata:");
778 response.PutHex64(tid_stop_info.details.exception.data[i]);
779 response.PutChar(';');
780 }
781 }
782
783 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000784}
785
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited(
787 NativeProcessProtocol *process) {
788 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000789
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
791 if (log)
792 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
793
794 PacketResult result = SendStopReasonForState(StateType::eStateExited);
795 if (result != PacketResult::Success) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000796 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
798 "notification for PID %" PRIu64 ", state: eStateExited",
799 __FUNCTION__, process->GetID());
800 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000801
Adrian Prantl05097242018-04-30 16:49:04 +0000802 // Close the pipe to the inferior terminal i/o if we launched it and set one
803 // up.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 MaybeCloseInferiorTerminalConnection();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000805
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 // We are ready to exit the debug monitor.
807 m_exit_now = true;
Pavel Labathd8b3c1a2017-12-18 09:44:29 +0000808 m_mainloop.RequestTermination();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000809}
810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped(
812 NativeProcessProtocol *process) {
813 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000814
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
816 if (log)
817 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000818
Adrian Prantl05097242018-04-30 16:49:04 +0000819 // Send the stop reason unless this is the stop after the launch or attach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 switch (m_inferior_prev_state) {
821 case eStateLaunching:
822 case eStateAttaching:
823 // Don't send anything per debugserver behavior.
824 break;
825 default:
826 // In all other cases, send the stop reason.
827 PacketResult result = SendStopReasonForState(StateType::eStateStopped);
828 if (result != PacketResult::Success) {
829 if (log)
830 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
831 "notification for PID %" PRIu64 ", state: eStateExited",
832 __FUNCTION__, process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000833 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 break;
835 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000836}
837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838void GDBRemoteCommunicationServerLLGS::ProcessStateChanged(
839 NativeProcessProtocol *process, lldb::StateType state) {
840 assert(process && "process cannot be NULL");
841 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
842 if (log) {
843 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
844 "NativeProcessProtocol pid %" PRIu64 ", state: %s",
845 __FUNCTION__, process->GetID(), StateAsCString(state));
846 }
847
848 switch (state) {
849 case StateType::eStateRunning:
850 StartSTDIOForwarding();
851 break;
852
853 case StateType::eStateStopped:
Adrian Prantl05097242018-04-30 16:49:04 +0000854 // Make sure we get all of the pending stdout/stderr from the inferior and
855 // send it to the lldb host before we send the state change notification
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 SendProcessOutput();
857 // Then stop the forwarding, so that any late output (see llvm.org/pr25652)
Adrian Prantl05097242018-04-30 16:49:04 +0000858 // does not interfere with our protocol.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 StopSTDIOForwarding();
860 HandleInferiorState_Stopped(process);
861 break;
862
863 case StateType::eStateExited:
864 // Same as above
865 SendProcessOutput();
866 StopSTDIOForwarding();
867 HandleInferiorState_Exited(process);
868 break;
869
870 default:
871 if (log) {
872 log->Printf("GDBRemoteCommunicationServerLLGS::%s didn't handle state "
873 "change for pid %" PRIu64 ", new state: %s",
874 __FUNCTION__, process->GetID(), StateAsCString(state));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 break;
877 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000878
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 // Remember the previous state reported to us.
880 m_inferior_prev_state = state;
881}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) {
884 ClearProcessSpecificData();
885}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() {
888 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_COMM));
889
890 if (!m_handshake_completed) {
891 if (!HandshakeWithClient()) {
892 if (log)
893 log->Printf("GDBRemoteCommunicationServerLLGS::%s handshake with "
894 "client failed, exiting",
895 __FUNCTION__);
896 m_mainloop.RequestTermination();
897 return;
898 }
899 m_handshake_completed = true;
900 }
901
902 bool interrupt = false;
903 bool done = false;
Zachary Turner97206d52017-05-12 04:51:55 +0000904 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 while (true) {
Pavel Labath1eff73c2016-11-24 10:54:49 +0000906 const PacketResult result = GetPacketAndSendResponse(
907 std::chrono::microseconds(0), error, interrupt, done);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 if (result == PacketResult::ErrorReplyTimeout)
909 break; // No more packets in the queue
910
911 if ((result != PacketResult::Success)) {
912 if (log)
913 log->Printf("GDBRemoteCommunicationServerLLGS::%s processing a packet "
914 "failed: %s",
915 __FUNCTION__, error.AsCString());
916 m_mainloop.RequestTermination();
917 break;
918 }
919 }
920}
921
Zachary Turner97206d52017-05-12 04:51:55 +0000922Status GDBRemoteCommunicationServerLLGS::InitializeConnection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 std::unique_ptr<Connection> &&connection) {
924 IOObjectSP read_object_sp = connection->GetReadObject();
925 GDBRemoteCommunicationServer::SetConnection(connection.release());
926
Zachary Turner97206d52017-05-12 04:51:55 +0000927 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 m_network_handle_up = m_mainloop.RegisterReadObject(
929 read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); },
930 error);
931 return error;
932}
933
934GDBRemoteCommunication::PacketResult
935GDBRemoteCommunicationServerLLGS::SendONotification(const char *buffer,
936 uint32_t len) {
937 if ((buffer == nullptr) || (len == 0)) {
938 // Nothing to send.
939 return PacketResult::Success;
940 }
941
942 StreamString response;
943 response.PutChar('O');
944 response.PutBytesAsRawHex8(buffer, len);
945
946 return SendPacketNoLock(response.GetString());
947}
948
Zachary Turner97206d52017-05-12 04:51:55 +0000949Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) {
950 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951
952 // Set up the reading/handling of process I/O
953 std::unique_ptr<ConnectionFileDescriptor> conn_up(
954 new ConnectionFileDescriptor(fd, true));
955 if (!conn_up) {
956 error.SetErrorString("failed to create ConnectionFileDescriptor");
957 return error;
958 }
959
960 m_stdio_communication.SetCloseOnEOF(false);
961 m_stdio_communication.SetConnection(conn_up.release());
962 if (!m_stdio_communication.IsConnected()) {
963 error.SetErrorString(
964 "failed to set connection for inferior I/O communication");
965 return error;
966 }
967
Zachary Turner97206d52017-05-12 04:51:55 +0000968 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969}
970
971void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() {
972 // Don't forward if not connected (e.g. when attaching).
973 if (!m_stdio_communication.IsConnected())
974 return;
975
Zachary Turner97206d52017-05-12 04:51:55 +0000976 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 lldbassert(!m_stdio_handle_up);
978 m_stdio_handle_up = m_mainloop.RegisterReadObject(
979 m_stdio_communication.GetConnection()->GetReadObject(),
980 [this](MainLoopBase &) { SendProcessOutput(); }, error);
981
982 if (!m_stdio_handle_up) {
983 // Not much we can do about the failure. Log it and continue without
984 // forwarding.
985 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
986 log->Printf("GDBRemoteCommunicationServerLLGS::%s Failed to set up stdio "
987 "forwarding: %s",
988 __FUNCTION__, error.AsCString());
989 }
990}
991
992void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() {
993 m_stdio_handle_up.reset();
994}
995
996void GDBRemoteCommunicationServerLLGS::SendProcessOutput() {
997 char buffer[1024];
998 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +0000999 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 while (true) {
Pavel Labathc4063ee2016-11-25 11:58:44 +00001001 size_t bytes_read = m_stdio_communication.Read(
1002 buffer, sizeof buffer, std::chrono::microseconds(0), status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 switch (status) {
1004 case eConnectionStatusSuccess:
1005 SendONotification(buffer, bytes_read);
1006 break;
1007 case eConnectionStatusLostConnection:
1008 case eConnectionStatusEndOfFile:
1009 case eConnectionStatusError:
1010 case eConnectionStatusNoConnection:
1011 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
1012 log->Printf("GDBRemoteCommunicationServerLLGS::%s Stopping stdio "
1013 "forwarding as communication returned status %d (error: "
1014 "%s)",
1015 __FUNCTION__, status, error.AsCString());
1016 m_stdio_handle_up.reset();
1017 return;
1018
1019 case eConnectionStatusInterrupted:
1020 case eConnectionStatusTimedOut:
1021 return;
1022 }
1023 }
1024}
1025
1026GDBRemoteCommunication::PacketResult
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001027GDBRemoteCommunicationServerLLGS::Handle_jTraceStart(
1028 StringExtractorGDBRemote &packet) {
1029 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1030 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001031 if (!m_debugged_process_up ||
1032 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001033 return SendErrorResponse(68);
1034
1035 if (!packet.ConsumeFront("jTraceStart:"))
1036 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1037
1038 TraceOptions options;
1039 uint64_t type = std::numeric_limits<uint64_t>::max();
1040 uint64_t buffersize = std::numeric_limits<uint64_t>::max();
1041 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1042 uint64_t metabuffersize = std::numeric_limits<uint64_t>::max();
1043
1044 auto json_object = StructuredData::ParseJSON(packet.Peek());
1045
1046 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001047 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001048 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1049
1050 auto json_dict = json_object->GetAsDictionary();
1051
Pavel Labath4c950232017-05-26 13:53:39 +00001052 json_dict->GetValueForKeyAsInteger("metabuffersize", metabuffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001053 options.setMetaDataBufferSize(metabuffersize);
1054
Pavel Labath4c950232017-05-26 13:53:39 +00001055 json_dict->GetValueForKeyAsInteger("buffersize", buffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001056 options.setTraceBufferSize(buffersize);
1057
Pavel Labath4c950232017-05-26 13:53:39 +00001058 json_dict->GetValueForKeyAsInteger("type", type);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001059 options.setType(static_cast<lldb::TraceType>(type));
1060
Pavel Labath4c950232017-05-26 13:53:39 +00001061 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001062 options.setThreadID(tid);
1063
1064 StructuredData::ObjectSP custom_params_sp =
1065 json_dict->GetValueForKey("params");
1066 if (custom_params_sp &&
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001067 custom_params_sp->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001068 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1069
1070 options.setTraceParams(
1071 static_pointer_cast<StructuredData::Dictionary>(custom_params_sp));
1072
1073 if (buffersize == std::numeric_limits<uint64_t>::max() ||
1074 type != lldb::TraceType::eTraceTypeProcessorTrace) {
1075 LLDB_LOG(log, "Ill formed packet buffersize = {0} type = {1}", buffersize,
1076 type);
1077 return SendIllFormedResponse(packet, "JTrace:start: Ill formed packet ");
1078 }
1079
1080 Status error;
1081 lldb::user_id_t uid = LLDB_INVALID_UID;
Pavel Labath82abefa2017-07-18 09:24:48 +00001082 uid = m_debugged_process_up->StartTrace(options, error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001083 LLDB_LOG(log, "uid is {0} , error is {1}", uid, error.GetError());
1084 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001085 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001086
1087 StreamGDBRemote response;
1088 response.Printf("%" PRIx64, uid);
1089 return SendPacketNoLock(response.GetString());
1090}
1091
1092GDBRemoteCommunication::PacketResult
1093GDBRemoteCommunicationServerLLGS::Handle_jTraceStop(
1094 StringExtractorGDBRemote &packet) {
1095 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001096 if (!m_debugged_process_up ||
1097 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001098 return SendErrorResponse(68);
1099
1100 if (!packet.ConsumeFront("jTraceStop:"))
1101 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1102
1103 lldb::user_id_t uid = LLDB_INVALID_UID;
1104 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1105
1106 auto json_object = StructuredData::ParseJSON(packet.Peek());
1107
1108 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001109 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001110 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1111
1112 auto json_dict = json_object->GetAsDictionary();
1113
Pavel Labath4c950232017-05-26 13:53:39 +00001114 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001115 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1116
Pavel Labath4c950232017-05-26 13:53:39 +00001117 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001118
Pavel Labath82abefa2017-07-18 09:24:48 +00001119 Status error = m_debugged_process_up->StopTrace(uid, tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001120
1121 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001122 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001123
1124 return SendOKResponse();
1125}
1126
1127GDBRemoteCommunication::PacketResult
1128GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead(
1129 StringExtractorGDBRemote &packet) {
1130
1131 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001132 if (!m_debugged_process_up ||
1133 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001134 return SendErrorResponse(68);
1135
1136 if (!packet.ConsumeFront("jTraceConfigRead:"))
1137 return SendIllFormedResponse(packet,
1138 "jTraceConfigRead: Ill formed packet ");
1139
1140 lldb::user_id_t uid = LLDB_INVALID_UID;
1141 lldb::tid_t threadid = LLDB_INVALID_THREAD_ID;
1142
1143 auto json_object = StructuredData::ParseJSON(packet.Peek());
1144
1145 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001146 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001147 return SendIllFormedResponse(packet,
1148 "jTraceConfigRead: Ill formed packet ");
1149
1150 auto json_dict = json_object->GetAsDictionary();
1151
Pavel Labath4c950232017-05-26 13:53:39 +00001152 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001153 return SendIllFormedResponse(packet,
1154 "jTraceConfigRead: Ill formed packet ");
1155
Pavel Labath4c950232017-05-26 13:53:39 +00001156 json_dict->GetValueForKeyAsInteger("threadid", threadid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001157
1158 TraceOptions options;
1159 StreamGDBRemote response;
1160
1161 options.setThreadID(threadid);
Pavel Labath82abefa2017-07-18 09:24:48 +00001162 Status error = m_debugged_process_up->GetTraceConfig(uid, options);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001163
1164 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001165 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001166
1167 StreamGDBRemote escaped_response;
1168 StructuredData::Dictionary json_packet;
1169
1170 json_packet.AddIntegerItem("type", options.getType());
1171 json_packet.AddIntegerItem("buffersize", options.getTraceBufferSize());
1172 json_packet.AddIntegerItem("metabuffersize", options.getMetaDataBufferSize());
1173
1174 StructuredData::DictionarySP custom_params = options.getTraceParams();
1175 if (custom_params)
1176 json_packet.AddItem("params", custom_params);
1177
1178 StreamString json_string;
1179 json_packet.Dump(json_string, false);
1180 escaped_response.PutEscapedBytes(json_string.GetData(),
1181 json_string.GetSize());
1182 return SendPacketNoLock(escaped_response.GetString());
1183}
1184
1185GDBRemoteCommunication::PacketResult
1186GDBRemoteCommunicationServerLLGS::Handle_jTraceRead(
1187 StringExtractorGDBRemote &packet) {
1188
1189 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001190 if (!m_debugged_process_up ||
1191 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001192 return SendErrorResponse(68);
1193
1194 enum PacketType { MetaData, BufferData };
1195 PacketType tracetype = MetaData;
1196
1197 if (packet.ConsumeFront("jTraceBufferRead:"))
1198 tracetype = BufferData;
1199 else if (packet.ConsumeFront("jTraceMetaRead:"))
1200 tracetype = MetaData;
1201 else {
1202 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1203 }
1204
1205 lldb::user_id_t uid = LLDB_INVALID_UID;
1206
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001207 uint64_t byte_count = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001208 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001209 uint64_t offset = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001210
1211 auto json_object = StructuredData::ParseJSON(packet.Peek());
1212
1213 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001214 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001215 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1216
1217 auto json_dict = json_object->GetAsDictionary();
1218
Pavel Labath4c950232017-05-26 13:53:39 +00001219 if (!json_dict->GetValueForKeyAsInteger("traceid", uid) ||
1220 !json_dict->GetValueForKeyAsInteger("offset", offset) ||
1221 !json_dict->GetValueForKeyAsInteger("buffersize", byte_count))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001222 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1223
Pavel Labath4c950232017-05-26 13:53:39 +00001224 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001225
1226 // Allocate the response buffer.
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001227 std::unique_ptr<uint8_t[]> buffer (new (std::nothrow) uint8_t[byte_count]);
1228 if (!buffer)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001229 return SendErrorResponse(0x78);
1230
1231 StreamGDBRemote response;
1232 Status error;
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001233 llvm::MutableArrayRef<uint8_t> buf(buffer.get(), byte_count);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001234
1235 if (tracetype == BufferData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001236 error = m_debugged_process_up->GetData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001237 else if (tracetype == MetaData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001238 error = m_debugged_process_up->GetMetaData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001239
1240 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001241 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001242
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001243 for (auto i : buf)
1244 response.PutHex8(i);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001245
1246 StreamGDBRemote escaped_response;
1247 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
1248 return SendPacketNoLock(escaped_response.GetString());
1249}
1250
1251GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo(
1253 StringExtractorGDBRemote &packet) {
1254 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001255 if (!m_debugged_process_up ||
1256 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 return SendErrorResponse(68);
1258
Pavel Labath82abefa2017-07-18 09:24:48 +00001259 lldb::pid_t pid = m_debugged_process_up->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260
1261 if (pid == LLDB_INVALID_PROCESS_ID)
1262 return SendErrorResponse(1);
1263
1264 ProcessInstanceInfo proc_info;
1265 if (!Host::GetProcessInfo(pid, proc_info))
1266 return SendErrorResponse(1);
1267
1268 StreamString response;
1269 CreateProcessInfoResponse_DebugServerStyle(proc_info, response);
1270 return SendPacketNoLock(response.GetString());
1271}
1272
1273GDBRemoteCommunication::PacketResult
1274GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) {
1275 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001276 if (!m_debugged_process_up ||
1277 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 return SendErrorResponse(68);
1279
Adrian Prantl05097242018-04-30 16:49:04 +00001280 // Make sure we set the current thread so g and p packets return the data the
1281 // gdb will expect.
Pavel Labath82abefa2017-07-18 09:24:48 +00001282 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 SetCurrentThreadID(tid);
1284
Pavel Labatha5be48b2017-10-17 15:52:16 +00001285 NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread();
1286 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 return SendErrorResponse(69);
1288
1289 StreamString response;
Pavel Labatha5be48b2017-10-17 15:52:16 +00001290 response.Printf("QC%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291
1292 return SendPacketNoLock(response.GetString());
1293}
1294
1295GDBRemoteCommunication::PacketResult
1296GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) {
1297 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1298
1299 StopSTDIOForwarding();
1300
Pavel Labath82abefa2017-07-18 09:24:48 +00001301 if (!m_debugged_process_up) {
1302 LLDB_LOG(log, "No debugged process found.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 return PacketResult::Success;
1304 }
1305
Pavel Labath82abefa2017-07-18 09:24:48 +00001306 Status error = m_debugged_process_up->Kill();
1307 if (error.Fail())
1308 LLDB_LOG(log, "Failed to kill debugged process {0}: {1}",
1309 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310
1311 // No OK response for kill packet.
1312 // return SendOKResponse ();
1313 return PacketResult::Success;
1314}
1315
1316GDBRemoteCommunication::PacketResult
1317GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR(
1318 StringExtractorGDBRemote &packet) {
1319 packet.SetFilePos(::strlen("QSetDisableASLR:"));
1320 if (packet.GetU32(0))
1321 m_process_launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
1322 else
1323 m_process_launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
1324 return SendOKResponse();
1325}
1326
1327GDBRemoteCommunication::PacketResult
1328GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir(
1329 StringExtractorGDBRemote &packet) {
1330 packet.SetFilePos(::strlen("QSetWorkingDir:"));
1331 std::string path;
1332 packet.GetHexByteString(path);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001333 m_process_launch_info.SetWorkingDirectory(FileSpec(path));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 return SendOKResponse();
1335}
1336
1337GDBRemoteCommunication::PacketResult
1338GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir(
1339 StringExtractorGDBRemote &packet) {
1340 FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()};
1341 if (working_dir) {
1342 StreamString response;
1343 response.PutCStringAsRawHex8(working_dir.GetCString());
1344 return SendPacketNoLock(response.GetString());
1345 }
1346
1347 return SendErrorResponse(14);
1348}
1349
1350GDBRemoteCommunication::PacketResult
1351GDBRemoteCommunicationServerLLGS::Handle_C(StringExtractorGDBRemote &packet) {
1352 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1353 if (log)
1354 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1355
1356 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001357 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 if (log)
1359 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1360 "shared pointer",
1361 __FUNCTION__);
1362 return SendErrorResponse(0x36);
1363 }
1364
1365 // Pull out the signal number.
1366 packet.SetFilePos(::strlen("C"));
1367 if (packet.GetBytesLeft() < 1) {
1368 // Shouldn't be using a C without a signal.
1369 return SendIllFormedResponse(packet, "C packet specified without signal.");
1370 }
1371 const uint32_t signo =
1372 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1373 if (signo == std::numeric_limits<uint32_t>::max())
1374 return SendIllFormedResponse(packet, "failed to parse signal number");
1375
1376 // Handle optional continue address.
1377 if (packet.GetBytesLeft() > 0) {
1378 // FIXME add continue at address support for $C{signo}[;{continue-address}].
1379 if (*packet.Peek() == ';')
1380 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1381 else
1382 return SendIllFormedResponse(
1383 packet, "unexpected content after $C{signal-number}");
1384 }
1385
1386 ResumeActionList resume_actions(StateType::eStateRunning, 0);
Zachary Turner97206d52017-05-12 04:51:55 +00001387 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388
1389 // We have two branches: what to do if a continue thread is specified (in
Adrian Prantl05097242018-04-30 16:49:04 +00001390 // which case we target sending the signal to that thread), or when we don't
1391 // have a continue thread set (in which case we send a signal to the
1392 // process).
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393
1394 // TODO discuss with Greg Clayton, make sure this makes sense.
1395
1396 lldb::tid_t signal_tid = GetContinueThreadID();
1397 if (signal_tid != LLDB_INVALID_THREAD_ID) {
1398 // The resume action for the continue thread (or all threads if a continue
1399 // thread is not set).
1400 ResumeAction action = {GetContinueThreadID(), StateType::eStateRunning,
1401 static_cast<int>(signo)};
1402
1403 // Add the action for the continue thread (or all threads when the continue
1404 // thread isn't present).
1405 resume_actions.Append(action);
1406 } else {
1407 // Send the signal to the process since we weren't targeting a specific
1408 // continue thread with the signal.
Pavel Labath82abefa2017-07-18 09:24:48 +00001409 error = m_debugged_process_up->Signal(signo);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001411 LLDB_LOG(log, "failed to send signal for process {0}: {1}",
1412 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413
1414 return SendErrorResponse(0x52);
1415 }
1416 }
1417
1418 // Resume the threads.
Pavel Labath82abefa2017-07-18 09:24:48 +00001419 error = m_debugged_process_up->Resume(resume_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001421 LLDB_LOG(log, "failed to resume threads for process {0}: {1}",
1422 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423
1424 return SendErrorResponse(0x38);
1425 }
1426
1427 // Don't send an "OK" packet; response is the stopped/exited message.
1428 return PacketResult::Success;
1429}
1430
1431GDBRemoteCommunication::PacketResult
1432GDBRemoteCommunicationServerLLGS::Handle_c(StringExtractorGDBRemote &packet) {
1433 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1434 if (log)
1435 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1436
1437 packet.SetFilePos(packet.GetFilePos() + ::strlen("c"));
1438
1439 // For now just support all continue.
1440 const bool has_continue_address = (packet.GetBytesLeft() > 0);
1441 if (has_continue_address) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001442 LLDB_LOG(log, "not implemented for c[address] variant [{0} remains]",
1443 packet.Peek());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1445 }
1446
1447 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001448 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449 if (log)
1450 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1451 "shared pointer",
1452 __FUNCTION__);
1453 return SendErrorResponse(0x36);
1454 }
1455
1456 // Build the ResumeActionList
1457 ResumeActionList actions(StateType::eStateRunning, 0);
1458
Pavel Labath82abefa2017-07-18 09:24:48 +00001459 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001461 LLDB_LOG(log, "c failed for process {0}: {1}",
1462 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1464 }
1465
Pavel Labath82abefa2017-07-18 09:24:48 +00001466 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467 // No response required from continue.
1468 return PacketResult::Success;
1469}
1470
1471GDBRemoteCommunication::PacketResult
1472GDBRemoteCommunicationServerLLGS::Handle_vCont_actions(
1473 StringExtractorGDBRemote &packet) {
1474 StreamString response;
1475 response.Printf("vCont;c;C;s;S");
1476
1477 return SendPacketNoLock(response.GetString());
1478}
1479
1480GDBRemoteCommunication::PacketResult
1481GDBRemoteCommunicationServerLLGS::Handle_vCont(
1482 StringExtractorGDBRemote &packet) {
1483 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1484 if (log)
1485 log->Printf("GDBRemoteCommunicationServerLLGS::%s handling vCont packet",
1486 __FUNCTION__);
1487
1488 packet.SetFilePos(::strlen("vCont"));
1489
1490 if (packet.GetBytesLeft() == 0) {
1491 if (log)
1492 log->Printf("GDBRemoteCommunicationServerLLGS::%s missing action from "
1493 "vCont package",
1494 __FUNCTION__);
1495 return SendIllFormedResponse(packet, "Missing action from vCont package");
1496 }
1497
1498 // Check if this is all continue (no options or ";c").
1499 if (::strcmp(packet.Peek(), ";c") == 0) {
1500 // Move past the ';', then do a simple 'c'.
1501 packet.SetFilePos(packet.GetFilePos() + 1);
1502 return Handle_c(packet);
1503 } else if (::strcmp(packet.Peek(), ";s") == 0) {
1504 // Move past the ';', then do a simple 's'.
1505 packet.SetFilePos(packet.GetFilePos() + 1);
1506 return Handle_s(packet);
1507 }
1508
1509 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001510 if (!m_debugged_process_up) {
1511 LLDB_LOG(log, "no debugged process");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 return SendErrorResponse(0x36);
1513 }
1514
1515 ResumeActionList thread_actions;
1516
1517 while (packet.GetBytesLeft() && *packet.Peek() == ';') {
1518 // Skip the semi-colon.
1519 packet.GetChar();
1520
1521 // Build up the thread action.
1522 ResumeAction thread_action;
1523 thread_action.tid = LLDB_INVALID_THREAD_ID;
1524 thread_action.state = eStateInvalid;
1525 thread_action.signal = 0;
1526
1527 const char action = packet.GetChar();
1528 switch (action) {
1529 case 'C':
1530 thread_action.signal = packet.GetHexMaxU32(false, 0);
1531 if (thread_action.signal == 0)
1532 return SendIllFormedResponse(
1533 packet, "Could not parse signal in vCont packet C action");
1534 LLVM_FALLTHROUGH;
1535
1536 case 'c':
1537 // Continue
1538 thread_action.state = eStateRunning;
1539 break;
1540
1541 case 'S':
1542 thread_action.signal = packet.GetHexMaxU32(false, 0);
1543 if (thread_action.signal == 0)
1544 return SendIllFormedResponse(
1545 packet, "Could not parse signal in vCont packet S action");
1546 LLVM_FALLTHROUGH;
1547
1548 case 's':
1549 // Step
1550 thread_action.state = eStateStepping;
1551 break;
Pavel Labathabadc222015-11-27 13:33:29 +00001552
Tamas Berghammere13c2732015-02-11 10:29:30 +00001553 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 return SendIllFormedResponse(packet, "Unsupported vCont action");
1555 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00001556 }
1557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 // Parse out optional :{thread-id} value.
1559 if (packet.GetBytesLeft() && (*packet.Peek() == ':')) {
1560 // Consume the separator.
1561 packet.GetChar();
Tamas Berghammere13c2732015-02-11 10:29:30 +00001562
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 thread_action.tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1564 if (thread_action.tid == LLDB_INVALID_THREAD_ID)
1565 return SendIllFormedResponse(
1566 packet, "Could not parse thread number in vCont packet");
Pavel Labath77dc9562015-07-13 10:44:55 +00001567 }
1568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 thread_actions.Append(thread_action);
1570 }
Pavel Labath77dc9562015-07-13 10:44:55 +00001571
Pavel Labath82abefa2017-07-18 09:24:48 +00001572 Status error = m_debugged_process_up->Resume(thread_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001574 LLDB_LOG(log, "vCont failed for process {0}: {1}",
1575 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1577 }
1578
Pavel Labath82abefa2017-07-18 09:24:48 +00001579 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 // No response required from vCont.
1581 return PacketResult::Success;
Pavel Labath77dc9562015-07-13 10:44:55 +00001582}
1583
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) {
1585 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001586 LLDB_LOG(log, "setting current thread id to {0}", tid);
Pavel Labath77dc9562015-07-13 10:44:55 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 m_current_tid = tid;
Pavel Labath82abefa2017-07-18 09:24:48 +00001589 if (m_debugged_process_up)
1590 m_debugged_process_up->SetCurrentThreadID(m_current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591}
1592
1593void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) {
1594 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001595 LLDB_LOG(log, "setting continue thread id to {0}", tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596
1597 m_continue_tid = tid;
Pavel Labath77dc9562015-07-13 10:44:55 +00001598}
1599
Tamas Berghammere13c2732015-02-11 10:29:30 +00001600GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601GDBRemoteCommunicationServerLLGS::Handle_stop_reason(
1602 StringExtractorGDBRemote &packet) {
1603 // Handle the $? gdbremote command.
Tamas Berghammere13c2732015-02-11 10:29:30 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 // If no process, indicate error
Pavel Labath82abefa2017-07-18 09:24:48 +00001606 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607 return SendErrorResponse(02);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001608
Pavel Labath82abefa2017-07-18 09:24:48 +00001609 return SendStopReasonForState(m_debugged_process_up->GetState());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001610}
1611
1612GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613GDBRemoteCommunicationServerLLGS::SendStopReasonForState(
1614 lldb::StateType process_state) {
1615 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00001616
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 switch (process_state) {
1618 case eStateAttaching:
1619 case eStateLaunching:
1620 case eStateRunning:
1621 case eStateStepping:
1622 case eStateDetached:
1623 // NOTE: gdb protocol doc looks like it should return $OK
1624 // when everything is running (i.e. no stopped result).
1625 return PacketResult::Success; // Ignore
Tamas Berghammere13c2732015-02-11 10:29:30 +00001626
Kate Stoneb9c1b512016-09-06 20:57:50 +00001627 case eStateSuspended:
1628 case eStateStopped:
1629 case eStateCrashed: {
Pavel Labath82abefa2017-07-18 09:24:48 +00001630 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Adrian Prantl05097242018-04-30 16:49:04 +00001631 // Make sure we set the current thread so g and p packets return the data
1632 // the gdb will expect.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 SetCurrentThreadID(tid);
1634 return SendStopReplyPacketForThread(tid);
1635 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 case eStateInvalid:
1638 case eStateUnloaded:
1639 case eStateExited:
Pavel Labath82abefa2017-07-18 09:24:48 +00001640 return SendWResponse(m_debugged_process_up.get());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001641
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 default:
Pavel Labath82abefa2017-07-18 09:24:48 +00001643 LLDB_LOG(log, "pid {0}, current state reporting not handled: {1}",
1644 m_debugged_process_up->GetID(), process_state);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 break;
1646 }
Pavel Labath424b2012015-07-28 09:06:56 +00001647
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 return SendErrorResponse(0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001649}
1650
1651GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo(
1653 StringExtractorGDBRemote &packet) {
1654 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001655 if (!m_debugged_process_up ||
1656 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001658
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659 // Ensure we have a thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001660 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0);
1661 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 return SendErrorResponse(69);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001663
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 // Get the register context for the first thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00001665 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666
1667 // Parse out the register number from the request.
1668 packet.SetFilePos(strlen("qRegisterInfo"));
1669 const uint32_t reg_index =
1670 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1671 if (reg_index == std::numeric_limits<uint32_t>::max())
1672 return SendErrorResponse(69);
1673
1674 // Return the end of registers response if we've iterated one past the end of
1675 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001676 if (reg_index >= reg_context.GetUserRegisterCount())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 return SendErrorResponse(69);
1678
Pavel Labathd37349f2017-11-10 11:05:49 +00001679 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 if (!reg_info)
1681 return SendErrorResponse(69);
1682
1683 // Build the reginfos response.
1684 StreamGDBRemote response;
1685
1686 response.PutCString("name:");
1687 response.PutCString(reg_info->name);
1688 response.PutChar(';');
1689
1690 if (reg_info->alt_name && reg_info->alt_name[0]) {
1691 response.PutCString("alt-name:");
1692 response.PutCString(reg_info->alt_name);
1693 response.PutChar(';');
1694 }
1695
1696 response.Printf("bitsize:%" PRIu32 ";offset:%" PRIu32 ";",
1697 reg_info->byte_size * 8, reg_info->byte_offset);
1698
1699 switch (reg_info->encoding) {
1700 case eEncodingUint:
1701 response.PutCString("encoding:uint;");
1702 break;
1703 case eEncodingSint:
1704 response.PutCString("encoding:sint;");
1705 break;
1706 case eEncodingIEEE754:
1707 response.PutCString("encoding:ieee754;");
1708 break;
1709 case eEncodingVector:
1710 response.PutCString("encoding:vector;");
1711 break;
1712 default:
1713 break;
1714 }
1715
1716 switch (reg_info->format) {
1717 case eFormatBinary:
1718 response.PutCString("format:binary;");
1719 break;
1720 case eFormatDecimal:
1721 response.PutCString("format:decimal;");
1722 break;
1723 case eFormatHex:
1724 response.PutCString("format:hex;");
1725 break;
1726 case eFormatFloat:
1727 response.PutCString("format:float;");
1728 break;
1729 case eFormatVectorOfSInt8:
1730 response.PutCString("format:vector-sint8;");
1731 break;
1732 case eFormatVectorOfUInt8:
1733 response.PutCString("format:vector-uint8;");
1734 break;
1735 case eFormatVectorOfSInt16:
1736 response.PutCString("format:vector-sint16;");
1737 break;
1738 case eFormatVectorOfUInt16:
1739 response.PutCString("format:vector-uint16;");
1740 break;
1741 case eFormatVectorOfSInt32:
1742 response.PutCString("format:vector-sint32;");
1743 break;
1744 case eFormatVectorOfUInt32:
1745 response.PutCString("format:vector-uint32;");
1746 break;
1747 case eFormatVectorOfFloat32:
1748 response.PutCString("format:vector-float32;");
1749 break;
Valentina Giusticda0ae42016-09-08 14:16:45 +00001750 case eFormatVectorOfUInt64:
1751 response.PutCString("format:vector-uint64;");
1752 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753 case eFormatVectorOfUInt128:
1754 response.PutCString("format:vector-uint128;");
1755 break;
1756 default:
1757 break;
1758 };
1759
1760 const char *const register_set_name =
Pavel Labathd37349f2017-11-10 11:05:49 +00001761 reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 if (register_set_name) {
1763 response.PutCString("set:");
1764 response.PutCString(register_set_name);
1765 response.PutChar(';');
1766 }
1767
1768 if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] !=
1769 LLDB_INVALID_REGNUM)
1770 response.Printf("ehframe:%" PRIu32 ";",
1771 reg_info->kinds[RegisterKind::eRegisterKindEHFrame]);
1772
1773 if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
1774 response.Printf("dwarf:%" PRIu32 ";",
1775 reg_info->kinds[RegisterKind::eRegisterKindDWARF]);
1776
1777 switch (reg_info->kinds[RegisterKind::eRegisterKindGeneric]) {
1778 case LLDB_REGNUM_GENERIC_PC:
1779 response.PutCString("generic:pc;");
1780 break;
1781 case LLDB_REGNUM_GENERIC_SP:
1782 response.PutCString("generic:sp;");
1783 break;
1784 case LLDB_REGNUM_GENERIC_FP:
1785 response.PutCString("generic:fp;");
1786 break;
1787 case LLDB_REGNUM_GENERIC_RA:
1788 response.PutCString("generic:ra;");
1789 break;
1790 case LLDB_REGNUM_GENERIC_FLAGS:
1791 response.PutCString("generic:flags;");
1792 break;
1793 case LLDB_REGNUM_GENERIC_ARG1:
1794 response.PutCString("generic:arg1;");
1795 break;
1796 case LLDB_REGNUM_GENERIC_ARG2:
1797 response.PutCString("generic:arg2;");
1798 break;
1799 case LLDB_REGNUM_GENERIC_ARG3:
1800 response.PutCString("generic:arg3;");
1801 break;
1802 case LLDB_REGNUM_GENERIC_ARG4:
1803 response.PutCString("generic:arg4;");
1804 break;
1805 case LLDB_REGNUM_GENERIC_ARG5:
1806 response.PutCString("generic:arg5;");
1807 break;
1808 case LLDB_REGNUM_GENERIC_ARG6:
1809 response.PutCString("generic:arg6;");
1810 break;
1811 case LLDB_REGNUM_GENERIC_ARG7:
1812 response.PutCString("generic:arg7;");
1813 break;
1814 case LLDB_REGNUM_GENERIC_ARG8:
1815 response.PutCString("generic:arg8;");
1816 break;
1817 default:
1818 break;
1819 }
1820
1821 if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) {
1822 response.PutCString("container-regs:");
1823 int i = 0;
1824 for (const uint32_t *reg_num = reg_info->value_regs;
1825 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1826 if (i > 0)
1827 response.PutChar(',');
1828 response.Printf("%" PRIx32, *reg_num);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001829 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 response.PutChar(';');
1831 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001832
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) {
1834 response.PutCString("invalidate-regs:");
1835 int i = 0;
1836 for (const uint32_t *reg_num = reg_info->invalidate_regs;
1837 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1838 if (i > 0)
1839 response.PutChar(',');
1840 response.Printf("%" PRIx32, *reg_num);
1841 }
1842 response.PutChar(';');
1843 }
1844
1845 if (reg_info->dynamic_size_dwarf_expr_bytes) {
1846 const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len;
1847 response.PutCString("dynamic_size_dwarf_expr_bytes:");
1848 for (uint32_t i = 0; i < dwarf_opcode_len; ++i)
1849 response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]);
1850 response.PutChar(';');
1851 }
1852 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001853}
1854
1855GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo(
1857 StringExtractorGDBRemote &packet) {
1858 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1859
1860 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001861 if (!m_debugged_process_up ||
1862 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
1863 LLDB_LOG(log, "no process ({0}), returning OK",
1864 m_debugged_process_up ? "invalid process id"
1865 : "null m_debugged_process_up");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001866 return SendOKResponse();
1867 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001868
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 StreamGDBRemote response;
1870 response.PutChar('m');
1871
Pavel Labath82abefa2017-07-18 09:24:48 +00001872 LLDB_LOG(log, "starting thread iteration");
Pavel Labatha5be48b2017-10-17 15:52:16 +00001873 NativeThreadProtocol *thread;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874 uint32_t thread_index;
1875 for (thread_index = 0,
Pavel Labatha5be48b2017-10-17 15:52:16 +00001876 thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
1877 thread; ++thread_index,
1878 thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
1879 LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index,
1880 thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001881 if (thread_index > 0)
1882 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +00001883 response.Printf("%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884 }
1885
Pavel Labath82abefa2017-07-18 09:24:48 +00001886 LLDB_LOG(log, "finished thread iteration");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 return SendPacketNoLock(response.GetString());
1888}
1889
1890GDBRemoteCommunication::PacketResult
1891GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo(
1892 StringExtractorGDBRemote &packet) {
1893 // FIXME for now we return the full thread list in the initial packet and
1894 // always do nothing here.
1895 return SendPacketNoLock("l");
1896}
1897
1898GDBRemoteCommunication::PacketResult
1899GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) {
1900 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1901
1902 // Parse out the register number from the request.
1903 packet.SetFilePos(strlen("p"));
1904 const uint32_t reg_index =
1905 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1906 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1907 if (log)
1908 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1909 "parse register number from request \"%s\"",
1910 __FUNCTION__, packet.GetStringRef().c_str());
1911 return SendErrorResponse(0x15);
1912 }
1913
1914 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001915 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
1916 if (!thread) {
1917 LLDB_LOG(log, "failed, no thread available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918 return SendErrorResponse(0x15);
1919 }
1920
1921 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00001922 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923
1924 // Return the end of registers response if we've iterated one past the end of
1925 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001926 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927 if (log)
1928 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1929 "register %" PRIu32 " beyond register count %" PRIu32,
1930 __FUNCTION__, reg_index,
Pavel Labathd37349f2017-11-10 11:05:49 +00001931 reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001932 return SendErrorResponse(0x15);
1933 }
1934
Pavel Labathd37349f2017-11-10 11:05:49 +00001935 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001936 if (!reg_info) {
1937 if (log)
1938 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1939 "register %" PRIu32 " returned NULL",
1940 __FUNCTION__, reg_index);
1941 return SendErrorResponse(0x15);
1942 }
1943
1944 // Build the reginfos response.
1945 StreamGDBRemote response;
1946
1947 // Retrieve the value
1948 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +00001949 Status error = reg_context.ReadRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950 if (error.Fail()) {
1951 if (log)
1952 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, read of "
1953 "requested register %" PRIu32 " (%s) failed: %s",
1954 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
1955 return SendErrorResponse(0x15);
1956 }
1957
1958 const uint8_t *const data =
1959 reinterpret_cast<const uint8_t *>(reg_value.GetBytes());
1960 if (!data) {
1961 if (log)
1962 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get data "
1963 "bytes from requested register %" PRIu32,
1964 __FUNCTION__, reg_index);
1965 return SendErrorResponse(0x15);
1966 }
1967
1968 // FIXME flip as needed to get data in big/little endian format for this host.
1969 for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i)
1970 response.PutHex8(data[i]);
1971
1972 return SendPacketNoLock(response.GetString());
1973}
1974
1975GDBRemoteCommunication::PacketResult
1976GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) {
1977 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1978
1979 // Ensure there is more content.
1980 if (packet.GetBytesLeft() < 1)
1981 return SendIllFormedResponse(packet, "Empty P packet");
1982
1983 // Parse out the register number from the request.
1984 packet.SetFilePos(strlen("P"));
1985 const uint32_t reg_index =
1986 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1987 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1988 if (log)
1989 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1990 "parse register number from request \"%s\"",
1991 __FUNCTION__, packet.GetStringRef().c_str());
1992 return SendErrorResponse(0x29);
1993 }
1994
1995 // Note debugserver would send an E30 here.
1996 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '='))
1997 return SendIllFormedResponse(
1998 packet, "P packet missing '=' char after register number");
1999
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 // Parse out the value.
2001 uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register
2002 size_t reg_size = packet.GetHexBytesAvail(reg_bytes);
2003
2004 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002005 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2006 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002007 if (log)
2008 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread "
2009 "available (thread index 0)",
2010 __FUNCTION__);
2011 return SendErrorResponse(0x28);
2012 }
2013
2014 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00002015 NativeRegisterContext &reg_context = thread->GetRegisterContext();
2016 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002017 if (!reg_info) {
2018 if (log)
2019 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2020 "register %" PRIu32 " returned NULL",
2021 __FUNCTION__, reg_index);
2022 return SendErrorResponse(0x48);
2023 }
2024
2025 // Return the end of registers response if we've iterated one past the end of
2026 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00002027 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028 if (log)
2029 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2030 "register %" PRIu32 " beyond register count %" PRIu32,
Pavel Labathd37349f2017-11-10 11:05:49 +00002031 __FUNCTION__, reg_index, reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002032 return SendErrorResponse(0x47);
2033 }
2034
Adrian Prantl05097242018-04-30 16:49:04 +00002035 // The dwarf expression are evaluate on host site which may cause register
2036 // size to change Hence the reg_size may not be same as reg_info->bytes_size
Kate Stoneb9c1b512016-09-06 20:57:50 +00002037 if ((reg_size != reg_info->byte_size) &&
2038 !(reg_info->dynamic_size_dwarf_expr_bytes)) {
2039 return SendIllFormedResponse(packet, "P packet register size is incorrect");
2040 }
2041
2042 // Build the reginfos response.
2043 StreamGDBRemote response;
2044
Pavel Labath578a4252017-11-09 10:43:16 +00002045 RegisterValue reg_value(
2046 reg_bytes, reg_size,
2047 m_debugged_process_up->GetArchitecture().GetByteOrder());
Pavel Labathd37349f2017-11-10 11:05:49 +00002048 Status error = reg_context.WriteRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 if (error.Fail()) {
2050 if (log)
2051 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, write of "
2052 "requested register %" PRIu32 " (%s) failed: %s",
2053 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
2054 return SendErrorResponse(0x32);
2055 }
2056
2057 return SendOKResponse();
2058}
2059
2060GDBRemoteCommunication::PacketResult
2061GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) {
2062 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2063
2064 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002065 if (!m_debugged_process_up ||
2066 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067 if (log)
2068 log->Printf(
2069 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2070 __FUNCTION__);
2071 return SendErrorResponse(0x15);
2072 }
2073
2074 // Parse out which variant of $H is requested.
2075 packet.SetFilePos(strlen("H"));
2076 if (packet.GetBytesLeft() < 1) {
2077 if (log)
2078 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, H command "
2079 "missing {g,c} variant",
2080 __FUNCTION__);
2081 return SendIllFormedResponse(packet, "H command missing {g,c} variant");
2082 }
2083
2084 const char h_variant = packet.GetChar();
2085 switch (h_variant) {
2086 case 'g':
2087 break;
2088
2089 case 'c':
2090 break;
2091
2092 default:
2093 if (log)
2094 log->Printf(
2095 "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c",
2096 __FUNCTION__, h_variant);
2097 return SendIllFormedResponse(packet,
2098 "H variant unsupported, should be c or g");
2099 }
2100
2101 // Parse out the thread number.
2102 // FIXME return a parse success/fail value. All values are valid here.
2103 const lldb::tid_t tid =
2104 packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max());
2105
2106 // Ensure we have the given thread when not specifying -1 (all threads) or 0
2107 // (any thread).
2108 if (tid != LLDB_INVALID_THREAD_ID && tid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +00002109 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2110 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002111 if (log)
2112 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
2113 " not found",
2114 __FUNCTION__, tid);
2115 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002118
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119 // Now switch the given thread type.
2120 switch (h_variant) {
2121 case 'g':
2122 SetCurrentThreadID(tid);
2123 break;
2124
2125 case 'c':
2126 SetContinueThreadID(tid);
2127 break;
2128
2129 default:
2130 assert(false && "unsupported $H variant - shouldn't get here");
2131 return SendIllFormedResponse(packet,
2132 "H variant unsupported, should be c or g");
2133 }
2134
2135 return SendOKResponse();
2136}
2137
2138GDBRemoteCommunication::PacketResult
2139GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) {
2140 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2141
2142 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002143 if (!m_debugged_process_up ||
2144 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145 if (log)
2146 log->Printf(
2147 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2148 __FUNCTION__);
2149 return SendErrorResponse(0x15);
2150 }
2151
2152 packet.SetFilePos(::strlen("I"));
2153 uint8_t tmp[4096];
2154 for (;;) {
2155 size_t read = packet.GetHexBytesAvail(tmp);
2156 if (read == 0) {
2157 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002158 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 // write directly to stdin *this might block if stdin buffer is full*
2160 // TODO: enqueue this block in circular buffer and send window size to
2161 // remote host
2162 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00002163 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 m_stdio_communication.Write(tmp, read, status, &error);
2165 if (error.Fail()) {
2166 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002169
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002171}
2172
2173GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174GDBRemoteCommunicationServerLLGS::Handle_interrupt(
2175 StringExtractorGDBRemote &packet) {
2176 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2177
2178 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002179 if (!m_debugged_process_up ||
2180 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
2181 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182 return SendErrorResponse(0x15);
2183 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002184
Kate Stoneb9c1b512016-09-06 20:57:50 +00002185 // Interrupt the process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002186 Status error = m_debugged_process_up->Interrupt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002188 LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(),
2189 error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
2191 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002192
Pavel Labath82abefa2017-07-18 09:24:48 +00002193 LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002194
Kate Stoneb9c1b512016-09-06 20:57:50 +00002195 // No response required from stop all.
2196 return PacketResult::Success;
2197}
Tamas Berghammere13c2732015-02-11 10:29:30 +00002198
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199GDBRemoteCommunication::PacketResult
2200GDBRemoteCommunicationServerLLGS::Handle_memory_read(
2201 StringExtractorGDBRemote &packet) {
2202 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002203
Pavel Labath82abefa2017-07-18 09:24:48 +00002204 if (!m_debugged_process_up ||
2205 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002206 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002207 log->Printf(
2208 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2209 __FUNCTION__);
2210 return SendErrorResponse(0x15);
2211 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002212
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213 // Parse out the memory address.
2214 packet.SetFilePos(strlen("m"));
2215 if (packet.GetBytesLeft() < 1)
2216 return SendIllFormedResponse(packet, "Too short m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002217
Kate Stoneb9c1b512016-09-06 20:57:50 +00002218 // Read the address. Punting on validation.
2219 // FIXME replace with Hex U64 read with no default value that fails on failed
2220 // read.
2221 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002222
Kate Stoneb9c1b512016-09-06 20:57:50 +00002223 // Validate comma.
2224 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2225 return SendIllFormedResponse(packet, "Comma sep missing in m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002226
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227 // Get # bytes to read.
2228 if (packet.GetBytesLeft() < 1)
2229 return SendIllFormedResponse(packet, "Length missing in m packet");
2230
2231 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2232 if (byte_count == 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002233 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002234 log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to read: "
2235 "zero-length packet",
2236 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002237 return SendOKResponse();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 // Allocate the response buffer.
2241 std::string buf(byte_count, '\0');
2242 if (buf.empty())
2243 return SendErrorResponse(0x78);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002244
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245 // Retrieve the process memory.
2246 size_t bytes_read = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002247 Status error = m_debugged_process_up->ReadMemoryWithoutTrap(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248 read_addr, &buf[0], byte_count, bytes_read);
2249 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002250 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2252 " mem 0x%" PRIx64 ": failed to read. Error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002253 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002254 error.AsCString());
2255 return SendErrorResponse(0x08);
2256 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002257
Kate Stoneb9c1b512016-09-06 20:57:50 +00002258 if (bytes_read == 0) {
2259 if (log)
2260 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2261 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes",
Pavel Labath82abefa2017-07-18 09:24:48 +00002262 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002263 byte_count);
2264 return SendErrorResponse(0x08);
2265 }
2266
2267 StreamGDBRemote response;
2268 packet.SetFilePos(0);
2269 char kind = packet.GetChar('?');
2270 if (kind == 'x')
2271 response.PutEscapedBytes(buf.data(), byte_count);
2272 else {
2273 assert(kind == 'm');
2274 for (size_t i = 0; i < bytes_read; ++i)
2275 response.PutHex8(buf[i]);
2276 }
2277
2278 return SendPacketNoLock(response.GetString());
2279}
2280
2281GDBRemoteCommunication::PacketResult
2282GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) {
2283 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2284
Pavel Labath82abefa2017-07-18 09:24:48 +00002285 if (!m_debugged_process_up ||
2286 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 if (log)
2288 log->Printf(
2289 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2290 __FUNCTION__);
2291 return SendErrorResponse(0x15);
2292 }
2293
2294 // Parse out the memory address.
2295 packet.SetFilePos(strlen("M"));
2296 if (packet.GetBytesLeft() < 1)
2297 return SendIllFormedResponse(packet, "Too short M packet");
2298
2299 // Read the address. Punting on validation.
2300 // FIXME replace with Hex U64 read with no default value that fails on failed
2301 // read.
2302 const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0);
2303
2304 // Validate comma.
2305 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2306 return SendIllFormedResponse(packet, "Comma sep missing in M packet");
2307
2308 // Get # bytes to read.
2309 if (packet.GetBytesLeft() < 1)
2310 return SendIllFormedResponse(packet, "Length missing in M packet");
2311
2312 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2313 if (byte_count == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002314 LLDB_LOG(log, "nothing to write: zero-length packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002315 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 }
2317
2318 // Validate colon.
2319 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':'))
2320 return SendIllFormedResponse(
2321 packet, "Comma sep missing in M packet after byte length");
2322
2323 // Allocate the conversion buffer.
2324 std::vector<uint8_t> buf(byte_count, 0);
2325 if (buf.empty())
2326 return SendErrorResponse(0x78);
2327
2328 // Convert the hex memory write contents to bytes.
2329 StreamGDBRemote response;
2330 const uint64_t convert_count = packet.GetHexBytes(buf, 0);
2331 if (convert_count != byte_count) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002332 LLDB_LOG(log,
2333 "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} "
2334 "to convert.",
2335 m_debugged_process_up->GetID(), write_addr, byte_count,
2336 convert_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 return SendIllFormedResponse(packet, "M content byte length specified did "
2338 "not match hex-encoded content "
2339 "length");
2340 }
2341
2342 // Write the process memory.
2343 size_t bytes_written = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002344 Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0],
Zachary Turner97206d52017-05-12 04:51:55 +00002345 byte_count, bytes_written);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002347 LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}",
2348 m_debugged_process_up->GetID(), write_addr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349 return SendErrorResponse(0x09);
2350 }
2351
2352 if (bytes_written == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002353 LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes",
2354 m_debugged_process_up->GetID(), write_addr, byte_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002355 return SendErrorResponse(0x09);
2356 }
2357
2358 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002359}
2360
2361GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported(
2363 StringExtractorGDBRemote &packet) {
2364 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002365
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366 // Currently only the NativeProcessProtocol knows if it can handle a
Adrian Prantl05097242018-04-30 16:49:04 +00002367 // qMemoryRegionInfoSupported request, but we're not guaranteed to be
2368 // attached to a process. For now we'll assume the client only asks this
2369 // when a process is being debugged.
Tamas Berghammere13c2732015-02-11 10:29:30 +00002370
Kate Stoneb9c1b512016-09-06 20:57:50 +00002371 // Ensure we have a process running; otherwise, we can't figure this out
2372 // since we won't have a NativeProcessProtocol.
Pavel Labath82abefa2017-07-18 09:24:48 +00002373 if (!m_debugged_process_up ||
2374 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002375 if (log)
2376 log->Printf(
2377 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2378 __FUNCTION__);
2379 return SendErrorResponse(0x15);
2380 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002381
Kate Stoneb9c1b512016-09-06 20:57:50 +00002382 // Test if we can get any region back when asking for the region around NULL.
2383 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002384 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002385 m_debugged_process_up->GetMemoryRegionInfo(0, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386 if (error.Fail()) {
2387 // We don't support memory region info collection for this
2388 // NativeProcessProtocol.
2389 return SendUnimplementedResponse("");
2390 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002391
Kate Stoneb9c1b512016-09-06 20:57:50 +00002392 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002393}
2394
2395GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
2397 StringExtractorGDBRemote &packet) {
2398 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002399
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002401 if (!m_debugged_process_up ||
2402 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403 if (log)
2404 log->Printf(
2405 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2406 __FUNCTION__);
2407 return SendErrorResponse(0x15);
2408 }
2409
2410 // Parse out the memory address.
2411 packet.SetFilePos(strlen("qMemoryRegionInfo:"));
2412 if (packet.GetBytesLeft() < 1)
2413 return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet");
2414
2415 // Read the address. Punting on validation.
2416 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
2417
2418 StreamGDBRemote response;
2419
2420 // Get the memory region info for the target address.
2421 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002422 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002423 m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 if (error.Fail()) {
2425 // Return the error message.
2426
2427 response.PutCString("error:");
2428 response.PutCStringAsRawHex8(error.AsCString());
2429 response.PutChar(';');
2430 } else {
2431 // Range start and size.
2432 response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";",
2433 region_info.GetRange().GetRangeBase(),
2434 region_info.GetRange().GetByteSize());
2435
2436 // Permissions.
2437 if (region_info.GetReadable() || region_info.GetWritable() ||
2438 region_info.GetExecutable()) {
2439 // Write permissions info.
2440 response.PutCString("permissions:");
2441
2442 if (region_info.GetReadable())
2443 response.PutChar('r');
2444 if (region_info.GetWritable())
2445 response.PutChar('w');
2446 if (region_info.GetExecutable())
2447 response.PutChar('x');
2448
2449 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002450 }
2451
Kate Stoneb9c1b512016-09-06 20:57:50 +00002452 // Name
2453 ConstString name = region_info.GetName();
2454 if (name) {
2455 response.PutCString("name:");
2456 response.PutCStringAsRawHex8(name.AsCString());
2457 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002459 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002460
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002462}
2463
2464GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002465GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) {
2466 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002467 if (!m_debugged_process_up ||
2468 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002469 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002470 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471 return SendErrorResponse(0x15);
2472 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002473
Kate Stoneb9c1b512016-09-06 20:57:50 +00002474 // Parse out software or hardware breakpoint or watchpoint requested.
2475 packet.SetFilePos(strlen("Z"));
2476 if (packet.GetBytesLeft() < 1)
2477 return SendIllFormedResponse(
2478 packet, "Too short Z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002479
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 bool want_breakpoint = true;
2481 bool want_hardware = false;
2482 uint32_t watch_flags = 0;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002483
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484 const GDBStoppointType stoppoint_type =
2485 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2486 switch (stoppoint_type) {
2487 case eBreakpointSoftware:
2488 want_hardware = false;
2489 want_breakpoint = true;
2490 break;
2491 case eBreakpointHardware:
2492 want_hardware = true;
2493 want_breakpoint = true;
2494 break;
2495 case eWatchpointWrite:
2496 watch_flags = 1;
2497 want_hardware = true;
2498 want_breakpoint = false;
2499 break;
2500 case eWatchpointRead:
2501 watch_flags = 2;
2502 want_hardware = true;
2503 want_breakpoint = false;
2504 break;
2505 case eWatchpointReadWrite:
2506 watch_flags = 3;
2507 want_hardware = true;
2508 want_breakpoint = false;
2509 break;
2510 case eStoppointInvalid:
2511 return SendIllFormedResponse(
2512 packet, "Z packet had invalid software/hardware specifier");
2513 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002514
Kate Stoneb9c1b512016-09-06 20:57:50 +00002515 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2516 return SendIllFormedResponse(
2517 packet, "Malformed Z packet, expecting comma after stoppoint type");
2518
2519 // Parse out the stoppoint address.
2520 if (packet.GetBytesLeft() < 1)
2521 return SendIllFormedResponse(packet, "Too short Z packet, missing address");
2522 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
2523
2524 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2525 return SendIllFormedResponse(
2526 packet, "Malformed Z packet, expecting comma after address");
2527
2528 // Parse out the stoppoint size (i.e. size hint for opcode size).
2529 const uint32_t size =
2530 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
2531 if (size == std::numeric_limits<uint32_t>::max())
2532 return SendIllFormedResponse(
2533 packet, "Malformed Z packet, failed to parse size argument");
2534
2535 if (want_breakpoint) {
2536 // Try to set the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002537 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002538 m_debugged_process_up->SetBreakpoint(addr, size, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539 if (error.Success())
2540 return SendOKResponse();
2541 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002542 LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}",
2543 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 return SendErrorResponse(0x09);
2545 } else {
2546 // Try to set the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002547 const Status error = m_debugged_process_up->SetWatchpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002548 addr, size, watch_flags, want_hardware);
2549 if (error.Success())
2550 return SendOKResponse();
2551 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002552 LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}",
2553 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002554 return SendErrorResponse(0x09);
2555 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002556}
2557
2558GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002559GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) {
2560 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002561 if (!m_debugged_process_up ||
2562 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002563 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002564 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002565 return SendErrorResponse(0x15);
2566 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002567
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 // Parse out software or hardware breakpoint or watchpoint requested.
2569 packet.SetFilePos(strlen("z"));
2570 if (packet.GetBytesLeft() < 1)
2571 return SendIllFormedResponse(
2572 packet, "Too short z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002573
Kate Stoneb9c1b512016-09-06 20:57:50 +00002574 bool want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002575 bool want_hardware = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002576
Kate Stoneb9c1b512016-09-06 20:57:50 +00002577 const GDBStoppointType stoppoint_type =
2578 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2579 switch (stoppoint_type) {
2580 case eBreakpointHardware:
2581 want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002582 want_hardware = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002583 break;
2584 case eBreakpointSoftware:
2585 want_breakpoint = true;
2586 break;
2587 case eWatchpointWrite:
2588 want_breakpoint = false;
2589 break;
2590 case eWatchpointRead:
2591 want_breakpoint = false;
2592 break;
2593 case eWatchpointReadWrite:
2594 want_breakpoint = false;
2595 break;
2596 default:
2597 return SendIllFormedResponse(
2598 packet, "z packet had invalid software/hardware specifier");
2599 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002600
Kate Stoneb9c1b512016-09-06 20:57:50 +00002601 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2602 return SendIllFormedResponse(
2603 packet, "Malformed z packet, expecting comma after stoppoint type");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002604
Kate Stoneb9c1b512016-09-06 20:57:50 +00002605 // Parse out the stoppoint address.
2606 if (packet.GetBytesLeft() < 1)
2607 return SendIllFormedResponse(packet, "Too short z packet, missing address");
2608 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002609
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2611 return SendIllFormedResponse(
2612 packet, "Malformed z packet, expecting comma after address");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002613
Kate Stoneb9c1b512016-09-06 20:57:50 +00002614 /*
2615 // Parse out the stoppoint size (i.e. size hint for opcode size).
2616 const uint32_t size = packet.GetHexMaxU32 (false,
2617 std::numeric_limits<uint32_t>::max ());
2618 if (size == std::numeric_limits<uint32_t>::max ())
2619 return SendIllFormedResponse(packet, "Malformed z packet, failed to parse
2620 size argument");
2621 */
Tamas Berghammere13c2732015-02-11 10:29:30 +00002622
Kate Stoneb9c1b512016-09-06 20:57:50 +00002623 if (want_breakpoint) {
2624 // Try to clear the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002625 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002626 m_debugged_process_up->RemoveBreakpoint(addr, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002627 if (error.Success())
2628 return SendOKResponse();
2629 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002630 LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}",
2631 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002632 return SendErrorResponse(0x09);
2633 } else {
2634 // Try to clear the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002635 const Status error = m_debugged_process_up->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002636 if (error.Success())
2637 return SendOKResponse();
2638 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002639 LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}",
2640 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002641 return SendErrorResponse(0x09);
2642 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002643}
2644
2645GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002646GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) {
2647 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002648
Kate Stoneb9c1b512016-09-06 20:57:50 +00002649 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002650 if (!m_debugged_process_up ||
2651 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652 if (log)
2653 log->Printf(
2654 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2655 __FUNCTION__);
2656 return SendErrorResponse(0x32);
2657 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002658
Kate Stoneb9c1b512016-09-06 20:57:50 +00002659 // We first try to use a continue thread id. If any one or any all set, use
Adrian Prantl05097242018-04-30 16:49:04 +00002660 // the current thread. Bail out if we don't have a thread id.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002661 lldb::tid_t tid = GetContinueThreadID();
2662 if (tid == 0 || tid == LLDB_INVALID_THREAD_ID)
2663 tid = GetCurrentThreadID();
2664 if (tid == LLDB_INVALID_THREAD_ID)
2665 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002666
Kate Stoneb9c1b512016-09-06 20:57:50 +00002667 // Double check that we have such a thread.
2668 // TODO investigate: on MacOSX we might need to do an UpdateThreads () here.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002669 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2670 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002671 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002672
Kate Stoneb9c1b512016-09-06 20:57:50 +00002673 // Create the step action for the given thread.
2674 ResumeAction action = {tid, eStateStepping, 0};
Tamas Berghammere13c2732015-02-11 10:29:30 +00002675
Kate Stoneb9c1b512016-09-06 20:57:50 +00002676 // Setup the actions list.
2677 ResumeActionList actions;
2678 actions.Append(action);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002679
Kate Stoneb9c1b512016-09-06 20:57:50 +00002680 // All other threads stop while we're single stepping a thread.
2681 actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0);
Pavel Labath82abefa2017-07-18 09:24:48 +00002682 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002683 if (error.Fail()) {
2684 if (log)
2685 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2686 " tid %" PRIu64 " Resume() failed with error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002687 __FUNCTION__, m_debugged_process_up->GetID(), tid,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002688 error.AsCString());
2689 return SendErrorResponse(0x49);
2690 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002691
Kate Stoneb9c1b512016-09-06 20:57:50 +00002692 // No response here - the stop or exit will come from the resulting action.
2693 return PacketResult::Success;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002694}
2695
2696GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002697GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read(
2698 StringExtractorGDBRemote &packet) {
2699// *BSD impls should be able to do this too.
Kamil Rytarowskic93408a2017-03-21 17:27:59 +00002700#if defined(__linux__) || defined(__NetBSD__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002701 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002702
Kate Stoneb9c1b512016-09-06 20:57:50 +00002703 // Parse out the offset.
2704 packet.SetFilePos(strlen("qXfer:auxv:read::"));
2705 if (packet.GetBytesLeft() < 1)
2706 return SendIllFormedResponse(packet,
2707 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002708
Kate Stoneb9c1b512016-09-06 20:57:50 +00002709 const uint64_t auxv_offset =
2710 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2711 if (auxv_offset == std::numeric_limits<uint64_t>::max())
2712 return SendIllFormedResponse(packet,
2713 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002714
Kate Stoneb9c1b512016-09-06 20:57:50 +00002715 // Parse out comma.
2716 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ',')
2717 return SendIllFormedResponse(
2718 packet, "qXfer:auxv:read:: packet missing comma after offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002719
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 // Parse out the length.
2721 const uint64_t auxv_length =
2722 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2723 if (auxv_length == std::numeric_limits<uint64_t>::max())
2724 return SendIllFormedResponse(packet,
2725 "qXfer:auxv:read:: packet missing length");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002726
Kate Stoneb9c1b512016-09-06 20:57:50 +00002727 // Grab the auxv data if we need it.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002728 if (!m_active_auxv_buffer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002729 // Make sure we have a valid process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002730 if (!m_debugged_process_up ||
2731 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002732 if (log)
2733 log->Printf(
2734 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2735 __FUNCTION__);
2736 return SendErrorResponse(0x10);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002737 }
2738
Kate Stoneb9c1b512016-09-06 20:57:50 +00002739 // Grab the auxv data.
Pavel Labath82abefa2017-07-18 09:24:48 +00002740 auto buffer_or_error = m_debugged_process_up->GetAuxvData();
Pavel Labathb7f0f452017-03-17 11:08:40 +00002741 if (!buffer_or_error) {
2742 std::error_code ec = buffer_or_error.getError();
2743 LLDB_LOG(log, "no auxv data retrieved: {0}", ec.message());
2744 return SendErrorResponse(ec.value());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002745 }
Pavel Labathb7f0f452017-03-17 11:08:40 +00002746 m_active_auxv_buffer_up = std::move(*buffer_or_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002747 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002748
Kate Stoneb9c1b512016-09-06 20:57:50 +00002749 StreamGDBRemote response;
2750 bool done_with_buffer = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002751
Pavel Labathb7f0f452017-03-17 11:08:40 +00002752 llvm::StringRef buffer = m_active_auxv_buffer_up->getBuffer();
2753 if (auxv_offset >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002754 // We have nothing left to send. Mark the buffer as complete.
2755 response.PutChar('l');
2756 done_with_buffer = true;
2757 } else {
2758 // Figure out how many bytes are available starting at the given offset.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002759 buffer = buffer.drop_front(auxv_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002760
2761 // Mark the response type according to whether we're reading the remainder
2762 // of the auxv data.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002763 if (auxv_length >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002764 // There will be nothing left to read after this
2765 response.PutChar('l');
2766 done_with_buffer = true;
2767 } else {
2768 // There will still be bytes to read after this request.
2769 response.PutChar('m');
Pavel Labathb7f0f452017-03-17 11:08:40 +00002770 buffer = buffer.take_front(auxv_length);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002771 }
2772
Kate Stoneb9c1b512016-09-06 20:57:50 +00002773 // Now write the data in encoded binary form.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002774 response.PutEscapedBytes(buffer.data(), buffer.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002775 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002776
Kate Stoneb9c1b512016-09-06 20:57:50 +00002777 if (done_with_buffer)
Pavel Labathb7f0f452017-03-17 11:08:40 +00002778 m_active_auxv_buffer_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002779
2780 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002781#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002782 return SendUnimplementedResponse("not implemented on this platform");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002783#endif
2784}
2785
2786GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002787GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState(
2788 StringExtractorGDBRemote &packet) {
2789 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002790
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791 // Move past packet name.
2792 packet.SetFilePos(strlen("QSaveRegisterState"));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002793
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002795 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2796 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 if (m_thread_suffix_supported)
2798 return SendIllFormedResponse(
2799 packet, "No thread specified in QSaveRegisterState packet");
2800 else
2801 return SendIllFormedResponse(packet,
2802 "No thread was is set with the Hg packet");
2803 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002804
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002806 NativeRegisterContext& reg_context = thread->GetRegisterContext();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002807
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 // Save registers to a buffer.
2809 DataBufferSP register_data_sp;
Pavel Labathd37349f2017-11-10 11:05:49 +00002810 Status error = reg_context.ReadAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002811 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002812 LLDB_LOG(log, "pid {0} failed to save all register values: {1}",
2813 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002814 return SendErrorResponse(0x75);
2815 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002816
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817 // Allocate a new save id.
2818 const uint32_t save_id = GetNextSavedRegistersID();
2819 assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) &&
2820 "GetNextRegisterSaveID() returned an existing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002821
Kate Stoneb9c1b512016-09-06 20:57:50 +00002822 // Save the register data buffer under the save id.
2823 {
2824 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2825 m_saved_registers_map[save_id] = register_data_sp;
2826 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002827
Kate Stoneb9c1b512016-09-06 20:57:50 +00002828 // Write the response.
2829 StreamGDBRemote response;
2830 response.Printf("%" PRIu32, save_id);
2831 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002832}
2833
2834GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002835GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState(
2836 StringExtractorGDBRemote &packet) {
2837 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002838
Kate Stoneb9c1b512016-09-06 20:57:50 +00002839 // Parse out save id.
2840 packet.SetFilePos(strlen("QRestoreRegisterState:"));
2841 if (packet.GetBytesLeft() < 1)
2842 return SendIllFormedResponse(
2843 packet, "QRestoreRegisterState packet missing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002844
Kate Stoneb9c1b512016-09-06 20:57:50 +00002845 const uint32_t save_id = packet.GetU32(0);
2846 if (save_id == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002847 LLDB_LOG(log, "QRestoreRegisterState packet has malformed save id, "
2848 "expecting decimal uint32_t");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002849 return SendErrorResponse(0x76);
2850 }
2851
2852 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002853 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2854 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002855 if (m_thread_suffix_supported)
2856 return SendIllFormedResponse(
2857 packet, "No thread specified in QRestoreRegisterState packet");
2858 else
2859 return SendIllFormedResponse(packet,
2860 "No thread was is set with the Hg packet");
2861 }
2862
2863 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002864 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002865
2866 // Retrieve register state buffer, then remove from the list.
2867 DataBufferSP register_data_sp;
2868 {
2869 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2870
2871 // Find the register set buffer for the given save id.
2872 auto it = m_saved_registers_map.find(save_id);
2873 if (it == m_saved_registers_map.end()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002874 LLDB_LOG(log,
2875 "pid {0} does not have a register set save buffer for id {1}",
2876 m_debugged_process_up->GetID(), save_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002877 return SendErrorResponse(0x77);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002879 register_data_sp = it->second;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002880
Kate Stoneb9c1b512016-09-06 20:57:50 +00002881 // Remove it from the map.
2882 m_saved_registers_map.erase(it);
2883 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002884
Pavel Labathd37349f2017-11-10 11:05:49 +00002885 Status error = reg_context.WriteAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002886 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002887 LLDB_LOG(log, "pid {0} failed to restore all register values: {1}",
2888 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 return SendErrorResponse(0x77);
2890 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002891
Kate Stoneb9c1b512016-09-06 20:57:50 +00002892 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002893}
2894
2895GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002896GDBRemoteCommunicationServerLLGS::Handle_vAttach(
2897 StringExtractorGDBRemote &packet) {
2898 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002899
Kate Stoneb9c1b512016-09-06 20:57:50 +00002900 // Consume the ';' after vAttach.
2901 packet.SetFilePos(strlen("vAttach"));
2902 if (!packet.GetBytesLeft() || packet.GetChar() != ';')
2903 return SendIllFormedResponse(packet, "vAttach missing expected ';'");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002904
Kate Stoneb9c1b512016-09-06 20:57:50 +00002905 // Grab the PID to which we will attach (assume hex encoding).
2906 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
2907 if (pid == LLDB_INVALID_PROCESS_ID)
2908 return SendIllFormedResponse(packet,
2909 "vAttach failed to parse the process id");
2910
2911 // Attempt to attach.
2912 if (log)
2913 log->Printf("GDBRemoteCommunicationServerLLGS::%s attempting to attach to "
2914 "pid %" PRIu64,
2915 __FUNCTION__, pid);
2916
Zachary Turner97206d52017-05-12 04:51:55 +00002917 Status error = AttachToProcess(pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002918
2919 if (error.Fail()) {
2920 if (log)
2921 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to attach to "
2922 "pid %" PRIu64 ": %s\n",
2923 __FUNCTION__, pid, error.AsCString());
Pavel Labatha70512a2018-04-11 13:30:54 +00002924 return SendErrorResponse(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002925 }
2926
2927 // Notify we attached by sending a stop packet.
Pavel Labath82abefa2017-07-18 09:24:48 +00002928 return SendStopReasonForState(m_debugged_process_up->GetState());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002929}
2930
2931GDBRemoteCommunication::PacketResult
2932GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) {
2933 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2934
2935 StopSTDIOForwarding();
2936
2937 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002938 if (!m_debugged_process_up ||
2939 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002940 if (log)
2941 log->Printf(
2942 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2943 __FUNCTION__);
2944 return SendErrorResponse(0x15);
2945 }
2946
2947 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2948
2949 // Consume the ';' after D.
2950 packet.SetFilePos(1);
2951 if (packet.GetBytesLeft()) {
2952 if (packet.GetChar() != ';')
2953 return SendIllFormedResponse(packet, "D missing expected ';'");
2954
2955 // Grab the PID from which we will detach (assume hex encoding).
2956 pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002957 if (pid == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958 return SendIllFormedResponse(packet, "D failed to parse the process id");
2959 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002960
Pavel Labath82abefa2017-07-18 09:24:48 +00002961 if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_up->GetID() != pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002962 return SendIllFormedResponse(packet, "Invalid pid");
2963 }
2964
Pavel Labath82abefa2017-07-18 09:24:48 +00002965 const Status error = m_debugged_process_up->Detach();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002966 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002967 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002968 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to detach from "
2969 "pid %" PRIu64 ": %s\n",
Pavel Labath82abefa2017-07-18 09:24:48 +00002970 __FUNCTION__, m_debugged_process_up->GetID(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002971 error.AsCString());
2972 return SendErrorResponse(0x01);
2973 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002974
Kate Stoneb9c1b512016-09-06 20:57:50 +00002975 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002976}
2977
2978GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002979GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo(
2980 StringExtractorGDBRemote &packet) {
2981 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002982
Kate Stoneb9c1b512016-09-06 20:57:50 +00002983 packet.SetFilePos(strlen("qThreadStopInfo"));
2984 const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
2985 if (tid == LLDB_INVALID_THREAD_ID) {
Pavel Labath4a4bb122015-07-16 14:14:35 +00002986 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002987 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
2988 "parse thread id from request \"%s\"",
2989 __FUNCTION__, packet.GetStringRef().c_str());
2990 return SendErrorResponse(0x15);
2991 }
2992 return SendStopReplyPacketForThread(tid);
2993}
2994
2995GDBRemoteCommunication::PacketResult
2996GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo(
2997 StringExtractorGDBRemote &) {
2998 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2999
3000 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003001 if (!m_debugged_process_up ||
3002 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003003 return SendErrorResponse(50);
Pavel Labath82abefa2017-07-18 09:24:48 +00003004 LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003005
Kate Stoneb9c1b512016-09-06 20:57:50 +00003006 StreamString response;
3007 const bool threads_with_valid_stop_info_only = false;
3008 JSONArray::SP threads_array_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +00003009 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003010 if (!threads_array_sp) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003011 LLDB_LOG(log, "failed to prepare a packet for pid {0}",
3012 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003013 return SendErrorResponse(52);
3014 }
Pavel Labath4a4bb122015-07-16 14:14:35 +00003015
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 threads_array_sp->Write(response);
3017 StreamGDBRemote escaped_response;
3018 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
3019 return SendPacketNoLock(escaped_response.GetString());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003020}
3021
3022GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003023GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo(
3024 StringExtractorGDBRemote &packet) {
3025 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003026 if (!m_debugged_process_up ||
3027 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003029
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030 packet.SetFilePos(strlen("qWatchpointSupportInfo"));
3031 if (packet.GetBytesLeft() == 0)
3032 return SendOKResponse();
3033 if (packet.GetChar() != ':')
3034 return SendErrorResponse(67);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003035
Pavel Labath82abefa2017-07-18 09:24:48 +00003036 auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo();
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003037
Kate Stoneb9c1b512016-09-06 20:57:50 +00003038 StreamGDBRemote response;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003039 if (hw_debug_cap == llvm::None)
3040 response.Printf("num:0;");
3041 else
3042 response.Printf("num:%d;", hw_debug_cap->second);
3043
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00003045}
3046
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003047GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003048GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress(
3049 StringExtractorGDBRemote &packet) {
3050 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003051 if (!m_debugged_process_up ||
3052 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 return SendErrorResponse(67);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003054
Kate Stoneb9c1b512016-09-06 20:57:50 +00003055 packet.SetFilePos(strlen("qFileLoadAddress:"));
3056 if (packet.GetBytesLeft() == 0)
3057 return SendErrorResponse(68);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003058
Kate Stoneb9c1b512016-09-06 20:57:50 +00003059 std::string file_name;
3060 packet.GetHexByteString(file_name);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003061
Kate Stoneb9c1b512016-09-06 20:57:50 +00003062 lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +00003063 Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00003064 m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003065 if (error.Fail())
3066 return SendErrorResponse(69);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003067
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068 if (file_load_address == LLDB_INVALID_ADDRESS)
3069 return SendErrorResponse(1); // File not loaded
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071 StreamGDBRemote response;
3072 response.PutHex64(file_load_address);
3073 return SendPacketNoLock(response.GetString());
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003074}
3075
Pavel Labath4a705e72017-02-24 09:29:14 +00003076GDBRemoteCommunication::PacketResult
3077GDBRemoteCommunicationServerLLGS::Handle_QPassSignals(
3078 StringExtractorGDBRemote &packet) {
3079 std::vector<int> signals;
3080 packet.SetFilePos(strlen("QPassSignals:"));
3081
Adrian Prantl05097242018-04-30 16:49:04 +00003082 // Read sequence of hex signal numbers divided by a semicolon and optionally
3083 // spaces.
Pavel Labath4a705e72017-02-24 09:29:14 +00003084 while (packet.GetBytesLeft() > 0) {
3085 int signal = packet.GetS32(-1, 16);
3086 if (signal < 0)
3087 return SendIllFormedResponse(packet, "Failed to parse signal number.");
3088 signals.push_back(signal);
3089
3090 packet.SkipSpaces();
3091 char separator = packet.GetChar();
3092 if (separator == '\0')
3093 break; // End of string
3094 if (separator != ';')
3095 return SendIllFormedResponse(packet, "Invalid separator,"
3096 " expected semicolon.");
3097 }
3098
3099 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003100 if (!m_debugged_process_up)
Pavel Labath4a705e72017-02-24 09:29:14 +00003101 return SendErrorResponse(68);
3102
Pavel Labath82abefa2017-07-18 09:24:48 +00003103 Status error = m_debugged_process_up->IgnoreSignals(signals);
Pavel Labath4a705e72017-02-24 09:29:14 +00003104 if (error.Fail())
3105 return SendErrorResponse(69);
3106
3107 return SendOKResponse();
3108}
3109
Kate Stoneb9c1b512016-09-06 20:57:50 +00003110void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() {
3111 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003112
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113 // Tell the stdio connection to shut down.
3114 if (m_stdio_communication.IsConnected()) {
3115 auto connection = m_stdio_communication.GetConnection();
3116 if (connection) {
Zachary Turner97206d52017-05-12 04:51:55 +00003117 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003118 connection->Disconnect(&error);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003119
Kate Stoneb9c1b512016-09-06 20:57:50 +00003120 if (error.Success()) {
3121 if (log)
3122 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3123 "terminal stdio - SUCCESS",
3124 __FUNCTION__);
3125 } else {
3126 if (log)
3127 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3128 "terminal stdio - FAIL: %s",
3129 __FUNCTION__, error.AsCString());
3130 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003133}
3134
Pavel Labatha5be48b2017-10-17 15:52:16 +00003135NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
Kate Stoneb9c1b512016-09-06 20:57:50 +00003136 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003137 // We have no thread if we don't have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003138 if (!m_debugged_process_up ||
3139 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003140 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +00003141
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142 // If the client hasn't asked for thread suffix support, there will not be a
Adrian Prantl05097242018-04-30 16:49:04 +00003143 // thread suffix. Use the current thread in that case.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 if (!m_thread_suffix_supported) {
3145 const lldb::tid_t current_tid = GetCurrentThreadID();
3146 if (current_tid == LLDB_INVALID_THREAD_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003147 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003148 else if (current_tid == 0) {
3149 // Pick a thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003150 return m_debugged_process_up->GetThreadAtIndex(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151 } else
Pavel Labath82abefa2017-07-18 09:24:48 +00003152 return m_debugged_process_up->GetThreadByID(current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003154
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003156
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 // Parse out the ';'.
3158 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') {
Tamas Berghammere13c2732015-02-11 10:29:30 +00003159 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3161 "error: expected ';' prior to start of thread suffix: packet "
3162 "contents = '%s'",
3163 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003164 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003165 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003166
Kate Stoneb9c1b512016-09-06 20:57:50 +00003167 if (!packet.GetBytesLeft())
Pavel Labatha5be48b2017-10-17 15:52:16 +00003168 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003169
3170 // Parse out thread: portion.
3171 if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) {
3172 if (log)
3173 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3174 "error: expected 'thread:' but not found, packet contents = "
3175 "'%s'",
3176 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003177 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003178 }
3179 packet.SetFilePos(packet.GetFilePos() + strlen("thread:"));
3180 const lldb::tid_t tid = packet.GetHexMaxU64(false, 0);
3181 if (tid != 0)
Pavel Labath82abefa2017-07-18 09:24:48 +00003182 return m_debugged_process_up->GetThreadByID(tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003183
Pavel Labatha5be48b2017-10-17 15:52:16 +00003184 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003185}
3186
3187lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const {
3188 if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) {
Adrian Prantl05097242018-04-30 16:49:04 +00003189 // Use whatever the debug process says is the current thread id since the
3190 // protocol either didn't specify or specified we want any/all threads
3191 // marked as the current thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003192 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003193 return LLDB_INVALID_THREAD_ID;
Pavel Labath82abefa2017-07-18 09:24:48 +00003194 return m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003195 }
3196 // Use the specific current thread id set by the gdb remote protocol.
3197 return m_current_tid;
3198}
3199
3200uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() {
3201 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
3202 return m_next_saved_registers_id++;
3203}
3204
3205void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() {
Pavel Labathb7f0f452017-03-17 11:08:40 +00003206 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003207
Pavel Labathb7f0f452017-03-17 11:08:40 +00003208 LLDB_LOG(log, "clearing auxv buffer: {0}", m_active_auxv_buffer_up.get());
3209 m_active_auxv_buffer_up.reset();
Tamas Berghammere13c2732015-02-11 10:29:30 +00003210}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003211
3212FileSpec
Kate Stoneb9c1b512016-09-06 20:57:50 +00003213GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path,
3214 const ArchSpec &arch) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003215 if (m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003216 FileSpec file_spec;
Pavel Labath82abefa2017-07-18 09:24:48 +00003217 if (m_debugged_process_up
Kate Stoneb9c1b512016-09-06 20:57:50 +00003218 ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec)
3219 .Success()) {
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +00003220 if (FileSystem::Instance().Exists(file_spec))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003221 return file_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003222 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003223 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003224
Kate Stoneb9c1b512016-09-06 20:57:50 +00003225 return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003226}