File: | build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp |
Warning: | line 519, column 13 Value stored to 'descriptor' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- GDBRemoteCommunicationServerCommon.cpp ----------------------------===// |
2 | // |
3 | // 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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "GDBRemoteCommunicationServerCommon.h" |
10 | |
11 | #include <cerrno> |
12 | |
13 | #ifdef __APPLE__ |
14 | #include <TargetConditionals.h> |
15 | #endif |
16 | |
17 | #include <chrono> |
18 | #include <cstring> |
19 | |
20 | #include "lldb/Core/ModuleSpec.h" |
21 | #include "lldb/Host/Config.h" |
22 | #include "lldb/Host/File.h" |
23 | #include "lldb/Host/FileAction.h" |
24 | #include "lldb/Host/FileSystem.h" |
25 | #include "lldb/Host/Host.h" |
26 | #include "lldb/Host/HostInfo.h" |
27 | #include "lldb/Host/SafeMachO.h" |
28 | #include "lldb/Interpreter/OptionArgParser.h" |
29 | #include "lldb/Symbol/ObjectFile.h" |
30 | #include "lldb/Target/Platform.h" |
31 | #include "lldb/Utility/Endian.h" |
32 | #include "lldb/Utility/GDBRemote.h" |
33 | #include "lldb/Utility/LLDBLog.h" |
34 | #include "lldb/Utility/Log.h" |
35 | #include "lldb/Utility/StreamString.h" |
36 | #include "lldb/Utility/StructuredData.h" |
37 | #include "llvm/ADT/StringSwitch.h" |
38 | #include "llvm/ADT/Triple.h" |
39 | #include "llvm/Support/JSON.h" |
40 | |
41 | #include "ProcessGDBRemoteLog.h" |
42 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
43 | |
44 | #ifdef __ANDROID__ |
45 | #include "lldb/Host/android/HostInfoAndroid.h" |
46 | #endif |
47 | |
48 | |
49 | using namespace lldb; |
50 | using namespace lldb_private::process_gdb_remote; |
51 | using namespace lldb_private; |
52 | |
53 | #ifdef __ANDROID__ |
54 | const static uint32_t g_default_packet_timeout_sec = 20; // seconds |
55 | #else |
56 | const static uint32_t g_default_packet_timeout_sec = 0; // not specified |
57 | #endif |
58 | |
59 | // GDBRemoteCommunicationServerCommon constructor |
60 | GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon( |
61 | const char *comm_name, const char *listener_name) |
62 | : GDBRemoteCommunicationServer(comm_name, listener_name), |
63 | m_process_launch_info(), m_process_launch_error(), m_proc_infos(), |
64 | m_proc_infos_index(0) { |
65 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A, |
66 | &GDBRemoteCommunicationServerCommon::Handle_A); |
67 | RegisterMemberFunctionHandler( |
68 | StringExtractorGDBRemote::eServerPacketType_QEnvironment, |
69 | &GDBRemoteCommunicationServerCommon::Handle_QEnvironment); |
70 | RegisterMemberFunctionHandler( |
71 | StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded, |
72 | &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded); |
73 | RegisterMemberFunctionHandler( |
74 | StringExtractorGDBRemote::eServerPacketType_qfProcessInfo, |
75 | &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo); |
76 | RegisterMemberFunctionHandler( |
77 | StringExtractorGDBRemote::eServerPacketType_qGroupName, |
78 | &GDBRemoteCommunicationServerCommon::Handle_qGroupName); |
79 | RegisterMemberFunctionHandler( |
80 | StringExtractorGDBRemote::eServerPacketType_qHostInfo, |
81 | &GDBRemoteCommunicationServerCommon::Handle_qHostInfo); |
82 | RegisterMemberFunctionHandler( |
83 | StringExtractorGDBRemote::eServerPacketType_QLaunchArch, |
84 | &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch); |
85 | RegisterMemberFunctionHandler( |
86 | StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess, |
87 | &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess); |
88 | RegisterMemberFunctionHandler( |
89 | StringExtractorGDBRemote::eServerPacketType_qEcho, |
90 | &GDBRemoteCommunicationServerCommon::Handle_qEcho); |
91 | RegisterMemberFunctionHandler( |
92 | StringExtractorGDBRemote::eServerPacketType_qModuleInfo, |
93 | &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo); |
94 | RegisterMemberFunctionHandler( |
95 | StringExtractorGDBRemote::eServerPacketType_jModulesInfo, |
96 | &GDBRemoteCommunicationServerCommon::Handle_jModulesInfo); |
97 | RegisterMemberFunctionHandler( |
98 | StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod, |
99 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod); |
100 | RegisterMemberFunctionHandler( |
101 | StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir, |
102 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir); |
103 | RegisterMemberFunctionHandler( |
104 | StringExtractorGDBRemote::eServerPacketType_qPlatform_shell, |
105 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell); |
106 | RegisterMemberFunctionHandler( |
107 | StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID, |
108 | &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID); |
109 | RegisterMemberFunctionHandler( |
110 | StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError, |
111 | &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError); |
112 | RegisterMemberFunctionHandler( |
113 | StringExtractorGDBRemote::eServerPacketType_QSetSTDERR, |
114 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR); |
115 | RegisterMemberFunctionHandler( |
116 | StringExtractorGDBRemote::eServerPacketType_QSetSTDIN, |
117 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN); |
118 | RegisterMemberFunctionHandler( |
119 | StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT, |
120 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT); |
121 | RegisterMemberFunctionHandler( |
122 | StringExtractorGDBRemote::eServerPacketType_qSpeedTest, |
123 | &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest); |
124 | RegisterMemberFunctionHandler( |
125 | StringExtractorGDBRemote::eServerPacketType_qsProcessInfo, |
126 | &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo); |
127 | RegisterMemberFunctionHandler( |
128 | StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode, |
129 | &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode); |
130 | RegisterMemberFunctionHandler( |
131 | StringExtractorGDBRemote::eServerPacketType_qSupported, |
132 | &GDBRemoteCommunicationServerCommon::Handle_qSupported); |
133 | RegisterMemberFunctionHandler( |
134 | StringExtractorGDBRemote::eServerPacketType_qUserName, |
135 | &GDBRemoteCommunicationServerCommon::Handle_qUserName); |
136 | RegisterMemberFunctionHandler( |
137 | StringExtractorGDBRemote::eServerPacketType_vFile_close, |
138 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Close); |
139 | RegisterMemberFunctionHandler( |
140 | StringExtractorGDBRemote::eServerPacketType_vFile_exists, |
141 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists); |
142 | RegisterMemberFunctionHandler( |
143 | StringExtractorGDBRemote::eServerPacketType_vFile_md5, |
144 | &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5); |
145 | RegisterMemberFunctionHandler( |
146 | StringExtractorGDBRemote::eServerPacketType_vFile_mode, |
147 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode); |
148 | RegisterMemberFunctionHandler( |
149 | StringExtractorGDBRemote::eServerPacketType_vFile_open, |
150 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Open); |
151 | RegisterMemberFunctionHandler( |
152 | StringExtractorGDBRemote::eServerPacketType_vFile_pread, |
153 | &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead); |
154 | RegisterMemberFunctionHandler( |
155 | StringExtractorGDBRemote::eServerPacketType_vFile_pwrite, |
156 | &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite); |
157 | RegisterMemberFunctionHandler( |
158 | StringExtractorGDBRemote::eServerPacketType_vFile_size, |
159 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Size); |
160 | RegisterMemberFunctionHandler( |
161 | StringExtractorGDBRemote::eServerPacketType_vFile_fstat, |
162 | &GDBRemoteCommunicationServerCommon::Handle_vFile_FStat); |
163 | RegisterMemberFunctionHandler( |
164 | StringExtractorGDBRemote::eServerPacketType_vFile_stat, |
165 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat); |
166 | RegisterMemberFunctionHandler( |
167 | StringExtractorGDBRemote::eServerPacketType_vFile_symlink, |
168 | &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink); |
169 | RegisterMemberFunctionHandler( |
170 | StringExtractorGDBRemote::eServerPacketType_vFile_unlink, |
171 | &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink); |
172 | } |
173 | |
174 | // Destructor |
175 | GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon() = |
176 | default; |
177 | |
178 | GDBRemoteCommunication::PacketResult |
179 | GDBRemoteCommunicationServerCommon::Handle_qHostInfo( |
180 | StringExtractorGDBRemote &packet) { |
181 | StreamString response; |
182 | |
183 | // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00 |
184 | |
185 | ArchSpec host_arch(HostInfo::GetArchitecture()); |
186 | const llvm::Triple &host_triple = host_arch.GetTriple(); |
187 | response.PutCString("triple:"); |
188 | response.PutStringAsRawHex8(host_triple.getTriple()); |
189 | response.Printf(";ptrsize:%u;", host_arch.GetAddressByteSize()); |
190 | |
191 | const char *distribution_id = host_arch.GetDistributionId().AsCString(); |
192 | if (distribution_id) { |
193 | response.PutCString("distribution_id:"); |
194 | response.PutStringAsRawHex8(distribution_id); |
195 | response.PutCString(";"); |
196 | } |
197 | |
198 | #if defined(__APPLE__) |
199 | // For parity with debugserver, we'll include the vendor key. |
200 | response.PutCString("vendor:apple;"); |
201 | |
202 | // Send out MachO info. |
203 | uint32_t cpu = host_arch.GetMachOCPUType(); |
204 | uint32_t sub = host_arch.GetMachOCPUSubType(); |
205 | if (cpu != LLDB_INVALID_CPUTYPE(0xFFFFFFFEu)) |
206 | response.Printf("cputype:%u;", cpu); |
207 | if (sub != LLDB_INVALID_CPUTYPE(0xFFFFFFFEu)) |
208 | response.Printf("cpusubtype:%u;", sub); |
209 | |
210 | if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) { |
211 | // Indicate the OS type. |
212 | #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 |
213 | response.PutCString("ostype:tvos;"); |
214 | #elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 |
215 | response.PutCString("ostype:watchos;"); |
216 | #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 |
217 | response.PutCString("ostype:bridgeos;"); |
218 | #else |
219 | response.PutCString("ostype:ios;"); |
220 | #endif |
221 | |
222 | // On arm, we use "synchronous" watchpoints which means the exception is |
223 | // delivered before the instruction executes. |
224 | response.PutCString("watchpoint_exceptions_received:before;"); |
225 | } else { |
226 | response.PutCString("ostype:macosx;"); |
227 | response.Printf("watchpoint_exceptions_received:after;"); |
228 | } |
229 | |
230 | #else |
231 | if (host_arch.GetMachine() == llvm::Triple::aarch64 || |
232 | host_arch.GetMachine() == llvm::Triple::aarch64_32 || |
233 | host_arch.GetMachine() == llvm::Triple::aarch64_be || |
234 | host_arch.GetMachine() == llvm::Triple::arm || |
235 | host_arch.GetMachine() == llvm::Triple::armeb || host_arch.IsMIPS()) |
236 | response.Printf("watchpoint_exceptions_received:before;"); |
237 | else |
238 | response.Printf("watchpoint_exceptions_received:after;"); |
239 | #endif |
240 | |
241 | switch (endian::InlHostByteOrder()) { |
242 | case eByteOrderBig: |
243 | response.PutCString("endian:big;"); |
244 | break; |
245 | case eByteOrderLittle: |
246 | response.PutCString("endian:little;"); |
247 | break; |
248 | case eByteOrderPDP: |
249 | response.PutCString("endian:pdp;"); |
250 | break; |
251 | default: |
252 | response.PutCString("endian:unknown;"); |
253 | break; |
254 | } |
255 | |
256 | llvm::VersionTuple version = HostInfo::GetOSVersion(); |
257 | if (!version.empty()) { |
258 | response.Format("os_version:{0}", version.getAsString()); |
259 | response.PutChar(';'); |
260 | } |
261 | |
262 | #if defined(__APPLE__) |
263 | llvm::VersionTuple maccatalyst_version = HostInfo::GetMacCatalystVersion(); |
264 | if (!maccatalyst_version.empty()) { |
265 | response.Format("maccatalyst_version:{0}", |
266 | maccatalyst_version.getAsString()); |
267 | response.PutChar(';'); |
268 | } |
269 | #endif |
270 | |
271 | if (llvm::Optional<std::string> s = HostInfo::GetOSBuildString()) { |
272 | response.PutCString("os_build:"); |
273 | response.PutStringAsRawHex8(*s); |
274 | response.PutChar(';'); |
275 | } |
276 | if (llvm::Optional<std::string> s = HostInfo::GetOSKernelDescription()) { |
277 | response.PutCString("os_kernel:"); |
278 | response.PutStringAsRawHex8(*s); |
279 | response.PutChar(';'); |
280 | } |
281 | |
282 | std::string s; |
283 | #if defined(__APPLE__) |
284 | |
285 | #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
286 | // 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 |
289 | response.PutCString("hostname:"); |
290 | response.PutStringAsRawHex8("127.0.0.1"); |
291 | response.PutChar(';'); |
292 | #else // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
293 | if (HostInfo::GetHostname(s)) { |
294 | response.PutCString("hostname:"); |
295 | response.PutStringAsRawHex8(s); |
296 | response.PutChar(';'); |
297 | } |
298 | #endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
299 | |
300 | #else // #if defined(__APPLE__) |
301 | if (HostInfo::GetHostname(s)) { |
302 | response.PutCString("hostname:"); |
303 | response.PutStringAsRawHex8(s); |
304 | response.PutChar(';'); |
305 | } |
306 | #endif // #if defined(__APPLE__) |
307 | |
308 | if (g_default_packet_timeout_sec > 0) |
309 | response.Printf("default_packet_timeout:%u;", g_default_packet_timeout_sec); |
310 | |
311 | return SendPacketNoLock(response.GetString()); |
312 | } |
313 | |
314 | GDBRemoteCommunication::PacketResult |
315 | GDBRemoteCommunicationServerCommon::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_ID0); |
320 | if (pid != LLDB_INVALID_PROCESS_ID0) { |
321 | ProcessInstanceInfo proc_info; |
322 | if (Host::GetProcessInfo(pid, proc_info)) { |
323 | StreamString response; |
324 | CreateProcessInfoResponse(proc_info, response); |
325 | return SendPacketNoLock(response.GetString()); |
326 | } |
327 | } |
328 | return SendErrorResponse(1); |
329 | } |
330 | |
331 | GDBRemoteCommunication::PacketResult |
332 | GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo( |
333 | StringExtractorGDBRemote &packet) { |
334 | m_proc_infos_index = 0; |
335 | m_proc_infos.clear(); |
336 | |
337 | ProcessInstanceInfoMatch match_info; |
338 | packet.SetFilePos(::strlen("qfProcessInfo")); |
339 | if (packet.GetChar() == ':') { |
340 | llvm::StringRef key; |
341 | llvm::StringRef value; |
342 | 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); |
348 | match_info.GetProcessInfo().GetExecutableFile().SetFile( |
349 | file, FileSpec::Style::native); |
350 | } else if (key.equals("name_match")) { |
351 | 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); |
358 | match_info.SetNameMatchType(name_match); |
359 | if (name_match == NameMatch::Ignore) |
360 | return SendErrorResponse(2); |
361 | } else if (key.equals("pid")) { |
362 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID0; |
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_ID0; |
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(4294967295U); |
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(4294967295U); |
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(4294967295U); |
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(4294967295U); |
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( |
393 | OptionArgParser::ToBoolean(value, false, &success)); |
394 | } else if (key.equals("triple")) { |
395 | match_info.GetProcessInfo().GetArchitecture() = |
396 | HostInfo::GetAugmentedArchSpec(value); |
397 | } else { |
398 | success = false; |
399 | } |
400 | |
401 | if (!success) |
402 | return SendErrorResponse(2); |
403 | } |
404 | } |
405 | |
406 | if (Host::FindProcesses(match_info, m_proc_infos)) { |
407 | // We found something, return the first item by calling the get subsequent |
408 | // process info packet handler... |
409 | return Handle_qsProcessInfo(packet); |
410 | } |
411 | return SendErrorResponse(3); |
412 | } |
413 | |
414 | GDBRemoteCommunication::PacketResult |
415 | GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo( |
416 | StringExtractorGDBRemote &packet) { |
417 | if (m_proc_infos_index < m_proc_infos.size()) { |
418 | StreamString response; |
419 | CreateProcessInfoResponse(m_proc_infos[m_proc_infos_index], response); |
420 | ++m_proc_infos_index; |
421 | return SendPacketNoLock(response.GetString()); |
422 | } |
423 | return SendErrorResponse(4); |
424 | } |
425 | |
426 | GDBRemoteCommunication::PacketResult |
427 | GDBRemoteCommunicationServerCommon::Handle_qUserName( |
428 | StringExtractorGDBRemote &packet) { |
429 | #if LLDB_ENABLE_POSIX1 |
430 | Log *log = GetLog(LLDBLog::Process); |
431 | LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s begin", __FUNCTION__)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("GDBRemoteCommunicationServerCommon::%s begin" , __FUNCTION__); } while (0); |
432 | |
433 | // Packet format: "qUserName:%i" where %i is the uid |
434 | packet.SetFilePos(::strlen("qUserName:")); |
435 | uint32_t uid = packet.GetU32(UINT32_MAX(4294967295U)); |
436 | if (uid != UINT32_MAX(4294967295U)) { |
437 | if (llvm::Optional<llvm::StringRef> name = |
438 | HostInfo::GetUserIDResolver().GetUserName(uid)) { |
439 | StreamString response; |
440 | response.PutStringAsRawHex8(*name); |
441 | return SendPacketNoLock(response.GetString()); |
442 | } |
443 | } |
444 | LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s end", __FUNCTION__)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("GDBRemoteCommunicationServerCommon::%s end" , __FUNCTION__); } while (0); |
445 | #endif |
446 | return SendErrorResponse(5); |
447 | } |
448 | |
449 | GDBRemoteCommunication::PacketResult |
450 | GDBRemoteCommunicationServerCommon::Handle_qGroupName( |
451 | StringExtractorGDBRemote &packet) { |
452 | #if LLDB_ENABLE_POSIX1 |
453 | // Packet format: "qGroupName:%i" where %i is the gid |
454 | packet.SetFilePos(::strlen("qGroupName:")); |
455 | uint32_t gid = packet.GetU32(UINT32_MAX(4294967295U)); |
456 | if (gid != UINT32_MAX(4294967295U)) { |
457 | if (llvm::Optional<llvm::StringRef> name = |
458 | HostInfo::GetUserIDResolver().GetGroupName(gid)) { |
459 | StreamString response; |
460 | response.PutStringAsRawHex8(*name); |
461 | return SendPacketNoLock(response.GetString()); |
462 | } |
463 | } |
464 | #endif |
465 | return SendErrorResponse(6); |
466 | } |
467 | |
468 | GDBRemoteCommunication::PacketResult |
469 | GDBRemoteCommunicationServerCommon::Handle_qSpeedTest( |
470 | StringExtractorGDBRemote &packet) { |
471 | packet.SetFilePos(::strlen("qSpeedTest:")); |
472 | |
473 | llvm::StringRef key; |
474 | llvm::StringRef value; |
475 | bool success = packet.GetNameColonValue(key, value); |
476 | if (success && key.equals("response_size")) { |
477 | uint32_t response_size = 0; |
478 | if (!value.getAsInteger(0, response_size)) { |
479 | if (response_size == 0) |
480 | return SendOKResponse(); |
481 | StreamString response; |
482 | uint32_t bytes_left = response_size; |
483 | response.PutCString("data:"); |
484 | while (bytes_left > 0) { |
485 | if (bytes_left >= 26) { |
486 | response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
487 | bytes_left -= 26; |
488 | } else { |
489 | response.Printf("%*.*s;", bytes_left, bytes_left, |
490 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
491 | bytes_left = 0; |
492 | } |
493 | } |
494 | return SendPacketNoLock(response.GetString()); |
495 | } |
496 | } |
497 | return SendErrorResponse(7); |
498 | } |
499 | |
500 | GDBRemoteCommunication::PacketResult |
501 | GDBRemoteCommunicationServerCommon::Handle_vFile_Open( |
502 | StringExtractorGDBRemote &packet) { |
503 | packet.SetFilePos(::strlen("vFile:open:")); |
504 | std::string path; |
505 | packet.GetHexByteStringTerminatedBy(path, ','); |
506 | if (!path.empty()) { |
507 | if (packet.GetChar() == ',') { |
508 | auto flags = File::OpenOptions(packet.GetHexMaxU32(false, 0)); |
509 | if (packet.GetChar() == ',') { |
510 | mode_t mode = packet.GetHexMaxU32(false, 0600); |
511 | FileSpec path_spec(path); |
512 | FileSystem::Instance().Resolve(path_spec); |
513 | // Do not close fd. |
514 | auto file = FileSystem::Instance().Open(path_spec, flags, mode, false); |
515 | |
516 | StreamString response; |
517 | response.PutChar('F'); |
518 | |
519 | int descriptor = File::kInvalidDescriptor; |
Value stored to 'descriptor' during its initialization is never read | |
520 | if (file) { |
521 | descriptor = file.get()->GetDescriptor(); |
522 | response.Printf("%x", descriptor); |
523 | } else { |
524 | response.PutCString("-1"); |
525 | std::error_code code = errorToErrorCode(file.takeError()); |
526 | if (code.category() == std::system_category()) { |
527 | response.Printf(",%x", code.value()); |
528 | } |
529 | } |
530 | |
531 | return SendPacketNoLock(response.GetString()); |
532 | } |
533 | } |
534 | } |
535 | return SendErrorResponse(18); |
536 | } |
537 | |
538 | static GDBErrno system_errno_to_gdb(int err) { |
539 | switch (err) { |
540 | #define HANDLE_ERRNO(name, value) \ |
541 | case name: \ |
542 | return GDB_##name; |
543 | #include "Plugins/Process/gdb-remote/GDBRemoteErrno.def" |
544 | default: |
545 | return GDB_EUNKNOWN; |
546 | } |
547 | } |
548 | |
549 | GDBRemoteCommunication::PacketResult |
550 | GDBRemoteCommunicationServerCommon::Handle_vFile_Close( |
551 | StringExtractorGDBRemote &packet) { |
552 | packet.SetFilePos(::strlen("vFile:close:")); |
553 | int fd = packet.GetS32(-1, 16); |
554 | int err = -1; |
555 | int save_errno = 0; |
556 | if (fd >= 0) { |
557 | NativeFile file(fd, File::OpenOptions(0), true); |
558 | Status error = file.Close(); |
559 | err = 0; |
560 | save_errno = error.GetError(); |
561 | } else { |
562 | save_errno = EINVAL22; |
563 | } |
564 | StreamString response; |
565 | response.PutChar('F'); |
566 | response.Printf("%x", err); |
567 | if (save_errno) |
568 | response.Printf(",%x", system_errno_to_gdb(save_errno)); |
569 | return SendPacketNoLock(response.GetString()); |
570 | } |
571 | |
572 | GDBRemoteCommunication::PacketResult |
573 | GDBRemoteCommunicationServerCommon::Handle_vFile_pRead( |
574 | StringExtractorGDBRemote &packet) { |
575 | StreamGDBRemote response; |
576 | packet.SetFilePos(::strlen("vFile:pread:")); |
577 | int fd = packet.GetS32(-1, 16); |
578 | if (packet.GetChar() == ',') { |
579 | size_t count = packet.GetHexMaxU64(false, SIZE_MAX(18446744073709551615UL)); |
580 | if (packet.GetChar() == ',') { |
581 | off_t offset = packet.GetHexMaxU32(false, UINT32_MAX(4294967295U)); |
582 | if (count == SIZE_MAX(18446744073709551615UL)) { |
583 | response.Printf("F-1:%x", EINVAL22); |
584 | return SendPacketNoLock(response.GetString()); |
585 | } |
586 | |
587 | std::string buffer(count, 0); |
588 | NativeFile file(fd, File::eOpenOptionReadOnly, false); |
589 | Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset); |
590 | const int save_errno = error.GetError(); |
591 | response.PutChar('F'); |
592 | if (error.Success()) { |
593 | response.Printf("%zx", count); |
594 | response.PutChar(';'); |
595 | response.PutEscapedBytes(&buffer[0], count); |
596 | } else { |
597 | response.PutCString("-1"); |
598 | if (save_errno) |
599 | response.Printf(",%x", system_errno_to_gdb(save_errno)); |
600 | } |
601 | return SendPacketNoLock(response.GetString()); |
602 | } |
603 | } |
604 | return SendErrorResponse(21); |
605 | } |
606 | |
607 | GDBRemoteCommunication::PacketResult |
608 | GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite( |
609 | StringExtractorGDBRemote &packet) { |
610 | packet.SetFilePos(::strlen("vFile:pwrite:")); |
611 | |
612 | StreamGDBRemote response; |
613 | response.PutChar('F'); |
614 | |
615 | int fd = packet.GetS32(-1, 16); |
616 | if (packet.GetChar() == ',') { |
617 | off_t offset = packet.GetHexMaxU32(false, UINT32_MAX(4294967295U)); |
618 | if (packet.GetChar() == ',') { |
619 | std::string buffer; |
620 | if (packet.GetEscapedBinaryData(buffer)) { |
621 | NativeFile file(fd, File::eOpenOptionWriteOnly, false); |
622 | size_t count = buffer.size(); |
623 | Status error = |
624 | file.Write(static_cast<const void *>(&buffer[0]), count, offset); |
625 | const int save_errno = error.GetError(); |
626 | if (error.Success()) |
627 | response.Printf("%zx", count); |
628 | else { |
629 | response.PutCString("-1"); |
630 | if (save_errno) |
631 | response.Printf(",%x", system_errno_to_gdb(save_errno)); |
632 | } |
633 | } else { |
634 | response.Printf("-1,%x", EINVAL22); |
635 | } |
636 | return SendPacketNoLock(response.GetString()); |
637 | } |
638 | } |
639 | return SendErrorResponse(27); |
640 | } |
641 | |
642 | GDBRemoteCommunication::PacketResult |
643 | GDBRemoteCommunicationServerCommon::Handle_vFile_Size( |
644 | StringExtractorGDBRemote &packet) { |
645 | packet.SetFilePos(::strlen("vFile:size:")); |
646 | std::string path; |
647 | packet.GetHexByteString(path); |
648 | if (!path.empty()) { |
649 | uint64_t Size; |
650 | if (llvm::sys::fs::file_size(path, Size)) |
651 | return SendErrorResponse(5); |
652 | StreamString response; |
653 | response.PutChar('F'); |
654 | response.PutHex64(Size); |
655 | if (Size == UINT64_MAX(18446744073709551615UL)) { |
656 | response.PutChar(','); |
657 | response.PutHex64(Size); // TODO: replace with Host::GetSyswideErrorCode() |
658 | } |
659 | return SendPacketNoLock(response.GetString()); |
660 | } |
661 | return SendErrorResponse(22); |
662 | } |
663 | |
664 | GDBRemoteCommunication::PacketResult |
665 | GDBRemoteCommunicationServerCommon::Handle_vFile_Mode( |
666 | StringExtractorGDBRemote &packet) { |
667 | packet.SetFilePos(::strlen("vFile:mode:")); |
668 | std::string path; |
669 | packet.GetHexByteString(path); |
670 | if (!path.empty()) { |
671 | FileSpec file_spec(path); |
672 | FileSystem::Instance().Resolve(file_spec); |
673 | std::error_code ec; |
674 | const uint32_t mode = FileSystem::Instance().GetPermissions(file_spec, ec); |
675 | StreamString response; |
676 | if (mode != llvm::sys::fs::perms_not_known) |
677 | response.Printf("F%x", mode); |
678 | else |
679 | response.Printf("F-1,%x", (int)Status(ec).GetError()); |
680 | return SendPacketNoLock(response.GetString()); |
681 | } |
682 | return SendErrorResponse(23); |
683 | } |
684 | |
685 | GDBRemoteCommunication::PacketResult |
686 | GDBRemoteCommunicationServerCommon::Handle_vFile_Exists( |
687 | StringExtractorGDBRemote &packet) { |
688 | packet.SetFilePos(::strlen("vFile:exists:")); |
689 | std::string path; |
690 | packet.GetHexByteString(path); |
691 | if (!path.empty()) { |
692 | bool retcode = llvm::sys::fs::exists(path); |
693 | StreamString response; |
694 | response.PutChar('F'); |
695 | response.PutChar(','); |
696 | if (retcode) |
697 | response.PutChar('1'); |
698 | else |
699 | response.PutChar('0'); |
700 | return SendPacketNoLock(response.GetString()); |
701 | } |
702 | return SendErrorResponse(24); |
703 | } |
704 | |
705 | GDBRemoteCommunication::PacketResult |
706 | GDBRemoteCommunicationServerCommon::Handle_vFile_symlink( |
707 | StringExtractorGDBRemote &packet) { |
708 | packet.SetFilePos(::strlen("vFile:symlink:")); |
709 | std::string dst, src; |
710 | packet.GetHexByteStringTerminatedBy(dst, ','); |
711 | packet.GetChar(); // Skip ',' char |
712 | packet.GetHexByteString(src); |
713 | |
714 | FileSpec src_spec(src); |
715 | FileSystem::Instance().Resolve(src_spec); |
716 | Status error = FileSystem::Instance().Symlink(src_spec, FileSpec(dst)); |
717 | |
718 | StreamString response; |
719 | response.Printf("F%x,%x", error.GetError(), error.GetError()); |
720 | return SendPacketNoLock(response.GetString()); |
721 | } |
722 | |
723 | GDBRemoteCommunication::PacketResult |
724 | GDBRemoteCommunicationServerCommon::Handle_vFile_unlink( |
725 | StringExtractorGDBRemote &packet) { |
726 | packet.SetFilePos(::strlen("vFile:unlink:")); |
727 | std::string path; |
728 | packet.GetHexByteString(path); |
729 | Status error(llvm::sys::fs::remove(path)); |
730 | StreamString response; |
731 | response.Printf("F%x,%x", error.GetError(), error.GetError()); |
732 | return SendPacketNoLock(response.GetString()); |
733 | } |
734 | |
735 | GDBRemoteCommunication::PacketResult |
736 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( |
737 | StringExtractorGDBRemote &packet) { |
738 | packet.SetFilePos(::strlen("qPlatform_shell:")); |
739 | std::string path; |
740 | std::string working_dir; |
741 | packet.GetHexByteStringTerminatedBy(path, ','); |
742 | if (!path.empty()) { |
743 | if (packet.GetChar() == ',') { |
744 | // FIXME: add timeout to qPlatform_shell packet |
745 | // uint32_t timeout = packet.GetHexMaxU32(false, 32); |
746 | if (packet.GetChar() == ',') |
747 | packet.GetHexByteString(working_dir); |
748 | int status, signo; |
749 | std::string output; |
750 | FileSpec working_spec(working_dir); |
751 | FileSystem::Instance().Resolve(working_spec); |
752 | Status err = |
753 | Host::RunShellCommand(path.c_str(), working_spec, &status, &signo, |
754 | &output, std::chrono::seconds(10)); |
755 | StreamGDBRemote response; |
756 | if (err.Fail()) { |
757 | response.PutCString("F,"); |
758 | response.PutHex32(UINT32_MAX(4294967295U)); |
759 | } else { |
760 | response.PutCString("F,"); |
761 | response.PutHex32(status); |
762 | response.PutChar(','); |
763 | response.PutHex32(signo); |
764 | response.PutChar(','); |
765 | response.PutEscapedBytes(output.c_str(), output.size()); |
766 | } |
767 | return SendPacketNoLock(response.GetString()); |
768 | } |
769 | } |
770 | return SendErrorResponse(24); |
771 | } |
772 | |
773 | template <typename T, typename U> |
774 | static void fill_clamp(T &dest, U src, typename T::value_type fallback) { |
775 | static_assert(std::is_unsigned<typename T::value_type>::value, |
776 | "Destination type must be unsigned."); |
777 | using UU = typename std::make_unsigned<U>::type; |
778 | constexpr auto T_max = std::numeric_limits<typename T::value_type>::max(); |
779 | dest = src >= 0 && static_cast<UU>(src) <= T_max ? src : fallback; |
780 | } |
781 | |
782 | GDBRemoteCommunication::PacketResult |
783 | GDBRemoteCommunicationServerCommon::Handle_vFile_FStat( |
784 | StringExtractorGDBRemote &packet) { |
785 | StreamGDBRemote response; |
786 | packet.SetFilePos(::strlen("vFile:fstat:")); |
787 | int fd = packet.GetS32(-1, 16); |
788 | |
789 | struct stat file_stats; |
790 | if (::fstat(fd, &file_stats) == -1) { |
791 | const int save_errno = errno(*__errno_location ()); |
792 | response.Printf("F-1,%x", system_errno_to_gdb(save_errno)); |
793 | return SendPacketNoLock(response.GetString()); |
794 | } |
795 | |
796 | GDBRemoteFStatData data; |
797 | fill_clamp(data.gdb_st_dev, file_stats.st_dev, 0); |
798 | fill_clamp(data.gdb_st_ino, file_stats.st_ino, 0); |
799 | data.gdb_st_mode = file_stats.st_mode; |
800 | fill_clamp(data.gdb_st_nlink, file_stats.st_nlink, UINT32_MAX(4294967295U)); |
801 | fill_clamp(data.gdb_st_uid, file_stats.st_uid, 0); |
802 | fill_clamp(data.gdb_st_gid, file_stats.st_gid, 0); |
803 | fill_clamp(data.gdb_st_rdev, file_stats.st_rdev, 0); |
804 | data.gdb_st_size = file_stats.st_size; |
805 | #if !defined(_WIN32) |
806 | data.gdb_st_blksize = file_stats.st_blksize; |
807 | data.gdb_st_blocks = file_stats.st_blocks; |
808 | #else |
809 | data.gdb_st_blksize = 0; |
810 | data.gdb_st_blocks = 0; |
811 | #endif |
812 | fill_clamp(data.gdb_st_atime, file_stats.st_atimest_atim.tv_sec, 0); |
813 | fill_clamp(data.gdb_st_mtime, file_stats.st_mtimest_mtim.tv_sec, 0); |
814 | fill_clamp(data.gdb_st_ctime, file_stats.st_ctimest_ctim.tv_sec, 0); |
815 | |
816 | response.Printf("F%zx;", sizeof(data)); |
817 | response.PutEscapedBytes(&data, sizeof(data)); |
818 | return SendPacketNoLock(response.GetString()); |
819 | } |
820 | |
821 | GDBRemoteCommunication::PacketResult |
822 | GDBRemoteCommunicationServerCommon::Handle_vFile_Stat( |
823 | StringExtractorGDBRemote &packet) { |
824 | return SendUnimplementedResponse( |
825 | "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented"); |
826 | } |
827 | |
828 | GDBRemoteCommunication::PacketResult |
829 | GDBRemoteCommunicationServerCommon::Handle_vFile_MD5( |
830 | StringExtractorGDBRemote &packet) { |
831 | packet.SetFilePos(::strlen("vFile:MD5:")); |
832 | std::string path; |
833 | packet.GetHexByteString(path); |
834 | if (!path.empty()) { |
835 | StreamGDBRemote response; |
836 | auto Result = llvm::sys::fs::md5_contents(path); |
837 | if (!Result) { |
838 | response.PutCString("F,"); |
839 | response.PutCString("x"); |
840 | } else { |
841 | response.PutCString("F,"); |
842 | response.PutHex64(Result->low()); |
843 | response.PutHex64(Result->high()); |
844 | } |
845 | return SendPacketNoLock(response.GetString()); |
846 | } |
847 | return SendErrorResponse(25); |
848 | } |
849 | |
850 | GDBRemoteCommunication::PacketResult |
851 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir( |
852 | StringExtractorGDBRemote &packet) { |
853 | packet.SetFilePos(::strlen("qPlatform_mkdir:")); |
854 | mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX(4294967295U)); |
855 | if (packet.GetChar() == ',') { |
856 | std::string path; |
857 | packet.GetHexByteString(path); |
858 | Status error(llvm::sys::fs::create_directory(path, mode)); |
859 | |
860 | StreamGDBRemote response; |
861 | response.Printf("F%x", error.GetError()); |
862 | |
863 | return SendPacketNoLock(response.GetString()); |
864 | } |
865 | return SendErrorResponse(20); |
866 | } |
867 | |
868 | GDBRemoteCommunication::PacketResult |
869 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod( |
870 | StringExtractorGDBRemote &packet) { |
871 | packet.SetFilePos(::strlen("qPlatform_chmod:")); |
872 | |
873 | auto perms = |
874 | static_cast<llvm::sys::fs::perms>(packet.GetHexMaxU32(false, UINT32_MAX(4294967295U))); |
875 | if (packet.GetChar() == ',') { |
876 | std::string path; |
877 | packet.GetHexByteString(path); |
878 | Status error(llvm::sys::fs::setPermissions(path, perms)); |
879 | |
880 | StreamGDBRemote response; |
881 | response.Printf("F%x", error.GetError()); |
882 | |
883 | return SendPacketNoLock(response.GetString()); |
884 | } |
885 | return SendErrorResponse(19); |
886 | } |
887 | |
888 | GDBRemoteCommunication::PacketResult |
889 | GDBRemoteCommunicationServerCommon::Handle_qSupported( |
890 | StringExtractorGDBRemote &packet) { |
891 | // Parse client-indicated features. |
892 | llvm::SmallVector<llvm::StringRef, 4> client_features; |
893 | packet.GetStringRef().split(client_features, ';'); |
894 | return SendPacketNoLock(llvm::join(HandleFeatures(client_features), ";")); |
895 | } |
896 | |
897 | GDBRemoteCommunication::PacketResult |
898 | GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError( |
899 | StringExtractorGDBRemote &packet) { |
900 | packet.SetFilePos(::strlen("QSetDetachOnError:")); |
901 | if (packet.GetU32(0)) |
902 | m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError); |
903 | else |
904 | m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError); |
905 | return SendOKResponse(); |
906 | } |
907 | |
908 | GDBRemoteCommunication::PacketResult |
909 | GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode( |
910 | StringExtractorGDBRemote &packet) { |
911 | // Send response first before changing m_send_acks to we ack this packet |
912 | PacketResult packet_result = SendOKResponse(); |
913 | m_send_acks = false; |
914 | return packet_result; |
915 | } |
916 | |
917 | GDBRemoteCommunication::PacketResult |
918 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN( |
919 | StringExtractorGDBRemote &packet) { |
920 | packet.SetFilePos(::strlen("QSetSTDIN:")); |
921 | FileAction file_action; |
922 | std::string path; |
923 | packet.GetHexByteString(path); |
924 | const bool read = true; |
925 | const bool write = false; |
926 | if (file_action.Open(STDIN_FILENO0, FileSpec(path), read, write)) { |
927 | m_process_launch_info.AppendFileAction(file_action); |
928 | return SendOKResponse(); |
929 | } |
930 | return SendErrorResponse(15); |
931 | } |
932 | |
933 | GDBRemoteCommunication::PacketResult |
934 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT( |
935 | StringExtractorGDBRemote &packet) { |
936 | packet.SetFilePos(::strlen("QSetSTDOUT:")); |
937 | FileAction file_action; |
938 | std::string path; |
939 | packet.GetHexByteString(path); |
940 | const bool read = false; |
941 | const bool write = true; |
942 | if (file_action.Open(STDOUT_FILENO1, FileSpec(path), read, write)) { |
943 | m_process_launch_info.AppendFileAction(file_action); |
944 | return SendOKResponse(); |
945 | } |
946 | return SendErrorResponse(16); |
947 | } |
948 | |
949 | GDBRemoteCommunication::PacketResult |
950 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR( |
951 | StringExtractorGDBRemote &packet) { |
952 | packet.SetFilePos(::strlen("QSetSTDERR:")); |
953 | FileAction file_action; |
954 | std::string path; |
955 | packet.GetHexByteString(path); |
956 | const bool read = false; |
957 | const bool write = true; |
958 | if (file_action.Open(STDERR_FILENO2, FileSpec(path), read, write)) { |
959 | m_process_launch_info.AppendFileAction(file_action); |
960 | return SendOKResponse(); |
961 | } |
962 | return SendErrorResponse(17); |
963 | } |
964 | |
965 | GDBRemoteCommunication::PacketResult |
966 | GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess( |
967 | StringExtractorGDBRemote &packet) { |
968 | if (m_process_launch_error.Success()) |
969 | return SendOKResponse(); |
970 | StreamString response; |
971 | response.PutChar('E'); |
972 | response.PutCString(m_process_launch_error.AsCString("<unknown error>")); |
973 | return SendPacketNoLock(response.GetString()); |
974 | } |
975 | |
976 | GDBRemoteCommunication::PacketResult |
977 | GDBRemoteCommunicationServerCommon::Handle_QEnvironment( |
978 | StringExtractorGDBRemote &packet) { |
979 | packet.SetFilePos(::strlen("QEnvironment:")); |
980 | const uint32_t bytes_left = packet.GetBytesLeft(); |
981 | if (bytes_left > 0) { |
982 | m_process_launch_info.GetEnvironment().insert(packet.Peek()); |
983 | return SendOKResponse(); |
984 | } |
985 | return SendErrorResponse(12); |
986 | } |
987 | |
988 | GDBRemoteCommunication::PacketResult |
989 | GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded( |
990 | StringExtractorGDBRemote &packet) { |
991 | packet.SetFilePos(::strlen("QEnvironmentHexEncoded:")); |
992 | const uint32_t bytes_left = packet.GetBytesLeft(); |
993 | if (bytes_left > 0) { |
994 | std::string str; |
995 | packet.GetHexByteString(str); |
996 | m_process_launch_info.GetEnvironment().insert(str); |
997 | return SendOKResponse(); |
998 | } |
999 | return SendErrorResponse(12); |
1000 | } |
1001 | |
1002 | GDBRemoteCommunication::PacketResult |
1003 | GDBRemoteCommunicationServerCommon::Handle_QLaunchArch( |
1004 | StringExtractorGDBRemote &packet) { |
1005 | packet.SetFilePos(::strlen("QLaunchArch:")); |
1006 | const uint32_t bytes_left = packet.GetBytesLeft(); |
1007 | if (bytes_left > 0) { |
1008 | const char *arch_triple = packet.Peek(); |
1009 | m_process_launch_info.SetArchitecture( |
1010 | HostInfo::GetAugmentedArchSpec(arch_triple)); |
1011 | return SendOKResponse(); |
1012 | } |
1013 | return SendErrorResponse(13); |
1014 | } |
1015 | |
1016 | GDBRemoteCommunication::PacketResult |
1017 | GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) { |
1018 | // The 'A' packet is the most over designed packet ever here with redundant |
1019 | // argument indexes, redundant argument lengths and needed hex encoded |
1020 | // argument string values. Really all that is needed is a comma separated hex |
1021 | // encoded argument value list, but we will stay true to the documented |
1022 | // version of the 'A' packet here... |
1023 | |
1024 | Log *log = GetLog(LLDBLog::Process); |
1025 | int actual_arg_index = 0; |
1026 | |
1027 | packet.SetFilePos(1); // Skip the 'A' |
1028 | bool success = true; |
1029 | while (success && packet.GetBytesLeft() > 0) { |
1030 | // Decode the decimal argument string length. This length is the number of |
1031 | // hex nibbles in the argument string value. |
1032 | const uint32_t arg_len = packet.GetU32(UINT32_MAX(4294967295U)); |
1033 | if (arg_len == UINT32_MAX(4294967295U)) |
1034 | success = false; |
1035 | else { |
1036 | // Make sure the argument hex string length is followed by a comma |
1037 | if (packet.GetChar() != ',') |
1038 | success = false; |
1039 | else { |
1040 | // Decode the argument index. We ignore this really because who would |
1041 | // really send down the arguments in a random order??? |
1042 | const uint32_t arg_idx = packet.GetU32(UINT32_MAX(4294967295U)); |
1043 | if (arg_idx == UINT32_MAX(4294967295U)) |
1044 | success = false; |
1045 | else { |
1046 | // Make sure the argument index is followed by a comma |
1047 | if (packet.GetChar() != ',') |
1048 | success = false; |
1049 | else { |
1050 | // Decode the argument string value from hex bytes back into a UTF8 |
1051 | // string and make sure the length matches the one supplied in the |
1052 | // packet |
1053 | std::string arg; |
1054 | if (packet.GetHexByteStringFixedLength(arg, arg_len) != |
1055 | (arg_len / 2)) |
1056 | success = false; |
1057 | else { |
1058 | // If there are any bytes left |
1059 | if (packet.GetBytesLeft()) { |
1060 | if (packet.GetChar() != ',') |
1061 | success = false; |
1062 | } |
1063 | |
1064 | if (success) { |
1065 | if (arg_idx == 0) |
1066 | m_process_launch_info.GetExecutableFile().SetFile( |
1067 | arg, FileSpec::Style::native); |
1068 | m_process_launch_info.GetArguments().AppendArgument(arg); |
1069 | LLDB_LOGF(log, "LLGSPacketHandler::%s added arg %d: \"%s\"",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"" , __FUNCTION__, actual_arg_index, arg.c_str()); } while (0) |
1070 | __FUNCTION__, actual_arg_index, arg.c_str())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"" , __FUNCTION__, actual_arg_index, arg.c_str()); } while (0); |
1071 | ++actual_arg_index; |
1072 | } |
1073 | } |
1074 | } |
1075 | } |
1076 | } |
1077 | } |
1078 | } |
1079 | |
1080 | if (success) { |
1081 | m_process_launch_error = LaunchProcess(); |
1082 | if (m_process_launch_error.Success()) |
1083 | return SendOKResponse(); |
1084 | LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp" , __func__, "failed to launch exe: {0}", m_process_launch_error ); } while (0); |
1085 | } |
1086 | return SendErrorResponse(8); |
1087 | } |
1088 | |
1089 | GDBRemoteCommunication::PacketResult |
1090 | GDBRemoteCommunicationServerCommon::Handle_qEcho( |
1091 | StringExtractorGDBRemote &packet) { |
1092 | // Just echo back the exact same packet for qEcho... |
1093 | return SendPacketNoLock(packet.GetStringRef()); |
1094 | } |
1095 | |
1096 | GDBRemoteCommunication::PacketResult |
1097 | GDBRemoteCommunicationServerCommon::Handle_qModuleInfo( |
1098 | StringExtractorGDBRemote &packet) { |
1099 | packet.SetFilePos(::strlen("qModuleInfo:")); |
1100 | |
1101 | std::string module_path; |
1102 | packet.GetHexByteStringTerminatedBy(module_path, ';'); |
1103 | if (module_path.empty()) |
1104 | return SendErrorResponse(1); |
1105 | |
1106 | if (packet.GetChar() != ';') |
1107 | return SendErrorResponse(2); |
1108 | |
1109 | std::string triple; |
1110 | packet.GetHexByteString(triple); |
1111 | |
1112 | ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple); |
1113 | if (!matched_module_spec.GetFileSpec()) |
1114 | return SendErrorResponse(3); |
1115 | |
1116 | const auto file_offset = matched_module_spec.GetObjectOffset(); |
1117 | const auto file_size = matched_module_spec.GetObjectSize(); |
1118 | const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); |
1119 | |
1120 | StreamGDBRemote response; |
1121 | |
1122 | if (uuid_str.empty()) { |
1123 | auto Result = llvm::sys::fs::md5_contents( |
1124 | matched_module_spec.GetFileSpec().GetPath()); |
1125 | if (!Result) |
1126 | return SendErrorResponse(5); |
1127 | response.PutCString("md5:"); |
1128 | response.PutStringAsRawHex8(Result->digest()); |
1129 | } else { |
1130 | response.PutCString("uuid:"); |
1131 | response.PutStringAsRawHex8(uuid_str); |
1132 | } |
1133 | response.PutChar(';'); |
1134 | |
1135 | const auto &module_arch = matched_module_spec.GetArchitecture(); |
1136 | response.PutCString("triple:"); |
1137 | response.PutStringAsRawHex8(module_arch.GetTriple().getTriple()); |
1138 | response.PutChar(';'); |
1139 | |
1140 | response.PutCString("file_path:"); |
1141 | response.PutStringAsRawHex8(matched_module_spec.GetFileSpec().GetCString()); |
1142 | response.PutChar(';'); |
1143 | response.PutCString("file_offset:"); |
1144 | response.PutHex64(file_offset); |
1145 | response.PutChar(';'); |
1146 | response.PutCString("file_size:"); |
1147 | response.PutHex64(file_size); |
1148 | response.PutChar(';'); |
1149 | |
1150 | return SendPacketNoLock(response.GetString()); |
1151 | } |
1152 | |
1153 | GDBRemoteCommunication::PacketResult |
1154 | GDBRemoteCommunicationServerCommon::Handle_jModulesInfo( |
1155 | StringExtractorGDBRemote &packet) { |
1156 | namespace json = llvm::json; |
1157 | |
1158 | packet.SetFilePos(::strlen("jModulesInfo:")); |
1159 | |
1160 | StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek()); |
1161 | if (!object_sp) |
1162 | return SendErrorResponse(1); |
1163 | |
1164 | StructuredData::Array *packet_array = object_sp->GetAsArray(); |
1165 | if (!packet_array) |
1166 | return SendErrorResponse(2); |
1167 | |
1168 | json::Array response_array; |
1169 | for (size_t i = 0; i < packet_array->GetSize(); ++i) { |
1170 | StructuredData::Dictionary *query = |
1171 | packet_array->GetItemAtIndex(i)->GetAsDictionary(); |
1172 | if (!query) |
1173 | continue; |
1174 | llvm::StringRef file, triple; |
1175 | if (!query->GetValueForKeyAsString("file", file) || |
1176 | !query->GetValueForKeyAsString("triple", triple)) |
1177 | continue; |
1178 | |
1179 | ModuleSpec matched_module_spec = GetModuleInfo(file, triple); |
1180 | if (!matched_module_spec.GetFileSpec()) |
1181 | continue; |
1182 | |
1183 | const auto file_offset = matched_module_spec.GetObjectOffset(); |
1184 | const auto file_size = matched_module_spec.GetObjectSize(); |
1185 | const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); |
1186 | if (uuid_str.empty()) |
1187 | continue; |
1188 | const auto triple_str = |
1189 | matched_module_spec.GetArchitecture().GetTriple().getTriple(); |
1190 | const auto file_path = matched_module_spec.GetFileSpec().GetPath(); |
1191 | |
1192 | json::Object response{{"uuid", uuid_str}, |
1193 | {"triple", triple_str}, |
1194 | {"file_path", file_path}, |
1195 | {"file_offset", static_cast<int64_t>(file_offset)}, |
1196 | {"file_size", static_cast<int64_t>(file_size)}}; |
1197 | response_array.push_back(std::move(response)); |
1198 | } |
1199 | |
1200 | StreamString response; |
1201 | response.AsRawOstream() << std::move(response_array); |
1202 | StreamGDBRemote escaped_response; |
1203 | escaped_response.PutEscapedBytes(response.GetString().data(), |
1204 | response.GetSize()); |
1205 | return SendPacketNoLock(escaped_response.GetString()); |
1206 | } |
1207 | |
1208 | void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse( |
1209 | const ProcessInstanceInfo &proc_info, StreamString &response) { |
1210 | response.Printf( |
1211 | "pid:%" PRIu64"l" "u" ";ppid:%" PRIu64"l" "u" ";uid:%i;gid:%i;euid:%i;egid:%i;", |
1212 | proc_info.GetProcessID(), proc_info.GetParentProcessID(), |
1213 | proc_info.GetUserID(), proc_info.GetGroupID(), |
1214 | proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID()); |
1215 | response.PutCString("name:"); |
1216 | response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetCString()); |
1217 | |
1218 | response.PutChar(';'); |
1219 | response.PutCString("args:"); |
1220 | response.PutStringAsRawHex8(proc_info.GetArg0()); |
1221 | for (auto &arg : proc_info.GetArguments()) { |
1222 | response.PutChar('-'); |
1223 | response.PutStringAsRawHex8(arg.ref()); |
1224 | } |
1225 | |
1226 | response.PutChar(';'); |
1227 | const ArchSpec &proc_arch = proc_info.GetArchitecture(); |
1228 | if (proc_arch.IsValid()) { |
1229 | const llvm::Triple &proc_triple = proc_arch.GetTriple(); |
1230 | response.PutCString("triple:"); |
1231 | response.PutStringAsRawHex8(proc_triple.getTriple()); |
1232 | response.PutChar(';'); |
1233 | } |
1234 | } |
1235 | |
1236 | void GDBRemoteCommunicationServerCommon:: |
1237 | CreateProcessInfoResponse_DebugServerStyle( |
1238 | const ProcessInstanceInfo &proc_info, StreamString &response) { |
1239 | response.Printf("pid:%" PRIx64"l" "x" ";parent-pid:%" PRIx64"l" "x" |
1240 | ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;", |
1241 | proc_info.GetProcessID(), proc_info.GetParentProcessID(), |
1242 | proc_info.GetUserID(), proc_info.GetGroupID(), |
1243 | proc_info.GetEffectiveUserID(), |
1244 | proc_info.GetEffectiveGroupID()); |
1245 | |
1246 | const ArchSpec &proc_arch = proc_info.GetArchitecture(); |
1247 | if (proc_arch.IsValid()) { |
1248 | const llvm::Triple &proc_triple = proc_arch.GetTriple(); |
1249 | #if defined(__APPLE__) |
1250 | // We'll send cputype/cpusubtype. |
1251 | const uint32_t cpu_type = proc_arch.GetMachOCPUType(); |
1252 | if (cpu_type != 0) |
1253 | response.Printf("cputype:%" PRIx32"x" ";", cpu_type); |
1254 | |
1255 | const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType(); |
1256 | if (cpu_subtype != 0) |
1257 | response.Printf("cpusubtype:%" PRIx32"x" ";", cpu_subtype); |
1258 | |
1259 | const std::string vendor = proc_triple.getVendorName().str(); |
1260 | if (!vendor.empty()) |
1261 | response.Printf("vendor:%s;", vendor.c_str()); |
1262 | #else |
1263 | // We'll send the triple. |
1264 | response.PutCString("triple:"); |
1265 | response.PutStringAsRawHex8(proc_triple.getTriple()); |
1266 | response.PutChar(';'); |
1267 | #endif |
1268 | std::string ostype = std::string(proc_triple.getOSName()); |
1269 | // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64. |
1270 | if (proc_triple.getVendor() == llvm::Triple::Apple) { |
1271 | switch (proc_triple.getArch()) { |
1272 | case llvm::Triple::arm: |
1273 | case llvm::Triple::thumb: |
1274 | case llvm::Triple::aarch64: |
1275 | case llvm::Triple::aarch64_32: |
1276 | ostype = "ios"; |
1277 | break; |
1278 | default: |
1279 | // No change. |
1280 | break; |
1281 | } |
1282 | } |
1283 | response.Printf("ostype:%s;", ostype.c_str()); |
1284 | |
1285 | switch (proc_arch.GetByteOrder()) { |
1286 | case lldb::eByteOrderLittle: |
1287 | response.PutCString("endian:little;"); |
1288 | break; |
1289 | case lldb::eByteOrderBig: |
1290 | response.PutCString("endian:big;"); |
1291 | break; |
1292 | case lldb::eByteOrderPDP: |
1293 | response.PutCString("endian:pdp;"); |
1294 | break; |
1295 | default: |
1296 | // Nothing. |
1297 | break; |
1298 | } |
1299 | // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer |
1300 | // size is 4 and for N64 it is 8 |
1301 | std::string abi = proc_arch.GetTargetABI(); |
1302 | if (!abi.empty()) |
1303 | response.Printf("elf_abi:%s;", abi.c_str()); |
1304 | response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize()); |
1305 | } |
1306 | } |
1307 | |
1308 | FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile( |
1309 | const std::string &module_path, const ArchSpec &arch) { |
1310 | #ifdef __ANDROID__ |
1311 | return HostInfoAndroid::ResolveLibraryPath(module_path, arch); |
1312 | #else |
1313 | FileSpec file_spec(module_path); |
1314 | FileSystem::Instance().Resolve(file_spec); |
1315 | return file_spec; |
1316 | #endif |
1317 | } |
1318 | |
1319 | ModuleSpec |
1320 | GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path, |
1321 | llvm::StringRef triple) { |
1322 | ArchSpec arch(triple); |
1323 | |
1324 | FileSpec req_module_path_spec(module_path); |
1325 | FileSystem::Instance().Resolve(req_module_path_spec); |
1326 | |
1327 | const FileSpec module_path_spec = |
1328 | FindModuleFile(req_module_path_spec.GetPath(), arch); |
1329 | const ModuleSpec module_spec(module_path_spec, arch); |
1330 | |
1331 | ModuleSpecList module_specs; |
1332 | if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, |
1333 | module_specs)) |
1334 | return ModuleSpec(); |
1335 | |
1336 | ModuleSpec matched_module_spec; |
1337 | if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) |
1338 | return ModuleSpec(); |
1339 | |
1340 | return matched_module_spec; |
1341 | } |
1342 | |
1343 | std::vector<std::string> GDBRemoteCommunicationServerCommon::HandleFeatures( |
1344 | const llvm::ArrayRef<llvm::StringRef> client_features) { |
1345 | // 128KBytes is a reasonable max packet size--debugger can always use less. |
1346 | constexpr uint32_t max_packet_size = 128 * 1024; |
1347 | |
1348 | // Features common to platform server and llgs. |
1349 | return { |
1350 | llvm::formatv("PacketSize={0}", max_packet_size), |
1351 | "QStartNoAckMode+", |
1352 | "qEcho+", |
1353 | "native-signals+", |
1354 | }; |
1355 | } |