blob: 5bc0c25c5872f031eece60b827d69026182fda4d [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerCommon.cpp ------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tamas Berghammere13c2732015-02-11 10:29:30 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "GDBRemoteCommunicationServerCommon.h"
10
11#include <errno.h>
12
Todd Fialae77fce02016-09-04 00:18:56 +000013#ifdef __APPLE__
14#include <TargetConditionals.h>
15#endif
16
Tamas Berghammere13c2732015-02-11 10:29:30 +000017#include <chrono>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <cstring>
Tamas Berghammere13c2732015-02-11 10:29:30 +000019
Oleksiy Vyalov6801be32015-02-25 22:15:44 +000020#include "lldb/Core/ModuleSpec.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000021#include "lldb/Host/Config.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000022#include "lldb/Host/File.h"
Pavel Labatheef758e2019-02-04 14:28:08 +000023#include "lldb/Host/FileAction.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000024#include "lldb/Host/FileSystem.h"
25#include "lldb/Host/Host.h"
26#include "lldb/Host/HostInfo.h"
Pavel Labath77044732018-09-12 12:26:05 +000027#include "lldb/Host/SafeMachO.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000028#include "lldb/Interpreter/OptionArgParser.h"
Oleksiy Vyalov6801be32015-02-25 22:15:44 +000029#include "lldb/Symbol/ObjectFile.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000030#include "lldb/Target/Platform.h"
31#include "lldb/Target/Process.h"
Zachary Turner01c32432017-02-14 19:06:07 +000032#include "lldb/Utility/Endian.h"
Pavel Labath2f1fbae2016-09-08 10:07:04 +000033#include "lldb/Utility/JSON.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000034#include "lldb/Utility/Log.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000035#include "lldb/Utility/StreamGDBRemote.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000036#include "lldb/Utility/StreamString.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#include "llvm/ADT/Triple.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000038
Tamas Berghammere13c2732015-02-11 10:29:30 +000039#include "ProcessGDBRemoteLog.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000040#include "lldb/Utility/StringExtractorGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000041
Tamas Berghammerdad4db72015-03-13 11:16:08 +000042#ifdef __ANDROID__
43#include "lldb/Host/android/HostInfoAndroid.h"
44#endif
45
Zachary Turner54695a32016-08-29 19:58:14 +000046#include "llvm/ADT/StringSwitch.h"
47
Tamas Berghammere13c2732015-02-11 10:29:30 +000048using namespace lldb;
49using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000050using namespace lldb_private::process_gdb_remote;
Tamas Berghammere13c2732015-02-11 10:29:30 +000051
Tamas Berghammer2d52afd2015-02-26 11:37:21 +000052#ifdef __ANDROID__
Kate Stoneb9c1b512016-09-06 20:57:50 +000053const static uint32_t g_default_packet_timeout_sec = 20; // seconds
Tamas Berghammer2d52afd2015-02-26 11:37:21 +000054#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000055const static uint32_t g_default_packet_timeout_sec = 0; // not specified
Tamas Berghammer2d52afd2015-02-26 11:37:21 +000056#endif
57
Tamas Berghammere13c2732015-02-11 10:29:30 +000058//----------------------------------------------------------------------
59// GDBRemoteCommunicationServerCommon constructor
60//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000061GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon(
62 const char *comm_name, const char *listener_name)
63 : GDBRemoteCommunicationServer(comm_name, listener_name),
64 m_process_launch_info(), m_process_launch_error(), m_proc_infos(),
65 m_proc_infos_index(0), m_thread_suffix_supported(false),
66 m_list_threads_in_stop_reply(false) {
67 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A,
68 &GDBRemoteCommunicationServerCommon::Handle_A);
69 RegisterMemberFunctionHandler(
70 StringExtractorGDBRemote::eServerPacketType_QEnvironment,
71 &GDBRemoteCommunicationServerCommon::Handle_QEnvironment);
72 RegisterMemberFunctionHandler(
73 StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded,
74 &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded);
75 RegisterMemberFunctionHandler(
76 StringExtractorGDBRemote::eServerPacketType_qfProcessInfo,
77 &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo);
78 RegisterMemberFunctionHandler(
79 StringExtractorGDBRemote::eServerPacketType_qGroupName,
80 &GDBRemoteCommunicationServerCommon::Handle_qGroupName);
81 RegisterMemberFunctionHandler(
82 StringExtractorGDBRemote::eServerPacketType_qHostInfo,
83 &GDBRemoteCommunicationServerCommon::Handle_qHostInfo);
84 RegisterMemberFunctionHandler(
85 StringExtractorGDBRemote::eServerPacketType_QLaunchArch,
86 &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch);
87 RegisterMemberFunctionHandler(
88 StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess,
89 &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess);
90 RegisterMemberFunctionHandler(
91 StringExtractorGDBRemote::eServerPacketType_QListThreadsInStopReply,
92 &GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply);
93 RegisterMemberFunctionHandler(
94 StringExtractorGDBRemote::eServerPacketType_qEcho,
95 &GDBRemoteCommunicationServerCommon::Handle_qEcho);
96 RegisterMemberFunctionHandler(
97 StringExtractorGDBRemote::eServerPacketType_qModuleInfo,
98 &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo);
99 RegisterMemberFunctionHandler(
Pavel Labath2f1fbae2016-09-08 10:07:04 +0000100 StringExtractorGDBRemote::eServerPacketType_jModulesInfo,
101 &GDBRemoteCommunicationServerCommon::Handle_jModulesInfo);
102 RegisterMemberFunctionHandler(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod,
104 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod);
105 RegisterMemberFunctionHandler(
106 StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir,
107 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir);
108 RegisterMemberFunctionHandler(
109 StringExtractorGDBRemote::eServerPacketType_qPlatform_shell,
110 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell);
111 RegisterMemberFunctionHandler(
112 StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID,
113 &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID);
114 RegisterMemberFunctionHandler(
115 StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError,
116 &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError);
117 RegisterMemberFunctionHandler(
118 StringExtractorGDBRemote::eServerPacketType_QSetSTDERR,
119 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR);
120 RegisterMemberFunctionHandler(
121 StringExtractorGDBRemote::eServerPacketType_QSetSTDIN,
122 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN);
123 RegisterMemberFunctionHandler(
124 StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT,
125 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT);
126 RegisterMemberFunctionHandler(
127 StringExtractorGDBRemote::eServerPacketType_qSpeedTest,
128 &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest);
129 RegisterMemberFunctionHandler(
130 StringExtractorGDBRemote::eServerPacketType_qsProcessInfo,
131 &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo);
132 RegisterMemberFunctionHandler(
133 StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode,
134 &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode);
135 RegisterMemberFunctionHandler(
136 StringExtractorGDBRemote::eServerPacketType_qSupported,
137 &GDBRemoteCommunicationServerCommon::Handle_qSupported);
138 RegisterMemberFunctionHandler(
139 StringExtractorGDBRemote::eServerPacketType_QThreadSuffixSupported,
140 &GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported);
141 RegisterMemberFunctionHandler(
142 StringExtractorGDBRemote::eServerPacketType_qUserName,
143 &GDBRemoteCommunicationServerCommon::Handle_qUserName);
144 RegisterMemberFunctionHandler(
145 StringExtractorGDBRemote::eServerPacketType_vFile_close,
146 &GDBRemoteCommunicationServerCommon::Handle_vFile_Close);
147 RegisterMemberFunctionHandler(
148 StringExtractorGDBRemote::eServerPacketType_vFile_exists,
149 &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists);
150 RegisterMemberFunctionHandler(
151 StringExtractorGDBRemote::eServerPacketType_vFile_md5,
152 &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5);
153 RegisterMemberFunctionHandler(
154 StringExtractorGDBRemote::eServerPacketType_vFile_mode,
155 &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode);
156 RegisterMemberFunctionHandler(
157 StringExtractorGDBRemote::eServerPacketType_vFile_open,
158 &GDBRemoteCommunicationServerCommon::Handle_vFile_Open);
159 RegisterMemberFunctionHandler(
160 StringExtractorGDBRemote::eServerPacketType_vFile_pread,
161 &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead);
162 RegisterMemberFunctionHandler(
163 StringExtractorGDBRemote::eServerPacketType_vFile_pwrite,
164 &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite);
165 RegisterMemberFunctionHandler(
166 StringExtractorGDBRemote::eServerPacketType_vFile_size,
167 &GDBRemoteCommunicationServerCommon::Handle_vFile_Size);
168 RegisterMemberFunctionHandler(
169 StringExtractorGDBRemote::eServerPacketType_vFile_stat,
170 &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat);
171 RegisterMemberFunctionHandler(
172 StringExtractorGDBRemote::eServerPacketType_vFile_symlink,
173 &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink);
174 RegisterMemberFunctionHandler(
175 StringExtractorGDBRemote::eServerPacketType_vFile_unlink,
176 &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000177}
178
179//----------------------------------------------------------------------
180// Destructor
181//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon() {}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000183
184GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185GDBRemoteCommunicationServerCommon::Handle_qHostInfo(
186 StringExtractorGDBRemote &packet) {
187 StreamString response;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00
Tamas Berghammere13c2732015-02-11 10:29:30 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 ArchSpec host_arch(HostInfo::GetArchitecture());
192 const llvm::Triple &host_triple = host_arch.GetTriple();
193 response.PutCString("triple:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000194 response.PutStringAsRawHex8(host_triple.getTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 response.Printf(";ptrsize:%u;", host_arch.GetAddressByteSize());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 const char *distribution_id = host_arch.GetDistributionId().AsCString();
198 if (distribution_id) {
199 response.PutCString("distribution_id:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000200 response.PutStringAsRawHex8(distribution_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 response.PutCString(";");
202 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000203
Tamas Berghammere13c2732015-02-11 10:29:30 +0000204#if defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 // For parity with debugserver, we'll include the vendor key.
206 response.PutCString("vendor:apple;");
Todd Fialae77fce02016-09-04 00:18:56 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 // Send out MachO info.
209 uint32_t cpu = host_arch.GetMachOCPUType();
210 uint32_t sub = host_arch.GetMachOCPUSubType();
211 if (cpu != LLDB_INVALID_CPUTYPE)
212 response.Printf("cputype:%u;", cpu);
213 if (sub != LLDB_INVALID_CPUTYPE)
214 response.Printf("cpusubtype:%u;", sub);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000215
Aaron Smith3a142492019-02-07 18:46:25 +0000216 if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217// Indicate the OS type.
218#if defined(TARGET_OS_TV) && TARGET_OS_TV == 1
219 response.PutCString("ostype:tvos;");
220#elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1
221 response.PutCString("ostype:watchos;");
Jason Molenda8c0d1062018-02-05 23:10:51 +0000222#elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1
223 response.PutCString("ostype:bridgeos;");
Todd Fialae77fce02016-09-04 00:18:56 +0000224#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 response.PutCString("ostype:ios;");
Todd Fialae77fce02016-09-04 00:18:56 +0000226#endif
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 // On arm, we use "synchronous" watchpoints which means the exception is
229 // delivered before the instruction executes.
230 response.PutCString("watchpoint_exceptions_received:before;");
231 } else {
232 response.PutCString("ostype:macosx;");
233 response.Printf("watchpoint_exceptions_received:after;");
234 }
Todd Fialae77fce02016-09-04 00:18:56 +0000235
Tamas Berghammere13c2732015-02-11 10:29:30 +0000236#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 if (host_arch.GetMachine() == llvm::Triple::aarch64 ||
238 host_arch.GetMachine() == llvm::Triple::aarch64_be ||
239 host_arch.GetMachine() == llvm::Triple::arm ||
240 host_arch.GetMachine() == llvm::Triple::armeb ||
241 host_arch.GetMachine() == llvm::Triple::mips64 ||
242 host_arch.GetMachine() == llvm::Triple::mips64el ||
243 host_arch.GetMachine() == llvm::Triple::mips ||
244 host_arch.GetMachine() == llvm::Triple::mipsel)
245 response.Printf("watchpoint_exceptions_received:before;");
246 else
247 response.Printf("watchpoint_exceptions_received:after;");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000248#endif
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 switch (endian::InlHostByteOrder()) {
251 case eByteOrderBig:
252 response.PutCString("endian:big;");
253 break;
254 case eByteOrderLittle:
255 response.PutCString("endian:little;");
256 break;
257 case eByteOrderPDP:
258 response.PutCString("endian:pdp;");
259 break;
260 default:
261 response.PutCString("endian:unknown;");
262 break;
263 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000264
Pavel Labath2272c482018-06-18 15:02:23 +0000265 llvm::VersionTuple version = HostInfo::GetOSVersion();
266 if (!version.empty()) {
267 response.Format("os_version:{0}", version.getAsString());
268 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 std::string s;
272 if (HostInfo::GetOSBuildString(s)) {
273 response.PutCString("os_build:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000274 response.PutStringAsRawHex8(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 response.PutChar(';');
276 }
277 if (HostInfo::GetOSKernelDescription(s)) {
278 response.PutCString("os_kernel:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000279 response.PutStringAsRawHex8(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 response.PutChar(';');
281 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000282
283#if defined(__APPLE__)
284
285#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
Adrian Prantl05097242018-04-30 16:49:04 +0000286 // For iOS devices, we are connected through a USB Mux so we never pretend to
287 // actually have a hostname as far as the remote lldb that is connecting to
288 // this lldb-platform is concerned
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 response.PutCString("hostname:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000290 response.PutStringAsRawHex8("127.0.0.1");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 response.PutChar(';');
292#else // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
293 if (HostInfo::GetHostname(s)) {
294 response.PutCString("hostname:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000295 response.PutStringAsRawHex8(s);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000296 response.PutChar(';');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 }
298#endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
Tamas Berghammere13c2732015-02-11 10:29:30 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300#else // #if defined(__APPLE__)
301 if (HostInfo::GetHostname(s)) {
302 response.PutCString("hostname:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000303 response.PutStringAsRawHex8(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 response.PutChar(';');
305 }
306#endif // #if defined(__APPLE__)
Tamas Berghammere13c2732015-02-11 10:29:30 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 if (g_default_packet_timeout_sec > 0)
309 response.Printf("default_packet_timeout:%u;", g_default_packet_timeout_sec);
Tamas Berghammer2d52afd2015-02-26 11:37:21 +0000310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000312}
313
314GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID(
316 StringExtractorGDBRemote &packet) {
317 // Packet format: "qProcessInfoPID:%i" where %i is the pid
318 packet.SetFilePos(::strlen("qProcessInfoPID:"));
319 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID);
320 if (pid != LLDB_INVALID_PROCESS_ID) {
321 ProcessInstanceInfo proc_info;
322 if (Host::GetProcessInfo(pid, proc_info)) {
323 StreamString response;
324 CreateProcessInfoResponse(proc_info, response);
325 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000326 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 }
328 return SendErrorResponse(1);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000329}
330
331GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo(
333 StringExtractorGDBRemote &packet) {
334 m_proc_infos_index = 0;
335 m_proc_infos.Clear();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 ProcessInstanceInfoMatch match_info;
338 packet.SetFilePos(::strlen("qfProcessInfo"));
339 if (packet.GetChar() == ':') {
Zachary Turner54695a32016-08-29 19:58:14 +0000340 llvm::StringRef key;
341 llvm::StringRef value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 while (packet.GetNameColonValue(key, value)) {
343 bool success = true;
344 if (key.equals("name")) {
345 StringExtractor extractor(value);
346 std::string file;
347 extractor.GetHexByteString(file);
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000348 match_info.GetProcessInfo().GetExecutableFile().SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000349 file, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 } else if (key.equals("name_match")) {
Pavel Labathc4a33952017-02-20 11:35:33 +0000351 NameMatch name_match = llvm::StringSwitch<NameMatch>(value)
352 .Case("equals", NameMatch::Equals)
353 .Case("starts_with", NameMatch::StartsWith)
354 .Case("ends_with", NameMatch::EndsWith)
355 .Case("contains", NameMatch::Contains)
356 .Case("regex", NameMatch::RegularExpression)
357 .Default(NameMatch::Ignore);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 match_info.SetNameMatchType(name_match);
Pavel Labathc4a33952017-02-20 11:35:33 +0000359 if (name_match == NameMatch::Ignore)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 return SendErrorResponse(2);
361 } else if (key.equals("pid")) {
362 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
363 if (value.getAsInteger(0, pid))
364 return SendErrorResponse(2);
365 match_info.GetProcessInfo().SetProcessID(pid);
366 } else if (key.equals("parent_pid")) {
367 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
368 if (value.getAsInteger(0, pid))
369 return SendErrorResponse(2);
370 match_info.GetProcessInfo().SetParentProcessID(pid);
371 } else if (key.equals("uid")) {
372 uint32_t uid = UINT32_MAX;
373 if (value.getAsInteger(0, uid))
374 return SendErrorResponse(2);
375 match_info.GetProcessInfo().SetUserID(uid);
376 } else if (key.equals("gid")) {
377 uint32_t gid = UINT32_MAX;
378 if (value.getAsInteger(0, gid))
379 return SendErrorResponse(2);
380 match_info.GetProcessInfo().SetGroupID(gid);
381 } else if (key.equals("euid")) {
382 uint32_t uid = UINT32_MAX;
383 if (value.getAsInteger(0, uid))
384 return SendErrorResponse(2);
385 match_info.GetProcessInfo().SetEffectiveUserID(uid);
386 } else if (key.equals("egid")) {
387 uint32_t gid = UINT32_MAX;
388 if (value.getAsInteger(0, gid))
389 return SendErrorResponse(2);
390 match_info.GetProcessInfo().SetEffectiveGroupID(gid);
391 } else if (key.equals("all_users")) {
392 match_info.SetMatchAllUsers(
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000393 OptionArgParser::ToBoolean(value, false, &success));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 } else if (key.equals("triple")) {
Pavel Labath7263f1b2017-10-31 10:56:03 +0000395 match_info.GetProcessInfo().GetArchitecture() =
396 HostInfo::GetAugmentedArchSpec(value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 } else {
398 success = false;
399 }
400
401 if (!success)
402 return SendErrorResponse(2);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 }
405
406 if (Host::FindProcesses(match_info, m_proc_infos)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000407 // We found something, return the first item by calling the get subsequent
408 // process info packet handler...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 return Handle_qsProcessInfo(packet);
410 }
411 return SendErrorResponse(3);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000412}
413
414GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo(
416 StringExtractorGDBRemote &packet) {
417 if (m_proc_infos_index < m_proc_infos.GetSize()) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000418 StreamString response;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 CreateProcessInfoResponse(
420 m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response);
421 ++m_proc_infos_index;
Zachary Turner26709df2016-08-27 15:52:29 +0000422 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 }
424 return SendErrorResponse(4);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000425}
426
427GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428GDBRemoteCommunicationServerCommon::Handle_qUserName(
429 StringExtractorGDBRemote &packet) {
430#if !defined(LLDB_DISABLE_POSIX)
431 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
432 if (log)
433 log->Printf("GDBRemoteCommunicationServerCommon::%s begin", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 // Packet format: "qUserName:%i" where %i is the uid
436 packet.SetFilePos(::strlen("qUserName:"));
437 uint32_t uid = packet.GetU32(UINT32_MAX);
438 if (uid != UINT32_MAX) {
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000439 if (llvm::Optional<llvm::StringRef> name =
440 HostInfo::GetUserIDResolver().GetUserName(uid)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 StreamString response;
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000442 response.PutStringAsRawHex8(*name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000444 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 }
446 if (log)
447 log->Printf("GDBRemoteCommunicationServerCommon::%s end", __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000448#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 return SendErrorResponse(5);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000450}
451
452GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453GDBRemoteCommunicationServerCommon::Handle_qGroupName(
454 StringExtractorGDBRemote &packet) {
455#if !defined(LLDB_DISABLE_POSIX)
456 // Packet format: "qGroupName:%i" where %i is the gid
457 packet.SetFilePos(::strlen("qGroupName:"));
458 uint32_t gid = packet.GetU32(UINT32_MAX);
459 if (gid != UINT32_MAX) {
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000460 if (llvm::Optional<llvm::StringRef> name =
461 HostInfo::GetUserIDResolver().GetGroupName(gid)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 StreamString response;
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000463 response.PutStringAsRawHex8(*name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000465 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000467#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 return SendErrorResponse(6);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000469}
470
471GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472GDBRemoteCommunicationServerCommon::Handle_qSpeedTest(
473 StringExtractorGDBRemote &packet) {
474 packet.SetFilePos(::strlen("qSpeedTest:"));
475
476 llvm::StringRef key;
477 llvm::StringRef value;
478 bool success = packet.GetNameColonValue(key, value);
479 if (success && key.equals("response_size")) {
480 uint32_t response_size = 0;
481 if (!value.getAsInteger(0, response_size)) {
482 if (response_size == 0)
483 return SendOKResponse();
484 StreamString response;
485 uint32_t bytes_left = response_size;
486 response.PutCString("data:");
487 while (bytes_left > 0) {
488 if (bytes_left >= 26) {
489 response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
490 bytes_left -= 26;
491 } else {
492 response.Printf("%*.*s;", bytes_left, bytes_left,
493 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
494 bytes_left = 0;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 }
497 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 }
500 return SendErrorResponse(7);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000501}
502
503GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504GDBRemoteCommunicationServerCommon::Handle_vFile_Open(
505 StringExtractorGDBRemote &packet) {
506 packet.SetFilePos(::strlen("vFile:open:"));
507 std::string path;
508 packet.GetHexByteStringTerminatedBy(path, ',');
509 if (!path.empty()) {
510 if (packet.GetChar() == ',') {
Aaron Smith3a142492019-02-07 18:46:25 +0000511 uint32_t flags = packet.GetHexMaxU32(false, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 if (packet.GetChar() == ',') {
513 mode_t mode = packet.GetHexMaxU32(false, 0600);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000514 FileSpec path_spec(path);
515 FileSystem::Instance().Resolve(path_spec);
Aaron Smith3a142492019-02-07 18:46:25 +0000516 File file;
517 // Do not close fd.
518 Status error =
519 FileSystem::Instance().Open(file, path_spec, flags, mode, false);
520 const int save_errno = error.GetError();
Tamas Berghammere13c2732015-02-11 10:29:30 +0000521 StreamString response;
522 response.PutChar('F');
Aaron Smith3a142492019-02-07 18:46:25 +0000523 response.Printf("%i", file.GetDescriptor());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 if (save_errno)
525 response.Printf(",%i", save_errno);
Zachary Turner26709df2016-08-27 15:52:29 +0000526 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 }
530 return SendErrorResponse(18);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000531}
532
533GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534GDBRemoteCommunicationServerCommon::Handle_vFile_Close(
535 StringExtractorGDBRemote &packet) {
536 packet.SetFilePos(::strlen("vFile:close:"));
537 int fd = packet.GetS32(-1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 int err = -1;
539 int save_errno = 0;
540 if (fd >= 0) {
Aaron Smith3a142492019-02-07 18:46:25 +0000541 File file(fd, true);
542 Status error = file.Close();
543 err = 0;
544 save_errno = error.GetError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 } else {
546 save_errno = EINVAL;
547 }
548 StreamString response;
549 response.PutChar('F');
550 response.Printf("%i", err);
551 if (save_errno)
552 response.Printf(",%i", save_errno);
553 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000554}
555
556GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557GDBRemoteCommunicationServerCommon::Handle_vFile_pRead(
558 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 StreamGDBRemote response;
560 packet.SetFilePos(::strlen("vFile:pread:"));
561 int fd = packet.GetS32(-1);
562 if (packet.GetChar() == ',') {
Aaron Smith3a142492019-02-07 18:46:25 +0000563 size_t count = packet.GetU64(UINT64_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 if (packet.GetChar() == ',') {
Aaron Smith3a142492019-02-07 18:46:25 +0000565 off_t offset = packet.GetU64(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 if (count == UINT64_MAX) {
567 response.Printf("F-1:%i", EINVAL);
Zachary Turner26709df2016-08-27 15:52:29 +0000568 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 }
570
571 std::string buffer(count, 0);
Aaron Smith3a142492019-02-07 18:46:25 +0000572 File file(fd, false);
573 Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset);
574 const ssize_t bytes_read = error.Success() ? count : -1;
575 const int save_errno = error.GetError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 response.PutChar('F');
577 response.Printf("%zi", bytes_read);
578 if (save_errno)
579 response.Printf(",%i", save_errno);
580 else {
581 response.PutChar(';');
582 response.PutEscapedBytes(&buffer[0], bytes_read);
583 }
584 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 }
587 return SendErrorResponse(21);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590GDBRemoteCommunication::PacketResult
591GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite(
592 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 packet.SetFilePos(::strlen("vFile:pwrite:"));
594
595 StreamGDBRemote response;
596 response.PutChar('F');
597
598 int fd = packet.GetU32(UINT32_MAX);
599 if (packet.GetChar() == ',') {
600 off_t offset = packet.GetU64(UINT32_MAX);
601 if (packet.GetChar() == ',') {
602 std::string buffer;
603 if (packet.GetEscapedBinaryData(buffer)) {
Aaron Smith3a142492019-02-07 18:46:25 +0000604 File file(fd, false);
605 size_t count = buffer.size();
606 Status error =
607 file.Write(static_cast<const void *>(&buffer[0]), count, offset);
608 const ssize_t bytes_written = error.Success() ? count : -1;
609 const int save_errno = error.GetError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 response.Printf("%zi", bytes_written);
611 if (save_errno)
612 response.Printf(",%i", save_errno);
613 } else {
614 response.Printf("-1,%i", EINVAL);
615 }
616 return SendPacketNoLock(response.GetString());
617 }
618 }
619 return SendErrorResponse(27);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620}
621
622GDBRemoteCommunication::PacketResult
623GDBRemoteCommunicationServerCommon::Handle_vFile_Size(
624 StringExtractorGDBRemote &packet) {
625 packet.SetFilePos(::strlen("vFile:size:"));
626 std::string path;
627 packet.GetHexByteString(path);
628 if (!path.empty()) {
Zachary Turner07db3f72017-03-21 05:47:57 +0000629 uint64_t Size;
630 if (llvm::sys::fs::file_size(path, Size))
631 return SendErrorResponse(5);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 StreamString response;
633 response.PutChar('F');
Zachary Turner07db3f72017-03-21 05:47:57 +0000634 response.PutHex64(Size);
635 if (Size == UINT64_MAX) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 response.PutChar(',');
Zachary Turner07db3f72017-03-21 05:47:57 +0000637 response.PutHex64(Size); // TODO: replace with Host::GetSyswideErrorCode()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 }
Zachary Turner26709df2016-08-27 15:52:29 +0000639 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 }
641 return SendErrorResponse(22);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000642}
643
644GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645GDBRemoteCommunicationServerCommon::Handle_vFile_Mode(
646 StringExtractorGDBRemote &packet) {
647 packet.SetFilePos(::strlen("vFile:mode:"));
648 std::string path;
649 packet.GetHexByteString(path);
650 if (!path.empty()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000651 FileSpec file_spec(path);
652 FileSystem::Instance().Resolve(file_spec);
Jonas Devlieghere73ed6072018-11-01 22:46:49 +0000653 std::error_code ec;
654 const uint32_t mode = FileSystem::Instance().GetPermissions(file_spec, ec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 StreamString response;
656 response.Printf("F%u", mode);
Jonas Devlieghere73ed6072018-11-01 22:46:49 +0000657 if (mode == 0 || ec)
658 response.Printf(",%i", (int)Status(ec).GetError());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 return SendPacketNoLock(response.GetString());
660 }
661 return SendErrorResponse(23);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000662}
663
664GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665GDBRemoteCommunicationServerCommon::Handle_vFile_Exists(
666 StringExtractorGDBRemote &packet) {
667 packet.SetFilePos(::strlen("vFile:exists:"));
668 std::string path;
669 packet.GetHexByteString(path);
670 if (!path.empty()) {
Zachary Turner07db3f72017-03-21 05:47:57 +0000671 bool retcode = llvm::sys::fs::exists(path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 StreamString response;
673 response.PutChar('F');
674 response.PutChar(',');
675 if (retcode)
676 response.PutChar('1');
Tamas Berghammere13c2732015-02-11 10:29:30 +0000677 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 response.PutChar('0');
679 return SendPacketNoLock(response.GetString());
680 }
681 return SendErrorResponse(24);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000682}
683
684GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685GDBRemoteCommunicationServerCommon::Handle_vFile_symlink(
686 StringExtractorGDBRemote &packet) {
687 packet.SetFilePos(::strlen("vFile:symlink:"));
688 std::string dst, src;
689 packet.GetHexByteStringTerminatedBy(dst, ',');
690 packet.GetChar(); // Skip ',' char
691 packet.GetHexByteString(src);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000692
693 FileSpec src_spec(src);
694 FileSystem::Instance().Resolve(src_spec);
695 Status error = FileSystem::Instance().Symlink(src_spec, FileSpec(dst));
696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 StreamString response;
698 response.Printf("F%u,%u", error.GetError(), error.GetError());
699 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000700}
701
702GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703GDBRemoteCommunicationServerCommon::Handle_vFile_unlink(
704 StringExtractorGDBRemote &packet) {
705 packet.SetFilePos(::strlen("vFile:unlink:"));
706 std::string path;
707 packet.GetHexByteString(path);
Zachary Turner97206d52017-05-12 04:51:55 +0000708 Status error(llvm::sys::fs::remove(path));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 StreamString response;
710 response.Printf("F%u,%u", error.GetError(), error.GetError());
711 return SendPacketNoLock(response.GetString());
712}
713
714GDBRemoteCommunication::PacketResult
715GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell(
716 StringExtractorGDBRemote &packet) {
717 packet.SetFilePos(::strlen("qPlatform_shell:"));
718 std::string path;
719 std::string working_dir;
720 packet.GetHexByteStringTerminatedBy(path, ',');
721 if (!path.empty()) {
722 if (packet.GetChar() == ',') {
723 // FIXME: add timeout to qPlatform_shell packet
724 // uint32_t timeout = packet.GetHexMaxU32(false, 32);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 if (packet.GetChar() == ',')
726 packet.GetHexByteString(working_dir);
727 int status, signo;
728 std::string output;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000729 FileSpec working_spec(working_dir);
730 FileSystem::Instance().Resolve(working_spec);
731 Status err =
732 Host::RunShellCommand(path.c_str(), working_spec, &status, &signo,
733 &output, std::chrono::seconds(10));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 StreamGDBRemote response;
735 if (err.Fail()) {
736 response.PutCString("F,");
737 response.PutHex32(UINT32_MAX);
738 } else {
739 response.PutCString("F,");
740 response.PutHex32(status);
741 response.PutChar(',');
742 response.PutHex32(signo);
743 response.PutChar(',');
744 response.PutEscapedBytes(output.c_str(), output.size());
745 }
746 return SendPacketNoLock(response.GetString());
747 }
748 }
749 return SendErrorResponse(24);
750}
751
752GDBRemoteCommunication::PacketResult
753GDBRemoteCommunicationServerCommon::Handle_vFile_Stat(
754 StringExtractorGDBRemote &packet) {
755 return SendUnimplementedResponse(
756 "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
757}
758
759GDBRemoteCommunication::PacketResult
760GDBRemoteCommunicationServerCommon::Handle_vFile_MD5(
761 StringExtractorGDBRemote &packet) {
762 packet.SetFilePos(::strlen("vFile:MD5:"));
763 std::string path;
764 packet.GetHexByteString(path);
765 if (!path.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 StreamGDBRemote response;
Zachary Turner076a2592017-03-20 23:54:54 +0000767 auto Result = llvm::sys::fs::md5_contents(path);
768 if (!Result) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 response.PutCString("F,");
770 response.PutCString("x");
771 } else {
772 response.PutCString("F,");
Zachary Turner076a2592017-03-20 23:54:54 +0000773 response.PutHex64(Result->low());
774 response.PutHex64(Result->high());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 }
776 return SendPacketNoLock(response.GetString());
777 }
778 return SendErrorResponse(25);
779}
780
781GDBRemoteCommunication::PacketResult
782GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir(
783 StringExtractorGDBRemote &packet) {
784 packet.SetFilePos(::strlen("qPlatform_mkdir:"));
785 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
786 if (packet.GetChar() == ',') {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000787 std::string path;
788 packet.GetHexByteString(path);
Zachary Turner97206d52017-05-12 04:51:55 +0000789 Status error(llvm::sys::fs::create_directory(path, mode));
Oleksiy Vyalov6801be32015-02-25 22:15:44 +0000790
791 StreamGDBRemote response;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 response.Printf("F%u", error.GetError());
Oleksiy Vyalov6801be32015-02-25 22:15:44 +0000793
Zachary Turner26709df2016-08-27 15:52:29 +0000794 return SendPacketNoLock(response.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 }
796 return SendErrorResponse(20);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +0000797}
798
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799GDBRemoteCommunication::PacketResult
800GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod(
801 StringExtractorGDBRemote &packet) {
802 packet.SetFilePos(::strlen("qPlatform_chmod:"));
803
Zachary Turner6934e0a2017-03-19 05:49:43 +0000804 auto perms =
805 static_cast<llvm::sys::fs::perms>(packet.GetHexMaxU32(false, UINT32_MAX));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 if (packet.GetChar() == ',') {
807 std::string path;
808 packet.GetHexByteString(path);
Zachary Turner97206d52017-05-12 04:51:55 +0000809 Status error(llvm::sys::fs::setPermissions(path, perms));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810
811 StreamGDBRemote response;
812 response.Printf("F%u", error.GetError());
813
814 return SendPacketNoLock(response.GetString());
815 }
816 return SendErrorResponse(19);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000817}
818
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819GDBRemoteCommunication::PacketResult
820GDBRemoteCommunicationServerCommon::Handle_qSupported(
821 StringExtractorGDBRemote &packet) {
822 StreamGDBRemote response;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 // Features common to lldb-platform and llgs.
825 uint32_t max_packet_size = 128 * 1024; // 128KBytes is a reasonable max packet
826 // size--debugger can always use less
827 response.Printf("PacketSize=%x", max_packet_size);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000828
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 response.PutCString(";QStartNoAckMode+");
830 response.PutCString(";QThreadSuffixSupported+");
831 response.PutCString(";QListThreadsInStopReply+");
832 response.PutCString(";qEcho+");
Kamil Rytarowskic93408a2017-03-21 17:27:59 +0000833#if defined(__linux__) || defined(__NetBSD__)
Pavel Labath4a705e72017-02-24 09:29:14 +0000834 response.PutCString(";QPassSignals+");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 response.PutCString(";qXfer:auxv:read+");
Tamas Berghammere13c2732015-02-11 10:29:30 +0000836#endif
Tamas Berghammere13c2732015-02-11 10:29:30 +0000837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 return SendPacketNoLock(response.GetString());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000839}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841GDBRemoteCommunication::PacketResult
842GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported(
843 StringExtractorGDBRemote &packet) {
844 m_thread_suffix_supported = true;
845 return SendOKResponse();
846}
847
848GDBRemoteCommunication::PacketResult
849GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply(
850 StringExtractorGDBRemote &packet) {
851 m_list_threads_in_stop_reply = true;
852 return SendOKResponse();
853}
854
855GDBRemoteCommunication::PacketResult
856GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError(
857 StringExtractorGDBRemote &packet) {
858 packet.SetFilePos(::strlen("QSetDetachOnError:"));
859 if (packet.GetU32(0))
860 m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
861 else
862 m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError);
863 return SendOKResponse();
864}
865
866GDBRemoteCommunication::PacketResult
867GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode(
868 StringExtractorGDBRemote &packet) {
869 // Send response first before changing m_send_acks to we ack this packet
870 PacketResult packet_result = SendOKResponse();
871 m_send_acks = false;
872 return packet_result;
873}
874
875GDBRemoteCommunication::PacketResult
876GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN(
877 StringExtractorGDBRemote &packet) {
878 packet.SetFilePos(::strlen("QSetSTDIN:"));
879 FileAction file_action;
880 std::string path;
881 packet.GetHexByteString(path);
882 const bool read = true;
883 const bool write = false;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000884 if (file_action.Open(STDIN_FILENO, FileSpec(path), read, write)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 m_process_launch_info.AppendFileAction(file_action);
886 return SendOKResponse();
887 }
888 return SendErrorResponse(15);
889}
890
891GDBRemoteCommunication::PacketResult
892GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT(
893 StringExtractorGDBRemote &packet) {
894 packet.SetFilePos(::strlen("QSetSTDOUT:"));
895 FileAction file_action;
896 std::string path;
897 packet.GetHexByteString(path);
898 const bool read = false;
899 const bool write = true;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000900 if (file_action.Open(STDOUT_FILENO, FileSpec(path), read, write)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 m_process_launch_info.AppendFileAction(file_action);
902 return SendOKResponse();
903 }
904 return SendErrorResponse(16);
905}
906
907GDBRemoteCommunication::PacketResult
908GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR(
909 StringExtractorGDBRemote &packet) {
910 packet.SetFilePos(::strlen("QSetSTDERR:"));
911 FileAction file_action;
912 std::string path;
913 packet.GetHexByteString(path);
914 const bool read = false;
915 const bool write = true;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000916 if (file_action.Open(STDERR_FILENO, FileSpec(path), read, write)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 m_process_launch_info.AppendFileAction(file_action);
918 return SendOKResponse();
919 }
920 return SendErrorResponse(17);
921}
922
923GDBRemoteCommunication::PacketResult
924GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess(
925 StringExtractorGDBRemote &packet) {
926 if (m_process_launch_error.Success())
927 return SendOKResponse();
928 StreamString response;
929 response.PutChar('E');
930 response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
931 return SendPacketNoLock(response.GetString());
932}
933
934GDBRemoteCommunication::PacketResult
935GDBRemoteCommunicationServerCommon::Handle_QEnvironment(
936 StringExtractorGDBRemote &packet) {
937 packet.SetFilePos(::strlen("QEnvironment:"));
938 const uint32_t bytes_left = packet.GetBytesLeft();
939 if (bytes_left > 0) {
Pavel Labath62930e52018-01-10 11:57:31 +0000940 m_process_launch_info.GetEnvironment().insert(packet.Peek());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 return SendOKResponse();
942 }
943 return SendErrorResponse(12);
944}
945
946GDBRemoteCommunication::PacketResult
947GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded(
948 StringExtractorGDBRemote &packet) {
949 packet.SetFilePos(::strlen("QEnvironmentHexEncoded:"));
950 const uint32_t bytes_left = packet.GetBytesLeft();
951 if (bytes_left > 0) {
952 std::string str;
953 packet.GetHexByteString(str);
Pavel Labath62930e52018-01-10 11:57:31 +0000954 m_process_launch_info.GetEnvironment().insert(str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 return SendOKResponse();
956 }
957 return SendErrorResponse(12);
958}
959
960GDBRemoteCommunication::PacketResult
961GDBRemoteCommunicationServerCommon::Handle_QLaunchArch(
962 StringExtractorGDBRemote &packet) {
963 packet.SetFilePos(::strlen("QLaunchArch:"));
964 const uint32_t bytes_left = packet.GetBytesLeft();
965 if (bytes_left > 0) {
966 const char *arch_triple = packet.Peek();
Aaron Smith3a142492019-02-07 18:46:25 +0000967 m_process_launch_info.SetArchitecture(
968 HostInfo::GetAugmentedArchSpec(arch_triple));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 return SendOKResponse();
970 }
971 return SendErrorResponse(13);
972}
973
974GDBRemoteCommunication::PacketResult
975GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) {
Adrian Prantl05097242018-04-30 16:49:04 +0000976 // The 'A' packet is the most over designed packet ever here with redundant
977 // argument indexes, redundant argument lengths and needed hex encoded
978 // argument string values. Really all that is needed is a comma separated hex
979 // encoded argument value list, but we will stay true to the documented
980 // version of the 'A' packet here...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981
982 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
983 int actual_arg_index = 0;
984
985 packet.SetFilePos(1); // Skip the 'A'
986 bool success = true;
987 while (success && packet.GetBytesLeft() > 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000988 // Decode the decimal argument string length. This length is the number of
989 // hex nibbles in the argument string value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 const uint32_t arg_len = packet.GetU32(UINT32_MAX);
991 if (arg_len == UINT32_MAX)
992 success = false;
993 else {
994 // Make sure the argument hex string length is followed by a comma
995 if (packet.GetChar() != ',')
996 success = false;
997 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000998 // Decode the argument index. We ignore this really because who would
999 // really send down the arguments in a random order???
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1001 if (arg_idx == UINT32_MAX)
1002 success = false;
1003 else {
1004 // Make sure the argument index is followed by a comma
1005 if (packet.GetChar() != ',')
1006 success = false;
1007 else {
Adrian Prantl05097242018-04-30 16:49:04 +00001008 // Decode the argument string value from hex bytes back into a UTF8
1009 // string and make sure the length matches the one supplied in the
1010 // packet
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 std::string arg;
1012 if (packet.GetHexByteStringFixedLength(arg, arg_len) !=
1013 (arg_len / 2))
1014 success = false;
1015 else {
1016 // If there are any bytes left
1017 if (packet.GetBytesLeft()) {
1018 if (packet.GetChar() != ',')
1019 success = false;
1020 }
1021
1022 if (success) {
1023 if (arg_idx == 0)
Jonas Devlieghere937348c2018-06-13 22:08:14 +00001024 m_process_launch_info.GetExecutableFile().SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001025 arg, FileSpec::Style::native);
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001026 m_process_launch_info.GetArguments().AppendArgument(arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 if (log)
1028 log->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"",
1029 __FUNCTION__, actual_arg_index, arg.c_str());
1030 ++actual_arg_index;
1031 }
1032 }
1033 }
1034 }
1035 }
1036 }
1037 }
1038
1039 if (success) {
1040 m_process_launch_error = LaunchProcess();
Pavel Labath96e600f2017-07-07 11:02:19 +00001041 if (m_process_launch_error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 return SendOKResponse();
Pavel Labath96e600f2017-07-07 11:02:19 +00001043 LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 }
1045 return SendErrorResponse(8);
1046}
1047
1048GDBRemoteCommunication::PacketResult
1049GDBRemoteCommunicationServerCommon::Handle_qEcho(
1050 StringExtractorGDBRemote &packet) {
1051 // Just echo back the exact same packet for qEcho...
1052 return SendPacketNoLock(packet.GetStringRef());
1053}
1054
1055GDBRemoteCommunication::PacketResult
1056GDBRemoteCommunicationServerCommon::Handle_qModuleInfo(
1057 StringExtractorGDBRemote &packet) {
1058 packet.SetFilePos(::strlen("qModuleInfo:"));
1059
1060 std::string module_path;
1061 packet.GetHexByteStringTerminatedBy(module_path, ';');
1062 if (module_path.empty())
1063 return SendErrorResponse(1);
1064
1065 if (packet.GetChar() != ';')
1066 return SendErrorResponse(2);
1067
1068 std::string triple;
1069 packet.GetHexByteString(triple);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001071 ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple);
1072 if (!matched_module_spec.GetFileSpec())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 return SendErrorResponse(3);
1074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 const auto file_offset = matched_module_spec.GetObjectOffset();
1076 const auto file_size = matched_module_spec.GetObjectSize();
1077 const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1078
1079 StreamGDBRemote response;
1080
1081 if (uuid_str.empty()) {
Aaron Smith3a142492019-02-07 18:46:25 +00001082 auto Result = llvm::sys::fs::md5_contents(
1083 matched_module_spec.GetFileSpec().GetPath());
Zachary Turner076a2592017-03-20 23:54:54 +00001084 if (!Result)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 return SendErrorResponse(5);
1086 response.PutCString("md5:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001087 response.PutStringAsRawHex8(Result->digest());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 } else {
1089 response.PutCString("uuid:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001090 response.PutStringAsRawHex8(uuid_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 }
1092 response.PutChar(';');
1093
1094 const auto &module_arch = matched_module_spec.GetArchitecture();
1095 response.PutCString("triple:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001096 response.PutStringAsRawHex8(module_arch.GetTriple().getTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 response.PutChar(';');
1098
1099 response.PutCString("file_path:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001100 response.PutStringAsRawHex8(matched_module_spec.GetFileSpec().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 response.PutChar(';');
1102 response.PutCString("file_offset:");
1103 response.PutHex64(file_offset);
1104 response.PutChar(';');
1105 response.PutCString("file_size:");
1106 response.PutHex64(file_size);
1107 response.PutChar(';');
1108
1109 return SendPacketNoLock(response.GetString());
1110}
1111
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001112GDBRemoteCommunication::PacketResult
1113GDBRemoteCommunicationServerCommon::Handle_jModulesInfo(
1114 StringExtractorGDBRemote &packet) {
1115 packet.SetFilePos(::strlen("jModulesInfo:"));
1116
1117 StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek());
1118 if (!object_sp)
1119 return SendErrorResponse(1);
1120
1121 StructuredData::Array *packet_array = object_sp->GetAsArray();
1122 if (!packet_array)
1123 return SendErrorResponse(2);
1124
1125 JSONArray::SP response_array_sp = std::make_shared<JSONArray>();
1126 for (size_t i = 0; i < packet_array->GetSize(); ++i) {
1127 StructuredData::Dictionary *query =
1128 packet_array->GetItemAtIndex(i)->GetAsDictionary();
1129 if (!query)
1130 continue;
Zachary Turner28333212017-05-12 05:49:54 +00001131 llvm::StringRef file, triple;
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001132 if (!query->GetValueForKeyAsString("file", file) ||
1133 !query->GetValueForKeyAsString("triple", triple))
1134 continue;
1135
1136 ModuleSpec matched_module_spec = GetModuleInfo(file, triple);
1137 if (!matched_module_spec.GetFileSpec())
1138 continue;
1139
1140 const auto file_offset = matched_module_spec.GetObjectOffset();
1141 const auto file_size = matched_module_spec.GetObjectSize();
1142 const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1143
1144 if (uuid_str.empty())
1145 continue;
1146
1147 JSONObject::SP response = std::make_shared<JSONObject>();
1148 response_array_sp->AppendObject(response);
1149 response->SetObject("uuid", std::make_shared<JSONString>(uuid_str));
1150 response->SetObject(
1151 "triple",
1152 std::make_shared<JSONString>(
1153 matched_module_spec.GetArchitecture().GetTriple().getTriple()));
1154 response->SetObject("file_path",
1155 std::make_shared<JSONString>(
1156 matched_module_spec.GetFileSpec().GetPath()));
1157 response->SetObject("file_offset",
1158 std::make_shared<JSONNumber>(file_offset));
1159 response->SetObject("file_size", std::make_shared<JSONNumber>(file_size));
1160 }
1161
1162 StreamString response;
1163 response_array_sp->Write(response);
1164 StreamGDBRemote escaped_response;
Zachary Turnerc1564272016-11-16 21:15:24 +00001165 escaped_response.PutEscapedBytes(response.GetString().data(),
1166 response.GetSize());
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001167 return SendPacketNoLock(escaped_response.GetString());
1168}
1169
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse(
1171 const ProcessInstanceInfo &proc_info, StreamString &response) {
1172 response.Printf(
1173 "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1174 proc_info.GetProcessID(), proc_info.GetParentProcessID(),
1175 proc_info.GetUserID(), proc_info.GetGroupID(),
1176 proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID());
1177 response.PutCString("name:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001178 response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 response.PutChar(';');
1180 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1181 if (proc_arch.IsValid()) {
1182 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1183 response.PutCString("triple:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001184 response.PutStringAsRawHex8(proc_triple.getTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 response.PutChar(';');
1186 }
1187}
1188
1189void GDBRemoteCommunicationServerCommon::
1190 CreateProcessInfoResponse_DebugServerStyle(
1191 const ProcessInstanceInfo &proc_info, StreamString &response) {
1192 response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64
1193 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1194 proc_info.GetProcessID(), proc_info.GetParentProcessID(),
1195 proc_info.GetUserID(), proc_info.GetGroupID(),
1196 proc_info.GetEffectiveUserID(),
1197 proc_info.GetEffectiveGroupID());
1198
1199 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1200 if (proc_arch.IsValid()) {
1201 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1202#if defined(__APPLE__)
1203 // We'll send cputype/cpusubtype.
1204 const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1205 if (cpu_type != 0)
1206 response.Printf("cputype:%" PRIx32 ";", cpu_type);
1207
1208 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1209 if (cpu_subtype != 0)
1210 response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype);
1211
1212 const std::string vendor = proc_triple.getVendorName();
1213 if (!vendor.empty())
1214 response.Printf("vendor:%s;", vendor.c_str());
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001215#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 // We'll send the triple.
1217 response.PutCString("triple:");
Pavel Labath7f815a92019-02-12 14:28:55 +00001218 response.PutStringAsRawHex8(proc_triple.getTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 response.PutChar(';');
1220#endif
1221 std::string ostype = proc_triple.getOSName();
1222 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1223 if (proc_triple.getVendor() == llvm::Triple::Apple) {
1224 switch (proc_triple.getArch()) {
1225 case llvm::Triple::arm:
1226 case llvm::Triple::thumb:
1227 case llvm::Triple::aarch64:
1228 ostype = "ios";
1229 break;
1230 default:
1231 // No change.
1232 break;
1233 }
1234 }
1235 response.Printf("ostype:%s;", ostype.c_str());
1236
1237 switch (proc_arch.GetByteOrder()) {
1238 case lldb::eByteOrderLittle:
1239 response.PutCString("endian:little;");
1240 break;
1241 case lldb::eByteOrderBig:
1242 response.PutCString("endian:big;");
1243 break;
1244 case lldb::eByteOrderPDP:
1245 response.PutCString("endian:pdp;");
1246 break;
1247 default:
1248 // Nothing.
1249 break;
1250 }
Adrian Prantl05097242018-04-30 16:49:04 +00001251 // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer
1252 // size is 4 and for N64 it is 8
Nitesh Jain8999edf2016-10-12 10:21:09 +00001253 std::string abi = proc_arch.GetTargetABI();
1254 if (!abi.empty())
1255 response.Printf("elf_abi:%s;", abi.c_str());
1256 response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 }
1258}
1259
1260FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile(
1261 const std::string &module_path, const ArchSpec &arch) {
1262#ifdef __ANDROID__
1263 return HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1264#else
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001265 FileSpec file_spec(module_path);
1266 FileSystem::Instance().Resolve(file_spec);
1267 return file_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001268#endif
1269}
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001270
Zachary Turner28333212017-05-12 05:49:54 +00001271ModuleSpec
1272GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path,
1273 llvm::StringRef triple) {
1274 ArchSpec arch(triple);
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001275
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001276 FileSpec req_module_path_spec(module_path);
1277 FileSystem::Instance().Resolve(req_module_path_spec);
1278
Pavel Labath2f1fbae2016-09-08 10:07:04 +00001279 const FileSpec module_path_spec =
1280 FindModuleFile(req_module_path_spec.GetPath(), arch);
1281 const ModuleSpec module_spec(module_path_spec, arch);
1282
1283 ModuleSpecList module_specs;
1284 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0,
1285 module_specs))
1286 return ModuleSpec();
1287
1288 ModuleSpec matched_module_spec;
1289 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1290 return ModuleSpec();
1291
1292 return matched_module_spec;
1293}