blob: 618f980f3dd6abae84263b72e88ced64f83c54f1 [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerLLGS.cpp --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <errno.h>
11
12#include "lldb/Host/Config.h"
13
14#include "GDBRemoteCommunicationServerLLGS.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000015#include "lldb/Utility/StreamGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000016
17// C Includes
18// C++ Includes
Tamas Berghammere13c2732015-02-11 10:29:30 +000019#include <chrono>
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include <cstring>
Tamas Berghammere13c2732015-02-11 10:29:30 +000021#include <thread>
22
23// Other libraries and framework includes
Tamas Berghammere13c2732015-02-11 10:29:30 +000024#include "lldb/Host/ConnectionFileDescriptor.h"
25#include "lldb/Host/Debug.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000026#include "lldb/Host/File.h"
27#include "lldb/Host/FileSystem.h"
28#include "lldb/Host/Host.h"
29#include "lldb/Host/HostInfo.h"
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000030#include "lldb/Host/PosixApi.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000031#include "lldb/Host/common/NativeProcessProtocol.h"
32#include "lldb/Host/common/NativeRegisterContext.h"
33#include "lldb/Host/common/NativeThreadProtocol.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000034#include "lldb/Target/FileAction.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000035#include "lldb/Target/MemoryRegionInfo.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000036#include "lldb/Utility/Args.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000037#include "lldb/Utility/DataBuffer.h"
Zachary Turner01c32432017-02-14 19:06:07 +000038#include "lldb/Utility/Endian.h"
Pavel Labath4a4bb122015-07-16 14:14:35 +000039#include "lldb/Utility/JSON.h"
Pavel Labathabadc222015-11-27 13:33:29 +000040#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000041#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000042#include "lldb/Utility/RegisterValue.h"
43#include "lldb/Utility/State.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000044#include "lldb/Utility/StreamString.h"
Pavel Labath5f7e5832017-02-10 12:21:22 +000045#include "lldb/Utility/UriParser.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000046#include "llvm/ADT/Triple.h"
47#include "llvm/Support/ScopedPrinter.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000048
49// Project includes
Tamas Berghammere13c2732015-02-11 10:29:30 +000050#include "ProcessGDBRemote.h"
51#include "ProcessGDBRemoteLog.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000052#include "lldb/Utility/StringExtractorGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000053
54using namespace lldb;
55using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000056using namespace lldb_private::process_gdb_remote;
Tamas Berghammer783bfc82015-06-18 20:43:56 +000057using namespace llvm;
Tamas Berghammere13c2732015-02-11 10:29:30 +000058
59//----------------------------------------------------------------------
60// GDBRemote Errors
61//----------------------------------------------------------------------
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063namespace {
64enum GDBRemoteServerError {
65 // Set to the first unused error number in literal form below
66 eErrorFirst = 29,
67 eErrorNoProcess = eErrorFirst,
68 eErrorResume,
69 eErrorExitStatus
70};
Tamas Berghammere13c2732015-02-11 10:29:30 +000071}
72
73//----------------------------------------------------------------------
74// GDBRemoteCommunicationServerLLGS constructor
75//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000076GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS(
Pavel Labath96e600f2017-07-07 11:02:19 +000077 MainLoop &mainloop, const NativeProcessProtocol::Factory &process_factory)
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 : GDBRemoteCommunicationServerCommon("gdb-remote.server",
79 "gdb-remote.server.rx_packet"),
Pavel Labath96e600f2017-07-07 11:02:19 +000080 m_mainloop(mainloop), m_process_factory(process_factory),
81 m_stdio_communication("process.stdio") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 RegisterPacketHandlers();
Tamas Berghammere13c2732015-02-11 10:29:30 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085void GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() {
86 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C,
87 &GDBRemoteCommunicationServerLLGS::Handle_C);
88 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c,
89 &GDBRemoteCommunicationServerLLGS::Handle_c);
90 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D,
91 &GDBRemoteCommunicationServerLLGS::Handle_D);
92 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H,
93 &GDBRemoteCommunicationServerLLGS::Handle_H);
94 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I,
95 &GDBRemoteCommunicationServerLLGS::Handle_I);
96 RegisterMemberFunctionHandler(
97 StringExtractorGDBRemote::eServerPacketType_interrupt,
98 &GDBRemoteCommunicationServerLLGS::Handle_interrupt);
99 RegisterMemberFunctionHandler(
100 StringExtractorGDBRemote::eServerPacketType_m,
101 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
102 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M,
103 &GDBRemoteCommunicationServerLLGS::Handle_M);
104 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p,
105 &GDBRemoteCommunicationServerLLGS::Handle_p);
106 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P,
107 &GDBRemoteCommunicationServerLLGS::Handle_P);
108 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC,
109 &GDBRemoteCommunicationServerLLGS::Handle_qC);
110 RegisterMemberFunctionHandler(
111 StringExtractorGDBRemote::eServerPacketType_qfThreadInfo,
112 &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo);
113 RegisterMemberFunctionHandler(
114 StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress,
115 &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress);
116 RegisterMemberFunctionHandler(
117 StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir,
118 &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir);
119 RegisterMemberFunctionHandler(
120 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo,
121 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo);
122 RegisterMemberFunctionHandler(
123 StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported,
124 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported);
125 RegisterMemberFunctionHandler(
126 StringExtractorGDBRemote::eServerPacketType_qProcessInfo,
127 &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo);
128 RegisterMemberFunctionHandler(
129 StringExtractorGDBRemote::eServerPacketType_qRegisterInfo,
130 &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo);
131 RegisterMemberFunctionHandler(
132 StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState,
133 &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState);
134 RegisterMemberFunctionHandler(
135 StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState,
136 &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState);
137 RegisterMemberFunctionHandler(
138 StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR,
139 &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR);
140 RegisterMemberFunctionHandler(
141 StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir,
142 &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir);
143 RegisterMemberFunctionHandler(
144 StringExtractorGDBRemote::eServerPacketType_qsThreadInfo,
145 &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo);
146 RegisterMemberFunctionHandler(
147 StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo,
148 &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo);
149 RegisterMemberFunctionHandler(
150 StringExtractorGDBRemote::eServerPacketType_jThreadsInfo,
151 &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo);
152 RegisterMemberFunctionHandler(
153 StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo,
154 &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo);
155 RegisterMemberFunctionHandler(
156 StringExtractorGDBRemote::eServerPacketType_qXfer_auxv_read,
157 &GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read);
158 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s,
159 &GDBRemoteCommunicationServerLLGS::Handle_s);
160 RegisterMemberFunctionHandler(
161 StringExtractorGDBRemote::eServerPacketType_stop_reason,
162 &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ?
163 RegisterMemberFunctionHandler(
164 StringExtractorGDBRemote::eServerPacketType_vAttach,
165 &GDBRemoteCommunicationServerLLGS::Handle_vAttach);
166 RegisterMemberFunctionHandler(
167 StringExtractorGDBRemote::eServerPacketType_vCont,
168 &GDBRemoteCommunicationServerLLGS::Handle_vCont);
169 RegisterMemberFunctionHandler(
170 StringExtractorGDBRemote::eServerPacketType_vCont_actions,
171 &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions);
172 RegisterMemberFunctionHandler(
173 StringExtractorGDBRemote::eServerPacketType_x,
174 &GDBRemoteCommunicationServerLLGS::Handle_memory_read);
175 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z,
176 &GDBRemoteCommunicationServerLLGS::Handle_Z);
177 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z,
178 &GDBRemoteCommunicationServerLLGS::Handle_z);
Pavel Labath4a705e72017-02-24 09:29:14 +0000179 RegisterMemberFunctionHandler(
180 StringExtractorGDBRemote::eServerPacketType_QPassSignals,
181 &GDBRemoteCommunicationServerLLGS::Handle_QPassSignals);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000182
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +0000183 RegisterMemberFunctionHandler(
184 StringExtractorGDBRemote::eServerPacketType_jTraceStart,
185 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStart);
186 RegisterMemberFunctionHandler(
187 StringExtractorGDBRemote::eServerPacketType_jTraceBufferRead,
188 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
189 RegisterMemberFunctionHandler(
190 StringExtractorGDBRemote::eServerPacketType_jTraceMetaRead,
191 &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead);
192 RegisterMemberFunctionHandler(
193 StringExtractorGDBRemote::eServerPacketType_jTraceStop,
194 &GDBRemoteCommunicationServerLLGS::Handle_jTraceStop);
195 RegisterMemberFunctionHandler(
196 StringExtractorGDBRemote::eServerPacketType_jTraceConfigRead,
197 &GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead);
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k,
Zachary Turner97206d52017-05-12 04:51:55 +0000200 [this](StringExtractorGDBRemote packet, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 bool &interrupt, bool &quit) {
202 quit = true;
203 return this->Handle_k(packet);
204 });
Tamas Berghammere13c2732015-02-11 10:29:30 +0000205}
206
Pavel Labath11e59172017-12-18 14:31:39 +0000207void GDBRemoteCommunicationServerLLGS::SetLaunchInfo(const ProcessLaunchInfo &info) {
208 m_process_launch_info = info;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000209}
210
Zachary Turner97206d52017-05-12 04:51:55 +0000211Status GDBRemoteCommunicationServerLLGS::LaunchProcess() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 if (!m_process_launch_info.GetArguments().GetArgumentCount())
Zachary Turner97206d52017-05-12 04:51:55 +0000215 return Status("%s: no process command line specified to launch",
216 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 const bool should_forward_stdio =
219 m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
220 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
221 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr;
222 m_process_launch_info.SetLaunchInSeparateProcessGroup(true);
223 m_process_launch_info.GetFlags().Set(eLaunchFlagDebug);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 const bool default_to_use_pty = true;
226 m_process_launch_info.FinalizeFileActions(nullptr, default_to_use_pty);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 {
229 std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex);
Pavel Labath82abefa2017-07-18 09:24:48 +0000230 assert(!m_debugged_process_up && "lldb-server creating debugged "
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 "process but one already exists");
Pavel Labath96e600f2017-07-07 11:02:19 +0000232 auto process_or =
233 m_process_factory.Launch(m_process_launch_info, *this, m_mainloop);
Pavel Labath8630d382017-12-14 14:56:29 +0000234 if (!process_or)
235 return Status(process_or.takeError());
Pavel Labath82abefa2017-07-18 09:24:48 +0000236 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000238
Adrian Prantl05097242018-04-30 16:49:04 +0000239 // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol as
240 // needed. llgs local-process debugging may specify PTY paths, which will
241 // make these file actions non-null process launch -i/e/o will also make
242 // these file actions non-null nullptr means that the traffic is expected to
243 // flow over gdb-remote protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (should_forward_stdio) {
245 // nullptr means it's not redirected to file or pty (in case of LLGS local)
Adrian Prantl05097242018-04-30 16:49:04 +0000246 // at least one of stdio will be transferred pty<->gdb-remote we need to
247 // give the pty master handle to this object to read and/or write
Pavel Labath82abefa2017-07-18 09:24:48 +0000248 LLDB_LOG(log,
249 "pid = {0}: setting up stdout/stderr redirection via $O "
250 "gdb-remote commands",
251 m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 // Setup stdout/stderr mapping from inferior to $O
Pavel Labath82abefa2017-07-18 09:24:48 +0000254 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 if (terminal_fd >= 0) {
256 if (log)
257 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
258 "inferior STDIO fd to %d",
259 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000260 Status status = SetSTDIOFileDescriptor(terminal_fd);
261 if (status.Fail())
262 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 } else {
264 if (log)
265 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
266 "inferior STDIO since terminal fd reported as %d",
267 __FUNCTION__, terminal_fd);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 } else {
Pavel Labath82abefa2017-07-18 09:24:48 +0000270 LLDB_LOG(log,
271 "pid = {0} skipping stdout/stderr redirection via $O: inferior "
272 "will communicate over client-provided file descriptors",
273 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 }
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 printf("Launched '%s' as process %" PRIu64 "...\n",
277 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
Pavel Labath82abefa2017-07-18 09:24:48 +0000278 m_debugged_process_up->GetID());
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000279
Pavel Labath96e600f2017-07-07 11:02:19 +0000280 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000281}
282
Zachary Turner97206d52017-05-12 04:51:55 +0000283Status GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
285 if (log)
286 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64,
287 __FUNCTION__, pid);
288
289 // Before we try to attach, make sure we aren't already monitoring something
290 // else.
Pavel Labath82abefa2017-07-18 09:24:48 +0000291 if (m_debugged_process_up &&
292 m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID)
Pavel Labatha70512a2018-04-11 13:30:54 +0000293 return Status("cannot attach to process %" PRIu64
Zachary Turner97206d52017-05-12 04:51:55 +0000294 " when another process with pid %" PRIu64
295 " is being debugged.",
Pavel Labath82abefa2017-07-18 09:24:48 +0000296 pid, m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297
298 // Try to attach.
Pavel Labath96e600f2017-07-07 11:02:19 +0000299 auto process_or = m_process_factory.Attach(pid, *this, m_mainloop);
300 if (!process_or) {
301 Status status(process_or.takeError());
302 llvm::errs() << llvm::formatv("failed to attach to process {0}: {1}", pid,
303 status);
304 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 }
Pavel Labath82abefa2017-07-18 09:24:48 +0000306 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307
308 // Setup stdout/stderr mapping from inferior.
Pavel Labath82abefa2017-07-18 09:24:48 +0000309 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 if (terminal_fd >= 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000311 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
313 "inferior STDIO fd to %d",
314 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000315 Status status = SetSTDIOFileDescriptor(terminal_fd);
316 if (status.Fail())
317 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 } else {
319 if (log)
320 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
321 "inferior STDIO since terminal fd reported as %d",
322 __FUNCTION__, terminal_fd);
323 }
324
325 printf("Attached to process %" PRIu64 "...\n", pid);
Pavel Labath96e600f2017-07-07 11:02:19 +0000326 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327}
328
329void GDBRemoteCommunicationServerLLGS::InitializeDelegate(
330 NativeProcessProtocol *process) {
331 assert(process && "process cannot be NULL");
332 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
333 if (log) {
334 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
335 "NativeProcessProtocol pid %" PRIu64 ", current state: %s",
336 __FUNCTION__, process->GetID(),
337 StateAsCString(process->GetState()));
338 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000339}
340
341GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342GDBRemoteCommunicationServerLLGS::SendWResponse(
343 NativeProcessProtocol *process) {
344 assert(process && "process cannot be NULL");
345 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 // send W notification
Pavel Labath3508fc82017-06-19 12:47:50 +0000348 auto wait_status = process->GetExitStatus();
349 if (!wait_status) {
350 LLDB_LOG(log, "pid = {0}, failed to retrieve process exit status",
351 process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 StreamGDBRemote response;
354 response.PutChar('E');
355 response.PutHex8(GDBRemoteServerError::eErrorExitStatus);
356 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 }
Pavel Labath3508fc82017-06-19 12:47:50 +0000358
359 LLDB_LOG(log, "pid = {0}, returning exit type {1}", process->GetID(),
360 *wait_status);
361
362 StreamGDBRemote response;
363 response.Format("{0:g}", *wait_status);
364 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000365}
366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367static void AppendHexValue(StreamString &response, const uint8_t *buf,
368 uint32_t buf_size, bool swap) {
369 int64_t i;
370 if (swap) {
371 for (i = buf_size - 1; i >= 0; i--)
372 response.PutHex8(buf[i]);
373 } else {
374 for (i = 0; i < buf_size; i++)
375 response.PutHex8(buf[i]);
376 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000377}
378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379static void WriteRegisterValueInHexFixedWidth(
Pavel Labathd37349f2017-11-10 11:05:49 +0000380 StreamString &response, NativeRegisterContext &reg_ctx,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000381 const RegisterInfo &reg_info, const RegisterValue *reg_value_p,
382 lldb::ByteOrder byte_order) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 RegisterValue reg_value;
384 if (!reg_value_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000385 Status error = reg_ctx.ReadRegister(&reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 if (error.Success())
387 reg_value_p = &reg_value;
388 // else log.
389 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 if (reg_value_p) {
392 AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(),
Pavel Labathe0a5b572017-01-20 14:17:16 +0000393 reg_value_p->GetByteSize(),
394 byte_order == lldb::eByteOrderLittle);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 } else {
396 // Zero-out any unreadable values.
397 if (reg_info.byte_size > 0) {
398 std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0');
399 AppendHexValue(response, zeros.data(), zeros.size(), false);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000402}
403
Pavel Labathe0a5b572017-01-20 14:17:16 +0000404static JSONObject::SP GetRegistersAsJSON(NativeThreadProtocol &thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath4a4bb122015-07-16 14:14:35 +0000406
Pavel Labathd37349f2017-11-10 11:05:49 +0000407 NativeRegisterContext& reg_ctx = thread.GetRegisterContext();
Pavel Labath4a4bb122015-07-16 14:14:35 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 JSONObject::SP register_object_sp = std::make_shared<JSONObject>();
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000410
411#ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET
Adrian Prantl05097242018-04-30 16:49:04 +0000412 // Expedite all registers in the first register set (i.e. should be GPRs)
413 // that are not contained in other registers.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0);
415 if (!reg_set_p)
416 return nullptr;
417 for (const uint32_t *reg_num_p = reg_set_p->registers;
418 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
419 uint32_t reg_num = *reg_num_p;
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000420#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 // Expedite only a couple of registers until we figure out why sending
Adrian Prantl05097242018-04-30 16:49:04 +0000422 // registers is expensive.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 static const uint32_t k_expedited_registers[] = {
424 LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP,
425 LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM};
Pavel Labathc4645bb2015-10-26 16:25:28 +0000426
Pavel Labathe0a5b572017-01-20 14:17:16 +0000427 for (const uint32_t *generic_reg_p = k_expedited_registers;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000429 uint32_t reg_num = reg_ctx.ConvertRegisterKindToRegisterNumber(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 eRegisterKindGeneric, *generic_reg_p);
431 if (reg_num == LLDB_INVALID_REGNUM)
432 continue; // Target does not support the given register.
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000433#endif
434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000436 reg_ctx.GetRegisterInfoAtIndex(reg_num);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 if (reg_info_p == nullptr) {
438 if (log)
439 log->Printf(
440 "%s failed to get register info for register index %" PRIu32,
441 __FUNCTION__, reg_num);
442 continue;
Pavel Labath4a4bb122015-07-16 14:14:35 +0000443 }
444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 if (reg_info_p->value_regs != nullptr)
446 continue; // Only expedite registers that are not contained in other
447 // registers.
Pavel Labath4a4bb122015-07-16 14:14:35 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000450 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 if (error.Fail()) {
452 if (log)
453 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
Pavel Labath05569f62015-07-23 09:09:29 +0000454 __FUNCTION__,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 reg_info_p->name ? reg_info_p->name : "<unnamed-register>",
456 reg_num, error.AsCString());
457 continue;
Pavel Labath05569f62015-07-23 09:09:29 +0000458 }
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 StreamString stream;
Pavel Labathd37349f2017-11-10 11:05:49 +0000461 WriteRegisterValueInHexFixedWidth(stream, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000462 &reg_value, lldb::eByteOrderBig);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463
464 register_object_sp->SetObject(
465 llvm::to_string(reg_num),
466 std::make_shared<JSONString>(stream.GetString()));
467 }
468
469 return register_object_sp;
Pavel Labath05569f62015-07-23 09:09:29 +0000470}
471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472static const char *GetStopReasonString(StopReason stop_reason) {
473 switch (stop_reason) {
474 case eStopReasonTrace:
475 return "trace";
476 case eStopReasonBreakpoint:
477 return "breakpoint";
478 case eStopReasonWatchpoint:
479 return "watchpoint";
480 case eStopReasonSignal:
481 return "signal";
482 case eStopReasonException:
483 return "exception";
484 case eStopReasonExec:
485 return "exec";
486 case eStopReasonInstrumentation:
487 case eStopReasonInvalid:
488 case eStopReasonPlanComplete:
489 case eStopReasonThreadExiting:
490 case eStopReasonNone:
491 break; // ignored
492 }
493 return nullptr;
494}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496static JSONArray::SP GetJSONThreadsInfo(NativeProcessProtocol &process,
497 bool abridged) {
498 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 JSONArray::SP threads_array_sp = std::make_shared<JSONArray>();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 // Ensure we can get info on the given thread.
503 uint32_t thread_idx = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000504 for (NativeThreadProtocol *thread;
505 (thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 ++thread_idx) {
507
Pavel Labatha5be48b2017-10-17 15:52:16 +0000508 lldb::tid_t tid = thread->GetID();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000509
510 // Grab the reason this thread stopped.
511 struct ThreadStopInfo tid_stop_info;
512 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000513 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 const int signum = tid_stop_info.details.signal.signo;
517 if (log) {
518 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
519 " tid %" PRIu64
520 " got signal signo = %d, reason = %d, exc_type = %" PRIu64,
521 __FUNCTION__, process.GetID(), tid, signum,
522 tid_stop_info.reason, tid_stop_info.details.exception.type);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000523 }
524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>();
526 threads_array_sp->AppendObject(thread_obj_sp);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000527
Pavel Labathe0a5b572017-01-20 14:17:16 +0000528 if (!abridged) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000529 if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread))
Pavel Labathe0a5b572017-01-20 14:17:16 +0000530 thread_obj_sp->SetObject("registers", registers_sp);
531 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000532
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 thread_obj_sp->SetObject("tid", std::make_shared<JSONNumber>(tid));
534 if (signum != 0)
535 thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(signum));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000536
Pavel Labatha5be48b2017-10-17 15:52:16 +0000537 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 if (!thread_name.empty())
539 thread_obj_sp->SetObject("name",
540 std::make_shared<JSONString>(thread_name));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542 if (const char *stop_reason_str = GetStopReasonString(tid_stop_info.reason))
543 thread_obj_sp->SetObject("reason",
544 std::make_shared<JSONString>(stop_reason_str));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000545
546 if (!description.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 thread_obj_sp->SetObject("description",
548 std::make_shared<JSONString>(description));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 if ((tid_stop_info.reason == eStopReasonException) &&
551 tid_stop_info.details.exception.type) {
552 thread_obj_sp->SetObject(
553 "metype",
554 std::make_shared<JSONNumber>(tid_stop_info.details.exception.type));
555
556 JSONArray::SP medata_array_sp = std::make_shared<JSONArray>();
557 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count;
558 ++i) {
559 medata_array_sp->AppendObject(std::make_shared<JSONNumber>(
560 tid_stop_info.details.exception.data[i]));
561 }
562 thread_obj_sp->SetObject("medata", medata_array_sp);
563 }
564
565 // TODO: Expedite interesting regions of inferior memory
566 }
567
568 return threads_array_sp;
569}
570
571GDBRemoteCommunication::PacketResult
572GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread(
573 lldb::tid_t tid) {
574 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
575
576 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +0000577 if (!m_debugged_process_up ||
578 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 return SendErrorResponse(50);
580
Pavel Labath82abefa2017-07-18 09:24:48 +0000581 LLDB_LOG(log, "preparing packet for pid {0} tid {1}",
582 m_debugged_process_up->GetID(), tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583
584 // Ensure we can get info on the given thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000585 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
586 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 return SendErrorResponse(51);
588
589 // Grab the reason this thread stopped.
590 struct ThreadStopInfo tid_stop_info;
591 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000592 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 return SendErrorResponse(52);
594
595 // FIXME implement register handling for exec'd inferiors.
Adrian Prantl05097242018-04-30 16:49:04 +0000596 // if (tid_stop_info.reason == eStopReasonExec) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 // const bool force = true;
598 // InitializeRegisters(force);
599 // }
600
601 StreamString response;
602 // Output the T packet with the thread
603 response.PutChar('T');
604 int signum = tid_stop_info.details.signal.signo;
Pavel Labath82abefa2017-07-18 09:24:48 +0000605 LLDB_LOG(
606 log,
607 "pid {0}, tid {1}, got signal signo = {2}, reason = {3}, exc_type = {4}",
608 m_debugged_process_up->GetID(), tid, signum, int(tid_stop_info.reason),
609 tid_stop_info.details.exception.type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610
611 // Print the signal number.
612 response.PutHex8(signum & 0xff);
613
614 // Include the tid.
615 response.Printf("thread:%" PRIx64 ";", tid);
616
617 // Include the thread name if there is one.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000618 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 if (!thread_name.empty()) {
620 size_t thread_name_len = thread_name.length();
621
622 if (::strcspn(thread_name.c_str(), "$#+-;:") == thread_name_len) {
623 response.PutCString("name:");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000624 response.PutCString(thread_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 } else {
626 // The thread name contains special chars, send as hex bytes.
627 response.PutCString("hexname:");
628 response.PutCStringAsRawHex8(thread_name.c_str());
629 }
630 response.PutChar(';');
631 }
632
Adrian Prantl05097242018-04-30 16:49:04 +0000633 // If a 'QListThreadsInStopReply' was sent to enable this feature, we will
634 // send all thread IDs back in the "threads" key whose value is a list of hex
635 // thread IDs separated by commas:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 // "threads:10a,10b,10c;"
Adrian Prantl05097242018-04-30 16:49:04 +0000637 // This will save the debugger from having to send a pair of qfThreadInfo and
638 // qsThreadInfo packets, but it also might take a lot of room in the stop
639 // reply packet, so it must be enabled only on systems where there are no
640 // limits on packet lengths.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 if (m_list_threads_in_stop_reply) {
642 response.PutCString("threads:");
643
644 uint32_t thread_index = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000645 NativeThreadProtocol *listed_thread;
646 for (listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
647 listed_thread; ++thread_index,
648 listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 if (thread_index > 0)
650 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +0000651 response.Printf("%" PRIx64, listed_thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 }
653 response.PutChar(';');
654
Adrian Prantl05097242018-04-30 16:49:04 +0000655 // Include JSON info that describes the stop reason for any threads that
656 // actually have stop reasons. We use the new "jstopinfo" key whose values
657 // is hex ascii JSON that contains the thread IDs thread stop info only for
658 // threads that have stop reasons. Only send this if we have more than one
659 // thread otherwise this packet has all the info it needs.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 if (thread_index > 0) {
661 const bool threads_with_valid_stop_info_only = true;
662 JSONArray::SP threads_info_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +0000663 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (threads_info_sp) {
665 response.PutCString("jstopinfo:");
666 StreamString unescaped_response;
667 threads_info_sp->Write(unescaped_response);
668 response.PutCStringAsRawHex8(unescaped_response.GetData());
669 response.PutChar(';');
Pavel Labath82abefa2017-07-18 09:24:48 +0000670 } else
671 LLDB_LOG(log, "failed to prepare a jstopinfo field for pid {0}",
672 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 }
Pavel Labathe0a5b572017-01-20 14:17:16 +0000674
675 uint32_t i = 0;
676 response.PutCString("thread-pcs");
677 char delimiter = ':';
Pavel Labatha5be48b2017-10-17 15:52:16 +0000678 for (NativeThreadProtocol *thread;
679 (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
Pavel Labathe0a5b572017-01-20 14:17:16 +0000680 ++i) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000681 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
Pavel Labathe0a5b572017-01-20 14:17:16 +0000682
Pavel Labathd37349f2017-11-10 11:05:49 +0000683 uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber(
Pavel Labathe0a5b572017-01-20 14:17:16 +0000684 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
685 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000686 reg_ctx.GetRegisterInfoAtIndex(reg_to_read);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000687
688 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000689 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000690 if (error.Fail()) {
691 if (log)
692 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
693 __FUNCTION__,
694 reg_info_p->name ? reg_info_p->name
695 : "<unnamed-register>",
696 reg_to_read, error.AsCString());
697 continue;
698 }
699
700 response.PutChar(delimiter);
701 delimiter = ',';
Pavel Labathd37349f2017-11-10 11:05:49 +0000702 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000703 &reg_value, endian::InlHostByteOrder());
704 }
705
706 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 }
708
709 //
710 // Expedite registers.
711 //
712
713 // Grab the register context.
Pavel Labathd37349f2017-11-10 11:05:49 +0000714 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
715 // Expedite all registers in the first register set (i.e. should be GPRs)
716 // that are not contained in other registers.
717 const RegisterSet *reg_set_p;
718 if (reg_ctx.GetRegisterSetCount() > 0 &&
719 ((reg_set_p = reg_ctx.GetRegisterSet(0)) != nullptr)) {
720 if (log)
721 log->Printf("GDBRemoteCommunicationServerLLGS::%s expediting registers "
722 "from set '%s' (registers set count: %zu)",
723 __FUNCTION__,
724 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
725 reg_set_p->num_registers);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726
Pavel Labathd37349f2017-11-10 11:05:49 +0000727 for (const uint32_t *reg_num_p = reg_set_p->registers;
728 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
729 const RegisterInfo *const reg_info_p =
730 reg_ctx.GetRegisterInfoAtIndex(*reg_num_p);
731 if (reg_info_p == nullptr) {
732 if (log)
733 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get "
734 "register info for register set '%s', register index "
735 "%" PRIu32,
736 __FUNCTION__,
737 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
738 *reg_num_p);
739 } else if (reg_info_p->value_regs == nullptr) {
740 // Only expediate registers that are not contained in other registers.
741 RegisterValue reg_value;
742 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
743 if (error.Success()) {
744 response.Printf("%.02x:", *reg_num_p);
745 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
746 &reg_value, lldb::eByteOrderBig);
747 response.PutChar(';');
748 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 if (log)
Pavel Labathd37349f2017-11-10 11:05:49 +0000750 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to read "
751 "register '%s' index %" PRIu32 ": %s",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 __FUNCTION__,
Pavel Labathd37349f2017-11-10 11:05:49 +0000753 reg_info_p->name ? reg_info_p->name
754 : "<unnamed-register>",
755 *reg_num_p, error.AsCString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000756 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 const char *reason_str = GetStopReasonString(tid_stop_info.reason);
762 if (reason_str != nullptr) {
763 response.Printf("reason:%s;", reason_str);
764 }
765
766 if (!description.empty()) {
767 // Description may contains special chars, send as hex bytes.
768 response.PutCString("description:");
769 response.PutCStringAsRawHex8(description.c_str());
770 response.PutChar(';');
771 } else if ((tid_stop_info.reason == eStopReasonException) &&
772 tid_stop_info.details.exception.type) {
773 response.PutCString("metype:");
774 response.PutHex64(tid_stop_info.details.exception.type);
775 response.PutCString(";mecount:");
776 response.PutHex32(tid_stop_info.details.exception.data_count);
777 response.PutChar(';');
778
779 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) {
780 response.PutCString("medata:");
781 response.PutHex64(tid_stop_info.details.exception.data[i]);
782 response.PutChar(';');
783 }
784 }
785
786 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000787}
788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited(
790 NativeProcessProtocol *process) {
791 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
794 if (log)
795 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
796
797 PacketResult result = SendStopReasonForState(StateType::eStateExited);
798 if (result != PacketResult::Success) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000799 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
801 "notification for PID %" PRIu64 ", state: eStateExited",
802 __FUNCTION__, process->GetID());
803 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000804
Adrian Prantl05097242018-04-30 16:49:04 +0000805 // Close the pipe to the inferior terminal i/o if we launched it and set one
806 // up.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 MaybeCloseInferiorTerminalConnection();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000808
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 // We are ready to exit the debug monitor.
810 m_exit_now = true;
Pavel Labathd8b3c1a2017-12-18 09:44:29 +0000811 m_mainloop.RequestTermination();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000812}
813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped(
815 NativeProcessProtocol *process) {
816 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
819 if (log)
820 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000821
Adrian Prantl05097242018-04-30 16:49:04 +0000822 // Send the stop reason unless this is the stop after the launch or attach.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 switch (m_inferior_prev_state) {
824 case eStateLaunching:
825 case eStateAttaching:
826 // Don't send anything per debugserver behavior.
827 break;
828 default:
829 // In all other cases, send the stop reason.
830 PacketResult result = SendStopReasonForState(StateType::eStateStopped);
831 if (result != PacketResult::Success) {
832 if (log)
833 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
834 "notification for PID %" PRIu64 ", state: eStateExited",
835 __FUNCTION__, process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 break;
838 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000839}
840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841void GDBRemoteCommunicationServerLLGS::ProcessStateChanged(
842 NativeProcessProtocol *process, lldb::StateType state) {
843 assert(process && "process cannot be NULL");
844 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
845 if (log) {
846 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
847 "NativeProcessProtocol pid %" PRIu64 ", state: %s",
848 __FUNCTION__, process->GetID(), StateAsCString(state));
849 }
850
851 switch (state) {
852 case StateType::eStateRunning:
853 StartSTDIOForwarding();
854 break;
855
856 case StateType::eStateStopped:
Adrian Prantl05097242018-04-30 16:49:04 +0000857 // Make sure we get all of the pending stdout/stderr from the inferior and
858 // send it to the lldb host before we send the state change notification
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 SendProcessOutput();
860 // Then stop the forwarding, so that any late output (see llvm.org/pr25652)
Adrian Prantl05097242018-04-30 16:49:04 +0000861 // does not interfere with our protocol.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 StopSTDIOForwarding();
863 HandleInferiorState_Stopped(process);
864 break;
865
866 case StateType::eStateExited:
867 // Same as above
868 SendProcessOutput();
869 StopSTDIOForwarding();
870 HandleInferiorState_Exited(process);
871 break;
872
873 default:
874 if (log) {
875 log->Printf("GDBRemoteCommunicationServerLLGS::%s didn't handle state "
876 "change for pid %" PRIu64 ", new state: %s",
877 __FUNCTION__, process->GetID(), StateAsCString(state));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 break;
880 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000881
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 // Remember the previous state reported to us.
883 m_inferior_prev_state = state;
884}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000885
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) {
887 ClearProcessSpecificData();
888}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000889
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() {
891 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_COMM));
892
893 if (!m_handshake_completed) {
894 if (!HandshakeWithClient()) {
895 if (log)
896 log->Printf("GDBRemoteCommunicationServerLLGS::%s handshake with "
897 "client failed, exiting",
898 __FUNCTION__);
899 m_mainloop.RequestTermination();
900 return;
901 }
902 m_handshake_completed = true;
903 }
904
905 bool interrupt = false;
906 bool done = false;
Zachary Turner97206d52017-05-12 04:51:55 +0000907 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 while (true) {
Pavel Labath1eff73c2016-11-24 10:54:49 +0000909 const PacketResult result = GetPacketAndSendResponse(
910 std::chrono::microseconds(0), error, interrupt, done);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 if (result == PacketResult::ErrorReplyTimeout)
912 break; // No more packets in the queue
913
914 if ((result != PacketResult::Success)) {
915 if (log)
916 log->Printf("GDBRemoteCommunicationServerLLGS::%s processing a packet "
917 "failed: %s",
918 __FUNCTION__, error.AsCString());
919 m_mainloop.RequestTermination();
920 break;
921 }
922 }
923}
924
Zachary Turner97206d52017-05-12 04:51:55 +0000925Status GDBRemoteCommunicationServerLLGS::InitializeConnection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 std::unique_ptr<Connection> &&connection) {
927 IOObjectSP read_object_sp = connection->GetReadObject();
928 GDBRemoteCommunicationServer::SetConnection(connection.release());
929
Zachary Turner97206d52017-05-12 04:51:55 +0000930 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 m_network_handle_up = m_mainloop.RegisterReadObject(
932 read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); },
933 error);
934 return error;
935}
936
937GDBRemoteCommunication::PacketResult
938GDBRemoteCommunicationServerLLGS::SendONotification(const char *buffer,
939 uint32_t len) {
940 if ((buffer == nullptr) || (len == 0)) {
941 // Nothing to send.
942 return PacketResult::Success;
943 }
944
945 StreamString response;
946 response.PutChar('O');
947 response.PutBytesAsRawHex8(buffer, len);
948
949 return SendPacketNoLock(response.GetString());
950}
951
Zachary Turner97206d52017-05-12 04:51:55 +0000952Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) {
953 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954
955 // Set up the reading/handling of process I/O
956 std::unique_ptr<ConnectionFileDescriptor> conn_up(
957 new ConnectionFileDescriptor(fd, true));
958 if (!conn_up) {
959 error.SetErrorString("failed to create ConnectionFileDescriptor");
960 return error;
961 }
962
963 m_stdio_communication.SetCloseOnEOF(false);
964 m_stdio_communication.SetConnection(conn_up.release());
965 if (!m_stdio_communication.IsConnected()) {
966 error.SetErrorString(
967 "failed to set connection for inferior I/O communication");
968 return error;
969 }
970
Zachary Turner97206d52017-05-12 04:51:55 +0000971 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972}
973
974void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() {
975 // Don't forward if not connected (e.g. when attaching).
976 if (!m_stdio_communication.IsConnected())
977 return;
978
Zachary Turner97206d52017-05-12 04:51:55 +0000979 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 lldbassert(!m_stdio_handle_up);
981 m_stdio_handle_up = m_mainloop.RegisterReadObject(
982 m_stdio_communication.GetConnection()->GetReadObject(),
983 [this](MainLoopBase &) { SendProcessOutput(); }, error);
984
985 if (!m_stdio_handle_up) {
986 // Not much we can do about the failure. Log it and continue without
987 // forwarding.
988 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
989 log->Printf("GDBRemoteCommunicationServerLLGS::%s Failed to set up stdio "
990 "forwarding: %s",
991 __FUNCTION__, error.AsCString());
992 }
993}
994
995void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() {
996 m_stdio_handle_up.reset();
997}
998
999void GDBRemoteCommunicationServerLLGS::SendProcessOutput() {
1000 char buffer[1024];
1001 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00001002 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 while (true) {
Pavel Labathc4063ee2016-11-25 11:58:44 +00001004 size_t bytes_read = m_stdio_communication.Read(
1005 buffer, sizeof buffer, std::chrono::microseconds(0), status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 switch (status) {
1007 case eConnectionStatusSuccess:
1008 SendONotification(buffer, bytes_read);
1009 break;
1010 case eConnectionStatusLostConnection:
1011 case eConnectionStatusEndOfFile:
1012 case eConnectionStatusError:
1013 case eConnectionStatusNoConnection:
1014 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
1015 log->Printf("GDBRemoteCommunicationServerLLGS::%s Stopping stdio "
1016 "forwarding as communication returned status %d (error: "
1017 "%s)",
1018 __FUNCTION__, status, error.AsCString());
1019 m_stdio_handle_up.reset();
1020 return;
1021
1022 case eConnectionStatusInterrupted:
1023 case eConnectionStatusTimedOut:
1024 return;
1025 }
1026 }
1027}
1028
1029GDBRemoteCommunication::PacketResult
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001030GDBRemoteCommunicationServerLLGS::Handle_jTraceStart(
1031 StringExtractorGDBRemote &packet) {
1032 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1033 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001034 if (!m_debugged_process_up ||
1035 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001036 return SendErrorResponse(68);
1037
1038 if (!packet.ConsumeFront("jTraceStart:"))
1039 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1040
1041 TraceOptions options;
1042 uint64_t type = std::numeric_limits<uint64_t>::max();
1043 uint64_t buffersize = std::numeric_limits<uint64_t>::max();
1044 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1045 uint64_t metabuffersize = std::numeric_limits<uint64_t>::max();
1046
1047 auto json_object = StructuredData::ParseJSON(packet.Peek());
1048
1049 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001050 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001051 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1052
1053 auto json_dict = json_object->GetAsDictionary();
1054
Pavel Labath4c950232017-05-26 13:53:39 +00001055 json_dict->GetValueForKeyAsInteger("metabuffersize", metabuffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001056 options.setMetaDataBufferSize(metabuffersize);
1057
Pavel Labath4c950232017-05-26 13:53:39 +00001058 json_dict->GetValueForKeyAsInteger("buffersize", buffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001059 options.setTraceBufferSize(buffersize);
1060
Pavel Labath4c950232017-05-26 13:53:39 +00001061 json_dict->GetValueForKeyAsInteger("type", type);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001062 options.setType(static_cast<lldb::TraceType>(type));
1063
Pavel Labath4c950232017-05-26 13:53:39 +00001064 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001065 options.setThreadID(tid);
1066
1067 StructuredData::ObjectSP custom_params_sp =
1068 json_dict->GetValueForKey("params");
1069 if (custom_params_sp &&
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001070 custom_params_sp->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001071 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1072
1073 options.setTraceParams(
1074 static_pointer_cast<StructuredData::Dictionary>(custom_params_sp));
1075
1076 if (buffersize == std::numeric_limits<uint64_t>::max() ||
1077 type != lldb::TraceType::eTraceTypeProcessorTrace) {
1078 LLDB_LOG(log, "Ill formed packet buffersize = {0} type = {1}", buffersize,
1079 type);
1080 return SendIllFormedResponse(packet, "JTrace:start: Ill formed packet ");
1081 }
1082
1083 Status error;
1084 lldb::user_id_t uid = LLDB_INVALID_UID;
Pavel Labath82abefa2017-07-18 09:24:48 +00001085 uid = m_debugged_process_up->StartTrace(options, error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001086 LLDB_LOG(log, "uid is {0} , error is {1}", uid, error.GetError());
1087 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001088 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001089
1090 StreamGDBRemote response;
1091 response.Printf("%" PRIx64, uid);
1092 return SendPacketNoLock(response.GetString());
1093}
1094
1095GDBRemoteCommunication::PacketResult
1096GDBRemoteCommunicationServerLLGS::Handle_jTraceStop(
1097 StringExtractorGDBRemote &packet) {
1098 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001099 if (!m_debugged_process_up ||
1100 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001101 return SendErrorResponse(68);
1102
1103 if (!packet.ConsumeFront("jTraceStop:"))
1104 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1105
1106 lldb::user_id_t uid = LLDB_INVALID_UID;
1107 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1108
1109 auto json_object = StructuredData::ParseJSON(packet.Peek());
1110
1111 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001112 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001113 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1114
1115 auto json_dict = json_object->GetAsDictionary();
1116
Pavel Labath4c950232017-05-26 13:53:39 +00001117 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001118 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1119
Pavel Labath4c950232017-05-26 13:53:39 +00001120 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001121
Pavel Labath82abefa2017-07-18 09:24:48 +00001122 Status error = m_debugged_process_up->StopTrace(uid, tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001123
1124 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001125 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001126
1127 return SendOKResponse();
1128}
1129
1130GDBRemoteCommunication::PacketResult
1131GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead(
1132 StringExtractorGDBRemote &packet) {
1133
1134 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001135 if (!m_debugged_process_up ||
1136 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001137 return SendErrorResponse(68);
1138
1139 if (!packet.ConsumeFront("jTraceConfigRead:"))
1140 return SendIllFormedResponse(packet,
1141 "jTraceConfigRead: Ill formed packet ");
1142
1143 lldb::user_id_t uid = LLDB_INVALID_UID;
1144 lldb::tid_t threadid = LLDB_INVALID_THREAD_ID;
1145
1146 auto json_object = StructuredData::ParseJSON(packet.Peek());
1147
1148 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001149 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001150 return SendIllFormedResponse(packet,
1151 "jTraceConfigRead: Ill formed packet ");
1152
1153 auto json_dict = json_object->GetAsDictionary();
1154
Pavel Labath4c950232017-05-26 13:53:39 +00001155 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001156 return SendIllFormedResponse(packet,
1157 "jTraceConfigRead: Ill formed packet ");
1158
Pavel Labath4c950232017-05-26 13:53:39 +00001159 json_dict->GetValueForKeyAsInteger("threadid", threadid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001160
1161 TraceOptions options;
1162 StreamGDBRemote response;
1163
1164 options.setThreadID(threadid);
Pavel Labath82abefa2017-07-18 09:24:48 +00001165 Status error = m_debugged_process_up->GetTraceConfig(uid, options);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001166
1167 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001168 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001169
1170 StreamGDBRemote escaped_response;
1171 StructuredData::Dictionary json_packet;
1172
1173 json_packet.AddIntegerItem("type", options.getType());
1174 json_packet.AddIntegerItem("buffersize", options.getTraceBufferSize());
1175 json_packet.AddIntegerItem("metabuffersize", options.getMetaDataBufferSize());
1176
1177 StructuredData::DictionarySP custom_params = options.getTraceParams();
1178 if (custom_params)
1179 json_packet.AddItem("params", custom_params);
1180
1181 StreamString json_string;
1182 json_packet.Dump(json_string, false);
1183 escaped_response.PutEscapedBytes(json_string.GetData(),
1184 json_string.GetSize());
1185 return SendPacketNoLock(escaped_response.GetString());
1186}
1187
1188GDBRemoteCommunication::PacketResult
1189GDBRemoteCommunicationServerLLGS::Handle_jTraceRead(
1190 StringExtractorGDBRemote &packet) {
1191
1192 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001193 if (!m_debugged_process_up ||
1194 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001195 return SendErrorResponse(68);
1196
1197 enum PacketType { MetaData, BufferData };
1198 PacketType tracetype = MetaData;
1199
1200 if (packet.ConsumeFront("jTraceBufferRead:"))
1201 tracetype = BufferData;
1202 else if (packet.ConsumeFront("jTraceMetaRead:"))
1203 tracetype = MetaData;
1204 else {
1205 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1206 }
1207
1208 lldb::user_id_t uid = LLDB_INVALID_UID;
1209
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001210 uint64_t byte_count = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001211 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001212 uint64_t offset = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001213
1214 auto json_object = StructuredData::ParseJSON(packet.Peek());
1215
1216 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001217 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001218 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1219
1220 auto json_dict = json_object->GetAsDictionary();
1221
Pavel Labath4c950232017-05-26 13:53:39 +00001222 if (!json_dict->GetValueForKeyAsInteger("traceid", uid) ||
1223 !json_dict->GetValueForKeyAsInteger("offset", offset) ||
1224 !json_dict->GetValueForKeyAsInteger("buffersize", byte_count))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001225 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1226
Pavel Labath4c950232017-05-26 13:53:39 +00001227 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001228
1229 // Allocate the response buffer.
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001230 std::unique_ptr<uint8_t[]> buffer (new (std::nothrow) uint8_t[byte_count]);
1231 if (!buffer)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001232 return SendErrorResponse(0x78);
1233
1234 StreamGDBRemote response;
1235 Status error;
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001236 llvm::MutableArrayRef<uint8_t> buf(buffer.get(), byte_count);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001237
1238 if (tracetype == BufferData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001239 error = m_debugged_process_up->GetData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001240 else if (tracetype == MetaData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001241 error = m_debugged_process_up->GetMetaData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001242
1243 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001244 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001245
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001246 for (auto i : buf)
1247 response.PutHex8(i);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001248
1249 StreamGDBRemote escaped_response;
1250 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
1251 return SendPacketNoLock(escaped_response.GetString());
1252}
1253
1254GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo(
1256 StringExtractorGDBRemote &packet) {
1257 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001258 if (!m_debugged_process_up ||
1259 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 return SendErrorResponse(68);
1261
Pavel Labath82abefa2017-07-18 09:24:48 +00001262 lldb::pid_t pid = m_debugged_process_up->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263
1264 if (pid == LLDB_INVALID_PROCESS_ID)
1265 return SendErrorResponse(1);
1266
1267 ProcessInstanceInfo proc_info;
1268 if (!Host::GetProcessInfo(pid, proc_info))
1269 return SendErrorResponse(1);
1270
1271 StreamString response;
1272 CreateProcessInfoResponse_DebugServerStyle(proc_info, response);
1273 return SendPacketNoLock(response.GetString());
1274}
1275
1276GDBRemoteCommunication::PacketResult
1277GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) {
1278 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001279 if (!m_debugged_process_up ||
1280 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 return SendErrorResponse(68);
1282
Adrian Prantl05097242018-04-30 16:49:04 +00001283 // Make sure we set the current thread so g and p packets return the data the
1284 // gdb will expect.
Pavel Labath82abefa2017-07-18 09:24:48 +00001285 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 SetCurrentThreadID(tid);
1287
Pavel Labatha5be48b2017-10-17 15:52:16 +00001288 NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread();
1289 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 return SendErrorResponse(69);
1291
1292 StreamString response;
Pavel Labatha5be48b2017-10-17 15:52:16 +00001293 response.Printf("QC%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294
1295 return SendPacketNoLock(response.GetString());
1296}
1297
1298GDBRemoteCommunication::PacketResult
1299GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) {
1300 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1301
1302 StopSTDIOForwarding();
1303
Pavel Labath82abefa2017-07-18 09:24:48 +00001304 if (!m_debugged_process_up) {
1305 LLDB_LOG(log, "No debugged process found.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 return PacketResult::Success;
1307 }
1308
Pavel Labath82abefa2017-07-18 09:24:48 +00001309 Status error = m_debugged_process_up->Kill();
1310 if (error.Fail())
1311 LLDB_LOG(log, "Failed to kill debugged process {0}: {1}",
1312 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313
1314 // No OK response for kill packet.
1315 // return SendOKResponse ();
1316 return PacketResult::Success;
1317}
1318
1319GDBRemoteCommunication::PacketResult
1320GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR(
1321 StringExtractorGDBRemote &packet) {
1322 packet.SetFilePos(::strlen("QSetDisableASLR:"));
1323 if (packet.GetU32(0))
1324 m_process_launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
1325 else
1326 m_process_launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
1327 return SendOKResponse();
1328}
1329
1330GDBRemoteCommunication::PacketResult
1331GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir(
1332 StringExtractorGDBRemote &packet) {
1333 packet.SetFilePos(::strlen("QSetWorkingDir:"));
1334 std::string path;
1335 packet.GetHexByteString(path);
1336 m_process_launch_info.SetWorkingDirectory(FileSpec{path, true});
1337 return SendOKResponse();
1338}
1339
1340GDBRemoteCommunication::PacketResult
1341GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir(
1342 StringExtractorGDBRemote &packet) {
1343 FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()};
1344 if (working_dir) {
1345 StreamString response;
1346 response.PutCStringAsRawHex8(working_dir.GetCString());
1347 return SendPacketNoLock(response.GetString());
1348 }
1349
1350 return SendErrorResponse(14);
1351}
1352
1353GDBRemoteCommunication::PacketResult
1354GDBRemoteCommunicationServerLLGS::Handle_C(StringExtractorGDBRemote &packet) {
1355 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1356 if (log)
1357 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1358
1359 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001360 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 if (log)
1362 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1363 "shared pointer",
1364 __FUNCTION__);
1365 return SendErrorResponse(0x36);
1366 }
1367
1368 // Pull out the signal number.
1369 packet.SetFilePos(::strlen("C"));
1370 if (packet.GetBytesLeft() < 1) {
1371 // Shouldn't be using a C without a signal.
1372 return SendIllFormedResponse(packet, "C packet specified without signal.");
1373 }
1374 const uint32_t signo =
1375 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1376 if (signo == std::numeric_limits<uint32_t>::max())
1377 return SendIllFormedResponse(packet, "failed to parse signal number");
1378
1379 // Handle optional continue address.
1380 if (packet.GetBytesLeft() > 0) {
1381 // FIXME add continue at address support for $C{signo}[;{continue-address}].
1382 if (*packet.Peek() == ';')
1383 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1384 else
1385 return SendIllFormedResponse(
1386 packet, "unexpected content after $C{signal-number}");
1387 }
1388
1389 ResumeActionList resume_actions(StateType::eStateRunning, 0);
Zachary Turner97206d52017-05-12 04:51:55 +00001390 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391
1392 // We have two branches: what to do if a continue thread is specified (in
Adrian Prantl05097242018-04-30 16:49:04 +00001393 // which case we target sending the signal to that thread), or when we don't
1394 // have a continue thread set (in which case we send a signal to the
1395 // process).
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396
1397 // TODO discuss with Greg Clayton, make sure this makes sense.
1398
1399 lldb::tid_t signal_tid = GetContinueThreadID();
1400 if (signal_tid != LLDB_INVALID_THREAD_ID) {
1401 // The resume action for the continue thread (or all threads if a continue
1402 // thread is not set).
1403 ResumeAction action = {GetContinueThreadID(), StateType::eStateRunning,
1404 static_cast<int>(signo)};
1405
1406 // Add the action for the continue thread (or all threads when the continue
1407 // thread isn't present).
1408 resume_actions.Append(action);
1409 } else {
1410 // Send the signal to the process since we weren't targeting a specific
1411 // continue thread with the signal.
Pavel Labath82abefa2017-07-18 09:24:48 +00001412 error = m_debugged_process_up->Signal(signo);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001414 LLDB_LOG(log, "failed to send signal for process {0}: {1}",
1415 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416
1417 return SendErrorResponse(0x52);
1418 }
1419 }
1420
1421 // Resume the threads.
Pavel Labath82abefa2017-07-18 09:24:48 +00001422 error = m_debugged_process_up->Resume(resume_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001424 LLDB_LOG(log, "failed to resume threads for process {0}: {1}",
1425 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426
1427 return SendErrorResponse(0x38);
1428 }
1429
1430 // Don't send an "OK" packet; response is the stopped/exited message.
1431 return PacketResult::Success;
1432}
1433
1434GDBRemoteCommunication::PacketResult
1435GDBRemoteCommunicationServerLLGS::Handle_c(StringExtractorGDBRemote &packet) {
1436 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1437 if (log)
1438 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1439
1440 packet.SetFilePos(packet.GetFilePos() + ::strlen("c"));
1441
1442 // For now just support all continue.
1443 const bool has_continue_address = (packet.GetBytesLeft() > 0);
1444 if (has_continue_address) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001445 LLDB_LOG(log, "not implemented for c[address] variant [{0} remains]",
1446 packet.Peek());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1448 }
1449
1450 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001451 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 if (log)
1453 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1454 "shared pointer",
1455 __FUNCTION__);
1456 return SendErrorResponse(0x36);
1457 }
1458
1459 // Build the ResumeActionList
1460 ResumeActionList actions(StateType::eStateRunning, 0);
1461
Pavel Labath82abefa2017-07-18 09:24:48 +00001462 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001464 LLDB_LOG(log, "c failed for process {0}: {1}",
1465 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1467 }
1468
Pavel Labath82abefa2017-07-18 09:24:48 +00001469 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 // No response required from continue.
1471 return PacketResult::Success;
1472}
1473
1474GDBRemoteCommunication::PacketResult
1475GDBRemoteCommunicationServerLLGS::Handle_vCont_actions(
1476 StringExtractorGDBRemote &packet) {
1477 StreamString response;
1478 response.Printf("vCont;c;C;s;S");
1479
1480 return SendPacketNoLock(response.GetString());
1481}
1482
1483GDBRemoteCommunication::PacketResult
1484GDBRemoteCommunicationServerLLGS::Handle_vCont(
1485 StringExtractorGDBRemote &packet) {
1486 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1487 if (log)
1488 log->Printf("GDBRemoteCommunicationServerLLGS::%s handling vCont packet",
1489 __FUNCTION__);
1490
1491 packet.SetFilePos(::strlen("vCont"));
1492
1493 if (packet.GetBytesLeft() == 0) {
1494 if (log)
1495 log->Printf("GDBRemoteCommunicationServerLLGS::%s missing action from "
1496 "vCont package",
1497 __FUNCTION__);
1498 return SendIllFormedResponse(packet, "Missing action from vCont package");
1499 }
1500
1501 // Check if this is all continue (no options or ";c").
1502 if (::strcmp(packet.Peek(), ";c") == 0) {
1503 // Move past the ';', then do a simple 'c'.
1504 packet.SetFilePos(packet.GetFilePos() + 1);
1505 return Handle_c(packet);
1506 } else if (::strcmp(packet.Peek(), ";s") == 0) {
1507 // Move past the ';', then do a simple 's'.
1508 packet.SetFilePos(packet.GetFilePos() + 1);
1509 return Handle_s(packet);
1510 }
1511
1512 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001513 if (!m_debugged_process_up) {
1514 LLDB_LOG(log, "no debugged process");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 return SendErrorResponse(0x36);
1516 }
1517
1518 ResumeActionList thread_actions;
1519
1520 while (packet.GetBytesLeft() && *packet.Peek() == ';') {
1521 // Skip the semi-colon.
1522 packet.GetChar();
1523
1524 // Build up the thread action.
1525 ResumeAction thread_action;
1526 thread_action.tid = LLDB_INVALID_THREAD_ID;
1527 thread_action.state = eStateInvalid;
1528 thread_action.signal = 0;
1529
1530 const char action = packet.GetChar();
1531 switch (action) {
1532 case 'C':
1533 thread_action.signal = packet.GetHexMaxU32(false, 0);
1534 if (thread_action.signal == 0)
1535 return SendIllFormedResponse(
1536 packet, "Could not parse signal in vCont packet C action");
1537 LLVM_FALLTHROUGH;
1538
1539 case 'c':
1540 // Continue
1541 thread_action.state = eStateRunning;
1542 break;
1543
1544 case 'S':
1545 thread_action.signal = packet.GetHexMaxU32(false, 0);
1546 if (thread_action.signal == 0)
1547 return SendIllFormedResponse(
1548 packet, "Could not parse signal in vCont packet S action");
1549 LLVM_FALLTHROUGH;
1550
1551 case 's':
1552 // Step
1553 thread_action.state = eStateStepping;
1554 break;
Pavel Labathabadc222015-11-27 13:33:29 +00001555
Tamas Berghammere13c2732015-02-11 10:29:30 +00001556 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 return SendIllFormedResponse(packet, "Unsupported vCont action");
1558 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00001559 }
1560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 // Parse out optional :{thread-id} value.
1562 if (packet.GetBytesLeft() && (*packet.Peek() == ':')) {
1563 // Consume the separator.
1564 packet.GetChar();
Tamas Berghammere13c2732015-02-11 10:29:30 +00001565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 thread_action.tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1567 if (thread_action.tid == LLDB_INVALID_THREAD_ID)
1568 return SendIllFormedResponse(
1569 packet, "Could not parse thread number in vCont packet");
Pavel Labath77dc9562015-07-13 10:44:55 +00001570 }
1571
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572 thread_actions.Append(thread_action);
1573 }
Pavel Labath77dc9562015-07-13 10:44:55 +00001574
Pavel Labath82abefa2017-07-18 09:24:48 +00001575 Status error = m_debugged_process_up->Resume(thread_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001577 LLDB_LOG(log, "vCont failed for process {0}: {1}",
1578 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1580 }
1581
Pavel Labath82abefa2017-07-18 09:24:48 +00001582 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 // No response required from vCont.
1584 return PacketResult::Success;
Pavel Labath77dc9562015-07-13 10:44:55 +00001585}
1586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) {
1588 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001589 LLDB_LOG(log, "setting current thread id to {0}", tid);
Pavel Labath77dc9562015-07-13 10:44:55 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 m_current_tid = tid;
Pavel Labath82abefa2017-07-18 09:24:48 +00001592 if (m_debugged_process_up)
1593 m_debugged_process_up->SetCurrentThreadID(m_current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594}
1595
1596void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) {
1597 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001598 LLDB_LOG(log, "setting continue thread id to {0}", tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599
1600 m_continue_tid = tid;
Pavel Labath77dc9562015-07-13 10:44:55 +00001601}
1602
Tamas Berghammere13c2732015-02-11 10:29:30 +00001603GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604GDBRemoteCommunicationServerLLGS::Handle_stop_reason(
1605 StringExtractorGDBRemote &packet) {
1606 // Handle the $? gdbremote command.
Tamas Berghammere13c2732015-02-11 10:29:30 +00001607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608 // If no process, indicate error
Pavel Labath82abefa2017-07-18 09:24:48 +00001609 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 return SendErrorResponse(02);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001611
Pavel Labath82abefa2017-07-18 09:24:48 +00001612 return SendStopReasonForState(m_debugged_process_up->GetState());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001613}
1614
1615GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616GDBRemoteCommunicationServerLLGS::SendStopReasonForState(
1617 lldb::StateType process_state) {
1618 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00001619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 switch (process_state) {
1621 case eStateAttaching:
1622 case eStateLaunching:
1623 case eStateRunning:
1624 case eStateStepping:
1625 case eStateDetached:
1626 // NOTE: gdb protocol doc looks like it should return $OK
1627 // when everything is running (i.e. no stopped result).
1628 return PacketResult::Success; // Ignore
Tamas Berghammere13c2732015-02-11 10:29:30 +00001629
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 case eStateSuspended:
1631 case eStateStopped:
1632 case eStateCrashed: {
Pavel Labath82abefa2017-07-18 09:24:48 +00001633 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Adrian Prantl05097242018-04-30 16:49:04 +00001634 // Make sure we set the current thread so g and p packets return the data
1635 // the gdb will expect.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 SetCurrentThreadID(tid);
1637 return SendStopReplyPacketForThread(tid);
1638 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001639
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 case eStateInvalid:
1641 case eStateUnloaded:
1642 case eStateExited:
Pavel Labath82abefa2017-07-18 09:24:48 +00001643 return SendWResponse(m_debugged_process_up.get());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001644
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 default:
Pavel Labath82abefa2017-07-18 09:24:48 +00001646 LLDB_LOG(log, "pid {0}, current state reporting not handled: {1}",
1647 m_debugged_process_up->GetID(), process_state);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 break;
1649 }
Pavel Labath424b2012015-07-28 09:06:56 +00001650
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 return SendErrorResponse(0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001652}
1653
1654GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo(
1656 StringExtractorGDBRemote &packet) {
1657 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001658 if (!m_debugged_process_up ||
1659 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001661
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 // Ensure we have a thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001663 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0);
1664 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 return SendErrorResponse(69);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001666
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 // Get the register context for the first thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00001668 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669
1670 // Parse out the register number from the request.
1671 packet.SetFilePos(strlen("qRegisterInfo"));
1672 const uint32_t reg_index =
1673 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1674 if (reg_index == std::numeric_limits<uint32_t>::max())
1675 return SendErrorResponse(69);
1676
1677 // Return the end of registers response if we've iterated one past the end of
1678 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001679 if (reg_index >= reg_context.GetUserRegisterCount())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 return SendErrorResponse(69);
1681
Pavel Labathd37349f2017-11-10 11:05:49 +00001682 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 if (!reg_info)
1684 return SendErrorResponse(69);
1685
1686 // Build the reginfos response.
1687 StreamGDBRemote response;
1688
1689 response.PutCString("name:");
1690 response.PutCString(reg_info->name);
1691 response.PutChar(';');
1692
1693 if (reg_info->alt_name && reg_info->alt_name[0]) {
1694 response.PutCString("alt-name:");
1695 response.PutCString(reg_info->alt_name);
1696 response.PutChar(';');
1697 }
1698
1699 response.Printf("bitsize:%" PRIu32 ";offset:%" PRIu32 ";",
1700 reg_info->byte_size * 8, reg_info->byte_offset);
1701
1702 switch (reg_info->encoding) {
1703 case eEncodingUint:
1704 response.PutCString("encoding:uint;");
1705 break;
1706 case eEncodingSint:
1707 response.PutCString("encoding:sint;");
1708 break;
1709 case eEncodingIEEE754:
1710 response.PutCString("encoding:ieee754;");
1711 break;
1712 case eEncodingVector:
1713 response.PutCString("encoding:vector;");
1714 break;
1715 default:
1716 break;
1717 }
1718
1719 switch (reg_info->format) {
1720 case eFormatBinary:
1721 response.PutCString("format:binary;");
1722 break;
1723 case eFormatDecimal:
1724 response.PutCString("format:decimal;");
1725 break;
1726 case eFormatHex:
1727 response.PutCString("format:hex;");
1728 break;
1729 case eFormatFloat:
1730 response.PutCString("format:float;");
1731 break;
1732 case eFormatVectorOfSInt8:
1733 response.PutCString("format:vector-sint8;");
1734 break;
1735 case eFormatVectorOfUInt8:
1736 response.PutCString("format:vector-uint8;");
1737 break;
1738 case eFormatVectorOfSInt16:
1739 response.PutCString("format:vector-sint16;");
1740 break;
1741 case eFormatVectorOfUInt16:
1742 response.PutCString("format:vector-uint16;");
1743 break;
1744 case eFormatVectorOfSInt32:
1745 response.PutCString("format:vector-sint32;");
1746 break;
1747 case eFormatVectorOfUInt32:
1748 response.PutCString("format:vector-uint32;");
1749 break;
1750 case eFormatVectorOfFloat32:
1751 response.PutCString("format:vector-float32;");
1752 break;
Valentina Giusticda0ae42016-09-08 14:16:45 +00001753 case eFormatVectorOfUInt64:
1754 response.PutCString("format:vector-uint64;");
1755 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756 case eFormatVectorOfUInt128:
1757 response.PutCString("format:vector-uint128;");
1758 break;
1759 default:
1760 break;
1761 };
1762
1763 const char *const register_set_name =
Pavel Labathd37349f2017-11-10 11:05:49 +00001764 reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765 if (register_set_name) {
1766 response.PutCString("set:");
1767 response.PutCString(register_set_name);
1768 response.PutChar(';');
1769 }
1770
1771 if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] !=
1772 LLDB_INVALID_REGNUM)
1773 response.Printf("ehframe:%" PRIu32 ";",
1774 reg_info->kinds[RegisterKind::eRegisterKindEHFrame]);
1775
1776 if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
1777 response.Printf("dwarf:%" PRIu32 ";",
1778 reg_info->kinds[RegisterKind::eRegisterKindDWARF]);
1779
1780 switch (reg_info->kinds[RegisterKind::eRegisterKindGeneric]) {
1781 case LLDB_REGNUM_GENERIC_PC:
1782 response.PutCString("generic:pc;");
1783 break;
1784 case LLDB_REGNUM_GENERIC_SP:
1785 response.PutCString("generic:sp;");
1786 break;
1787 case LLDB_REGNUM_GENERIC_FP:
1788 response.PutCString("generic:fp;");
1789 break;
1790 case LLDB_REGNUM_GENERIC_RA:
1791 response.PutCString("generic:ra;");
1792 break;
1793 case LLDB_REGNUM_GENERIC_FLAGS:
1794 response.PutCString("generic:flags;");
1795 break;
1796 case LLDB_REGNUM_GENERIC_ARG1:
1797 response.PutCString("generic:arg1;");
1798 break;
1799 case LLDB_REGNUM_GENERIC_ARG2:
1800 response.PutCString("generic:arg2;");
1801 break;
1802 case LLDB_REGNUM_GENERIC_ARG3:
1803 response.PutCString("generic:arg3;");
1804 break;
1805 case LLDB_REGNUM_GENERIC_ARG4:
1806 response.PutCString("generic:arg4;");
1807 break;
1808 case LLDB_REGNUM_GENERIC_ARG5:
1809 response.PutCString("generic:arg5;");
1810 break;
1811 case LLDB_REGNUM_GENERIC_ARG6:
1812 response.PutCString("generic:arg6;");
1813 break;
1814 case LLDB_REGNUM_GENERIC_ARG7:
1815 response.PutCString("generic:arg7;");
1816 break;
1817 case LLDB_REGNUM_GENERIC_ARG8:
1818 response.PutCString("generic:arg8;");
1819 break;
1820 default:
1821 break;
1822 }
1823
1824 if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) {
1825 response.PutCString("container-regs:");
1826 int i = 0;
1827 for (const uint32_t *reg_num = reg_info->value_regs;
1828 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1829 if (i > 0)
1830 response.PutChar(',');
1831 response.Printf("%" PRIx32, *reg_num);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 response.PutChar(';');
1834 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001835
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836 if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) {
1837 response.PutCString("invalidate-regs:");
1838 int i = 0;
1839 for (const uint32_t *reg_num = reg_info->invalidate_regs;
1840 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1841 if (i > 0)
1842 response.PutChar(',');
1843 response.Printf("%" PRIx32, *reg_num);
1844 }
1845 response.PutChar(';');
1846 }
1847
1848 if (reg_info->dynamic_size_dwarf_expr_bytes) {
1849 const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len;
1850 response.PutCString("dynamic_size_dwarf_expr_bytes:");
1851 for (uint32_t i = 0; i < dwarf_opcode_len; ++i)
1852 response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]);
1853 response.PutChar(';');
1854 }
1855 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001856}
1857
1858GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo(
1860 StringExtractorGDBRemote &packet) {
1861 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1862
1863 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001864 if (!m_debugged_process_up ||
1865 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
1866 LLDB_LOG(log, "no process ({0}), returning OK",
1867 m_debugged_process_up ? "invalid process id"
1868 : "null m_debugged_process_up");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 return SendOKResponse();
1870 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001871
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872 StreamGDBRemote response;
1873 response.PutChar('m');
1874
Pavel Labath82abefa2017-07-18 09:24:48 +00001875 LLDB_LOG(log, "starting thread iteration");
Pavel Labatha5be48b2017-10-17 15:52:16 +00001876 NativeThreadProtocol *thread;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877 uint32_t thread_index;
1878 for (thread_index = 0,
Pavel Labatha5be48b2017-10-17 15:52:16 +00001879 thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
1880 thread; ++thread_index,
1881 thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
1882 LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index,
1883 thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884 if (thread_index > 0)
1885 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +00001886 response.Printf("%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 }
1888
Pavel Labath82abefa2017-07-18 09:24:48 +00001889 LLDB_LOG(log, "finished thread iteration");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 return SendPacketNoLock(response.GetString());
1891}
1892
1893GDBRemoteCommunication::PacketResult
1894GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo(
1895 StringExtractorGDBRemote &packet) {
1896 // FIXME for now we return the full thread list in the initial packet and
1897 // always do nothing here.
1898 return SendPacketNoLock("l");
1899}
1900
1901GDBRemoteCommunication::PacketResult
1902GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) {
1903 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1904
1905 // Parse out the register number from the request.
1906 packet.SetFilePos(strlen("p"));
1907 const uint32_t reg_index =
1908 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1909 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1910 if (log)
1911 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1912 "parse register number from request \"%s\"",
1913 __FUNCTION__, packet.GetStringRef().c_str());
1914 return SendErrorResponse(0x15);
1915 }
1916
1917 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001918 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
1919 if (!thread) {
1920 LLDB_LOG(log, "failed, no thread available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001921 return SendErrorResponse(0x15);
1922 }
1923
1924 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00001925 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926
1927 // Return the end of registers response if we've iterated one past the end of
1928 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001929 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001930 if (log)
1931 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1932 "register %" PRIu32 " beyond register count %" PRIu32,
1933 __FUNCTION__, reg_index,
Pavel Labathd37349f2017-11-10 11:05:49 +00001934 reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001935 return SendErrorResponse(0x15);
1936 }
1937
Pavel Labathd37349f2017-11-10 11:05:49 +00001938 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001939 if (!reg_info) {
1940 if (log)
1941 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1942 "register %" PRIu32 " returned NULL",
1943 __FUNCTION__, reg_index);
1944 return SendErrorResponse(0x15);
1945 }
1946
1947 // Build the reginfos response.
1948 StreamGDBRemote response;
1949
1950 // Retrieve the value
1951 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +00001952 Status error = reg_context.ReadRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 if (error.Fail()) {
1954 if (log)
1955 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, read of "
1956 "requested register %" PRIu32 " (%s) failed: %s",
1957 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
1958 return SendErrorResponse(0x15);
1959 }
1960
1961 const uint8_t *const data =
1962 reinterpret_cast<const uint8_t *>(reg_value.GetBytes());
1963 if (!data) {
1964 if (log)
1965 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get data "
1966 "bytes from requested register %" PRIu32,
1967 __FUNCTION__, reg_index);
1968 return SendErrorResponse(0x15);
1969 }
1970
1971 // FIXME flip as needed to get data in big/little endian format for this host.
1972 for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i)
1973 response.PutHex8(data[i]);
1974
1975 return SendPacketNoLock(response.GetString());
1976}
1977
1978GDBRemoteCommunication::PacketResult
1979GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) {
1980 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1981
1982 // Ensure there is more content.
1983 if (packet.GetBytesLeft() < 1)
1984 return SendIllFormedResponse(packet, "Empty P packet");
1985
1986 // Parse out the register number from the request.
1987 packet.SetFilePos(strlen("P"));
1988 const uint32_t reg_index =
1989 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1990 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1991 if (log)
1992 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1993 "parse register number from request \"%s\"",
1994 __FUNCTION__, packet.GetStringRef().c_str());
1995 return SendErrorResponse(0x29);
1996 }
1997
1998 // Note debugserver would send an E30 here.
1999 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '='))
2000 return SendIllFormedResponse(
2001 packet, "P packet missing '=' char after register number");
2002
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 // Parse out the value.
2004 uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register
2005 size_t reg_size = packet.GetHexBytesAvail(reg_bytes);
2006
2007 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002008 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2009 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002010 if (log)
2011 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread "
2012 "available (thread index 0)",
2013 __FUNCTION__);
2014 return SendErrorResponse(0x28);
2015 }
2016
2017 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00002018 NativeRegisterContext &reg_context = thread->GetRegisterContext();
2019 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 if (!reg_info) {
2021 if (log)
2022 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2023 "register %" PRIu32 " returned NULL",
2024 __FUNCTION__, reg_index);
2025 return SendErrorResponse(0x48);
2026 }
2027
2028 // Return the end of registers response if we've iterated one past the end of
2029 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00002030 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 if (log)
2032 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2033 "register %" PRIu32 " beyond register count %" PRIu32,
Pavel Labathd37349f2017-11-10 11:05:49 +00002034 __FUNCTION__, reg_index, reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002035 return SendErrorResponse(0x47);
2036 }
2037
Adrian Prantl05097242018-04-30 16:49:04 +00002038 // The dwarf expression are evaluate on host site which may cause register
2039 // size to change Hence the reg_size may not be same as reg_info->bytes_size
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040 if ((reg_size != reg_info->byte_size) &&
2041 !(reg_info->dynamic_size_dwarf_expr_bytes)) {
2042 return SendIllFormedResponse(packet, "P packet register size is incorrect");
2043 }
2044
2045 // Build the reginfos response.
2046 StreamGDBRemote response;
2047
Pavel Labath578a4252017-11-09 10:43:16 +00002048 RegisterValue reg_value(
2049 reg_bytes, reg_size,
2050 m_debugged_process_up->GetArchitecture().GetByteOrder());
Pavel Labathd37349f2017-11-10 11:05:49 +00002051 Status error = reg_context.WriteRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002052 if (error.Fail()) {
2053 if (log)
2054 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, write of "
2055 "requested register %" PRIu32 " (%s) failed: %s",
2056 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
2057 return SendErrorResponse(0x32);
2058 }
2059
2060 return SendOKResponse();
2061}
2062
2063GDBRemoteCommunication::PacketResult
2064GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) {
2065 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2066
2067 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002068 if (!m_debugged_process_up ||
2069 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002070 if (log)
2071 log->Printf(
2072 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2073 __FUNCTION__);
2074 return SendErrorResponse(0x15);
2075 }
2076
2077 // Parse out which variant of $H is requested.
2078 packet.SetFilePos(strlen("H"));
2079 if (packet.GetBytesLeft() < 1) {
2080 if (log)
2081 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, H command "
2082 "missing {g,c} variant",
2083 __FUNCTION__);
2084 return SendIllFormedResponse(packet, "H command missing {g,c} variant");
2085 }
2086
2087 const char h_variant = packet.GetChar();
2088 switch (h_variant) {
2089 case 'g':
2090 break;
2091
2092 case 'c':
2093 break;
2094
2095 default:
2096 if (log)
2097 log->Printf(
2098 "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c",
2099 __FUNCTION__, h_variant);
2100 return SendIllFormedResponse(packet,
2101 "H variant unsupported, should be c or g");
2102 }
2103
2104 // Parse out the thread number.
2105 // FIXME return a parse success/fail value. All values are valid here.
2106 const lldb::tid_t tid =
2107 packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max());
2108
2109 // Ensure we have the given thread when not specifying -1 (all threads) or 0
2110 // (any thread).
2111 if (tid != LLDB_INVALID_THREAD_ID && tid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +00002112 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2113 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114 if (log)
2115 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
2116 " not found",
2117 __FUNCTION__, tid);
2118 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002121
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 // Now switch the given thread type.
2123 switch (h_variant) {
2124 case 'g':
2125 SetCurrentThreadID(tid);
2126 break;
2127
2128 case 'c':
2129 SetContinueThreadID(tid);
2130 break;
2131
2132 default:
2133 assert(false && "unsupported $H variant - shouldn't get here");
2134 return SendIllFormedResponse(packet,
2135 "H variant unsupported, should be c or g");
2136 }
2137
2138 return SendOKResponse();
2139}
2140
2141GDBRemoteCommunication::PacketResult
2142GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) {
2143 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2144
2145 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002146 if (!m_debugged_process_up ||
2147 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 if (log)
2149 log->Printf(
2150 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2151 __FUNCTION__);
2152 return SendErrorResponse(0x15);
2153 }
2154
2155 packet.SetFilePos(::strlen("I"));
2156 uint8_t tmp[4096];
2157 for (;;) {
2158 size_t read = packet.GetHexBytesAvail(tmp);
2159 if (read == 0) {
2160 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002162 // write directly to stdin *this might block if stdin buffer is full*
2163 // TODO: enqueue this block in circular buffer and send window size to
2164 // remote host
2165 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00002166 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 m_stdio_communication.Write(tmp, read, status, &error);
2168 if (error.Fail()) {
2169 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002172
Kate Stoneb9c1b512016-09-06 20:57:50 +00002173 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002174}
2175
2176GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177GDBRemoteCommunicationServerLLGS::Handle_interrupt(
2178 StringExtractorGDBRemote &packet) {
2179 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2180
2181 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002182 if (!m_debugged_process_up ||
2183 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
2184 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002185 return SendErrorResponse(0x15);
2186 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002187
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 // Interrupt the process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002189 Status error = m_debugged_process_up->Interrupt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002191 LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(),
2192 error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002193 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
2194 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002195
Pavel Labath82abefa2017-07-18 09:24:48 +00002196 LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002197
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198 // No response required from stop all.
2199 return PacketResult::Success;
2200}
Tamas Berghammere13c2732015-02-11 10:29:30 +00002201
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202GDBRemoteCommunication::PacketResult
2203GDBRemoteCommunicationServerLLGS::Handle_memory_read(
2204 StringExtractorGDBRemote &packet) {
2205 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002206
Pavel Labath82abefa2017-07-18 09:24:48 +00002207 if (!m_debugged_process_up ||
2208 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002209 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002210 log->Printf(
2211 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2212 __FUNCTION__);
2213 return SendErrorResponse(0x15);
2214 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002215
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216 // Parse out the memory address.
2217 packet.SetFilePos(strlen("m"));
2218 if (packet.GetBytesLeft() < 1)
2219 return SendIllFormedResponse(packet, "Too short m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002220
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221 // Read the address. Punting on validation.
2222 // FIXME replace with Hex U64 read with no default value that fails on failed
2223 // read.
2224 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002225
Kate Stoneb9c1b512016-09-06 20:57:50 +00002226 // Validate comma.
2227 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2228 return SendIllFormedResponse(packet, "Comma sep missing in m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002229
Kate Stoneb9c1b512016-09-06 20:57:50 +00002230 // Get # bytes to read.
2231 if (packet.GetBytesLeft() < 1)
2232 return SendIllFormedResponse(packet, "Length missing in m packet");
2233
2234 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2235 if (byte_count == 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002236 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to read: "
2238 "zero-length packet",
2239 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002240 return SendOKResponse();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002242
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 // Allocate the response buffer.
2244 std::string buf(byte_count, '\0');
2245 if (buf.empty())
2246 return SendErrorResponse(0x78);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002247
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248 // Retrieve the process memory.
2249 size_t bytes_read = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002250 Status error = m_debugged_process_up->ReadMemoryWithoutTrap(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 read_addr, &buf[0], byte_count, bytes_read);
2252 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002253 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002254 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2255 " mem 0x%" PRIx64 ": failed to read. Error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002256 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 error.AsCString());
2258 return SendErrorResponse(0x08);
2259 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002260
Kate Stoneb9c1b512016-09-06 20:57:50 +00002261 if (bytes_read == 0) {
2262 if (log)
2263 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2264 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes",
Pavel Labath82abefa2017-07-18 09:24:48 +00002265 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266 byte_count);
2267 return SendErrorResponse(0x08);
2268 }
2269
2270 StreamGDBRemote response;
2271 packet.SetFilePos(0);
2272 char kind = packet.GetChar('?');
2273 if (kind == 'x')
2274 response.PutEscapedBytes(buf.data(), byte_count);
2275 else {
2276 assert(kind == 'm');
2277 for (size_t i = 0; i < bytes_read; ++i)
2278 response.PutHex8(buf[i]);
2279 }
2280
2281 return SendPacketNoLock(response.GetString());
2282}
2283
2284GDBRemoteCommunication::PacketResult
2285GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) {
2286 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2287
Pavel Labath82abefa2017-07-18 09:24:48 +00002288 if (!m_debugged_process_up ||
2289 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 if (log)
2291 log->Printf(
2292 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2293 __FUNCTION__);
2294 return SendErrorResponse(0x15);
2295 }
2296
2297 // Parse out the memory address.
2298 packet.SetFilePos(strlen("M"));
2299 if (packet.GetBytesLeft() < 1)
2300 return SendIllFormedResponse(packet, "Too short M packet");
2301
2302 // Read the address. Punting on validation.
2303 // FIXME replace with Hex U64 read with no default value that fails on failed
2304 // read.
2305 const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0);
2306
2307 // Validate comma.
2308 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2309 return SendIllFormedResponse(packet, "Comma sep missing in M packet");
2310
2311 // Get # bytes to read.
2312 if (packet.GetBytesLeft() < 1)
2313 return SendIllFormedResponse(packet, "Length missing in M packet");
2314
2315 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2316 if (byte_count == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002317 LLDB_LOG(log, "nothing to write: zero-length packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002318 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319 }
2320
2321 // Validate colon.
2322 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':'))
2323 return SendIllFormedResponse(
2324 packet, "Comma sep missing in M packet after byte length");
2325
2326 // Allocate the conversion buffer.
2327 std::vector<uint8_t> buf(byte_count, 0);
2328 if (buf.empty())
2329 return SendErrorResponse(0x78);
2330
2331 // Convert the hex memory write contents to bytes.
2332 StreamGDBRemote response;
2333 const uint64_t convert_count = packet.GetHexBytes(buf, 0);
2334 if (convert_count != byte_count) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002335 LLDB_LOG(log,
2336 "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} "
2337 "to convert.",
2338 m_debugged_process_up->GetID(), write_addr, byte_count,
2339 convert_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002340 return SendIllFormedResponse(packet, "M content byte length specified did "
2341 "not match hex-encoded content "
2342 "length");
2343 }
2344
2345 // Write the process memory.
2346 size_t bytes_written = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002347 Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0],
Zachary Turner97206d52017-05-12 04:51:55 +00002348 byte_count, bytes_written);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002350 LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}",
2351 m_debugged_process_up->GetID(), write_addr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002352 return SendErrorResponse(0x09);
2353 }
2354
2355 if (bytes_written == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002356 LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes",
2357 m_debugged_process_up->GetID(), write_addr, byte_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 return SendErrorResponse(0x09);
2359 }
2360
2361 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002362}
2363
2364GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002365GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported(
2366 StringExtractorGDBRemote &packet) {
2367 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002368
Kate Stoneb9c1b512016-09-06 20:57:50 +00002369 // Currently only the NativeProcessProtocol knows if it can handle a
Adrian Prantl05097242018-04-30 16:49:04 +00002370 // qMemoryRegionInfoSupported request, but we're not guaranteed to be
2371 // attached to a process. For now we'll assume the client only asks this
2372 // when a process is being debugged.
Tamas Berghammere13c2732015-02-11 10:29:30 +00002373
Kate Stoneb9c1b512016-09-06 20:57:50 +00002374 // Ensure we have a process running; otherwise, we can't figure this out
2375 // since we won't have a NativeProcessProtocol.
Pavel Labath82abefa2017-07-18 09:24:48 +00002376 if (!m_debugged_process_up ||
2377 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002378 if (log)
2379 log->Printf(
2380 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2381 __FUNCTION__);
2382 return SendErrorResponse(0x15);
2383 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002384
Kate Stoneb9c1b512016-09-06 20:57:50 +00002385 // Test if we can get any region back when asking for the region around NULL.
2386 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002387 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002388 m_debugged_process_up->GetMemoryRegionInfo(0, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002389 if (error.Fail()) {
2390 // We don't support memory region info collection for this
2391 // NativeProcessProtocol.
2392 return SendUnimplementedResponse("");
2393 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002394
Kate Stoneb9c1b512016-09-06 20:57:50 +00002395 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002396}
2397
2398GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
2400 StringExtractorGDBRemote &packet) {
2401 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002402
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002404 if (!m_debugged_process_up ||
2405 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002406 if (log)
2407 log->Printf(
2408 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2409 __FUNCTION__);
2410 return SendErrorResponse(0x15);
2411 }
2412
2413 // Parse out the memory address.
2414 packet.SetFilePos(strlen("qMemoryRegionInfo:"));
2415 if (packet.GetBytesLeft() < 1)
2416 return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet");
2417
2418 // Read the address. Punting on validation.
2419 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
2420
2421 StreamGDBRemote response;
2422
2423 // Get the memory region info for the target address.
2424 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002425 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002426 m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002427 if (error.Fail()) {
2428 // Return the error message.
2429
2430 response.PutCString("error:");
2431 response.PutCStringAsRawHex8(error.AsCString());
2432 response.PutChar(';');
2433 } else {
2434 // Range start and size.
2435 response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";",
2436 region_info.GetRange().GetRangeBase(),
2437 region_info.GetRange().GetByteSize());
2438
2439 // Permissions.
2440 if (region_info.GetReadable() || region_info.GetWritable() ||
2441 region_info.GetExecutable()) {
2442 // Write permissions info.
2443 response.PutCString("permissions:");
2444
2445 if (region_info.GetReadable())
2446 response.PutChar('r');
2447 if (region_info.GetWritable())
2448 response.PutChar('w');
2449 if (region_info.GetExecutable())
2450 response.PutChar('x');
2451
2452 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002453 }
2454
Kate Stoneb9c1b512016-09-06 20:57:50 +00002455 // Name
2456 ConstString name = region_info.GetName();
2457 if (name) {
2458 response.PutCString("name:");
2459 response.PutCStringAsRawHex8(name.AsCString());
2460 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002463
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002465}
2466
2467GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) {
2469 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002470 if (!m_debugged_process_up ||
2471 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002472 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002473 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002474 return SendErrorResponse(0x15);
2475 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002476
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477 // Parse out software or hardware breakpoint or watchpoint requested.
2478 packet.SetFilePos(strlen("Z"));
2479 if (packet.GetBytesLeft() < 1)
2480 return SendIllFormedResponse(
2481 packet, "Too short Z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002482
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483 bool want_breakpoint = true;
2484 bool want_hardware = false;
2485 uint32_t watch_flags = 0;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002486
Kate Stoneb9c1b512016-09-06 20:57:50 +00002487 const GDBStoppointType stoppoint_type =
2488 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2489 switch (stoppoint_type) {
2490 case eBreakpointSoftware:
2491 want_hardware = false;
2492 want_breakpoint = true;
2493 break;
2494 case eBreakpointHardware:
2495 want_hardware = true;
2496 want_breakpoint = true;
2497 break;
2498 case eWatchpointWrite:
2499 watch_flags = 1;
2500 want_hardware = true;
2501 want_breakpoint = false;
2502 break;
2503 case eWatchpointRead:
2504 watch_flags = 2;
2505 want_hardware = true;
2506 want_breakpoint = false;
2507 break;
2508 case eWatchpointReadWrite:
2509 watch_flags = 3;
2510 want_hardware = true;
2511 want_breakpoint = false;
2512 break;
2513 case eStoppointInvalid:
2514 return SendIllFormedResponse(
2515 packet, "Z packet had invalid software/hardware specifier");
2516 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002517
Kate Stoneb9c1b512016-09-06 20:57:50 +00002518 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2519 return SendIllFormedResponse(
2520 packet, "Malformed Z packet, expecting comma after stoppoint type");
2521
2522 // Parse out the stoppoint address.
2523 if (packet.GetBytesLeft() < 1)
2524 return SendIllFormedResponse(packet, "Too short Z packet, missing address");
2525 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
2526
2527 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2528 return SendIllFormedResponse(
2529 packet, "Malformed Z packet, expecting comma after address");
2530
2531 // Parse out the stoppoint size (i.e. size hint for opcode size).
2532 const uint32_t size =
2533 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
2534 if (size == std::numeric_limits<uint32_t>::max())
2535 return SendIllFormedResponse(
2536 packet, "Malformed Z packet, failed to parse size argument");
2537
2538 if (want_breakpoint) {
2539 // Try to set the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002540 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002541 m_debugged_process_up->SetBreakpoint(addr, size, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002542 if (error.Success())
2543 return SendOKResponse();
2544 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002545 LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}",
2546 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002547 return SendErrorResponse(0x09);
2548 } else {
2549 // Try to set the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002550 const Status error = m_debugged_process_up->SetWatchpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002551 addr, size, watch_flags, want_hardware);
2552 if (error.Success())
2553 return SendOKResponse();
2554 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002555 LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}",
2556 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002557 return SendErrorResponse(0x09);
2558 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002559}
2560
2561GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002562GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) {
2563 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002564 if (!m_debugged_process_up ||
2565 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002566 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002567 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568 return SendErrorResponse(0x15);
2569 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002570
Kate Stoneb9c1b512016-09-06 20:57:50 +00002571 // Parse out software or hardware breakpoint or watchpoint requested.
2572 packet.SetFilePos(strlen("z"));
2573 if (packet.GetBytesLeft() < 1)
2574 return SendIllFormedResponse(
2575 packet, "Too short z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002576
Kate Stoneb9c1b512016-09-06 20:57:50 +00002577 bool want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002578 bool want_hardware = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002579
Kate Stoneb9c1b512016-09-06 20:57:50 +00002580 const GDBStoppointType stoppoint_type =
2581 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2582 switch (stoppoint_type) {
2583 case eBreakpointHardware:
2584 want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002585 want_hardware = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002586 break;
2587 case eBreakpointSoftware:
2588 want_breakpoint = true;
2589 break;
2590 case eWatchpointWrite:
2591 want_breakpoint = false;
2592 break;
2593 case eWatchpointRead:
2594 want_breakpoint = false;
2595 break;
2596 case eWatchpointReadWrite:
2597 want_breakpoint = false;
2598 break;
2599 default:
2600 return SendIllFormedResponse(
2601 packet, "z packet had invalid software/hardware specifier");
2602 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002603
Kate Stoneb9c1b512016-09-06 20:57:50 +00002604 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2605 return SendIllFormedResponse(
2606 packet, "Malformed z packet, expecting comma after stoppoint type");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002607
Kate Stoneb9c1b512016-09-06 20:57:50 +00002608 // Parse out the stoppoint address.
2609 if (packet.GetBytesLeft() < 1)
2610 return SendIllFormedResponse(packet, "Too short z packet, missing address");
2611 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002612
Kate Stoneb9c1b512016-09-06 20:57:50 +00002613 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2614 return SendIllFormedResponse(
2615 packet, "Malformed z packet, expecting comma after address");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002616
Kate Stoneb9c1b512016-09-06 20:57:50 +00002617 /*
2618 // Parse out the stoppoint size (i.e. size hint for opcode size).
2619 const uint32_t size = packet.GetHexMaxU32 (false,
2620 std::numeric_limits<uint32_t>::max ());
2621 if (size == std::numeric_limits<uint32_t>::max ())
2622 return SendIllFormedResponse(packet, "Malformed z packet, failed to parse
2623 size argument");
2624 */
Tamas Berghammere13c2732015-02-11 10:29:30 +00002625
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 if (want_breakpoint) {
2627 // Try to clear the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002628 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002629 m_debugged_process_up->RemoveBreakpoint(addr, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002630 if (error.Success())
2631 return SendOKResponse();
2632 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002633 LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}",
2634 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 return SendErrorResponse(0x09);
2636 } else {
2637 // Try to clear the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002638 const Status error = m_debugged_process_up->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002639 if (error.Success())
2640 return SendOKResponse();
2641 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002642 LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}",
2643 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002644 return SendErrorResponse(0x09);
2645 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002646}
2647
2648GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002649GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) {
2650 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002651
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002653 if (!m_debugged_process_up ||
2654 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002655 if (log)
2656 log->Printf(
2657 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2658 __FUNCTION__);
2659 return SendErrorResponse(0x32);
2660 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002661
Kate Stoneb9c1b512016-09-06 20:57:50 +00002662 // We first try to use a continue thread id. If any one or any all set, use
Adrian Prantl05097242018-04-30 16:49:04 +00002663 // the current thread. Bail out if we don't have a thread id.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002664 lldb::tid_t tid = GetContinueThreadID();
2665 if (tid == 0 || tid == LLDB_INVALID_THREAD_ID)
2666 tid = GetCurrentThreadID();
2667 if (tid == LLDB_INVALID_THREAD_ID)
2668 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002669
Kate Stoneb9c1b512016-09-06 20:57:50 +00002670 // Double check that we have such a thread.
2671 // TODO investigate: on MacOSX we might need to do an UpdateThreads () here.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002672 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2673 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002674 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002675
Kate Stoneb9c1b512016-09-06 20:57:50 +00002676 // Create the step action for the given thread.
2677 ResumeAction action = {tid, eStateStepping, 0};
Tamas Berghammere13c2732015-02-11 10:29:30 +00002678
Kate Stoneb9c1b512016-09-06 20:57:50 +00002679 // Setup the actions list.
2680 ResumeActionList actions;
2681 actions.Append(action);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002682
Kate Stoneb9c1b512016-09-06 20:57:50 +00002683 // All other threads stop while we're single stepping a thread.
2684 actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0);
Pavel Labath82abefa2017-07-18 09:24:48 +00002685 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002686 if (error.Fail()) {
2687 if (log)
2688 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2689 " tid %" PRIu64 " Resume() failed with error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002690 __FUNCTION__, m_debugged_process_up->GetID(), tid,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 error.AsCString());
2692 return SendErrorResponse(0x49);
2693 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002694
Kate Stoneb9c1b512016-09-06 20:57:50 +00002695 // No response here - the stop or exit will come from the resulting action.
2696 return PacketResult::Success;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002697}
2698
2699GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read(
2701 StringExtractorGDBRemote &packet) {
2702// *BSD impls should be able to do this too.
Kamil Rytarowskic93408a2017-03-21 17:27:59 +00002703#if defined(__linux__) || defined(__NetBSD__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002704 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002705
Kate Stoneb9c1b512016-09-06 20:57:50 +00002706 // Parse out the offset.
2707 packet.SetFilePos(strlen("qXfer:auxv:read::"));
2708 if (packet.GetBytesLeft() < 1)
2709 return SendIllFormedResponse(packet,
2710 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002711
Kate Stoneb9c1b512016-09-06 20:57:50 +00002712 const uint64_t auxv_offset =
2713 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2714 if (auxv_offset == std::numeric_limits<uint64_t>::max())
2715 return SendIllFormedResponse(packet,
2716 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002717
Kate Stoneb9c1b512016-09-06 20:57:50 +00002718 // Parse out comma.
2719 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ',')
2720 return SendIllFormedResponse(
2721 packet, "qXfer:auxv:read:: packet missing comma after offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002722
Kate Stoneb9c1b512016-09-06 20:57:50 +00002723 // Parse out the length.
2724 const uint64_t auxv_length =
2725 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2726 if (auxv_length == std::numeric_limits<uint64_t>::max())
2727 return SendIllFormedResponse(packet,
2728 "qXfer:auxv:read:: packet missing length");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002729
Kate Stoneb9c1b512016-09-06 20:57:50 +00002730 // Grab the auxv data if we need it.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002731 if (!m_active_auxv_buffer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002732 // Make sure we have a valid process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002733 if (!m_debugged_process_up ||
2734 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 if (log)
2736 log->Printf(
2737 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2738 __FUNCTION__);
2739 return SendErrorResponse(0x10);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002740 }
2741
Kate Stoneb9c1b512016-09-06 20:57:50 +00002742 // Grab the auxv data.
Pavel Labath82abefa2017-07-18 09:24:48 +00002743 auto buffer_or_error = m_debugged_process_up->GetAuxvData();
Pavel Labathb7f0f452017-03-17 11:08:40 +00002744 if (!buffer_or_error) {
2745 std::error_code ec = buffer_or_error.getError();
2746 LLDB_LOG(log, "no auxv data retrieved: {0}", ec.message());
2747 return SendErrorResponse(ec.value());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002748 }
Pavel Labathb7f0f452017-03-17 11:08:40 +00002749 m_active_auxv_buffer_up = std::move(*buffer_or_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002750 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002751
Kate Stoneb9c1b512016-09-06 20:57:50 +00002752 StreamGDBRemote response;
2753 bool done_with_buffer = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002754
Pavel Labathb7f0f452017-03-17 11:08:40 +00002755 llvm::StringRef buffer = m_active_auxv_buffer_up->getBuffer();
2756 if (auxv_offset >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002757 // We have nothing left to send. Mark the buffer as complete.
2758 response.PutChar('l');
2759 done_with_buffer = true;
2760 } else {
2761 // Figure out how many bytes are available starting at the given offset.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002762 buffer = buffer.drop_front(auxv_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002763
2764 // Mark the response type according to whether we're reading the remainder
2765 // of the auxv data.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002766 if (auxv_length >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002767 // There will be nothing left to read after this
2768 response.PutChar('l');
2769 done_with_buffer = true;
2770 } else {
2771 // There will still be bytes to read after this request.
2772 response.PutChar('m');
Pavel Labathb7f0f452017-03-17 11:08:40 +00002773 buffer = buffer.take_front(auxv_length);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002774 }
2775
Kate Stoneb9c1b512016-09-06 20:57:50 +00002776 // Now write the data in encoded binary form.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002777 response.PutEscapedBytes(buffer.data(), buffer.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002778 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002779
Kate Stoneb9c1b512016-09-06 20:57:50 +00002780 if (done_with_buffer)
Pavel Labathb7f0f452017-03-17 11:08:40 +00002781 m_active_auxv_buffer_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002782
2783 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002784#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002785 return SendUnimplementedResponse("not implemented on this platform");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002786#endif
2787}
2788
2789GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002790GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState(
2791 StringExtractorGDBRemote &packet) {
2792 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002793
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 // Move past packet name.
2795 packet.SetFilePos(strlen("QSaveRegisterState"));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002796
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002798 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2799 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002800 if (m_thread_suffix_supported)
2801 return SendIllFormedResponse(
2802 packet, "No thread specified in QSaveRegisterState packet");
2803 else
2804 return SendIllFormedResponse(packet,
2805 "No thread was is set with the Hg packet");
2806 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002807
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002809 NativeRegisterContext& reg_context = thread->GetRegisterContext();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002810
Kate Stoneb9c1b512016-09-06 20:57:50 +00002811 // Save registers to a buffer.
2812 DataBufferSP register_data_sp;
Pavel Labathd37349f2017-11-10 11:05:49 +00002813 Status error = reg_context.ReadAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002814 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002815 LLDB_LOG(log, "pid {0} failed to save all register values: {1}",
2816 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817 return SendErrorResponse(0x75);
2818 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002819
Kate Stoneb9c1b512016-09-06 20:57:50 +00002820 // Allocate a new save id.
2821 const uint32_t save_id = GetNextSavedRegistersID();
2822 assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) &&
2823 "GetNextRegisterSaveID() returned an existing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002824
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825 // Save the register data buffer under the save id.
2826 {
2827 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2828 m_saved_registers_map[save_id] = register_data_sp;
2829 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002830
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831 // Write the response.
2832 StreamGDBRemote response;
2833 response.Printf("%" PRIu32, save_id);
2834 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002835}
2836
2837GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState(
2839 StringExtractorGDBRemote &packet) {
2840 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002841
Kate Stoneb9c1b512016-09-06 20:57:50 +00002842 // Parse out save id.
2843 packet.SetFilePos(strlen("QRestoreRegisterState:"));
2844 if (packet.GetBytesLeft() < 1)
2845 return SendIllFormedResponse(
2846 packet, "QRestoreRegisterState packet missing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002847
Kate Stoneb9c1b512016-09-06 20:57:50 +00002848 const uint32_t save_id = packet.GetU32(0);
2849 if (save_id == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002850 LLDB_LOG(log, "QRestoreRegisterState packet has malformed save id, "
2851 "expecting decimal uint32_t");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002852 return SendErrorResponse(0x76);
2853 }
2854
2855 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002856 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2857 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 if (m_thread_suffix_supported)
2859 return SendIllFormedResponse(
2860 packet, "No thread specified in QRestoreRegisterState packet");
2861 else
2862 return SendIllFormedResponse(packet,
2863 "No thread was is set with the Hg packet");
2864 }
2865
2866 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002867 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002868
2869 // Retrieve register state buffer, then remove from the list.
2870 DataBufferSP register_data_sp;
2871 {
2872 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2873
2874 // Find the register set buffer for the given save id.
2875 auto it = m_saved_registers_map.find(save_id);
2876 if (it == m_saved_registers_map.end()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002877 LLDB_LOG(log,
2878 "pid {0} does not have a register set save buffer for id {1}",
2879 m_debugged_process_up->GetID(), save_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002880 return SendErrorResponse(0x77);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002882 register_data_sp = it->second;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002883
Kate Stoneb9c1b512016-09-06 20:57:50 +00002884 // Remove it from the map.
2885 m_saved_registers_map.erase(it);
2886 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002887
Pavel Labathd37349f2017-11-10 11:05:49 +00002888 Status error = reg_context.WriteAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002890 LLDB_LOG(log, "pid {0} failed to restore all register values: {1}",
2891 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002892 return SendErrorResponse(0x77);
2893 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002894
Kate Stoneb9c1b512016-09-06 20:57:50 +00002895 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002896}
2897
2898GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002899GDBRemoteCommunicationServerLLGS::Handle_vAttach(
2900 StringExtractorGDBRemote &packet) {
2901 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002902
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 // Consume the ';' after vAttach.
2904 packet.SetFilePos(strlen("vAttach"));
2905 if (!packet.GetBytesLeft() || packet.GetChar() != ';')
2906 return SendIllFormedResponse(packet, "vAttach missing expected ';'");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002907
Kate Stoneb9c1b512016-09-06 20:57:50 +00002908 // Grab the PID to which we will attach (assume hex encoding).
2909 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
2910 if (pid == LLDB_INVALID_PROCESS_ID)
2911 return SendIllFormedResponse(packet,
2912 "vAttach failed to parse the process id");
2913
2914 // Attempt to attach.
2915 if (log)
2916 log->Printf("GDBRemoteCommunicationServerLLGS::%s attempting to attach to "
2917 "pid %" PRIu64,
2918 __FUNCTION__, pid);
2919
Zachary Turner97206d52017-05-12 04:51:55 +00002920 Status error = AttachToProcess(pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002921
2922 if (error.Fail()) {
2923 if (log)
2924 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to attach to "
2925 "pid %" PRIu64 ": %s\n",
2926 __FUNCTION__, pid, error.AsCString());
Pavel Labatha70512a2018-04-11 13:30:54 +00002927 return SendErrorResponse(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928 }
2929
2930 // Notify we attached by sending a stop packet.
Pavel Labath82abefa2017-07-18 09:24:48 +00002931 return SendStopReasonForState(m_debugged_process_up->GetState());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002932}
2933
2934GDBRemoteCommunication::PacketResult
2935GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) {
2936 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2937
2938 StopSTDIOForwarding();
2939
2940 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002941 if (!m_debugged_process_up ||
2942 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002943 if (log)
2944 log->Printf(
2945 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2946 __FUNCTION__);
2947 return SendErrorResponse(0x15);
2948 }
2949
2950 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2951
2952 // Consume the ';' after D.
2953 packet.SetFilePos(1);
2954 if (packet.GetBytesLeft()) {
2955 if (packet.GetChar() != ';')
2956 return SendIllFormedResponse(packet, "D missing expected ';'");
2957
2958 // Grab the PID from which we will detach (assume hex encoding).
2959 pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002960 if (pid == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002961 return SendIllFormedResponse(packet, "D failed to parse the process id");
2962 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002963
Pavel Labath82abefa2017-07-18 09:24:48 +00002964 if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_up->GetID() != pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002965 return SendIllFormedResponse(packet, "Invalid pid");
2966 }
2967
Pavel Labath82abefa2017-07-18 09:24:48 +00002968 const Status error = m_debugged_process_up->Detach();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002969 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002970 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002971 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to detach from "
2972 "pid %" PRIu64 ": %s\n",
Pavel Labath82abefa2017-07-18 09:24:48 +00002973 __FUNCTION__, m_debugged_process_up->GetID(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974 error.AsCString());
2975 return SendErrorResponse(0x01);
2976 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002977
Kate Stoneb9c1b512016-09-06 20:57:50 +00002978 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002979}
2980
2981GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002982GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo(
2983 StringExtractorGDBRemote &packet) {
2984 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002985
Kate Stoneb9c1b512016-09-06 20:57:50 +00002986 packet.SetFilePos(strlen("qThreadStopInfo"));
2987 const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
2988 if (tid == LLDB_INVALID_THREAD_ID) {
Pavel Labath4a4bb122015-07-16 14:14:35 +00002989 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002990 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
2991 "parse thread id from request \"%s\"",
2992 __FUNCTION__, packet.GetStringRef().c_str());
2993 return SendErrorResponse(0x15);
2994 }
2995 return SendStopReplyPacketForThread(tid);
2996}
2997
2998GDBRemoteCommunication::PacketResult
2999GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo(
3000 StringExtractorGDBRemote &) {
3001 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
3002
3003 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003004 if (!m_debugged_process_up ||
3005 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003006 return SendErrorResponse(50);
Pavel Labath82abefa2017-07-18 09:24:48 +00003007 LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003008
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009 StreamString response;
3010 const bool threads_with_valid_stop_info_only = false;
3011 JSONArray::SP threads_array_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +00003012 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003013 if (!threads_array_sp) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003014 LLDB_LOG(log, "failed to prepare a packet for pid {0}",
3015 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 return SendErrorResponse(52);
3017 }
Pavel Labath4a4bb122015-07-16 14:14:35 +00003018
Kate Stoneb9c1b512016-09-06 20:57:50 +00003019 threads_array_sp->Write(response);
3020 StreamGDBRemote escaped_response;
3021 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
3022 return SendPacketNoLock(escaped_response.GetString());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003023}
3024
3025GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003026GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo(
3027 StringExtractorGDBRemote &packet) {
3028 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003029 if (!m_debugged_process_up ||
3030 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003032
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033 packet.SetFilePos(strlen("qWatchpointSupportInfo"));
3034 if (packet.GetBytesLeft() == 0)
3035 return SendOKResponse();
3036 if (packet.GetChar() != ':')
3037 return SendErrorResponse(67);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003038
Pavel Labath82abefa2017-07-18 09:24:48 +00003039 auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo();
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003040
Kate Stoneb9c1b512016-09-06 20:57:50 +00003041 StreamGDBRemote response;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003042 if (hw_debug_cap == llvm::None)
3043 response.Printf("num:0;");
3044 else
3045 response.Printf("num:%d;", hw_debug_cap->second);
3046
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00003048}
3049
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003050GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003051GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress(
3052 StringExtractorGDBRemote &packet) {
3053 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003054 if (!m_debugged_process_up ||
3055 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003056 return SendErrorResponse(67);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003057
Kate Stoneb9c1b512016-09-06 20:57:50 +00003058 packet.SetFilePos(strlen("qFileLoadAddress:"));
3059 if (packet.GetBytesLeft() == 0)
3060 return SendErrorResponse(68);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003061
Kate Stoneb9c1b512016-09-06 20:57:50 +00003062 std::string file_name;
3063 packet.GetHexByteString(file_name);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003064
Kate Stoneb9c1b512016-09-06 20:57:50 +00003065 lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +00003066 Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00003067 m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068 if (error.Fail())
3069 return SendErrorResponse(69);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071 if (file_load_address == LLDB_INVALID_ADDRESS)
3072 return SendErrorResponse(1); // File not loaded
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003073
Kate Stoneb9c1b512016-09-06 20:57:50 +00003074 StreamGDBRemote response;
3075 response.PutHex64(file_load_address);
3076 return SendPacketNoLock(response.GetString());
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003077}
3078
Pavel Labath4a705e72017-02-24 09:29:14 +00003079GDBRemoteCommunication::PacketResult
3080GDBRemoteCommunicationServerLLGS::Handle_QPassSignals(
3081 StringExtractorGDBRemote &packet) {
3082 std::vector<int> signals;
3083 packet.SetFilePos(strlen("QPassSignals:"));
3084
Adrian Prantl05097242018-04-30 16:49:04 +00003085 // Read sequence of hex signal numbers divided by a semicolon and optionally
3086 // spaces.
Pavel Labath4a705e72017-02-24 09:29:14 +00003087 while (packet.GetBytesLeft() > 0) {
3088 int signal = packet.GetS32(-1, 16);
3089 if (signal < 0)
3090 return SendIllFormedResponse(packet, "Failed to parse signal number.");
3091 signals.push_back(signal);
3092
3093 packet.SkipSpaces();
3094 char separator = packet.GetChar();
3095 if (separator == '\0')
3096 break; // End of string
3097 if (separator != ';')
3098 return SendIllFormedResponse(packet, "Invalid separator,"
3099 " expected semicolon.");
3100 }
3101
3102 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003103 if (!m_debugged_process_up)
Pavel Labath4a705e72017-02-24 09:29:14 +00003104 return SendErrorResponse(68);
3105
Pavel Labath82abefa2017-07-18 09:24:48 +00003106 Status error = m_debugged_process_up->IgnoreSignals(signals);
Pavel Labath4a705e72017-02-24 09:29:14 +00003107 if (error.Fail())
3108 return SendErrorResponse(69);
3109
3110 return SendOKResponse();
3111}
3112
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() {
3114 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003115
Kate Stoneb9c1b512016-09-06 20:57:50 +00003116 // Tell the stdio connection to shut down.
3117 if (m_stdio_communication.IsConnected()) {
3118 auto connection = m_stdio_communication.GetConnection();
3119 if (connection) {
Zachary Turner97206d52017-05-12 04:51:55 +00003120 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003121 connection->Disconnect(&error);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003122
Kate Stoneb9c1b512016-09-06 20:57:50 +00003123 if (error.Success()) {
3124 if (log)
3125 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3126 "terminal stdio - SUCCESS",
3127 __FUNCTION__);
3128 } else {
3129 if (log)
3130 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3131 "terminal stdio - FAIL: %s",
3132 __FUNCTION__, error.AsCString());
3133 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003135 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003136}
3137
Pavel Labatha5be48b2017-10-17 15:52:16 +00003138NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003140 // We have no thread if we don't have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003141 if (!m_debugged_process_up ||
3142 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003143 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +00003144
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145 // If the client hasn't asked for thread suffix support, there will not be a
Adrian Prantl05097242018-04-30 16:49:04 +00003146 // thread suffix. Use the current thread in that case.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147 if (!m_thread_suffix_supported) {
3148 const lldb::tid_t current_tid = GetCurrentThreadID();
3149 if (current_tid == LLDB_INVALID_THREAD_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003150 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151 else if (current_tid == 0) {
3152 // Pick a thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003153 return m_debugged_process_up->GetThreadAtIndex(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003154 } else
Pavel Labath82abefa2017-07-18 09:24:48 +00003155 return m_debugged_process_up->GetThreadByID(current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003157
Kate Stoneb9c1b512016-09-06 20:57:50 +00003158 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003159
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 // Parse out the ';'.
3161 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') {
Tamas Berghammere13c2732015-02-11 10:29:30 +00003162 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3164 "error: expected ';' prior to start of thread suffix: packet "
3165 "contents = '%s'",
3166 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003167 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003168 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003169
Kate Stoneb9c1b512016-09-06 20:57:50 +00003170 if (!packet.GetBytesLeft())
Pavel Labatha5be48b2017-10-17 15:52:16 +00003171 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003172
3173 // Parse out thread: portion.
3174 if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) {
3175 if (log)
3176 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3177 "error: expected 'thread:' but not found, packet contents = "
3178 "'%s'",
3179 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003180 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 }
3182 packet.SetFilePos(packet.GetFilePos() + strlen("thread:"));
3183 const lldb::tid_t tid = packet.GetHexMaxU64(false, 0);
3184 if (tid != 0)
Pavel Labath82abefa2017-07-18 09:24:48 +00003185 return m_debugged_process_up->GetThreadByID(tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003186
Pavel Labatha5be48b2017-10-17 15:52:16 +00003187 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003188}
3189
3190lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const {
3191 if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) {
Adrian Prantl05097242018-04-30 16:49:04 +00003192 // Use whatever the debug process says is the current thread id since the
3193 // protocol either didn't specify or specified we want any/all threads
3194 // marked as the current thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003195 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003196 return LLDB_INVALID_THREAD_ID;
Pavel Labath82abefa2017-07-18 09:24:48 +00003197 return m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003198 }
3199 // Use the specific current thread id set by the gdb remote protocol.
3200 return m_current_tid;
3201}
3202
3203uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() {
3204 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
3205 return m_next_saved_registers_id++;
3206}
3207
3208void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() {
Pavel Labathb7f0f452017-03-17 11:08:40 +00003209 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003210
Pavel Labathb7f0f452017-03-17 11:08:40 +00003211 LLDB_LOG(log, "clearing auxv buffer: {0}", m_active_auxv_buffer_up.get());
3212 m_active_auxv_buffer_up.reset();
Tamas Berghammere13c2732015-02-11 10:29:30 +00003213}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003214
3215FileSpec
Kate Stoneb9c1b512016-09-06 20:57:50 +00003216GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path,
3217 const ArchSpec &arch) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003218 if (m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003219 FileSpec file_spec;
Pavel Labath82abefa2017-07-18 09:24:48 +00003220 if (m_debugged_process_up
Kate Stoneb9c1b512016-09-06 20:57:50 +00003221 ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec)
3222 .Success()) {
3223 if (file_spec.Exists())
3224 return file_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003225 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003226 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003227
Kate Stoneb9c1b512016-09-06 20:57:50 +00003228 return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003229}