blob: 23c1547fbbf3336067b185470b694a73ce193bc8 [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 Berghammer9c9ecce2015-05-27 13:34:04 +000024#include "lldb/Core/RegisterValue.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000025#include "lldb/Core/State.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000026#include "lldb/Host/ConnectionFileDescriptor.h"
27#include "lldb/Host/Debug.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000028#include "lldb/Host/File.h"
29#include "lldb/Host/FileSystem.h"
30#include "lldb/Host/Host.h"
31#include "lldb/Host/HostInfo.h"
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000032#include "lldb/Host/PosixApi.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000033#include "lldb/Host/common/NativeProcessProtocol.h"
34#include "lldb/Host/common/NativeRegisterContext.h"
35#include "lldb/Host/common/NativeThreadProtocol.h"
36#include "lldb/Interpreter/Args.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000037#include "lldb/Target/FileAction.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000038#include "lldb/Target/MemoryRegionInfo.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000039#include "lldb/Utility/DataBuffer.h"
Zachary Turner01c32432017-02-14 19:06:07 +000040#include "lldb/Utility/Endian.h"
Pavel Labath4a4bb122015-07-16 14:14:35 +000041#include "lldb/Utility/JSON.h"
Pavel Labathabadc222015-11-27 13:33:29 +000042#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000043#include "lldb/Utility/Log.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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000052#include "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
Zachary Turner97206d52017-05-12 04:51:55 +0000207Status
208GDBRemoteCommunicationServerLLGS::SetLaunchArguments(const char *const args[],
209 int argc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 if ((argc < 1) || !args || !args[0] || !args[0][0])
Zachary Turner97206d52017-05-12 04:51:55 +0000211 return Status("%s: no process command line specified to launch",
212 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 m_process_launch_info.SetArguments(const_cast<const char **>(args), true);
Zachary Turner97206d52017-05-12 04:51:55 +0000215 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000216}
217
Zachary Turner97206d52017-05-12 04:51:55 +0000218Status
219GDBRemoteCommunicationServerLLGS::SetLaunchFlags(unsigned int launch_flags) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 m_process_launch_info.GetFlags().Set(launch_flags);
Zachary Turner97206d52017-05-12 04:51:55 +0000221 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000222}
223
Zachary Turner97206d52017-05-12 04:51:55 +0000224Status GDBRemoteCommunicationServerLLGS::LaunchProcess() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 if (!m_process_launch_info.GetArguments().GetArgumentCount())
Zachary Turner97206d52017-05-12 04:51:55 +0000228 return Status("%s: no process command line specified to launch",
229 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 const bool should_forward_stdio =
232 m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
233 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
234 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr;
235 m_process_launch_info.SetLaunchInSeparateProcessGroup(true);
236 m_process_launch_info.GetFlags().Set(eLaunchFlagDebug);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 const bool default_to_use_pty = true;
239 m_process_launch_info.FinalizeFileActions(nullptr, default_to_use_pty);
Pavel Labath5ad891f2016-07-21 14:54:03 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 {
242 std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex);
Pavel Labath82abefa2017-07-18 09:24:48 +0000243 assert(!m_debugged_process_up && "lldb-server creating debugged "
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 "process but one already exists");
Pavel Labath96e600f2017-07-07 11:02:19 +0000245 auto process_or =
246 m_process_factory.Launch(m_process_launch_info, *this, m_mainloop);
Pavel Labath8630d382017-12-14 14:56:29 +0000247 if (!process_or)
248 return Status(process_or.takeError());
Pavel Labath82abefa2017-07-18 09:24:48 +0000249 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol
253 // as needed.
254 // llgs local-process debugging may specify PTY paths, which will make these
255 // file actions non-null
256 // process launch -i/e/o will also make these file actions non-null
257 // nullptr means that the traffic is expected to flow over gdb-remote protocol
258 if (should_forward_stdio) {
259 // nullptr means it's not redirected to file or pty (in case of LLGS local)
260 // at least one of stdio will be transferred pty<->gdb-remote
261 // we need to give the pty master handle to this object to read and/or write
Pavel Labath82abefa2017-07-18 09:24:48 +0000262 LLDB_LOG(log,
263 "pid = {0}: setting up stdout/stderr redirection via $O "
264 "gdb-remote commands",
265 m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 // Setup stdout/stderr mapping from inferior to $O
Pavel Labath82abefa2017-07-18 09:24:48 +0000268 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (terminal_fd >= 0) {
270 if (log)
271 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
272 "inferior STDIO fd to %d",
273 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000274 Status status = SetSTDIOFileDescriptor(terminal_fd);
275 if (status.Fail())
276 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 } else {
278 if (log)
279 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
280 "inferior STDIO since terminal fd reported as %d",
281 __FUNCTION__, terminal_fd);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 } else {
Pavel Labath82abefa2017-07-18 09:24:48 +0000284 LLDB_LOG(log,
285 "pid = {0} skipping stdout/stderr redirection via $O: inferior "
286 "will communicate over client-provided file descriptors",
287 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 }
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 printf("Launched '%s' as process %" PRIu64 "...\n",
291 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
Pavel Labath82abefa2017-07-18 09:24:48 +0000292 m_debugged_process_up->GetID());
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000293
Pavel Labath96e600f2017-07-07 11:02:19 +0000294 return Status();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000295}
296
Zachary Turner97206d52017-05-12 04:51:55 +0000297Status GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
299 if (log)
300 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64,
301 __FUNCTION__, pid);
302
303 // Before we try to attach, make sure we aren't already monitoring something
304 // else.
Pavel Labath82abefa2017-07-18 09:24:48 +0000305 if (m_debugged_process_up &&
306 m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID)
Zachary Turner97206d52017-05-12 04:51:55 +0000307 return Status("cannot attach to a process %" PRIu64
308 " when another process with pid %" PRIu64
309 " is being debugged.",
Pavel Labath82abefa2017-07-18 09:24:48 +0000310 pid, m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311
312 // Try to attach.
Pavel Labath96e600f2017-07-07 11:02:19 +0000313 auto process_or = m_process_factory.Attach(pid, *this, m_mainloop);
314 if (!process_or) {
315 Status status(process_or.takeError());
316 llvm::errs() << llvm::formatv("failed to attach to process {0}: {1}", pid,
317 status);
318 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 }
Pavel Labath82abefa2017-07-18 09:24:48 +0000320 m_debugged_process_up = std::move(*process_or);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321
322 // Setup stdout/stderr mapping from inferior.
Pavel Labath82abefa2017-07-18 09:24:48 +0000323 auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 if (terminal_fd >= 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000325 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s setting "
327 "inferior STDIO fd to %d",
328 __FUNCTION__, terminal_fd);
Pavel Labath96e600f2017-07-07 11:02:19 +0000329 Status status = SetSTDIOFileDescriptor(terminal_fd);
330 if (status.Fail())
331 return status;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 } else {
333 if (log)
334 log->Printf("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
335 "inferior STDIO since terminal fd reported as %d",
336 __FUNCTION__, terminal_fd);
337 }
338
339 printf("Attached to process %" PRIu64 "...\n", pid);
Pavel Labath96e600f2017-07-07 11:02:19 +0000340 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341}
342
343void GDBRemoteCommunicationServerLLGS::InitializeDelegate(
344 NativeProcessProtocol *process) {
345 assert(process && "process cannot be NULL");
346 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
347 if (log) {
348 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
349 "NativeProcessProtocol pid %" PRIu64 ", current state: %s",
350 __FUNCTION__, process->GetID(),
351 StateAsCString(process->GetState()));
352 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000353}
354
355GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356GDBRemoteCommunicationServerLLGS::SendWResponse(
357 NativeProcessProtocol *process) {
358 assert(process && "process cannot be NULL");
359 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 // send W notification
Pavel Labath3508fc82017-06-19 12:47:50 +0000362 auto wait_status = process->GetExitStatus();
363 if (!wait_status) {
364 LLDB_LOG(log, "pid = {0}, failed to retrieve process exit status",
365 process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 StreamGDBRemote response;
368 response.PutChar('E');
369 response.PutHex8(GDBRemoteServerError::eErrorExitStatus);
370 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
Pavel Labath3508fc82017-06-19 12:47:50 +0000372
373 LLDB_LOG(log, "pid = {0}, returning exit type {1}", process->GetID(),
374 *wait_status);
375
376 StreamGDBRemote response;
377 response.Format("{0:g}", *wait_status);
378 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000379}
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381static void AppendHexValue(StreamString &response, const uint8_t *buf,
382 uint32_t buf_size, bool swap) {
383 int64_t i;
384 if (swap) {
385 for (i = buf_size - 1; i >= 0; i--)
386 response.PutHex8(buf[i]);
387 } else {
388 for (i = 0; i < buf_size; i++)
389 response.PutHex8(buf[i]);
390 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000391}
392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393static void WriteRegisterValueInHexFixedWidth(
Pavel Labathd37349f2017-11-10 11:05:49 +0000394 StreamString &response, NativeRegisterContext &reg_ctx,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000395 const RegisterInfo &reg_info, const RegisterValue *reg_value_p,
396 lldb::ByteOrder byte_order) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 RegisterValue reg_value;
398 if (!reg_value_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000399 Status error = reg_ctx.ReadRegister(&reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 if (error.Success())
401 reg_value_p = &reg_value;
402 // else log.
403 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 if (reg_value_p) {
406 AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(),
Pavel Labathe0a5b572017-01-20 14:17:16 +0000407 reg_value_p->GetByteSize(),
408 byte_order == lldb::eByteOrderLittle);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 } else {
410 // Zero-out any unreadable values.
411 if (reg_info.byte_size > 0) {
412 std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0');
413 AppendHexValue(response, zeros.data(), zeros.size(), false);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000416}
417
Pavel Labathe0a5b572017-01-20 14:17:16 +0000418static JSONObject::SP GetRegistersAsJSON(NativeThreadProtocol &thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath4a4bb122015-07-16 14:14:35 +0000420
Pavel Labathd37349f2017-11-10 11:05:49 +0000421 NativeRegisterContext& reg_ctx = thread.GetRegisterContext();
Pavel Labath4a4bb122015-07-16 14:14:35 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 JSONObject::SP register_object_sp = std::make_shared<JSONObject>();
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000424
425#ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 // Expedite all registers in the first register set (i.e. should be GPRs) that
427 // are not contained in other registers.
428 const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0);
429 if (!reg_set_p)
430 return nullptr;
431 for (const uint32_t *reg_num_p = reg_set_p->registers;
432 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
433 uint32_t reg_num = *reg_num_p;
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000434#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 // Expedite only a couple of registers until we figure out why sending
436 // registers is
437 // expensive.
438 static const uint32_t k_expedited_registers[] = {
439 LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP,
440 LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM};
Pavel Labathc4645bb2015-10-26 16:25:28 +0000441
Pavel Labathe0a5b572017-01-20 14:17:16 +0000442 for (const uint32_t *generic_reg_p = k_expedited_registers;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000444 uint32_t reg_num = reg_ctx.ConvertRegisterKindToRegisterNumber(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 eRegisterKindGeneric, *generic_reg_p);
446 if (reg_num == LLDB_INVALID_REGNUM)
447 continue; // Target does not support the given register.
Pavel Labathe4fc4ef2015-07-17 10:27:42 +0000448#endif
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000451 reg_ctx.GetRegisterInfoAtIndex(reg_num);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 if (reg_info_p == nullptr) {
453 if (log)
454 log->Printf(
455 "%s failed to get register info for register index %" PRIu32,
456 __FUNCTION__, reg_num);
457 continue;
Pavel Labath4a4bb122015-07-16 14:14:35 +0000458 }
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 if (reg_info_p->value_regs != nullptr)
461 continue; // Only expedite registers that are not contained in other
462 // registers.
Pavel Labath4a4bb122015-07-16 14:14:35 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000465 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 if (error.Fail()) {
467 if (log)
468 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
Pavel Labath05569f62015-07-23 09:09:29 +0000469 __FUNCTION__,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 reg_info_p->name ? reg_info_p->name : "<unnamed-register>",
471 reg_num, error.AsCString());
472 continue;
Pavel Labath05569f62015-07-23 09:09:29 +0000473 }
474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 StreamString stream;
Pavel Labathd37349f2017-11-10 11:05:49 +0000476 WriteRegisterValueInHexFixedWidth(stream, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000477 &reg_value, lldb::eByteOrderBig);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478
479 register_object_sp->SetObject(
480 llvm::to_string(reg_num),
481 std::make_shared<JSONString>(stream.GetString()));
482 }
483
484 return register_object_sp;
Pavel Labath05569f62015-07-23 09:09:29 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487static const char *GetStopReasonString(StopReason stop_reason) {
488 switch (stop_reason) {
489 case eStopReasonTrace:
490 return "trace";
491 case eStopReasonBreakpoint:
492 return "breakpoint";
493 case eStopReasonWatchpoint:
494 return "watchpoint";
495 case eStopReasonSignal:
496 return "signal";
497 case eStopReasonException:
498 return "exception";
499 case eStopReasonExec:
500 return "exec";
501 case eStopReasonInstrumentation:
502 case eStopReasonInvalid:
503 case eStopReasonPlanComplete:
504 case eStopReasonThreadExiting:
505 case eStopReasonNone:
506 break; // ignored
507 }
508 return nullptr;
509}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511static JSONArray::SP GetJSONThreadsInfo(NativeProcessProtocol &process,
512 bool abridged) {
513 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 JSONArray::SP threads_array_sp = std::make_shared<JSONArray>();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 // Ensure we can get info on the given thread.
518 uint32_t thread_idx = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000519 for (NativeThreadProtocol *thread;
520 (thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 ++thread_idx) {
522
Pavel Labatha5be48b2017-10-17 15:52:16 +0000523 lldb::tid_t tid = thread->GetID();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000524
525 // Grab the reason this thread stopped.
526 struct ThreadStopInfo tid_stop_info;
527 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000528 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000530
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 const int signum = tid_stop_info.details.signal.signo;
532 if (log) {
533 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
534 " tid %" PRIu64
535 " got signal signo = %d, reason = %d, exc_type = %" PRIu64,
536 __FUNCTION__, process.GetID(), tid, signum,
537 tid_stop_info.reason, tid_stop_info.details.exception.type);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000538 }
539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>();
541 threads_array_sp->AppendObject(thread_obj_sp);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000542
Pavel Labathe0a5b572017-01-20 14:17:16 +0000543 if (!abridged) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000544 if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread))
Pavel Labathe0a5b572017-01-20 14:17:16 +0000545 thread_obj_sp->SetObject("registers", registers_sp);
546 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 thread_obj_sp->SetObject("tid", std::make_shared<JSONNumber>(tid));
549 if (signum != 0)
550 thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(signum));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000551
Pavel Labatha5be48b2017-10-17 15:52:16 +0000552 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 if (!thread_name.empty())
554 thread_obj_sp->SetObject("name",
555 std::make_shared<JSONString>(thread_name));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 if (const char *stop_reason_str = GetStopReasonString(tid_stop_info.reason))
558 thread_obj_sp->SetObject("reason",
559 std::make_shared<JSONString>(stop_reason_str));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000560
561 if (!description.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 thread_obj_sp->SetObject("description",
563 std::make_shared<JSONString>(description));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 if ((tid_stop_info.reason == eStopReasonException) &&
566 tid_stop_info.details.exception.type) {
567 thread_obj_sp->SetObject(
568 "metype",
569 std::make_shared<JSONNumber>(tid_stop_info.details.exception.type));
570
571 JSONArray::SP medata_array_sp = std::make_shared<JSONArray>();
572 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count;
573 ++i) {
574 medata_array_sp->AppendObject(std::make_shared<JSONNumber>(
575 tid_stop_info.details.exception.data[i]));
576 }
577 thread_obj_sp->SetObject("medata", medata_array_sp);
578 }
579
580 // TODO: Expedite interesting regions of inferior memory
581 }
582
583 return threads_array_sp;
584}
585
586GDBRemoteCommunication::PacketResult
587GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread(
588 lldb::tid_t tid) {
589 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
590
591 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +0000592 if (!m_debugged_process_up ||
593 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 return SendErrorResponse(50);
595
Pavel Labath82abefa2017-07-18 09:24:48 +0000596 LLDB_LOG(log, "preparing packet for pid {0} tid {1}",
597 m_debugged_process_up->GetID(), tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598
599 // Ensure we can get info on the given thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000600 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
601 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 return SendErrorResponse(51);
603
604 // Grab the reason this thread stopped.
605 struct ThreadStopInfo tid_stop_info;
606 std::string description;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000607 if (!thread->GetStopReason(tid_stop_info, description))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 return SendErrorResponse(52);
609
610 // FIXME implement register handling for exec'd inferiors.
611 // if (tid_stop_info.reason == eStopReasonExec)
612 // {
613 // const bool force = true;
614 // InitializeRegisters(force);
615 // }
616
617 StreamString response;
618 // Output the T packet with the thread
619 response.PutChar('T');
620 int signum = tid_stop_info.details.signal.signo;
Pavel Labath82abefa2017-07-18 09:24:48 +0000621 LLDB_LOG(
622 log,
623 "pid {0}, tid {1}, got signal signo = {2}, reason = {3}, exc_type = {4}",
624 m_debugged_process_up->GetID(), tid, signum, int(tid_stop_info.reason),
625 tid_stop_info.details.exception.type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626
627 // Print the signal number.
628 response.PutHex8(signum & 0xff);
629
630 // Include the tid.
631 response.Printf("thread:%" PRIx64 ";", tid);
632
633 // Include the thread name if there is one.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000634 const std::string thread_name = thread->GetName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 if (!thread_name.empty()) {
636 size_t thread_name_len = thread_name.length();
637
638 if (::strcspn(thread_name.c_str(), "$#+-;:") == thread_name_len) {
639 response.PutCString("name:");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000640 response.PutCString(thread_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 } else {
642 // The thread name contains special chars, send as hex bytes.
643 response.PutCString("hexname:");
644 response.PutCStringAsRawHex8(thread_name.c_str());
645 }
646 response.PutChar(';');
647 }
648
649 // If a 'QListThreadsInStopReply' was sent to enable this feature, we
650 // will send all thread IDs back in the "threads" key whose value is
651 // a list of hex thread IDs separated by commas:
652 // "threads:10a,10b,10c;"
653 // This will save the debugger from having to send a pair of qfThreadInfo
654 // and qsThreadInfo packets, but it also might take a lot of room in the
655 // stop reply packet, so it must be enabled only on systems where there
656 // are no limits on packet lengths.
657 if (m_list_threads_in_stop_reply) {
658 response.PutCString("threads:");
659
660 uint32_t thread_index = 0;
Pavel Labatha5be48b2017-10-17 15:52:16 +0000661 NativeThreadProtocol *listed_thread;
662 for (listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
663 listed_thread; ++thread_index,
664 listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 if (thread_index > 0)
666 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +0000667 response.Printf("%" PRIx64, listed_thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 }
669 response.PutChar(';');
670
671 // Include JSON info that describes the stop reason for any threads
672 // that actually have stop reasons. We use the new "jstopinfo" key
673 // whose values is hex ascii JSON that contains the thread IDs
674 // thread stop info only for threads that have stop reasons. Only send
675 // this if we have more than one thread otherwise this packet has all
676 // the info it needs.
677 if (thread_index > 0) {
678 const bool threads_with_valid_stop_info_only = true;
679 JSONArray::SP threads_info_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +0000680 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 if (threads_info_sp) {
682 response.PutCString("jstopinfo:");
683 StreamString unescaped_response;
684 threads_info_sp->Write(unescaped_response);
685 response.PutCStringAsRawHex8(unescaped_response.GetData());
686 response.PutChar(';');
Pavel Labath82abefa2017-07-18 09:24:48 +0000687 } else
688 LLDB_LOG(log, "failed to prepare a jstopinfo field for pid {0}",
689 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 }
Pavel Labathe0a5b572017-01-20 14:17:16 +0000691
692 uint32_t i = 0;
693 response.PutCString("thread-pcs");
694 char delimiter = ':';
Pavel Labatha5be48b2017-10-17 15:52:16 +0000695 for (NativeThreadProtocol *thread;
696 (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
Pavel Labathe0a5b572017-01-20 14:17:16 +0000697 ++i) {
Pavel Labathd37349f2017-11-10 11:05:49 +0000698 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
Pavel Labathe0a5b572017-01-20 14:17:16 +0000699
Pavel Labathd37349f2017-11-10 11:05:49 +0000700 uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber(
Pavel Labathe0a5b572017-01-20 14:17:16 +0000701 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
702 const RegisterInfo *const reg_info_p =
Pavel Labathd37349f2017-11-10 11:05:49 +0000703 reg_ctx.GetRegisterInfoAtIndex(reg_to_read);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000704
705 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +0000706 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
Pavel Labathe0a5b572017-01-20 14:17:16 +0000707 if (error.Fail()) {
708 if (log)
709 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s",
710 __FUNCTION__,
711 reg_info_p->name ? reg_info_p->name
712 : "<unnamed-register>",
713 reg_to_read, error.AsCString());
714 continue;
715 }
716
717 response.PutChar(delimiter);
718 delimiter = ',';
Pavel Labathd37349f2017-11-10 11:05:49 +0000719 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
Pavel Labathe0a5b572017-01-20 14:17:16 +0000720 &reg_value, endian::InlHostByteOrder());
721 }
722
723 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 }
725
726 //
727 // Expedite registers.
728 //
729
730 // Grab the register context.
Pavel Labathd37349f2017-11-10 11:05:49 +0000731 NativeRegisterContext& reg_ctx = thread->GetRegisterContext();
732 // Expedite all registers in the first register set (i.e. should be GPRs)
733 // that are not contained in other registers.
734 const RegisterSet *reg_set_p;
735 if (reg_ctx.GetRegisterSetCount() > 0 &&
736 ((reg_set_p = reg_ctx.GetRegisterSet(0)) != nullptr)) {
737 if (log)
738 log->Printf("GDBRemoteCommunicationServerLLGS::%s expediting registers "
739 "from set '%s' (registers set count: %zu)",
740 __FUNCTION__,
741 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
742 reg_set_p->num_registers);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743
Pavel Labathd37349f2017-11-10 11:05:49 +0000744 for (const uint32_t *reg_num_p = reg_set_p->registers;
745 *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) {
746 const RegisterInfo *const reg_info_p =
747 reg_ctx.GetRegisterInfoAtIndex(*reg_num_p);
748 if (reg_info_p == nullptr) {
749 if (log)
750 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get "
751 "register info for register set '%s', register index "
752 "%" PRIu32,
753 __FUNCTION__,
754 reg_set_p->name ? reg_set_p->name : "<unnamed-set>",
755 *reg_num_p);
756 } else if (reg_info_p->value_regs == nullptr) {
757 // Only expediate registers that are not contained in other registers.
758 RegisterValue reg_value;
759 Status error = reg_ctx.ReadRegister(reg_info_p, reg_value);
760 if (error.Success()) {
761 response.Printf("%.02x:", *reg_num_p);
762 WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p,
763 &reg_value, lldb::eByteOrderBig);
764 response.PutChar(';');
765 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 if (log)
Pavel Labathd37349f2017-11-10 11:05:49 +0000767 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to read "
768 "register '%s' index %" PRIu32 ": %s",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 __FUNCTION__,
Pavel Labathd37349f2017-11-10 11:05:49 +0000770 reg_info_p->name ? reg_info_p->name
771 : "<unnamed-register>",
772 *reg_num_p, error.AsCString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 const char *reason_str = GetStopReasonString(tid_stop_info.reason);
779 if (reason_str != nullptr) {
780 response.Printf("reason:%s;", reason_str);
781 }
782
783 if (!description.empty()) {
784 // Description may contains special chars, send as hex bytes.
785 response.PutCString("description:");
786 response.PutCStringAsRawHex8(description.c_str());
787 response.PutChar(';');
788 } else if ((tid_stop_info.reason == eStopReasonException) &&
789 tid_stop_info.details.exception.type) {
790 response.PutCString("metype:");
791 response.PutHex64(tid_stop_info.details.exception.type);
792 response.PutCString(";mecount:");
793 response.PutHex32(tid_stop_info.details.exception.data_count);
794 response.PutChar(';');
795
796 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) {
797 response.PutCString("medata:");
798 response.PutHex64(tid_stop_info.details.exception.data[i]);
799 response.PutChar(';');
800 }
801 }
802
803 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000804}
805
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited(
807 NativeProcessProtocol *process) {
808 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
811 if (log)
812 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
813
814 PacketResult result = SendStopReasonForState(StateType::eStateExited);
815 if (result != PacketResult::Success) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000816 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
818 "notification for PID %" PRIu64 ", state: eStateExited",
819 __FUNCTION__, process->GetID());
820 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000821
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 // Close the pipe to the inferior terminal i/o if we launched it
823 // and set one up.
824 MaybeCloseInferiorTerminalConnection();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000825
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826 // We are ready to exit the debug monitor.
827 m_exit_now = true;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000828}
829
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped(
831 NativeProcessProtocol *process) {
832 assert(process && "process cannot be NULL");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000833
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
835 if (log)
836 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 // Send the stop reason unless this is the stop after the
839 // launch or attach.
840 switch (m_inferior_prev_state) {
841 case eStateLaunching:
842 case eStateAttaching:
843 // Don't send anything per debugserver behavior.
844 break;
845 default:
846 // In all other cases, send the stop reason.
847 PacketResult result = SendStopReasonForState(StateType::eStateStopped);
848 if (result != PacketResult::Success) {
849 if (log)
850 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to send stop "
851 "notification for PID %" PRIu64 ", state: eStateExited",
852 __FUNCTION__, process->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000853 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 break;
855 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000856}
857
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858void GDBRemoteCommunicationServerLLGS::ProcessStateChanged(
859 NativeProcessProtocol *process, lldb::StateType state) {
860 assert(process && "process cannot be NULL");
861 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
862 if (log) {
863 log->Printf("GDBRemoteCommunicationServerLLGS::%s called with "
864 "NativeProcessProtocol pid %" PRIu64 ", state: %s",
865 __FUNCTION__, process->GetID(), StateAsCString(state));
866 }
867
868 switch (state) {
869 case StateType::eStateRunning:
870 StartSTDIOForwarding();
871 break;
872
873 case StateType::eStateStopped:
874 // Make sure we get all of the pending stdout/stderr from the inferior
875 // and send it to the lldb host before we send the state change
876 // notification
877 SendProcessOutput();
878 // Then stop the forwarding, so that any late output (see llvm.org/pr25652)
879 // does not
880 // interfere with our protocol.
881 StopSTDIOForwarding();
882 HandleInferiorState_Stopped(process);
883 break;
884
885 case StateType::eStateExited:
886 // Same as above
887 SendProcessOutput();
888 StopSTDIOForwarding();
889 HandleInferiorState_Exited(process);
890 break;
891
892 default:
893 if (log) {
894 log->Printf("GDBRemoteCommunicationServerLLGS::%s didn't handle state "
895 "change for pid %" PRIu64 ", new state: %s",
896 __FUNCTION__, process->GetID(), StateAsCString(state));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 break;
899 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 // Remember the previous state reported to us.
902 m_inferior_prev_state = state;
903}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) {
906 ClearProcessSpecificData();
907}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000908
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() {
910 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_COMM));
911
912 if (!m_handshake_completed) {
913 if (!HandshakeWithClient()) {
914 if (log)
915 log->Printf("GDBRemoteCommunicationServerLLGS::%s handshake with "
916 "client failed, exiting",
917 __FUNCTION__);
918 m_mainloop.RequestTermination();
919 return;
920 }
921 m_handshake_completed = true;
922 }
923
924 bool interrupt = false;
925 bool done = false;
Zachary Turner97206d52017-05-12 04:51:55 +0000926 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 while (true) {
Pavel Labath1eff73c2016-11-24 10:54:49 +0000928 const PacketResult result = GetPacketAndSendResponse(
929 std::chrono::microseconds(0), error, interrupt, done);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 if (result == PacketResult::ErrorReplyTimeout)
931 break; // No more packets in the queue
932
933 if ((result != PacketResult::Success)) {
934 if (log)
935 log->Printf("GDBRemoteCommunicationServerLLGS::%s processing a packet "
936 "failed: %s",
937 __FUNCTION__, error.AsCString());
938 m_mainloop.RequestTermination();
939 break;
940 }
941 }
942}
943
Zachary Turner97206d52017-05-12 04:51:55 +0000944Status GDBRemoteCommunicationServerLLGS::InitializeConnection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 std::unique_ptr<Connection> &&connection) {
946 IOObjectSP read_object_sp = connection->GetReadObject();
947 GDBRemoteCommunicationServer::SetConnection(connection.release());
948
Zachary Turner97206d52017-05-12 04:51:55 +0000949 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 m_network_handle_up = m_mainloop.RegisterReadObject(
951 read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); },
952 error);
953 return error;
954}
955
956GDBRemoteCommunication::PacketResult
957GDBRemoteCommunicationServerLLGS::SendONotification(const char *buffer,
958 uint32_t len) {
959 if ((buffer == nullptr) || (len == 0)) {
960 // Nothing to send.
961 return PacketResult::Success;
962 }
963
964 StreamString response;
965 response.PutChar('O');
966 response.PutBytesAsRawHex8(buffer, len);
967
968 return SendPacketNoLock(response.GetString());
969}
970
Zachary Turner97206d52017-05-12 04:51:55 +0000971Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) {
972 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973
974 // Set up the reading/handling of process I/O
975 std::unique_ptr<ConnectionFileDescriptor> conn_up(
976 new ConnectionFileDescriptor(fd, true));
977 if (!conn_up) {
978 error.SetErrorString("failed to create ConnectionFileDescriptor");
979 return error;
980 }
981
982 m_stdio_communication.SetCloseOnEOF(false);
983 m_stdio_communication.SetConnection(conn_up.release());
984 if (!m_stdio_communication.IsConnected()) {
985 error.SetErrorString(
986 "failed to set connection for inferior I/O communication");
987 return error;
988 }
989
Zachary Turner97206d52017-05-12 04:51:55 +0000990 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991}
992
993void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() {
994 // Don't forward if not connected (e.g. when attaching).
995 if (!m_stdio_communication.IsConnected())
996 return;
997
Zachary Turner97206d52017-05-12 04:51:55 +0000998 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 lldbassert(!m_stdio_handle_up);
1000 m_stdio_handle_up = m_mainloop.RegisterReadObject(
1001 m_stdio_communication.GetConnection()->GetReadObject(),
1002 [this](MainLoopBase &) { SendProcessOutput(); }, error);
1003
1004 if (!m_stdio_handle_up) {
1005 // Not much we can do about the failure. Log it and continue without
1006 // forwarding.
1007 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
1008 log->Printf("GDBRemoteCommunicationServerLLGS::%s Failed to set up stdio "
1009 "forwarding: %s",
1010 __FUNCTION__, error.AsCString());
1011 }
1012}
1013
1014void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() {
1015 m_stdio_handle_up.reset();
1016}
1017
1018void GDBRemoteCommunicationServerLLGS::SendProcessOutput() {
1019 char buffer[1024];
1020 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00001021 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022 while (true) {
Pavel Labathc4063ee2016-11-25 11:58:44 +00001023 size_t bytes_read = m_stdio_communication.Read(
1024 buffer, sizeof buffer, std::chrono::microseconds(0), status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 switch (status) {
1026 case eConnectionStatusSuccess:
1027 SendONotification(buffer, bytes_read);
1028 break;
1029 case eConnectionStatusLostConnection:
1030 case eConnectionStatusEndOfFile:
1031 case eConnectionStatusError:
1032 case eConnectionStatusNoConnection:
1033 if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS))
1034 log->Printf("GDBRemoteCommunicationServerLLGS::%s Stopping stdio "
1035 "forwarding as communication returned status %d (error: "
1036 "%s)",
1037 __FUNCTION__, status, error.AsCString());
1038 m_stdio_handle_up.reset();
1039 return;
1040
1041 case eConnectionStatusInterrupted:
1042 case eConnectionStatusTimedOut:
1043 return;
1044 }
1045 }
1046}
1047
1048GDBRemoteCommunication::PacketResult
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001049GDBRemoteCommunicationServerLLGS::Handle_jTraceStart(
1050 StringExtractorGDBRemote &packet) {
1051 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1052 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001053 if (!m_debugged_process_up ||
1054 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001055 return SendErrorResponse(68);
1056
1057 if (!packet.ConsumeFront("jTraceStart:"))
1058 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1059
1060 TraceOptions options;
1061 uint64_t type = std::numeric_limits<uint64_t>::max();
1062 uint64_t buffersize = std::numeric_limits<uint64_t>::max();
1063 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1064 uint64_t metabuffersize = std::numeric_limits<uint64_t>::max();
1065
1066 auto json_object = StructuredData::ParseJSON(packet.Peek());
1067
1068 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001069 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001070 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1071
1072 auto json_dict = json_object->GetAsDictionary();
1073
Pavel Labath4c950232017-05-26 13:53:39 +00001074 json_dict->GetValueForKeyAsInteger("metabuffersize", metabuffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001075 options.setMetaDataBufferSize(metabuffersize);
1076
Pavel Labath4c950232017-05-26 13:53:39 +00001077 json_dict->GetValueForKeyAsInteger("buffersize", buffersize);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001078 options.setTraceBufferSize(buffersize);
1079
Pavel Labath4c950232017-05-26 13:53:39 +00001080 json_dict->GetValueForKeyAsInteger("type", type);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001081 options.setType(static_cast<lldb::TraceType>(type));
1082
Pavel Labath4c950232017-05-26 13:53:39 +00001083 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001084 options.setThreadID(tid);
1085
1086 StructuredData::ObjectSP custom_params_sp =
1087 json_dict->GetValueForKey("params");
1088 if (custom_params_sp &&
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001089 custom_params_sp->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001090 return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet ");
1091
1092 options.setTraceParams(
1093 static_pointer_cast<StructuredData::Dictionary>(custom_params_sp));
1094
1095 if (buffersize == std::numeric_limits<uint64_t>::max() ||
1096 type != lldb::TraceType::eTraceTypeProcessorTrace) {
1097 LLDB_LOG(log, "Ill formed packet buffersize = {0} type = {1}", buffersize,
1098 type);
1099 return SendIllFormedResponse(packet, "JTrace:start: Ill formed packet ");
1100 }
1101
1102 Status error;
1103 lldb::user_id_t uid = LLDB_INVALID_UID;
Pavel Labath82abefa2017-07-18 09:24:48 +00001104 uid = m_debugged_process_up->StartTrace(options, error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001105 LLDB_LOG(log, "uid is {0} , error is {1}", uid, error.GetError());
1106 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001107 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001108
1109 StreamGDBRemote response;
1110 response.Printf("%" PRIx64, uid);
1111 return SendPacketNoLock(response.GetString());
1112}
1113
1114GDBRemoteCommunication::PacketResult
1115GDBRemoteCommunicationServerLLGS::Handle_jTraceStop(
1116 StringExtractorGDBRemote &packet) {
1117 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001118 if (!m_debugged_process_up ||
1119 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001120 return SendErrorResponse(68);
1121
1122 if (!packet.ConsumeFront("jTraceStop:"))
1123 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1124
1125 lldb::user_id_t uid = LLDB_INVALID_UID;
1126 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
1127
1128 auto json_object = StructuredData::ParseJSON(packet.Peek());
1129
1130 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001131 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001132 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1133
1134 auto json_dict = json_object->GetAsDictionary();
1135
Pavel Labath4c950232017-05-26 13:53:39 +00001136 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001137 return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet ");
1138
Pavel Labath4c950232017-05-26 13:53:39 +00001139 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001140
Pavel Labath82abefa2017-07-18 09:24:48 +00001141 Status error = m_debugged_process_up->StopTrace(uid, tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001142
1143 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001144 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001145
1146 return SendOKResponse();
1147}
1148
1149GDBRemoteCommunication::PacketResult
1150GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead(
1151 StringExtractorGDBRemote &packet) {
1152
1153 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001154 if (!m_debugged_process_up ||
1155 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001156 return SendErrorResponse(68);
1157
1158 if (!packet.ConsumeFront("jTraceConfigRead:"))
1159 return SendIllFormedResponse(packet,
1160 "jTraceConfigRead: Ill formed packet ");
1161
1162 lldb::user_id_t uid = LLDB_INVALID_UID;
1163 lldb::tid_t threadid = LLDB_INVALID_THREAD_ID;
1164
1165 auto json_object = StructuredData::ParseJSON(packet.Peek());
1166
1167 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001168 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001169 return SendIllFormedResponse(packet,
1170 "jTraceConfigRead: Ill formed packet ");
1171
1172 auto json_dict = json_object->GetAsDictionary();
1173
Pavel Labath4c950232017-05-26 13:53:39 +00001174 if (!json_dict->GetValueForKeyAsInteger("traceid", uid))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001175 return SendIllFormedResponse(packet,
1176 "jTraceConfigRead: Ill formed packet ");
1177
Pavel Labath4c950232017-05-26 13:53:39 +00001178 json_dict->GetValueForKeyAsInteger("threadid", threadid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001179
1180 TraceOptions options;
1181 StreamGDBRemote response;
1182
1183 options.setThreadID(threadid);
Pavel Labath82abefa2017-07-18 09:24:48 +00001184 Status error = m_debugged_process_up->GetTraceConfig(uid, options);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001185
1186 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001187 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001188
1189 StreamGDBRemote escaped_response;
1190 StructuredData::Dictionary json_packet;
1191
1192 json_packet.AddIntegerItem("type", options.getType());
1193 json_packet.AddIntegerItem("buffersize", options.getTraceBufferSize());
1194 json_packet.AddIntegerItem("metabuffersize", options.getMetaDataBufferSize());
1195
1196 StructuredData::DictionarySP custom_params = options.getTraceParams();
1197 if (custom_params)
1198 json_packet.AddItem("params", custom_params);
1199
1200 StreamString json_string;
1201 json_packet.Dump(json_string, false);
1202 escaped_response.PutEscapedBytes(json_string.GetData(),
1203 json_string.GetSize());
1204 return SendPacketNoLock(escaped_response.GetString());
1205}
1206
1207GDBRemoteCommunication::PacketResult
1208GDBRemoteCommunicationServerLLGS::Handle_jTraceRead(
1209 StringExtractorGDBRemote &packet) {
1210
1211 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001212 if (!m_debugged_process_up ||
1213 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001214 return SendErrorResponse(68);
1215
1216 enum PacketType { MetaData, BufferData };
1217 PacketType tracetype = MetaData;
1218
1219 if (packet.ConsumeFront("jTraceBufferRead:"))
1220 tracetype = BufferData;
1221 else if (packet.ConsumeFront("jTraceMetaRead:"))
1222 tracetype = MetaData;
1223 else {
1224 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1225 }
1226
1227 lldb::user_id_t uid = LLDB_INVALID_UID;
1228
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001229 uint64_t byte_count = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001230 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001231 uint64_t offset = std::numeric_limits<uint64_t>::max();
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001232
1233 auto json_object = StructuredData::ParseJSON(packet.Peek());
1234
1235 if (!json_object ||
Stephan Bergmanncf4321a2017-05-29 08:51:58 +00001236 json_object->GetType() != lldb::eStructuredDataTypeDictionary)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001237 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1238
1239 auto json_dict = json_object->GetAsDictionary();
1240
Pavel Labath4c950232017-05-26 13:53:39 +00001241 if (!json_dict->GetValueForKeyAsInteger("traceid", uid) ||
1242 !json_dict->GetValueForKeyAsInteger("offset", offset) ||
1243 !json_dict->GetValueForKeyAsInteger("buffersize", byte_count))
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001244 return SendIllFormedResponse(packet, "jTrace: Ill formed packet ");
1245
Pavel Labath4c950232017-05-26 13:53:39 +00001246 json_dict->GetValueForKeyAsInteger("threadid", tid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001247
1248 // Allocate the response buffer.
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001249 std::unique_ptr<uint8_t[]> buffer (new (std::nothrow) uint8_t[byte_count]);
1250 if (!buffer)
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001251 return SendErrorResponse(0x78);
1252
1253 StreamGDBRemote response;
1254 Status error;
Ravitheja Addepally0e62a122017-05-26 14:26:14 +00001255 llvm::MutableArrayRef<uint8_t> buf(buffer.get(), byte_count);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001256
1257 if (tracetype == BufferData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001258 error = m_debugged_process_up->GetData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001259 else if (tracetype == MetaData)
Pavel Labath82abefa2017-07-18 09:24:48 +00001260 error = m_debugged_process_up->GetMetaData(uid, tid, buf, offset);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001261
1262 if (error.Fail())
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +00001263 return SendErrorResponse(error);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001264
Ravitheja Addepally99e37692017-06-28 07:58:31 +00001265 for (auto i : buf)
1266 response.PutHex8(i);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +00001267
1268 StreamGDBRemote escaped_response;
1269 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
1270 return SendPacketNoLock(escaped_response.GetString());
1271}
1272
1273GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo(
1275 StringExtractorGDBRemote &packet) {
1276 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001277 if (!m_debugged_process_up ||
1278 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 return SendErrorResponse(68);
1280
Pavel Labath82abefa2017-07-18 09:24:48 +00001281 lldb::pid_t pid = m_debugged_process_up->GetID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282
1283 if (pid == LLDB_INVALID_PROCESS_ID)
1284 return SendErrorResponse(1);
1285
1286 ProcessInstanceInfo proc_info;
1287 if (!Host::GetProcessInfo(pid, proc_info))
1288 return SendErrorResponse(1);
1289
1290 StreamString response;
1291 CreateProcessInfoResponse_DebugServerStyle(proc_info, response);
1292 return SendPacketNoLock(response.GetString());
1293}
1294
1295GDBRemoteCommunication::PacketResult
1296GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) {
1297 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001298 if (!m_debugged_process_up ||
1299 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 return SendErrorResponse(68);
1301
1302 // Make sure we set the current thread so g and p packets return
1303 // the data the gdb will expect.
Pavel Labath82abefa2017-07-18 09:24:48 +00001304 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305 SetCurrentThreadID(tid);
1306
Pavel Labatha5be48b2017-10-17 15:52:16 +00001307 NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread();
1308 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 return SendErrorResponse(69);
1310
1311 StreamString response;
Pavel Labatha5be48b2017-10-17 15:52:16 +00001312 response.Printf("QC%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313
1314 return SendPacketNoLock(response.GetString());
1315}
1316
1317GDBRemoteCommunication::PacketResult
1318GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) {
1319 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1320
1321 StopSTDIOForwarding();
1322
Pavel Labath82abefa2017-07-18 09:24:48 +00001323 if (!m_debugged_process_up) {
1324 LLDB_LOG(log, "No debugged process found.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 return PacketResult::Success;
1326 }
1327
Pavel Labath82abefa2017-07-18 09:24:48 +00001328 Status error = m_debugged_process_up->Kill();
1329 if (error.Fail())
1330 LLDB_LOG(log, "Failed to kill debugged process {0}: {1}",
1331 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332
1333 // No OK response for kill packet.
1334 // return SendOKResponse ();
1335 return PacketResult::Success;
1336}
1337
1338GDBRemoteCommunication::PacketResult
1339GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR(
1340 StringExtractorGDBRemote &packet) {
1341 packet.SetFilePos(::strlen("QSetDisableASLR:"));
1342 if (packet.GetU32(0))
1343 m_process_launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
1344 else
1345 m_process_launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
1346 return SendOKResponse();
1347}
1348
1349GDBRemoteCommunication::PacketResult
1350GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir(
1351 StringExtractorGDBRemote &packet) {
1352 packet.SetFilePos(::strlen("QSetWorkingDir:"));
1353 std::string path;
1354 packet.GetHexByteString(path);
1355 m_process_launch_info.SetWorkingDirectory(FileSpec{path, true});
1356 return SendOKResponse();
1357}
1358
1359GDBRemoteCommunication::PacketResult
1360GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir(
1361 StringExtractorGDBRemote &packet) {
1362 FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()};
1363 if (working_dir) {
1364 StreamString response;
1365 response.PutCStringAsRawHex8(working_dir.GetCString());
1366 return SendPacketNoLock(response.GetString());
1367 }
1368
1369 return SendErrorResponse(14);
1370}
1371
1372GDBRemoteCommunication::PacketResult
1373GDBRemoteCommunicationServerLLGS::Handle_C(StringExtractorGDBRemote &packet) {
1374 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1375 if (log)
1376 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1377
1378 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001379 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 if (log)
1381 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1382 "shared pointer",
1383 __FUNCTION__);
1384 return SendErrorResponse(0x36);
1385 }
1386
1387 // Pull out the signal number.
1388 packet.SetFilePos(::strlen("C"));
1389 if (packet.GetBytesLeft() < 1) {
1390 // Shouldn't be using a C without a signal.
1391 return SendIllFormedResponse(packet, "C packet specified without signal.");
1392 }
1393 const uint32_t signo =
1394 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1395 if (signo == std::numeric_limits<uint32_t>::max())
1396 return SendIllFormedResponse(packet, "failed to parse signal number");
1397
1398 // Handle optional continue address.
1399 if (packet.GetBytesLeft() > 0) {
1400 // FIXME add continue at address support for $C{signo}[;{continue-address}].
1401 if (*packet.Peek() == ';')
1402 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1403 else
1404 return SendIllFormedResponse(
1405 packet, "unexpected content after $C{signal-number}");
1406 }
1407
1408 ResumeActionList resume_actions(StateType::eStateRunning, 0);
Zachary Turner97206d52017-05-12 04:51:55 +00001409 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410
1411 // We have two branches: what to do if a continue thread is specified (in
1412 // which case we target
1413 // sending the signal to that thread), or when we don't have a continue thread
1414 // set (in which
1415 // case we send a signal to the process).
1416
1417 // TODO discuss with Greg Clayton, make sure this makes sense.
1418
1419 lldb::tid_t signal_tid = GetContinueThreadID();
1420 if (signal_tid != LLDB_INVALID_THREAD_ID) {
1421 // The resume action for the continue thread (or all threads if a continue
1422 // thread is not set).
1423 ResumeAction action = {GetContinueThreadID(), StateType::eStateRunning,
1424 static_cast<int>(signo)};
1425
1426 // Add the action for the continue thread (or all threads when the continue
1427 // thread isn't present).
1428 resume_actions.Append(action);
1429 } else {
1430 // Send the signal to the process since we weren't targeting a specific
1431 // continue thread with the signal.
Pavel Labath82abefa2017-07-18 09:24:48 +00001432 error = m_debugged_process_up->Signal(signo);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001434 LLDB_LOG(log, "failed to send signal for process {0}: {1}",
1435 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436
1437 return SendErrorResponse(0x52);
1438 }
1439 }
1440
1441 // Resume the threads.
Pavel Labath82abefa2017-07-18 09:24:48 +00001442 error = m_debugged_process_up->Resume(resume_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001444 LLDB_LOG(log, "failed to resume threads for process {0}: {1}",
1445 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446
1447 return SendErrorResponse(0x38);
1448 }
1449
1450 // Don't send an "OK" packet; response is the stopped/exited message.
1451 return PacketResult::Success;
1452}
1453
1454GDBRemoteCommunication::PacketResult
1455GDBRemoteCommunicationServerLLGS::Handle_c(StringExtractorGDBRemote &packet) {
1456 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1457 if (log)
1458 log->Printf("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1459
1460 packet.SetFilePos(packet.GetFilePos() + ::strlen("c"));
1461
1462 // For now just support all continue.
1463 const bool has_continue_address = (packet.GetBytesLeft() > 0);
1464 if (has_continue_address) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001465 LLDB_LOG(log, "not implemented for c[address] variant [{0} remains]",
1466 packet.Peek());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467 return SendUnimplementedResponse(packet.GetStringRef().c_str());
1468 }
1469
1470 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001471 if (!m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472 if (log)
1473 log->Printf("GDBRemoteCommunicationServerLLGS::%s no debugged process "
1474 "shared pointer",
1475 __FUNCTION__);
1476 return SendErrorResponse(0x36);
1477 }
1478
1479 // Build the ResumeActionList
1480 ResumeActionList actions(StateType::eStateRunning, 0);
1481
Pavel Labath82abefa2017-07-18 09:24:48 +00001482 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001484 LLDB_LOG(log, "c failed for process {0}: {1}",
1485 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1487 }
1488
Pavel Labath82abefa2017-07-18 09:24:48 +00001489 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 // No response required from continue.
1491 return PacketResult::Success;
1492}
1493
1494GDBRemoteCommunication::PacketResult
1495GDBRemoteCommunicationServerLLGS::Handle_vCont_actions(
1496 StringExtractorGDBRemote &packet) {
1497 StreamString response;
1498 response.Printf("vCont;c;C;s;S");
1499
1500 return SendPacketNoLock(response.GetString());
1501}
1502
1503GDBRemoteCommunication::PacketResult
1504GDBRemoteCommunicationServerLLGS::Handle_vCont(
1505 StringExtractorGDBRemote &packet) {
1506 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1507 if (log)
1508 log->Printf("GDBRemoteCommunicationServerLLGS::%s handling vCont packet",
1509 __FUNCTION__);
1510
1511 packet.SetFilePos(::strlen("vCont"));
1512
1513 if (packet.GetBytesLeft() == 0) {
1514 if (log)
1515 log->Printf("GDBRemoteCommunicationServerLLGS::%s missing action from "
1516 "vCont package",
1517 __FUNCTION__);
1518 return SendIllFormedResponse(packet, "Missing action from vCont package");
1519 }
1520
1521 // Check if this is all continue (no options or ";c").
1522 if (::strcmp(packet.Peek(), ";c") == 0) {
1523 // Move past the ';', then do a simple 'c'.
1524 packet.SetFilePos(packet.GetFilePos() + 1);
1525 return Handle_c(packet);
1526 } else if (::strcmp(packet.Peek(), ";s") == 0) {
1527 // Move past the ';', then do a simple 's'.
1528 packet.SetFilePos(packet.GetFilePos() + 1);
1529 return Handle_s(packet);
1530 }
1531
1532 // Ensure we have a native process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001533 if (!m_debugged_process_up) {
1534 LLDB_LOG(log, "no debugged process");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 return SendErrorResponse(0x36);
1536 }
1537
1538 ResumeActionList thread_actions;
1539
1540 while (packet.GetBytesLeft() && *packet.Peek() == ';') {
1541 // Skip the semi-colon.
1542 packet.GetChar();
1543
1544 // Build up the thread action.
1545 ResumeAction thread_action;
1546 thread_action.tid = LLDB_INVALID_THREAD_ID;
1547 thread_action.state = eStateInvalid;
1548 thread_action.signal = 0;
1549
1550 const char action = packet.GetChar();
1551 switch (action) {
1552 case 'C':
1553 thread_action.signal = packet.GetHexMaxU32(false, 0);
1554 if (thread_action.signal == 0)
1555 return SendIllFormedResponse(
1556 packet, "Could not parse signal in vCont packet C action");
1557 LLVM_FALLTHROUGH;
1558
1559 case 'c':
1560 // Continue
1561 thread_action.state = eStateRunning;
1562 break;
1563
1564 case 'S':
1565 thread_action.signal = packet.GetHexMaxU32(false, 0);
1566 if (thread_action.signal == 0)
1567 return SendIllFormedResponse(
1568 packet, "Could not parse signal in vCont packet S action");
1569 LLVM_FALLTHROUGH;
1570
1571 case 's':
1572 // Step
1573 thread_action.state = eStateStepping;
1574 break;
Pavel Labathabadc222015-11-27 13:33:29 +00001575
Tamas Berghammere13c2732015-02-11 10:29:30 +00001576 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 return SendIllFormedResponse(packet, "Unsupported vCont action");
1578 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00001579 }
1580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 // Parse out optional :{thread-id} value.
1582 if (packet.GetBytesLeft() && (*packet.Peek() == ':')) {
1583 // Consume the separator.
1584 packet.GetChar();
Tamas Berghammere13c2732015-02-11 10:29:30 +00001585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 thread_action.tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1587 if (thread_action.tid == LLDB_INVALID_THREAD_ID)
1588 return SendIllFormedResponse(
1589 packet, "Could not parse thread number in vCont packet");
Pavel Labath77dc9562015-07-13 10:44:55 +00001590 }
1591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 thread_actions.Append(thread_action);
1593 }
Pavel Labath77dc9562015-07-13 10:44:55 +00001594
Pavel Labath82abefa2017-07-18 09:24:48 +00001595 Status error = m_debugged_process_up->Resume(thread_actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00001597 LLDB_LOG(log, "vCont failed for process {0}: {1}",
1598 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
1600 }
1601
Pavel Labath82abefa2017-07-18 09:24:48 +00001602 LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 // No response required from vCont.
1604 return PacketResult::Success;
Pavel Labath77dc9562015-07-13 10:44:55 +00001605}
1606
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) {
1608 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001609 LLDB_LOG(log, "setting current thread id to {0}", tid);
Pavel Labath77dc9562015-07-13 10:44:55 +00001610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611 m_current_tid = tid;
Pavel Labath82abefa2017-07-18 09:24:48 +00001612 if (m_debugged_process_up)
1613 m_debugged_process_up->SetCurrentThreadID(m_current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614}
1615
1616void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) {
1617 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Pavel Labath82abefa2017-07-18 09:24:48 +00001618 LLDB_LOG(log, "setting continue thread id to {0}", tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619
1620 m_continue_tid = tid;
Pavel Labath77dc9562015-07-13 10:44:55 +00001621}
1622
Tamas Berghammere13c2732015-02-11 10:29:30 +00001623GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624GDBRemoteCommunicationServerLLGS::Handle_stop_reason(
1625 StringExtractorGDBRemote &packet) {
1626 // Handle the $? gdbremote command.
Tamas Berghammere13c2732015-02-11 10:29:30 +00001627
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 // If no process, indicate error
Pavel Labath82abefa2017-07-18 09:24:48 +00001629 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 return SendErrorResponse(02);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001631
Pavel Labath82abefa2017-07-18 09:24:48 +00001632 return SendStopReasonForState(m_debugged_process_up->GetState());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001633}
1634
1635GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636GDBRemoteCommunicationServerLLGS::SendStopReasonForState(
1637 lldb::StateType process_state) {
1638 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00001639
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 switch (process_state) {
1641 case eStateAttaching:
1642 case eStateLaunching:
1643 case eStateRunning:
1644 case eStateStepping:
1645 case eStateDetached:
1646 // NOTE: gdb protocol doc looks like it should return $OK
1647 // when everything is running (i.e. no stopped result).
1648 return PacketResult::Success; // Ignore
Tamas Berghammere13c2732015-02-11 10:29:30 +00001649
Kate Stoneb9c1b512016-09-06 20:57:50 +00001650 case eStateSuspended:
1651 case eStateStopped:
1652 case eStateCrashed: {
Pavel Labath82abefa2017-07-18 09:24:48 +00001653 lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
Tamas Berghammere13c2732015-02-11 10:29:30 +00001654 // Make sure we set the current thread so g and p packets return
1655 // the data the gdb will expect.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 SetCurrentThreadID(tid);
1657 return SendStopReplyPacketForThread(tid);
1658 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 case eStateInvalid:
1661 case eStateUnloaded:
1662 case eStateExited:
Pavel Labath82abefa2017-07-18 09:24:48 +00001663 return SendWResponse(m_debugged_process_up.get());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 default:
Pavel Labath82abefa2017-07-18 09:24:48 +00001666 LLDB_LOG(log, "pid {0}, current state reporting not handled: {1}",
1667 m_debugged_process_up->GetID(), process_state);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668 break;
1669 }
Pavel Labath424b2012015-07-28 09:06:56 +00001670
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 return SendErrorResponse(0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001672}
1673
1674GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo(
1676 StringExtractorGDBRemote &packet) {
1677 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001678 if (!m_debugged_process_up ||
1679 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001681
Kate Stoneb9c1b512016-09-06 20:57:50 +00001682 // Ensure we have a thread.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001683 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0);
1684 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 return SendErrorResponse(69);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 // Get the register context for the first thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00001688 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689
1690 // Parse out the register number from the request.
1691 packet.SetFilePos(strlen("qRegisterInfo"));
1692 const uint32_t reg_index =
1693 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1694 if (reg_index == std::numeric_limits<uint32_t>::max())
1695 return SendErrorResponse(69);
1696
1697 // Return the end of registers response if we've iterated one past the end of
1698 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001699 if (reg_index >= reg_context.GetUserRegisterCount())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700 return SendErrorResponse(69);
1701
Pavel Labathd37349f2017-11-10 11:05:49 +00001702 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703 if (!reg_info)
1704 return SendErrorResponse(69);
1705
1706 // Build the reginfos response.
1707 StreamGDBRemote response;
1708
1709 response.PutCString("name:");
1710 response.PutCString(reg_info->name);
1711 response.PutChar(';');
1712
1713 if (reg_info->alt_name && reg_info->alt_name[0]) {
1714 response.PutCString("alt-name:");
1715 response.PutCString(reg_info->alt_name);
1716 response.PutChar(';');
1717 }
1718
1719 response.Printf("bitsize:%" PRIu32 ";offset:%" PRIu32 ";",
1720 reg_info->byte_size * 8, reg_info->byte_offset);
1721
1722 switch (reg_info->encoding) {
1723 case eEncodingUint:
1724 response.PutCString("encoding:uint;");
1725 break;
1726 case eEncodingSint:
1727 response.PutCString("encoding:sint;");
1728 break;
1729 case eEncodingIEEE754:
1730 response.PutCString("encoding:ieee754;");
1731 break;
1732 case eEncodingVector:
1733 response.PutCString("encoding:vector;");
1734 break;
1735 default:
1736 break;
1737 }
1738
1739 switch (reg_info->format) {
1740 case eFormatBinary:
1741 response.PutCString("format:binary;");
1742 break;
1743 case eFormatDecimal:
1744 response.PutCString("format:decimal;");
1745 break;
1746 case eFormatHex:
1747 response.PutCString("format:hex;");
1748 break;
1749 case eFormatFloat:
1750 response.PutCString("format:float;");
1751 break;
1752 case eFormatVectorOfSInt8:
1753 response.PutCString("format:vector-sint8;");
1754 break;
1755 case eFormatVectorOfUInt8:
1756 response.PutCString("format:vector-uint8;");
1757 break;
1758 case eFormatVectorOfSInt16:
1759 response.PutCString("format:vector-sint16;");
1760 break;
1761 case eFormatVectorOfUInt16:
1762 response.PutCString("format:vector-uint16;");
1763 break;
1764 case eFormatVectorOfSInt32:
1765 response.PutCString("format:vector-sint32;");
1766 break;
1767 case eFormatVectorOfUInt32:
1768 response.PutCString("format:vector-uint32;");
1769 break;
1770 case eFormatVectorOfFloat32:
1771 response.PutCString("format:vector-float32;");
1772 break;
Valentina Giusticda0ae42016-09-08 14:16:45 +00001773 case eFormatVectorOfUInt64:
1774 response.PutCString("format:vector-uint64;");
1775 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 case eFormatVectorOfUInt128:
1777 response.PutCString("format:vector-uint128;");
1778 break;
1779 default:
1780 break;
1781 };
1782
1783 const char *const register_set_name =
Pavel Labathd37349f2017-11-10 11:05:49 +00001784 reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 if (register_set_name) {
1786 response.PutCString("set:");
1787 response.PutCString(register_set_name);
1788 response.PutChar(';');
1789 }
1790
1791 if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] !=
1792 LLDB_INVALID_REGNUM)
1793 response.Printf("ehframe:%" PRIu32 ";",
1794 reg_info->kinds[RegisterKind::eRegisterKindEHFrame]);
1795
1796 if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
1797 response.Printf("dwarf:%" PRIu32 ";",
1798 reg_info->kinds[RegisterKind::eRegisterKindDWARF]);
1799
1800 switch (reg_info->kinds[RegisterKind::eRegisterKindGeneric]) {
1801 case LLDB_REGNUM_GENERIC_PC:
1802 response.PutCString("generic:pc;");
1803 break;
1804 case LLDB_REGNUM_GENERIC_SP:
1805 response.PutCString("generic:sp;");
1806 break;
1807 case LLDB_REGNUM_GENERIC_FP:
1808 response.PutCString("generic:fp;");
1809 break;
1810 case LLDB_REGNUM_GENERIC_RA:
1811 response.PutCString("generic:ra;");
1812 break;
1813 case LLDB_REGNUM_GENERIC_FLAGS:
1814 response.PutCString("generic:flags;");
1815 break;
1816 case LLDB_REGNUM_GENERIC_ARG1:
1817 response.PutCString("generic:arg1;");
1818 break;
1819 case LLDB_REGNUM_GENERIC_ARG2:
1820 response.PutCString("generic:arg2;");
1821 break;
1822 case LLDB_REGNUM_GENERIC_ARG3:
1823 response.PutCString("generic:arg3;");
1824 break;
1825 case LLDB_REGNUM_GENERIC_ARG4:
1826 response.PutCString("generic:arg4;");
1827 break;
1828 case LLDB_REGNUM_GENERIC_ARG5:
1829 response.PutCString("generic:arg5;");
1830 break;
1831 case LLDB_REGNUM_GENERIC_ARG6:
1832 response.PutCString("generic:arg6;");
1833 break;
1834 case LLDB_REGNUM_GENERIC_ARG7:
1835 response.PutCString("generic:arg7;");
1836 break;
1837 case LLDB_REGNUM_GENERIC_ARG8:
1838 response.PutCString("generic:arg8;");
1839 break;
1840 default:
1841 break;
1842 }
1843
1844 if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) {
1845 response.PutCString("container-regs:");
1846 int i = 0;
1847 for (const uint32_t *reg_num = reg_info->value_regs;
1848 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1849 if (i > 0)
1850 response.PutChar(',');
1851 response.Printf("%" PRIx32, *reg_num);
Tamas Berghammere13c2732015-02-11 10:29:30 +00001852 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853 response.PutChar(';');
1854 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001855
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) {
1857 response.PutCString("invalidate-regs:");
1858 int i = 0;
1859 for (const uint32_t *reg_num = reg_info->invalidate_regs;
1860 *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) {
1861 if (i > 0)
1862 response.PutChar(',');
1863 response.Printf("%" PRIx32, *reg_num);
1864 }
1865 response.PutChar(';');
1866 }
1867
1868 if (reg_info->dynamic_size_dwarf_expr_bytes) {
1869 const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len;
1870 response.PutCString("dynamic_size_dwarf_expr_bytes:");
1871 for (uint32_t i = 0; i < dwarf_opcode_len; ++i)
1872 response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]);
1873 response.PutChar(';');
1874 }
1875 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001876}
1877
1878GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo(
1880 StringExtractorGDBRemote &packet) {
1881 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1882
1883 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00001884 if (!m_debugged_process_up ||
1885 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
1886 LLDB_LOG(log, "no process ({0}), returning OK",
1887 m_debugged_process_up ? "invalid process id"
1888 : "null m_debugged_process_up");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001889 return SendOKResponse();
1890 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00001891
Kate Stoneb9c1b512016-09-06 20:57:50 +00001892 StreamGDBRemote response;
1893 response.PutChar('m');
1894
Pavel Labath82abefa2017-07-18 09:24:48 +00001895 LLDB_LOG(log, "starting thread iteration");
Pavel Labatha5be48b2017-10-17 15:52:16 +00001896 NativeThreadProtocol *thread;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897 uint32_t thread_index;
1898 for (thread_index = 0,
Pavel Labatha5be48b2017-10-17 15:52:16 +00001899 thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
1900 thread; ++thread_index,
1901 thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
1902 LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index,
1903 thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001904 if (thread_index > 0)
1905 response.PutChar(',');
Pavel Labatha5be48b2017-10-17 15:52:16 +00001906 response.Printf("%" PRIx64, thread->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001907 }
1908
Pavel Labath82abefa2017-07-18 09:24:48 +00001909 LLDB_LOG(log, "finished thread iteration");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 return SendPacketNoLock(response.GetString());
1911}
1912
1913GDBRemoteCommunication::PacketResult
1914GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo(
1915 StringExtractorGDBRemote &packet) {
1916 // FIXME for now we return the full thread list in the initial packet and
1917 // always do nothing here.
1918 return SendPacketNoLock("l");
1919}
1920
1921GDBRemoteCommunication::PacketResult
1922GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) {
1923 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
1924
1925 // Parse out the register number from the request.
1926 packet.SetFilePos(strlen("p"));
1927 const uint32_t reg_index =
1928 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
1929 if (reg_index == std::numeric_limits<uint32_t>::max()) {
1930 if (log)
1931 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
1932 "parse register number from request \"%s\"",
1933 __FUNCTION__, packet.GetStringRef().c_str());
1934 return SendErrorResponse(0x15);
1935 }
1936
1937 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00001938 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
1939 if (!thread) {
1940 LLDB_LOG(log, "failed, no thread available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941 return SendErrorResponse(0x15);
1942 }
1943
1944 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00001945 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946
1947 // Return the end of registers response if we've iterated one past the end of
1948 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00001949 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950 if (log)
1951 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1952 "register %" PRIu32 " beyond register count %" PRIu32,
1953 __FUNCTION__, reg_index,
Pavel Labathd37349f2017-11-10 11:05:49 +00001954 reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 return SendErrorResponse(0x15);
1956 }
1957
Pavel Labathd37349f2017-11-10 11:05:49 +00001958 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001959 if (!reg_info) {
1960 if (log)
1961 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
1962 "register %" PRIu32 " returned NULL",
1963 __FUNCTION__, reg_index);
1964 return SendErrorResponse(0x15);
1965 }
1966
1967 // Build the reginfos response.
1968 StreamGDBRemote response;
1969
1970 // Retrieve the value
1971 RegisterValue reg_value;
Pavel Labathd37349f2017-11-10 11:05:49 +00001972 Status error = reg_context.ReadRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001973 if (error.Fail()) {
1974 if (log)
1975 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, read of "
1976 "requested register %" PRIu32 " (%s) failed: %s",
1977 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
1978 return SendErrorResponse(0x15);
1979 }
1980
1981 const uint8_t *const data =
1982 reinterpret_cast<const uint8_t *>(reg_value.GetBytes());
1983 if (!data) {
1984 if (log)
1985 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to get data "
1986 "bytes from requested register %" PRIu32,
1987 __FUNCTION__, reg_index);
1988 return SendErrorResponse(0x15);
1989 }
1990
1991 // FIXME flip as needed to get data in big/little endian format for this host.
1992 for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i)
1993 response.PutHex8(data[i]);
1994
1995 return SendPacketNoLock(response.GetString());
1996}
1997
1998GDBRemoteCommunication::PacketResult
1999GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) {
2000 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2001
2002 // Ensure there is more content.
2003 if (packet.GetBytesLeft() < 1)
2004 return SendIllFormedResponse(packet, "Empty P packet");
2005
2006 // Parse out the register number from the request.
2007 packet.SetFilePos(strlen("P"));
2008 const uint32_t reg_index =
2009 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
2010 if (reg_index == std::numeric_limits<uint32_t>::max()) {
2011 if (log)
2012 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
2013 "parse register number from request \"%s\"",
2014 __FUNCTION__, packet.GetStringRef().c_str());
2015 return SendErrorResponse(0x29);
2016 }
2017
2018 // Note debugserver would send an E30 here.
2019 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '='))
2020 return SendIllFormedResponse(
2021 packet, "P packet missing '=' char after register number");
2022
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 // Parse out the value.
2024 uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register
2025 size_t reg_size = packet.GetHexBytesAvail(reg_bytes);
2026
2027 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002028 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2029 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030 if (log)
2031 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread "
2032 "available (thread index 0)",
2033 __FUNCTION__);
2034 return SendErrorResponse(0x28);
2035 }
2036
2037 // Get the thread's register context.
Pavel Labathd37349f2017-11-10 11:05:49 +00002038 NativeRegisterContext &reg_context = thread->GetRegisterContext();
2039 const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040 if (!reg_info) {
2041 if (log)
2042 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2043 "register %" PRIu32 " returned NULL",
2044 __FUNCTION__, reg_index);
2045 return SendErrorResponse(0x48);
2046 }
2047
2048 // Return the end of registers response if we've iterated one past the end of
2049 // the register set.
Pavel Labathd37349f2017-11-10 11:05:49 +00002050 if (reg_index >= reg_context.GetUserRegisterCount()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051 if (log)
2052 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, requested "
2053 "register %" PRIu32 " beyond register count %" PRIu32,
Pavel Labathd37349f2017-11-10 11:05:49 +00002054 __FUNCTION__, reg_index, reg_context.GetUserRegisterCount());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055 return SendErrorResponse(0x47);
2056 }
2057
2058 // The dwarf expression are evaluate on host site
2059 // which may cause register size to change
2060 // Hence the reg_size may not be same as reg_info->bytes_size
2061 if ((reg_size != reg_info->byte_size) &&
2062 !(reg_info->dynamic_size_dwarf_expr_bytes)) {
2063 return SendIllFormedResponse(packet, "P packet register size is incorrect");
2064 }
2065
2066 // Build the reginfos response.
2067 StreamGDBRemote response;
2068
Pavel Labath578a4252017-11-09 10:43:16 +00002069 RegisterValue reg_value(
2070 reg_bytes, reg_size,
2071 m_debugged_process_up->GetArchitecture().GetByteOrder());
Pavel Labathd37349f2017-11-10 11:05:49 +00002072 Status error = reg_context.WriteRegister(reg_info, reg_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002073 if (error.Fail()) {
2074 if (log)
2075 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, write of "
2076 "requested register %" PRIu32 " (%s) failed: %s",
2077 __FUNCTION__, reg_index, reg_info->name, error.AsCString());
2078 return SendErrorResponse(0x32);
2079 }
2080
2081 return SendOKResponse();
2082}
2083
2084GDBRemoteCommunication::PacketResult
2085GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) {
2086 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2087
2088 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002089 if (!m_debugged_process_up ||
2090 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 if (log)
2092 log->Printf(
2093 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2094 __FUNCTION__);
2095 return SendErrorResponse(0x15);
2096 }
2097
2098 // Parse out which variant of $H is requested.
2099 packet.SetFilePos(strlen("H"));
2100 if (packet.GetBytesLeft() < 1) {
2101 if (log)
2102 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, H command "
2103 "missing {g,c} variant",
2104 __FUNCTION__);
2105 return SendIllFormedResponse(packet, "H command missing {g,c} variant");
2106 }
2107
2108 const char h_variant = packet.GetChar();
2109 switch (h_variant) {
2110 case 'g':
2111 break;
2112
2113 case 'c':
2114 break;
2115
2116 default:
2117 if (log)
2118 log->Printf(
2119 "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c",
2120 __FUNCTION__, h_variant);
2121 return SendIllFormedResponse(packet,
2122 "H variant unsupported, should be c or g");
2123 }
2124
2125 // Parse out the thread number.
2126 // FIXME return a parse success/fail value. All values are valid here.
2127 const lldb::tid_t tid =
2128 packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max());
2129
2130 // Ensure we have the given thread when not specifying -1 (all threads) or 0
2131 // (any thread).
2132 if (tid != LLDB_INVALID_THREAD_ID && tid != 0) {
Pavel Labatha5be48b2017-10-17 15:52:16 +00002133 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2134 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002135 if (log)
2136 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
2137 " not found",
2138 __FUNCTION__, tid);
2139 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002140 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002142
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143 // Now switch the given thread type.
2144 switch (h_variant) {
2145 case 'g':
2146 SetCurrentThreadID(tid);
2147 break;
2148
2149 case 'c':
2150 SetContinueThreadID(tid);
2151 break;
2152
2153 default:
2154 assert(false && "unsupported $H variant - shouldn't get here");
2155 return SendIllFormedResponse(packet,
2156 "H variant unsupported, should be c or g");
2157 }
2158
2159 return SendOKResponse();
2160}
2161
2162GDBRemoteCommunication::PacketResult
2163GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) {
2164 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
2165
2166 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002167 if (!m_debugged_process_up ||
2168 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 if (log)
2170 log->Printf(
2171 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2172 __FUNCTION__);
2173 return SendErrorResponse(0x15);
2174 }
2175
2176 packet.SetFilePos(::strlen("I"));
2177 uint8_t tmp[4096];
2178 for (;;) {
2179 size_t read = packet.GetHexBytesAvail(tmp);
2180 if (read == 0) {
2181 break;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002182 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 // write directly to stdin *this might block if stdin buffer is full*
2184 // TODO: enqueue this block in circular buffer and send window size to
2185 // remote host
2186 ConnectionStatus status;
Zachary Turner97206d52017-05-12 04:51:55 +00002187 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 m_stdio_communication.Write(tmp, read, status, &error);
2189 if (error.Fail()) {
2190 return SendErrorResponse(0x15);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002191 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002192 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002193
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002195}
2196
2197GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198GDBRemoteCommunicationServerLLGS::Handle_interrupt(
2199 StringExtractorGDBRemote &packet) {
2200 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
2201
2202 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002203 if (!m_debugged_process_up ||
2204 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
2205 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 return SendErrorResponse(0x15);
2207 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002208
Kate Stoneb9c1b512016-09-06 20:57:50 +00002209 // Interrupt the process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002210 Status error = m_debugged_process_up->Interrupt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002211 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002212 LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(),
2213 error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002214 return SendErrorResponse(GDBRemoteServerError::eErrorResume);
2215 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002216
Pavel Labath82abefa2017-07-18 09:24:48 +00002217 LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002218
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 // No response required from stop all.
2220 return PacketResult::Success;
2221}
Tamas Berghammere13c2732015-02-11 10:29:30 +00002222
Kate Stoneb9c1b512016-09-06 20:57:50 +00002223GDBRemoteCommunication::PacketResult
2224GDBRemoteCommunicationServerLLGS::Handle_memory_read(
2225 StringExtractorGDBRemote &packet) {
2226 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002227
Pavel Labath82abefa2017-07-18 09:24:48 +00002228 if (!m_debugged_process_up ||
2229 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002230 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002231 log->Printf(
2232 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2233 __FUNCTION__);
2234 return SendErrorResponse(0x15);
2235 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002236
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 // Parse out the memory address.
2238 packet.SetFilePos(strlen("m"));
2239 if (packet.GetBytesLeft() < 1)
2240 return SendIllFormedResponse(packet, "Too short m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002241
Kate Stoneb9c1b512016-09-06 20:57:50 +00002242 // Read the address. Punting on validation.
2243 // FIXME replace with Hex U64 read with no default value that fails on failed
2244 // read.
2245 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002246
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 // Validate comma.
2248 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2249 return SendIllFormedResponse(packet, "Comma sep missing in m packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002250
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 // Get # bytes to read.
2252 if (packet.GetBytesLeft() < 1)
2253 return SendIllFormedResponse(packet, "Length missing in m packet");
2254
2255 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2256 if (byte_count == 0) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002257 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002258 log->Printf("GDBRemoteCommunicationServerLLGS::%s nothing to read: "
2259 "zero-length packet",
2260 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002261 return SendOKResponse();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002263
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 // Allocate the response buffer.
2265 std::string buf(byte_count, '\0');
2266 if (buf.empty())
2267 return SendErrorResponse(0x78);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002268
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269 // Retrieve the process memory.
2270 size_t bytes_read = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002271 Status error = m_debugged_process_up->ReadMemoryWithoutTrap(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272 read_addr, &buf[0], byte_count, bytes_read);
2273 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002274 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2276 " mem 0x%" PRIx64 ": failed to read. Error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002277 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002278 error.AsCString());
2279 return SendErrorResponse(0x08);
2280 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002281
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 if (bytes_read == 0) {
2283 if (log)
2284 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2285 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes",
Pavel Labath82abefa2017-07-18 09:24:48 +00002286 __FUNCTION__, m_debugged_process_up->GetID(), read_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 byte_count);
2288 return SendErrorResponse(0x08);
2289 }
2290
2291 StreamGDBRemote response;
2292 packet.SetFilePos(0);
2293 char kind = packet.GetChar('?');
2294 if (kind == 'x')
2295 response.PutEscapedBytes(buf.data(), byte_count);
2296 else {
2297 assert(kind == 'm');
2298 for (size_t i = 0; i < bytes_read; ++i)
2299 response.PutHex8(buf[i]);
2300 }
2301
2302 return SendPacketNoLock(response.GetString());
2303}
2304
2305GDBRemoteCommunication::PacketResult
2306GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) {
2307 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2308
Pavel Labath82abefa2017-07-18 09:24:48 +00002309 if (!m_debugged_process_up ||
2310 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311 if (log)
2312 log->Printf(
2313 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2314 __FUNCTION__);
2315 return SendErrorResponse(0x15);
2316 }
2317
2318 // Parse out the memory address.
2319 packet.SetFilePos(strlen("M"));
2320 if (packet.GetBytesLeft() < 1)
2321 return SendIllFormedResponse(packet, "Too short M packet");
2322
2323 // Read the address. Punting on validation.
2324 // FIXME replace with Hex U64 read with no default value that fails on failed
2325 // read.
2326 const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0);
2327
2328 // Validate comma.
2329 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ','))
2330 return SendIllFormedResponse(packet, "Comma sep missing in M packet");
2331
2332 // Get # bytes to read.
2333 if (packet.GetBytesLeft() < 1)
2334 return SendIllFormedResponse(packet, "Length missing in M packet");
2335
2336 const uint64_t byte_count = packet.GetHexMaxU64(false, 0);
2337 if (byte_count == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002338 LLDB_LOG(log, "nothing to write: zero-length packet");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002339 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002340 }
2341
2342 // Validate colon.
2343 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':'))
2344 return SendIllFormedResponse(
2345 packet, "Comma sep missing in M packet after byte length");
2346
2347 // Allocate the conversion buffer.
2348 std::vector<uint8_t> buf(byte_count, 0);
2349 if (buf.empty())
2350 return SendErrorResponse(0x78);
2351
2352 // Convert the hex memory write contents to bytes.
2353 StreamGDBRemote response;
2354 const uint64_t convert_count = packet.GetHexBytes(buf, 0);
2355 if (convert_count != byte_count) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002356 LLDB_LOG(log,
2357 "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} "
2358 "to convert.",
2359 m_debugged_process_up->GetID(), write_addr, byte_count,
2360 convert_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 return SendIllFormedResponse(packet, "M content byte length specified did "
2362 "not match hex-encoded content "
2363 "length");
2364 }
2365
2366 // Write the process memory.
2367 size_t bytes_written = 0;
Pavel Labath82abefa2017-07-18 09:24:48 +00002368 Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0],
Zachary Turner97206d52017-05-12 04:51:55 +00002369 byte_count, bytes_written);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002370 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002371 LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}",
2372 m_debugged_process_up->GetID(), write_addr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002373 return SendErrorResponse(0x09);
2374 }
2375
2376 if (bytes_written == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002377 LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes",
2378 m_debugged_process_up->GetID(), write_addr, byte_count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002379 return SendErrorResponse(0x09);
2380 }
2381
2382 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002383}
2384
2385GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported(
2387 StringExtractorGDBRemote &packet) {
2388 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002389
Kate Stoneb9c1b512016-09-06 20:57:50 +00002390 // Currently only the NativeProcessProtocol knows if it can handle a
2391 // qMemoryRegionInfoSupported
2392 // request, but we're not guaranteed to be attached to a process. For now
2393 // we'll assume the
2394 // client only asks this when a process is being debugged.
Tamas Berghammere13c2732015-02-11 10:29:30 +00002395
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396 // Ensure we have a process running; otherwise, we can't figure this out
2397 // since we won't have a NativeProcessProtocol.
Pavel Labath82abefa2017-07-18 09:24:48 +00002398 if (!m_debugged_process_up ||
2399 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 if (log)
2401 log->Printf(
2402 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2403 __FUNCTION__);
2404 return SendErrorResponse(0x15);
2405 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002406
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 // Test if we can get any region back when asking for the region around NULL.
2408 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002409 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002410 m_debugged_process_up->GetMemoryRegionInfo(0, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411 if (error.Fail()) {
2412 // We don't support memory region info collection for this
2413 // NativeProcessProtocol.
2414 return SendUnimplementedResponse("");
2415 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002418}
2419
2420GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002421GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
2422 StringExtractorGDBRemote &packet) {
2423 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002424
Kate Stoneb9c1b512016-09-06 20:57:50 +00002425 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002426 if (!m_debugged_process_up ||
2427 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002428 if (log)
2429 log->Printf(
2430 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2431 __FUNCTION__);
2432 return SendErrorResponse(0x15);
2433 }
2434
2435 // Parse out the memory address.
2436 packet.SetFilePos(strlen("qMemoryRegionInfo:"));
2437 if (packet.GetBytesLeft() < 1)
2438 return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet");
2439
2440 // Read the address. Punting on validation.
2441 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0);
2442
2443 StreamGDBRemote response;
2444
2445 // Get the memory region info for the target address.
2446 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +00002447 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002448 m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002449 if (error.Fail()) {
2450 // Return the error message.
2451
2452 response.PutCString("error:");
2453 response.PutCStringAsRawHex8(error.AsCString());
2454 response.PutChar(';');
2455 } else {
2456 // Range start and size.
2457 response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";",
2458 region_info.GetRange().GetRangeBase(),
2459 region_info.GetRange().GetByteSize());
2460
2461 // Permissions.
2462 if (region_info.GetReadable() || region_info.GetWritable() ||
2463 region_info.GetExecutable()) {
2464 // Write permissions info.
2465 response.PutCString("permissions:");
2466
2467 if (region_info.GetReadable())
2468 response.PutChar('r');
2469 if (region_info.GetWritable())
2470 response.PutChar('w');
2471 if (region_info.GetExecutable())
2472 response.PutChar('x');
2473
2474 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002475 }
2476
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477 // Name
2478 ConstString name = region_info.GetName();
2479 if (name) {
2480 response.PutCString("name:");
2481 response.PutCStringAsRawHex8(name.AsCString());
2482 response.PutChar(';');
Tamas Berghammere13c2732015-02-11 10:29:30 +00002483 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002485
Kate Stoneb9c1b512016-09-06 20:57:50 +00002486 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002487}
2488
2489GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002490GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) {
2491 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002492 if (!m_debugged_process_up ||
2493 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002494 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002495 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496 return SendErrorResponse(0x15);
2497 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002498
Kate Stoneb9c1b512016-09-06 20:57:50 +00002499 // Parse out software or hardware breakpoint or watchpoint requested.
2500 packet.SetFilePos(strlen("Z"));
2501 if (packet.GetBytesLeft() < 1)
2502 return SendIllFormedResponse(
2503 packet, "Too short Z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002504
Kate Stoneb9c1b512016-09-06 20:57:50 +00002505 bool want_breakpoint = true;
2506 bool want_hardware = false;
2507 uint32_t watch_flags = 0;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002508
Kate Stoneb9c1b512016-09-06 20:57:50 +00002509 const GDBStoppointType stoppoint_type =
2510 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2511 switch (stoppoint_type) {
2512 case eBreakpointSoftware:
2513 want_hardware = false;
2514 want_breakpoint = true;
2515 break;
2516 case eBreakpointHardware:
2517 want_hardware = true;
2518 want_breakpoint = true;
2519 break;
2520 case eWatchpointWrite:
2521 watch_flags = 1;
2522 want_hardware = true;
2523 want_breakpoint = false;
2524 break;
2525 case eWatchpointRead:
2526 watch_flags = 2;
2527 want_hardware = true;
2528 want_breakpoint = false;
2529 break;
2530 case eWatchpointReadWrite:
2531 watch_flags = 3;
2532 want_hardware = true;
2533 want_breakpoint = false;
2534 break;
2535 case eStoppointInvalid:
2536 return SendIllFormedResponse(
2537 packet, "Z packet had invalid software/hardware specifier");
2538 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002539
Kate Stoneb9c1b512016-09-06 20:57:50 +00002540 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2541 return SendIllFormedResponse(
2542 packet, "Malformed Z packet, expecting comma after stoppoint type");
2543
2544 // Parse out the stoppoint address.
2545 if (packet.GetBytesLeft() < 1)
2546 return SendIllFormedResponse(packet, "Too short Z packet, missing address");
2547 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
2548
2549 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2550 return SendIllFormedResponse(
2551 packet, "Malformed Z packet, expecting comma after address");
2552
2553 // Parse out the stoppoint size (i.e. size hint for opcode size).
2554 const uint32_t size =
2555 packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max());
2556 if (size == std::numeric_limits<uint32_t>::max())
2557 return SendIllFormedResponse(
2558 packet, "Malformed Z packet, failed to parse size argument");
2559
2560 if (want_breakpoint) {
2561 // Try to set the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002562 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002563 m_debugged_process_up->SetBreakpoint(addr, size, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002564 if (error.Success())
2565 return SendOKResponse();
2566 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002567 LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}",
2568 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569 return SendErrorResponse(0x09);
2570 } else {
2571 // Try to set the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002572 const Status error = m_debugged_process_up->SetWatchpoint(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573 addr, size, watch_flags, want_hardware);
2574 if (error.Success())
2575 return SendOKResponse();
2576 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002577 LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}",
2578 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002579 return SendErrorResponse(0x09);
2580 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002581}
2582
2583GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002584GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) {
2585 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002586 if (!m_debugged_process_up ||
2587 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002588 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002589 LLDB_LOG(log, "failed, no process available");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 return SendErrorResponse(0x15);
2591 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002592
Kate Stoneb9c1b512016-09-06 20:57:50 +00002593 // Parse out software or hardware breakpoint or watchpoint requested.
2594 packet.SetFilePos(strlen("z"));
2595 if (packet.GetBytesLeft() < 1)
2596 return SendIllFormedResponse(
2597 packet, "Too short z packet, missing software/hardware specifier");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002598
Kate Stoneb9c1b512016-09-06 20:57:50 +00002599 bool want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002600 bool want_hardware = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002601
Kate Stoneb9c1b512016-09-06 20:57:50 +00002602 const GDBStoppointType stoppoint_type =
2603 GDBStoppointType(packet.GetS32(eStoppointInvalid));
2604 switch (stoppoint_type) {
2605 case eBreakpointHardware:
2606 want_breakpoint = true;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00002607 want_hardware = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002608 break;
2609 case eBreakpointSoftware:
2610 want_breakpoint = true;
2611 break;
2612 case eWatchpointWrite:
2613 want_breakpoint = false;
2614 break;
2615 case eWatchpointRead:
2616 want_breakpoint = false;
2617 break;
2618 case eWatchpointReadWrite:
2619 want_breakpoint = false;
2620 break;
2621 default:
2622 return SendIllFormedResponse(
2623 packet, "z packet had invalid software/hardware specifier");
2624 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002625
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2627 return SendIllFormedResponse(
2628 packet, "Malformed z packet, expecting comma after stoppoint type");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002629
Kate Stoneb9c1b512016-09-06 20:57:50 +00002630 // Parse out the stoppoint address.
2631 if (packet.GetBytesLeft() < 1)
2632 return SendIllFormedResponse(packet, "Too short z packet, missing address");
2633 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002634
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',')
2636 return SendIllFormedResponse(
2637 packet, "Malformed z packet, expecting comma after address");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002638
Kate Stoneb9c1b512016-09-06 20:57:50 +00002639 /*
2640 // Parse out the stoppoint size (i.e. size hint for opcode size).
2641 const uint32_t size = packet.GetHexMaxU32 (false,
2642 std::numeric_limits<uint32_t>::max ());
2643 if (size == std::numeric_limits<uint32_t>::max ())
2644 return SendIllFormedResponse(packet, "Malformed z packet, failed to parse
2645 size argument");
2646 */
Tamas Berghammere13c2732015-02-11 10:29:30 +00002647
Kate Stoneb9c1b512016-09-06 20:57:50 +00002648 if (want_breakpoint) {
2649 // Try to clear the breakpoint.
Zachary Turner97206d52017-05-12 04:51:55 +00002650 const Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00002651 m_debugged_process_up->RemoveBreakpoint(addr, want_hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652 if (error.Success())
2653 return SendOKResponse();
2654 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002655 LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}",
2656 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002657 return SendErrorResponse(0x09);
2658 } else {
2659 // Try to clear the watchpoint.
Pavel Labath82abefa2017-07-18 09:24:48 +00002660 const Status error = m_debugged_process_up->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002661 if (error.Success())
2662 return SendOKResponse();
2663 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
Pavel Labath82abefa2017-07-18 09:24:48 +00002664 LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}",
2665 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002666 return SendErrorResponse(0x09);
2667 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002668}
2669
2670GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002671GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) {
2672 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002673
Kate Stoneb9c1b512016-09-06 20:57:50 +00002674 // Ensure we have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002675 if (!m_debugged_process_up ||
2676 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677 if (log)
2678 log->Printf(
2679 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2680 __FUNCTION__);
2681 return SendErrorResponse(0x32);
2682 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002683
Kate Stoneb9c1b512016-09-06 20:57:50 +00002684 // We first try to use a continue thread id. If any one or any all set, use
2685 // the current thread.
2686 // Bail out if we don't have a thread id.
2687 lldb::tid_t tid = GetContinueThreadID();
2688 if (tid == 0 || tid == LLDB_INVALID_THREAD_ID)
2689 tid = GetCurrentThreadID();
2690 if (tid == LLDB_INVALID_THREAD_ID)
2691 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002692
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 // Double check that we have such a thread.
2694 // TODO investigate: on MacOSX we might need to do an UpdateThreads () here.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002695 NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
2696 if (!thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002697 return SendErrorResponse(0x33);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002698
Kate Stoneb9c1b512016-09-06 20:57:50 +00002699 // Create the step action for the given thread.
2700 ResumeAction action = {tid, eStateStepping, 0};
Tamas Berghammere13c2732015-02-11 10:29:30 +00002701
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 // Setup the actions list.
2703 ResumeActionList actions;
2704 actions.Append(action);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002705
Kate Stoneb9c1b512016-09-06 20:57:50 +00002706 // All other threads stop while we're single stepping a thread.
2707 actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0);
Pavel Labath82abefa2017-07-18 09:24:48 +00002708 Status error = m_debugged_process_up->Resume(actions);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002709 if (error.Fail()) {
2710 if (log)
2711 log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
2712 " tid %" PRIu64 " Resume() failed with error: %s",
Pavel Labath82abefa2017-07-18 09:24:48 +00002713 __FUNCTION__, m_debugged_process_up->GetID(), tid,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002714 error.AsCString());
2715 return SendErrorResponse(0x49);
2716 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002717
Kate Stoneb9c1b512016-09-06 20:57:50 +00002718 // No response here - the stop or exit will come from the resulting action.
2719 return PacketResult::Success;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002720}
2721
2722GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002723GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read(
2724 StringExtractorGDBRemote &packet) {
2725// *BSD impls should be able to do this too.
Kamil Rytarowskic93408a2017-03-21 17:27:59 +00002726#if defined(__linux__) || defined(__NetBSD__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002727 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002728
Kate Stoneb9c1b512016-09-06 20:57:50 +00002729 // Parse out the offset.
2730 packet.SetFilePos(strlen("qXfer:auxv:read::"));
2731 if (packet.GetBytesLeft() < 1)
2732 return SendIllFormedResponse(packet,
2733 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002734
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 const uint64_t auxv_offset =
2736 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2737 if (auxv_offset == std::numeric_limits<uint64_t>::max())
2738 return SendIllFormedResponse(packet,
2739 "qXfer:auxv:read:: packet missing offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002740
Kate Stoneb9c1b512016-09-06 20:57:50 +00002741 // Parse out comma.
2742 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ',')
2743 return SendIllFormedResponse(
2744 packet, "qXfer:auxv:read:: packet missing comma after offset");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002745
Kate Stoneb9c1b512016-09-06 20:57:50 +00002746 // Parse out the length.
2747 const uint64_t auxv_length =
2748 packet.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max());
2749 if (auxv_length == std::numeric_limits<uint64_t>::max())
2750 return SendIllFormedResponse(packet,
2751 "qXfer:auxv:read:: packet missing length");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002752
Kate Stoneb9c1b512016-09-06 20:57:50 +00002753 // Grab the auxv data if we need it.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002754 if (!m_active_auxv_buffer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002755 // Make sure we have a valid process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002756 if (!m_debugged_process_up ||
2757 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002758 if (log)
2759 log->Printf(
2760 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2761 __FUNCTION__);
2762 return SendErrorResponse(0x10);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002763 }
2764
Kate Stoneb9c1b512016-09-06 20:57:50 +00002765 // Grab the auxv data.
Pavel Labath82abefa2017-07-18 09:24:48 +00002766 auto buffer_or_error = m_debugged_process_up->GetAuxvData();
Pavel Labathb7f0f452017-03-17 11:08:40 +00002767 if (!buffer_or_error) {
2768 std::error_code ec = buffer_or_error.getError();
2769 LLDB_LOG(log, "no auxv data retrieved: {0}", ec.message());
2770 return SendErrorResponse(ec.value());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002771 }
Pavel Labathb7f0f452017-03-17 11:08:40 +00002772 m_active_auxv_buffer_up = std::move(*buffer_or_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002773 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002774
Kate Stoneb9c1b512016-09-06 20:57:50 +00002775 StreamGDBRemote response;
2776 bool done_with_buffer = false;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002777
Pavel Labathb7f0f452017-03-17 11:08:40 +00002778 llvm::StringRef buffer = m_active_auxv_buffer_up->getBuffer();
2779 if (auxv_offset >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002780 // We have nothing left to send. Mark the buffer as complete.
2781 response.PutChar('l');
2782 done_with_buffer = true;
2783 } else {
2784 // Figure out how many bytes are available starting at the given offset.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002785 buffer = buffer.drop_front(auxv_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002786
2787 // Mark the response type according to whether we're reading the remainder
2788 // of the auxv data.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002789 if (auxv_length >= buffer.size()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002790 // There will be nothing left to read after this
2791 response.PutChar('l');
2792 done_with_buffer = true;
2793 } else {
2794 // There will still be bytes to read after this request.
2795 response.PutChar('m');
Pavel Labathb7f0f452017-03-17 11:08:40 +00002796 buffer = buffer.take_front(auxv_length);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002797 }
2798
Kate Stoneb9c1b512016-09-06 20:57:50 +00002799 // Now write the data in encoded binary form.
Pavel Labathb7f0f452017-03-17 11:08:40 +00002800 response.PutEscapedBytes(buffer.data(), buffer.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002802
Kate Stoneb9c1b512016-09-06 20:57:50 +00002803 if (done_with_buffer)
Pavel Labathb7f0f452017-03-17 11:08:40 +00002804 m_active_auxv_buffer_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805
2806 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002807#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 return SendUnimplementedResponse("not implemented on this platform");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002809#endif
2810}
2811
2812GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState(
2814 StringExtractorGDBRemote &packet) {
2815 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002816
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817 // Move past packet name.
2818 packet.SetFilePos(strlen("QSaveRegisterState"));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002819
Kate Stoneb9c1b512016-09-06 20:57:50 +00002820 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002821 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2822 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 if (m_thread_suffix_supported)
2824 return SendIllFormedResponse(
2825 packet, "No thread specified in QSaveRegisterState packet");
2826 else
2827 return SendIllFormedResponse(packet,
2828 "No thread was is set with the Hg packet");
2829 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002830
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002832 NativeRegisterContext& reg_context = thread->GetRegisterContext();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002833
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 // Save registers to a buffer.
2835 DataBufferSP register_data_sp;
Pavel Labathd37349f2017-11-10 11:05:49 +00002836 Status error = reg_context.ReadAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002837 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002838 LLDB_LOG(log, "pid {0} failed to save all register values: {1}",
2839 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002840 return SendErrorResponse(0x75);
2841 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002842
Kate Stoneb9c1b512016-09-06 20:57:50 +00002843 // Allocate a new save id.
2844 const uint32_t save_id = GetNextSavedRegistersID();
2845 assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) &&
2846 "GetNextRegisterSaveID() returned an existing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002847
Kate Stoneb9c1b512016-09-06 20:57:50 +00002848 // Save the register data buffer under the save id.
2849 {
2850 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2851 m_saved_registers_map[save_id] = register_data_sp;
2852 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002853
Kate Stoneb9c1b512016-09-06 20:57:50 +00002854 // Write the response.
2855 StreamGDBRemote response;
2856 response.Printf("%" PRIu32, save_id);
2857 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00002858}
2859
2860GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002861GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState(
2862 StringExtractorGDBRemote &packet) {
2863 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002864
Kate Stoneb9c1b512016-09-06 20:57:50 +00002865 // Parse out save id.
2866 packet.SetFilePos(strlen("QRestoreRegisterState:"));
2867 if (packet.GetBytesLeft() < 1)
2868 return SendIllFormedResponse(
2869 packet, "QRestoreRegisterState packet missing register save id");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002870
Kate Stoneb9c1b512016-09-06 20:57:50 +00002871 const uint32_t save_id = packet.GetU32(0);
2872 if (save_id == 0) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002873 LLDB_LOG(log, "QRestoreRegisterState packet has malformed save id, "
2874 "expecting decimal uint32_t");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002875 return SendErrorResponse(0x76);
2876 }
2877
2878 // Get the thread to use.
Pavel Labatha5be48b2017-10-17 15:52:16 +00002879 NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
2880 if (!thread) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002881 if (m_thread_suffix_supported)
2882 return SendIllFormedResponse(
2883 packet, "No thread specified in QRestoreRegisterState packet");
2884 else
2885 return SendIllFormedResponse(packet,
2886 "No thread was is set with the Hg packet");
2887 }
2888
2889 // Grab the register context for the thread.
Pavel Labathd37349f2017-11-10 11:05:49 +00002890 NativeRegisterContext &reg_context = thread->GetRegisterContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002891
2892 // Retrieve register state buffer, then remove from the list.
2893 DataBufferSP register_data_sp;
2894 {
2895 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
2896
2897 // Find the register set buffer for the given save id.
2898 auto it = m_saved_registers_map.find(save_id);
2899 if (it == m_saved_registers_map.end()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002900 LLDB_LOG(log,
2901 "pid {0} does not have a register set save buffer for id {1}",
2902 m_debugged_process_up->GetID(), save_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 return SendErrorResponse(0x77);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002905 register_data_sp = it->second;
Tamas Berghammere13c2732015-02-11 10:29:30 +00002906
Kate Stoneb9c1b512016-09-06 20:57:50 +00002907 // Remove it from the map.
2908 m_saved_registers_map.erase(it);
2909 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002910
Pavel Labathd37349f2017-11-10 11:05:49 +00002911 Status error = reg_context.WriteAllRegisterValues(register_data_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002912 if (error.Fail()) {
Pavel Labath82abefa2017-07-18 09:24:48 +00002913 LLDB_LOG(log, "pid {0} failed to restore all register values: {1}",
2914 m_debugged_process_up->GetID(), error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002915 return SendErrorResponse(0x77);
2916 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002917
Kate Stoneb9c1b512016-09-06 20:57:50 +00002918 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00002919}
2920
2921GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922GDBRemoteCommunicationServerLLGS::Handle_vAttach(
2923 StringExtractorGDBRemote &packet) {
2924 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00002925
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 // Consume the ';' after vAttach.
2927 packet.SetFilePos(strlen("vAttach"));
2928 if (!packet.GetBytesLeft() || packet.GetChar() != ';')
2929 return SendIllFormedResponse(packet, "vAttach missing expected ';'");
Tamas Berghammere13c2732015-02-11 10:29:30 +00002930
Kate Stoneb9c1b512016-09-06 20:57:50 +00002931 // Grab the PID to which we will attach (assume hex encoding).
2932 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
2933 if (pid == LLDB_INVALID_PROCESS_ID)
2934 return SendIllFormedResponse(packet,
2935 "vAttach failed to parse the process id");
2936
2937 // Attempt to attach.
2938 if (log)
2939 log->Printf("GDBRemoteCommunicationServerLLGS::%s attempting to attach to "
2940 "pid %" PRIu64,
2941 __FUNCTION__, pid);
2942
Zachary Turner97206d52017-05-12 04:51:55 +00002943 Status error = AttachToProcess(pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002944
2945 if (error.Fail()) {
2946 if (log)
2947 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to attach to "
2948 "pid %" PRIu64 ": %s\n",
2949 __FUNCTION__, pid, error.AsCString());
2950 return SendErrorResponse(0x01);
2951 }
2952
2953 // Notify we attached by sending a stop packet.
Pavel Labath82abefa2017-07-18 09:24:48 +00002954 return SendStopReasonForState(m_debugged_process_up->GetState());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002955}
2956
2957GDBRemoteCommunication::PacketResult
2958GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) {
2959 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
2960
2961 StopSTDIOForwarding();
2962
2963 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00002964 if (!m_debugged_process_up ||
2965 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002966 if (log)
2967 log->Printf(
2968 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2969 __FUNCTION__);
2970 return SendErrorResponse(0x15);
2971 }
2972
2973 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2974
2975 // Consume the ';' after D.
2976 packet.SetFilePos(1);
2977 if (packet.GetBytesLeft()) {
2978 if (packet.GetChar() != ';')
2979 return SendIllFormedResponse(packet, "D missing expected ';'");
2980
2981 // Grab the PID from which we will detach (assume hex encoding).
2982 pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16);
Tamas Berghammere13c2732015-02-11 10:29:30 +00002983 if (pid == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984 return SendIllFormedResponse(packet, "D failed to parse the process id");
2985 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00002986
Pavel Labath82abefa2017-07-18 09:24:48 +00002987 if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_up->GetID() != pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002988 return SendIllFormedResponse(packet, "Invalid pid");
2989 }
2990
Pavel Labath82abefa2017-07-18 09:24:48 +00002991 const Status error = m_debugged_process_up->Detach();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 if (error.Fail()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +00002993 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002994 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed to detach from "
2995 "pid %" PRIu64 ": %s\n",
Pavel Labath82abefa2017-07-18 09:24:48 +00002996 __FUNCTION__, m_debugged_process_up->GetID(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997 error.AsCString());
2998 return SendErrorResponse(0x01);
2999 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 return SendOKResponse();
Tamas Berghammere13c2732015-02-11 10:29:30 +00003002}
3003
3004GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003005GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo(
3006 StringExtractorGDBRemote &packet) {
3007 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003008
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009 packet.SetFilePos(strlen("qThreadStopInfo"));
3010 const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
3011 if (tid == LLDB_INVALID_THREAD_ID) {
Pavel Labath4a4bb122015-07-16 14:14:35 +00003012 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003013 log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, could not "
3014 "parse thread id from request \"%s\"",
3015 __FUNCTION__, packet.GetStringRef().c_str());
3016 return SendErrorResponse(0x15);
3017 }
3018 return SendStopReplyPacketForThread(tid);
3019}
3020
3021GDBRemoteCommunication::PacketResult
3022GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo(
3023 StringExtractorGDBRemote &) {
3024 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
3025
3026 // Ensure we have a debugged process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003027 if (!m_debugged_process_up ||
3028 (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 return SendErrorResponse(50);
Pavel Labath82abefa2017-07-18 09:24:48 +00003030 LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003031
Kate Stoneb9c1b512016-09-06 20:57:50 +00003032 StreamString response;
3033 const bool threads_with_valid_stop_info_only = false;
3034 JSONArray::SP threads_array_sp = GetJSONThreadsInfo(
Pavel Labath82abefa2017-07-18 09:24:48 +00003035 *m_debugged_process_up, threads_with_valid_stop_info_only);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003036 if (!threads_array_sp) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003037 LLDB_LOG(log, "failed to prepare a packet for pid {0}",
3038 m_debugged_process_up->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003039 return SendErrorResponse(52);
3040 }
Pavel Labath4a4bb122015-07-16 14:14:35 +00003041
Kate Stoneb9c1b512016-09-06 20:57:50 +00003042 threads_array_sp->Write(response);
3043 StreamGDBRemote escaped_response;
3044 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
3045 return SendPacketNoLock(escaped_response.GetString());
Pavel Labath4a4bb122015-07-16 14:14:35 +00003046}
3047
3048GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003049GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo(
3050 StringExtractorGDBRemote &packet) {
3051 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003052 if (!m_debugged_process_up ||
3053 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003054 return SendErrorResponse(68);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003055
Kate Stoneb9c1b512016-09-06 20:57:50 +00003056 packet.SetFilePos(strlen("qWatchpointSupportInfo"));
3057 if (packet.GetBytesLeft() == 0)
3058 return SendOKResponse();
3059 if (packet.GetChar() != ':')
3060 return SendErrorResponse(67);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003061
Pavel Labath82abefa2017-07-18 09:24:48 +00003062 auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo();
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003063
Kate Stoneb9c1b512016-09-06 20:57:50 +00003064 StreamGDBRemote response;
Omair Javaidd5ffbad2017-02-24 13:27:31 +00003065 if (hw_debug_cap == llvm::None)
3066 response.Printf("num:0;");
3067 else
3068 response.Printf("num:%d;", hw_debug_cap->second);
3069
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +00003071}
3072
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003073GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +00003074GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress(
3075 StringExtractorGDBRemote &packet) {
3076 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003077 if (!m_debugged_process_up ||
3078 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003079 return SendErrorResponse(67);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003080
Kate Stoneb9c1b512016-09-06 20:57:50 +00003081 packet.SetFilePos(strlen("qFileLoadAddress:"));
3082 if (packet.GetBytesLeft() == 0)
3083 return SendErrorResponse(68);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003084
Kate Stoneb9c1b512016-09-06 20:57:50 +00003085 std::string file_name;
3086 packet.GetHexByteString(file_name);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003087
Kate Stoneb9c1b512016-09-06 20:57:50 +00003088 lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +00003089 Status error =
Pavel Labath82abefa2017-07-18 09:24:48 +00003090 m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003091 if (error.Fail())
3092 return SendErrorResponse(69);
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003093
Kate Stoneb9c1b512016-09-06 20:57:50 +00003094 if (file_load_address == LLDB_INVALID_ADDRESS)
3095 return SendErrorResponse(1); // File not loaded
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003096
Kate Stoneb9c1b512016-09-06 20:57:50 +00003097 StreamGDBRemote response;
3098 response.PutHex64(file_load_address);
3099 return SendPacketNoLock(response.GetString());
Tamas Berghammer783bfc82015-06-18 20:43:56 +00003100}
3101
Pavel Labath4a705e72017-02-24 09:29:14 +00003102GDBRemoteCommunication::PacketResult
3103GDBRemoteCommunicationServerLLGS::Handle_QPassSignals(
3104 StringExtractorGDBRemote &packet) {
3105 std::vector<int> signals;
3106 packet.SetFilePos(strlen("QPassSignals:"));
3107
3108 // Read sequence of hex signal numbers divided by a semicolon and
3109 // optionally spaces.
3110 while (packet.GetBytesLeft() > 0) {
3111 int signal = packet.GetS32(-1, 16);
3112 if (signal < 0)
3113 return SendIllFormedResponse(packet, "Failed to parse signal number.");
3114 signals.push_back(signal);
3115
3116 packet.SkipSpaces();
3117 char separator = packet.GetChar();
3118 if (separator == '\0')
3119 break; // End of string
3120 if (separator != ';')
3121 return SendIllFormedResponse(packet, "Invalid separator,"
3122 " expected semicolon.");
3123 }
3124
3125 // Fail if we don't have a current process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003126 if (!m_debugged_process_up)
Pavel Labath4a705e72017-02-24 09:29:14 +00003127 return SendErrorResponse(68);
3128
Pavel Labath82abefa2017-07-18 09:24:48 +00003129 Status error = m_debugged_process_up->IgnoreSignals(signals);
Pavel Labath4a705e72017-02-24 09:29:14 +00003130 if (error.Fail())
3131 return SendErrorResponse(69);
3132
3133 return SendOKResponse();
3134}
3135
Kate Stoneb9c1b512016-09-06 20:57:50 +00003136void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() {
3137 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003138
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139 // Tell the stdio connection to shut down.
3140 if (m_stdio_communication.IsConnected()) {
3141 auto connection = m_stdio_communication.GetConnection();
3142 if (connection) {
Zachary Turner97206d52017-05-12 04:51:55 +00003143 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 connection->Disconnect(&error);
Tamas Berghammere13c2732015-02-11 10:29:30 +00003145
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 if (error.Success()) {
3147 if (log)
3148 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3149 "terminal stdio - SUCCESS",
3150 __FUNCTION__);
3151 } else {
3152 if (log)
3153 log->Printf("GDBRemoteCommunicationServerLLGS::%s disconnect process "
3154 "terminal stdio - FAIL: %s",
3155 __FUNCTION__, error.AsCString());
3156 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003158 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003159}
3160
Pavel Labatha5be48b2017-10-17 15:52:16 +00003161NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
Kate Stoneb9c1b512016-09-06 20:57:50 +00003162 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163 // We have no thread if we don't have a process.
Pavel Labath82abefa2017-07-18 09:24:48 +00003164 if (!m_debugged_process_up ||
3165 m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003166 return nullptr;
Tamas Berghammere13c2732015-02-11 10:29:30 +00003167
Kate Stoneb9c1b512016-09-06 20:57:50 +00003168 // If the client hasn't asked for thread suffix support, there will not be a
3169 // thread suffix.
3170 // Use the current thread in that case.
3171 if (!m_thread_suffix_supported) {
3172 const lldb::tid_t current_tid = GetCurrentThreadID();
3173 if (current_tid == LLDB_INVALID_THREAD_ID)
Pavel Labatha5be48b2017-10-17 15:52:16 +00003174 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003175 else if (current_tid == 0) {
3176 // Pick a thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003177 return m_debugged_process_up->GetThreadAtIndex(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003178 } else
Pavel Labath82abefa2017-07-18 09:24:48 +00003179 return m_debugged_process_up->GetThreadByID(current_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003180 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003181
Kate Stoneb9c1b512016-09-06 20:57:50 +00003182 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD));
Tamas Berghammere13c2732015-02-11 10:29:30 +00003183
Kate Stoneb9c1b512016-09-06 20:57:50 +00003184 // Parse out the ';'.
3185 if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') {
Tamas Berghammere13c2732015-02-11 10:29:30 +00003186 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003187 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3188 "error: expected ';' prior to start of thread suffix: packet "
3189 "contents = '%s'",
3190 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003191 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003192 }
Tamas Berghammere13c2732015-02-11 10:29:30 +00003193
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194 if (!packet.GetBytesLeft())
Pavel Labatha5be48b2017-10-17 15:52:16 +00003195 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003196
3197 // Parse out thread: portion.
3198 if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) {
3199 if (log)
3200 log->Printf("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
3201 "error: expected 'thread:' but not found, packet contents = "
3202 "'%s'",
3203 __FUNCTION__, packet.GetStringRef().c_str());
Pavel Labatha5be48b2017-10-17 15:52:16 +00003204 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003205 }
3206 packet.SetFilePos(packet.GetFilePos() + strlen("thread:"));
3207 const lldb::tid_t tid = packet.GetHexMaxU64(false, 0);
3208 if (tid != 0)
Pavel Labath82abefa2017-07-18 09:24:48 +00003209 return m_debugged_process_up->GetThreadByID(tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003210
Pavel Labatha5be48b2017-10-17 15:52:16 +00003211 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212}
3213
3214lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const {
3215 if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) {
3216 // Use whatever the debug process says is the current thread id
3217 // since the protocol either didn't specify or specified we want
3218 // any/all threads marked as the current thread.
Pavel Labath82abefa2017-07-18 09:24:48 +00003219 if (!m_debugged_process_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003220 return LLDB_INVALID_THREAD_ID;
Pavel Labath82abefa2017-07-18 09:24:48 +00003221 return m_debugged_process_up->GetCurrentThreadID();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222 }
3223 // Use the specific current thread id set by the gdb remote protocol.
3224 return m_current_tid;
3225}
3226
3227uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() {
3228 std::lock_guard<std::mutex> guard(m_saved_registers_mutex);
3229 return m_next_saved_registers_id++;
3230}
3231
3232void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() {
Pavel Labathb7f0f452017-03-17 11:08:40 +00003233 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234
Pavel Labathb7f0f452017-03-17 11:08:40 +00003235 LLDB_LOG(log, "clearing auxv buffer: {0}", m_active_auxv_buffer_up.get());
3236 m_active_auxv_buffer_up.reset();
Tamas Berghammere13c2732015-02-11 10:29:30 +00003237}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003238
3239FileSpec
Kate Stoneb9c1b512016-09-06 20:57:50 +00003240GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path,
3241 const ArchSpec &arch) {
Pavel Labath82abefa2017-07-18 09:24:48 +00003242 if (m_debugged_process_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003243 FileSpec file_spec;
Pavel Labath82abefa2017-07-18 09:24:48 +00003244 if (m_debugged_process_up
Kate Stoneb9c1b512016-09-06 20:57:50 +00003245 ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec)
3246 .Success()) {
3247 if (file_spec.Exists())
3248 return file_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003250 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003251
Kate Stoneb9c1b512016-09-06 20:57:50 +00003252 return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003253}