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