blob: 31202fca2bff68d9068cbbb30e7500ea8fb39799 [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerCommon.cpp ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "GDBRemoteCommunicationServerCommon.h"
11
12#include <errno.h>
13
14// C Includes
15// C++ Includes
16#include <cstring>
17#include <chrono>
18
19// Other libraries and framework includes
20#include "llvm/ADT/Triple.h"
21#include "lldb/Core/Log.h"
Oleksiy Vyalov6801be32015-02-25 22:15:44 +000022#include "lldb/Core/ModuleSpec.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000023#include "lldb/Core/StreamGDBRemote.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Host/Config.h"
26#include "lldb/Host/Endian.h"
27#include "lldb/Host/File.h"
28#include "lldb/Host/FileSystem.h"
29#include "lldb/Host/Host.h"
30#include "lldb/Host/HostInfo.h"
31#include "lldb/Host/StringConvert.h"
32#include "lldb/Interpreter/Args.h"
Oleksiy Vyalov6801be32015-02-25 22:15:44 +000033#include "lldb/Symbol/ObjectFile.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000034#include "lldb/Target/FileAction.h"
35#include "lldb/Target/Platform.h"
36#include "lldb/Target/Process.h"
37
38// Project includes
39#include "ProcessGDBRemoteLog.h"
40#include "Utility/StringExtractorGDBRemote.h"
41
Tamas Berghammerdad4db72015-03-13 11:16:08 +000042#ifdef __ANDROID__
43#include "lldb/Host/android/HostInfoAndroid.h"
44#endif
45
Tamas Berghammere13c2732015-02-11 10:29:30 +000046using namespace lldb;
47using namespace lldb_private;
48
Tamas Berghammer2d52afd2015-02-26 11:37:21 +000049#ifdef __ANDROID__
50 const static uint32_t g_default_packet_timeout_sec = 20; // seconds
51#else
52 const static uint32_t g_default_packet_timeout_sec = 0; // not specified
53#endif
54
Tamas Berghammere13c2732015-02-11 10:29:30 +000055//----------------------------------------------------------------------
56// GDBRemoteCommunicationServerCommon constructor
57//----------------------------------------------------------------------
58GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon(const char *comm_name, const char *listener_name) :
59 GDBRemoteCommunicationServer (comm_name, listener_name),
60 m_spawned_pids (),
61 m_spawned_pids_mutex (Mutex::eMutexTypeRecursive),
62 m_process_launch_info (),
63 m_process_launch_error (),
64 m_proc_infos (),
65 m_proc_infos_index (0),
66 m_thread_suffix_supported (false),
67 m_list_threads_in_stop_reply (false)
68{
69 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A,
70 &GDBRemoteCommunicationServerCommon::Handle_A);
71 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QEnvironment,
72 &GDBRemoteCommunicationServerCommon::Handle_QEnvironment);
73 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qfProcessInfo,
74 &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo);
75 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qGroupName,
76 &GDBRemoteCommunicationServerCommon::Handle_qGroupName);
77 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qHostInfo,
78 &GDBRemoteCommunicationServerCommon::Handle_qHostInfo);
79 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess,
80 &GDBRemoteCommunicationServerCommon::Handle_qKillSpawnedProcess);
81 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QLaunchArch,
82 &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch);
83 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess,
84 &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess);
85 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QListThreadsInStopReply,
86 &GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +000087 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qModuleInfo,
88 &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo);
Tamas Berghammere13c2732015-02-11 10:29:30 +000089 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod,
90 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod);
91 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir,
92 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir);
93 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_shell,
94 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell);
95 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID,
96 &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID);
97 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError,
98 &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError);
99 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDERR,
100 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR);
101 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDIN,
102 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN);
103 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT,
104 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT);
105 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qSpeedTest,
106 &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest);
107 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qsProcessInfo,
108 &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo);
109 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode,
110 &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode);
111 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qSupported,
112 &GDBRemoteCommunicationServerCommon::Handle_qSupported);
113 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QThreadSuffixSupported,
114 &GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported);
115 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qUserName,
116 &GDBRemoteCommunicationServerCommon::Handle_qUserName);
117 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_close,
118 &GDBRemoteCommunicationServerCommon::Handle_vFile_Close);
119 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_exists,
120 &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists);
121 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_md5,
122 &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5);
123 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_mode,
124 &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode);
125 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_open,
126 &GDBRemoteCommunicationServerCommon::Handle_vFile_Open);
127 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_pread,
128 &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead);
129 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_pwrite,
130 &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite);
131 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_size,
132 &GDBRemoteCommunicationServerCommon::Handle_vFile_Size);
133 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_stat,
134 &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat);
135 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_symlink,
136 &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink);
137 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_unlink,
138 &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink);
139}
140
141//----------------------------------------------------------------------
142// Destructor
143//----------------------------------------------------------------------
144GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon()
145{
146}
147
148GDBRemoteCommunication::PacketResult
149GDBRemoteCommunicationServerCommon::Handle_qHostInfo (StringExtractorGDBRemote &packet)
150{
151 StreamString response;
152
153 // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00
154
155 ArchSpec host_arch(HostInfo::GetArchitecture());
156 const llvm::Triple &host_triple = host_arch.GetTriple();
157 response.PutCString("triple:");
158 response.PutCStringAsRawHex8(host_triple.getTriple().c_str());
159 response.Printf (";ptrsize:%u;",host_arch.GetAddressByteSize());
160
161 const char* distribution_id = host_arch.GetDistributionId ().AsCString ();
162 if (distribution_id)
163 {
164 response.PutCString("distribution_id:");
165 response.PutCStringAsRawHex8(distribution_id);
166 response.PutCString(";");
167 }
168
169 // Only send out MachO info when lldb-platform/llgs is running on a MachO host.
170#if defined(__APPLE__)
171 uint32_t cpu = host_arch.GetMachOCPUType();
172 uint32_t sub = host_arch.GetMachOCPUSubType();
173 if (cpu != LLDB_INVALID_CPUTYPE)
174 response.Printf ("cputype:%u;", cpu);
175 if (sub != LLDB_INVALID_CPUTYPE)
176 response.Printf ("cpusubtype:%u;", sub);
177
178 if (cpu == ArchSpec::kCore_arm_any)
179 response.Printf("watchpoint_exceptions_received:before;"); // On armv7 we use "synchronous" watchpoints which means the exception is delivered before the instruction executes.
180 else
181 response.Printf("watchpoint_exceptions_received:after;");
182#else
183 response.Printf("watchpoint_exceptions_received:after;");
184#endif
185
186 switch (lldb::endian::InlHostByteOrder())
187 {
188 case eByteOrderBig: response.PutCString ("endian:big;"); break;
189 case eByteOrderLittle: response.PutCString ("endian:little;"); break;
190 case eByteOrderPDP: response.PutCString ("endian:pdp;"); break;
191 default: response.PutCString ("endian:unknown;"); break;
192 }
193
194 uint32_t major = UINT32_MAX;
195 uint32_t minor = UINT32_MAX;
196 uint32_t update = UINT32_MAX;
197 if (HostInfo::GetOSVersion(major, minor, update))
198 {
199 if (major != UINT32_MAX)
200 {
201 response.Printf("os_version:%u", major);
202 if (minor != UINT32_MAX)
203 {
204 response.Printf(".%u", minor);
205 if (update != UINT32_MAX)
206 response.Printf(".%u", update);
207 }
208 response.PutChar(';');
209 }
210 }
211
212 std::string s;
213 if (HostInfo::GetOSBuildString(s))
214 {
215 response.PutCString ("os_build:");
216 response.PutCStringAsRawHex8(s.c_str());
217 response.PutChar(';');
218 }
219 if (HostInfo::GetOSKernelDescription(s))
220 {
221 response.PutCString ("os_kernel:");
222 response.PutCStringAsRawHex8(s.c_str());
223 response.PutChar(';');
224 }
225
226#if defined(__APPLE__)
227
228#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
229 // For iOS devices, we are connected through a USB Mux so we never pretend
230 // to actually have a hostname as far as the remote lldb that is connecting
231 // to this lldb-platform is concerned
232 response.PutCString ("hostname:");
233 response.PutCStringAsRawHex8("127.0.0.1");
234 response.PutChar(';');
235#else // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
236 if (HostInfo::GetHostname(s))
237 {
238 response.PutCString ("hostname:");
239 response.PutCStringAsRawHex8(s.c_str());
240 response.PutChar(';');
241 }
242#endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
243
244#else // #if defined(__APPLE__)
245 if (HostInfo::GetHostname(s))
246 {
247 response.PutCString ("hostname:");
248 response.PutCStringAsRawHex8(s.c_str());
249 response.PutChar(';');
250 }
251#endif // #if defined(__APPLE__)
252
Tamas Berghammer2d52afd2015-02-26 11:37:21 +0000253 if (g_default_packet_timeout_sec > 0)
254 response.Printf ("default_packet_timeout:%u;", g_default_packet_timeout_sec);
255
Tamas Berghammere13c2732015-02-11 10:29:30 +0000256 return SendPacketNoLock (response.GetData(), response.GetSize());
257}
258
259GDBRemoteCommunication::PacketResult
260GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID (StringExtractorGDBRemote &packet)
261{
262 // Packet format: "qProcessInfoPID:%i" where %i is the pid
263 packet.SetFilePos (::strlen ("qProcessInfoPID:"));
264 lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID);
265 if (pid != LLDB_INVALID_PROCESS_ID)
266 {
267 ProcessInstanceInfo proc_info;
268 if (Host::GetProcessInfo (pid, proc_info))
269 {
270 StreamString response;
271 CreateProcessInfoResponse (proc_info, response);
272 return SendPacketNoLock (response.GetData(), response.GetSize());
273 }
274 }
275 return SendErrorResponse (1);
276}
277
278GDBRemoteCommunication::PacketResult
279GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo (StringExtractorGDBRemote &packet)
280{
281 m_proc_infos_index = 0;
282 m_proc_infos.Clear();
283
284 ProcessInstanceInfoMatch match_info;
285 packet.SetFilePos(::strlen ("qfProcessInfo"));
286 if (packet.GetChar() == ':')
287 {
288
289 std::string key;
290 std::string value;
291 while (packet.GetNameColonValue(key, value))
292 {
293 bool success = true;
294 if (key.compare("name") == 0)
295 {
296 StringExtractor extractor;
297 extractor.GetStringRef().swap(value);
298 extractor.GetHexByteString (value);
299 match_info.GetProcessInfo().GetExecutableFile().SetFile(value.c_str(), false);
300 }
301 else if (key.compare("name_match") == 0)
302 {
303 if (value.compare("equals") == 0)
304 {
305 match_info.SetNameMatchType (eNameMatchEquals);
306 }
307 else if (value.compare("starts_with") == 0)
308 {
309 match_info.SetNameMatchType (eNameMatchStartsWith);
310 }
311 else if (value.compare("ends_with") == 0)
312 {
313 match_info.SetNameMatchType (eNameMatchEndsWith);
314 }
315 else if (value.compare("contains") == 0)
316 {
317 match_info.SetNameMatchType (eNameMatchContains);
318 }
319 else if (value.compare("regex") == 0)
320 {
321 match_info.SetNameMatchType (eNameMatchRegularExpression);
322 }
323 else
324 {
325 success = false;
326 }
327 }
328 else if (key.compare("pid") == 0)
329 {
330 match_info.GetProcessInfo().SetProcessID (StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
331 }
332 else if (key.compare("parent_pid") == 0)
333 {
334 match_info.GetProcessInfo().SetParentProcessID (StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
335 }
336 else if (key.compare("uid") == 0)
337 {
338 match_info.GetProcessInfo().SetUserID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
339 }
340 else if (key.compare("gid") == 0)
341 {
342 match_info.GetProcessInfo().SetGroupID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
343 }
344 else if (key.compare("euid") == 0)
345 {
346 match_info.GetProcessInfo().SetEffectiveUserID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
347 }
348 else if (key.compare("egid") == 0)
349 {
350 match_info.GetProcessInfo().SetEffectiveGroupID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
351 }
352 else if (key.compare("all_users") == 0)
353 {
354 match_info.SetMatchAllUsers(Args::StringToBoolean(value.c_str(), false, &success));
355 }
356 else if (key.compare("triple") == 0)
357 {
358 match_info.GetProcessInfo().GetArchitecture().SetTriple (value.c_str(), NULL);
359 }
360 else
361 {
362 success = false;
363 }
364
365 if (!success)
366 return SendErrorResponse (2);
367 }
368 }
369
370 if (Host::FindProcesses (match_info, m_proc_infos))
371 {
372 // We found something, return the first item by calling the get
373 // subsequent process info packet handler...
374 return Handle_qsProcessInfo (packet);
375 }
376 return SendErrorResponse (3);
377}
378
379GDBRemoteCommunication::PacketResult
380GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo (StringExtractorGDBRemote &packet)
381{
382 if (m_proc_infos_index < m_proc_infos.GetSize())
383 {
384 StreamString response;
385 CreateProcessInfoResponse (m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response);
386 ++m_proc_infos_index;
387 return SendPacketNoLock (response.GetData(), response.GetSize());
388 }
389 return SendErrorResponse (4);
390}
391
392GDBRemoteCommunication::PacketResult
393GDBRemoteCommunicationServerCommon::Handle_qUserName (StringExtractorGDBRemote &packet)
394{
395#if !defined(LLDB_DISABLE_POSIX)
396 // Packet format: "qUserName:%i" where %i is the uid
397 packet.SetFilePos(::strlen ("qUserName:"));
398 uint32_t uid = packet.GetU32 (UINT32_MAX);
399 if (uid != UINT32_MAX)
400 {
401 std::string name;
402 if (HostInfo::LookupUserName(uid, name))
403 {
404 StreamString response;
405 response.PutCStringAsRawHex8 (name.c_str());
406 return SendPacketNoLock (response.GetData(), response.GetSize());
407 }
408 }
409#endif
410 return SendErrorResponse (5);
411
412}
413
414GDBRemoteCommunication::PacketResult
415GDBRemoteCommunicationServerCommon::Handle_qGroupName (StringExtractorGDBRemote &packet)
416{
417#if !defined(LLDB_DISABLE_POSIX)
418 // Packet format: "qGroupName:%i" where %i is the gid
419 packet.SetFilePos(::strlen ("qGroupName:"));
420 uint32_t gid = packet.GetU32 (UINT32_MAX);
421 if (gid != UINT32_MAX)
422 {
423 std::string name;
424 if (HostInfo::LookupGroupName(gid, name))
425 {
426 StreamString response;
427 response.PutCStringAsRawHex8 (name.c_str());
428 return SendPacketNoLock (response.GetData(), response.GetSize());
429 }
430 }
431#endif
432 return SendErrorResponse (6);
433}
434
435GDBRemoteCommunication::PacketResult
436GDBRemoteCommunicationServerCommon::Handle_qSpeedTest (StringExtractorGDBRemote &packet)
437{
438 packet.SetFilePos(::strlen ("qSpeedTest:"));
439
440 std::string key;
441 std::string value;
442 bool success = packet.GetNameColonValue(key, value);
443 if (success && key.compare("response_size") == 0)
444 {
445 uint32_t response_size = StringConvert::ToUInt32(value.c_str(), 0, 0, &success);
446 if (success)
447 {
448 if (response_size == 0)
449 return SendOKResponse();
450 StreamString response;
451 uint32_t bytes_left = response_size;
452 response.PutCString("data:");
453 while (bytes_left > 0)
454 {
455 if (bytes_left >= 26)
456 {
457 response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
458 bytes_left -= 26;
459 }
460 else
461 {
462 response.Printf ("%*.*s;", bytes_left, bytes_left, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
463 bytes_left = 0;
464 }
465 }
466 return SendPacketNoLock (response.GetData(), response.GetSize());
467 }
468 }
469 return SendErrorResponse (7);
470}
471
472GDBRemoteCommunication::PacketResult
473GDBRemoteCommunicationServerCommon::Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet)
474{
475 packet.SetFilePos(::strlen ("qKillSpawnedProcess:"));
476
477 lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID);
478
479 // verify that we know anything about this pid.
480 // Scope for locker
481 {
482 Mutex::Locker locker (m_spawned_pids_mutex);
483 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
484 {
485 // not a pid we know about
486 return SendErrorResponse (10);
487 }
488 }
489
490 // go ahead and attempt to kill the spawned process
491 if (KillSpawnedProcess (pid))
492 return SendOKResponse ();
493 else
494 return SendErrorResponse (11);
495}
496
497bool
498GDBRemoteCommunicationServerCommon::KillSpawnedProcess (lldb::pid_t pid)
499{
500 // make sure we know about this process
501 {
502 Mutex::Locker locker (m_spawned_pids_mutex);
503 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
504 return false;
505 }
506
507 // first try a SIGTERM (standard kill)
508 Host::Kill (pid, SIGTERM);
509
510 // check if that worked
511 for (size_t i=0; i<10; ++i)
512 {
513 {
514 Mutex::Locker locker (m_spawned_pids_mutex);
515 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
516 {
517 // it is now killed
518 return true;
519 }
520 }
521 usleep (10000);
522 }
523
524 // check one more time after the final usleep
525 {
526 Mutex::Locker locker (m_spawned_pids_mutex);
527 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
528 return true;
529 }
530
531 // the launched process still lives. Now try killing it again,
532 // this time with an unblockable signal.
533 Host::Kill (pid, SIGKILL);
534
535 for (size_t i=0; i<10; ++i)
536 {
537 {
538 Mutex::Locker locker (m_spawned_pids_mutex);
539 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
540 {
541 // it is now killed
542 return true;
543 }
544 }
545 usleep (10000);
546 }
547
548 // check one more time after the final usleep
549 // Scope for locker
550 {
551 Mutex::Locker locker (m_spawned_pids_mutex);
552 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
553 return true;
554 }
555
556 // no luck - the process still lives
557 return false;
558}
559
560GDBRemoteCommunication::PacketResult
561GDBRemoteCommunicationServerCommon::Handle_vFile_Open (StringExtractorGDBRemote &packet)
562{
563 packet.SetFilePos(::strlen("vFile:open:"));
564 std::string path;
565 packet.GetHexByteStringTerminatedBy(path,',');
566 if (!path.empty())
567 {
568 if (packet.GetChar() == ',')
569 {
570 uint32_t flags = packet.GetHexMaxU32(false, 0);
571 if (packet.GetChar() == ',')
572 {
573 mode_t mode = packet.GetHexMaxU32(false, 0600);
574 Error error;
575 int fd = ::open (path.c_str(), flags, mode);
576 const int save_errno = fd == -1 ? errno : 0;
577 StreamString response;
578 response.PutChar('F');
579 response.Printf("%i", fd);
580 if (save_errno)
581 response.Printf(",%i", save_errno);
582 return SendPacketNoLock(response.GetData(), response.GetSize());
583 }
584 }
585 }
586 return SendErrorResponse(18);
587}
588
589GDBRemoteCommunication::PacketResult
590GDBRemoteCommunicationServerCommon::Handle_vFile_Close (StringExtractorGDBRemote &packet)
591{
592 packet.SetFilePos(::strlen("vFile:close:"));
593 int fd = packet.GetS32(-1);
594 Error error;
595 int err = -1;
596 int save_errno = 0;
597 if (fd >= 0)
598 {
599 err = close(fd);
600 save_errno = err == -1 ? errno : 0;
601 }
602 else
603 {
604 save_errno = EINVAL;
605 }
606 StreamString response;
607 response.PutChar('F');
608 response.Printf("%i", err);
609 if (save_errno)
610 response.Printf(",%i", save_errno);
611 return SendPacketNoLock(response.GetData(), response.GetSize());
612}
613
614GDBRemoteCommunication::PacketResult
615GDBRemoteCommunicationServerCommon::Handle_vFile_pRead (StringExtractorGDBRemote &packet)
616{
617#ifdef _WIN32
618 // Not implemented on Windows
619 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pRead() unimplemented");
620#else
621 StreamGDBRemote response;
622 packet.SetFilePos(::strlen("vFile:pread:"));
623 int fd = packet.GetS32(-1);
624 if (packet.GetChar() == ',')
625 {
626 uint64_t count = packet.GetU64(UINT64_MAX);
627 if (packet.GetChar() == ',')
628 {
629 uint64_t offset = packet.GetU64(UINT32_MAX);
630 if (count == UINT64_MAX)
631 {
632 response.Printf("F-1:%i", EINVAL);
633 return SendPacketNoLock(response.GetData(), response.GetSize());
634 }
635
636 std::string buffer(count, 0);
637 const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset);
638 const int save_errno = bytes_read == -1 ? errno : 0;
639 response.PutChar('F');
640 response.Printf("%zi", bytes_read);
641 if (save_errno)
642 response.Printf(",%i", save_errno);
643 else
644 {
645 response.PutChar(';');
646 response.PutEscapedBytes(&buffer[0], bytes_read);
647 }
648 return SendPacketNoLock(response.GetData(), response.GetSize());
649 }
650 }
651 return SendErrorResponse(21);
652
653#endif
654}
655
656GDBRemoteCommunication::PacketResult
657GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite (StringExtractorGDBRemote &packet)
658{
659#ifdef _WIN32
660 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite() unimplemented");
661#else
662 packet.SetFilePos(::strlen("vFile:pwrite:"));
663
664 StreamGDBRemote response;
665 response.PutChar('F');
666
667 int fd = packet.GetU32(UINT32_MAX);
668 if (packet.GetChar() == ',')
669 {
670 off_t offset = packet.GetU64(UINT32_MAX);
671 if (packet.GetChar() == ',')
672 {
673 std::string buffer;
674 if (packet.GetEscapedBinaryData(buffer))
675 {
676 const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset);
677 const int save_errno = bytes_written == -1 ? errno : 0;
678 response.Printf("%zi", bytes_written);
679 if (save_errno)
680 response.Printf(",%i", save_errno);
681 }
682 else
683 {
684 response.Printf ("-1,%i", EINVAL);
685 }
686 return SendPacketNoLock(response.GetData(), response.GetSize());
687 }
688 }
689 return SendErrorResponse(27);
690#endif
691}
692
693GDBRemoteCommunication::PacketResult
694GDBRemoteCommunicationServerCommon::Handle_vFile_Size (StringExtractorGDBRemote &packet)
695{
696 packet.SetFilePos(::strlen("vFile:size:"));
697 std::string path;
698 packet.GetHexByteString(path);
699 if (!path.empty())
700 {
701 lldb::user_id_t retcode = FileSystem::GetFileSize(FileSpec(path.c_str(), false));
702 StreamString response;
703 response.PutChar('F');
704 response.PutHex64(retcode);
705 if (retcode == UINT64_MAX)
706 {
707 response.PutChar(',');
708 response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode()
709 }
710 return SendPacketNoLock(response.GetData(), response.GetSize());
711 }
712 return SendErrorResponse(22);
713}
714
715GDBRemoteCommunication::PacketResult
716GDBRemoteCommunicationServerCommon::Handle_vFile_Mode (StringExtractorGDBRemote &packet)
717{
718 packet.SetFilePos(::strlen("vFile:mode:"));
719 std::string path;
720 packet.GetHexByteString(path);
721 if (!path.empty())
722 {
723 Error error;
724 const uint32_t mode = File::GetPermissions(path.c_str(), error);
725 StreamString response;
726 response.Printf("F%u", mode);
727 if (mode == 0 || error.Fail())
728 response.Printf(",%i", (int)error.GetError());
729 return SendPacketNoLock(response.GetData(), response.GetSize());
730 }
731 return SendErrorResponse(23);
732}
733
734GDBRemoteCommunication::PacketResult
735GDBRemoteCommunicationServerCommon::Handle_vFile_Exists (StringExtractorGDBRemote &packet)
736{
737 packet.SetFilePos(::strlen("vFile:exists:"));
738 std::string path;
739 packet.GetHexByteString(path);
740 if (!path.empty())
741 {
742 bool retcode = FileSystem::GetFileExists(FileSpec(path.c_str(), false));
743 StreamString response;
744 response.PutChar('F');
745 response.PutChar(',');
746 if (retcode)
747 response.PutChar('1');
748 else
749 response.PutChar('0');
750 return SendPacketNoLock(response.GetData(), response.GetSize());
751 }
752 return SendErrorResponse(24);
753}
754
755GDBRemoteCommunication::PacketResult
756GDBRemoteCommunicationServerCommon::Handle_vFile_symlink (StringExtractorGDBRemote &packet)
757{
758 packet.SetFilePos(::strlen("vFile:symlink:"));
759 std::string dst, src;
760 packet.GetHexByteStringTerminatedBy(dst, ',');
761 packet.GetChar(); // Skip ',' char
762 packet.GetHexByteString(src);
763 Error error = FileSystem::Symlink(src.c_str(), dst.c_str());
764 StreamString response;
765 response.Printf("F%u,%u", error.GetError(), error.GetError());
766 return SendPacketNoLock(response.GetData(), response.GetSize());
767}
768
769GDBRemoteCommunication::PacketResult
770GDBRemoteCommunicationServerCommon::Handle_vFile_unlink (StringExtractorGDBRemote &packet)
771{
772 packet.SetFilePos(::strlen("vFile:unlink:"));
773 std::string path;
774 packet.GetHexByteString(path);
775 Error error = FileSystem::Unlink(path.c_str());
776 StreamString response;
777 response.Printf("F%u,%u", error.GetError(), error.GetError());
778 return SendPacketNoLock(response.GetData(), response.GetSize());
779}
780
781GDBRemoteCommunication::PacketResult
782GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell (StringExtractorGDBRemote &packet)
783{
784 packet.SetFilePos(::strlen("qPlatform_shell:"));
785 std::string path;
786 std::string working_dir;
787 packet.GetHexByteStringTerminatedBy(path,',');
788 if (!path.empty())
789 {
790 if (packet.GetChar() == ',')
791 {
792 // FIXME: add timeout to qPlatform_shell packet
793 // uint32_t timeout = packet.GetHexMaxU32(false, 32);
794 uint32_t timeout = 10;
795 if (packet.GetChar() == ',')
796 packet.GetHexByteString(working_dir);
797 int status, signo;
798 std::string output;
799 Error err = Host::RunShellCommand(path.c_str(),
800 working_dir.empty() ? NULL : working_dir.c_str(),
801 &status, &signo, &output, timeout);
802 StreamGDBRemote response;
803 if (err.Fail())
804 {
805 response.PutCString("F,");
806 response.PutHex32(UINT32_MAX);
807 }
808 else
809 {
810 response.PutCString("F,");
811 response.PutHex32(status);
812 response.PutChar(',');
813 response.PutHex32(signo);
814 response.PutChar(',');
815 response.PutEscapedBytes(output.c_str(), output.size());
816 }
817 return SendPacketNoLock(response.GetData(), response.GetSize());
818 }
819 }
820 return SendErrorResponse(24);
821}
822
823
824GDBRemoteCommunication::PacketResult
825GDBRemoteCommunicationServerCommon::Handle_vFile_Stat (StringExtractorGDBRemote &packet)
826{
827 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
828}
829
830GDBRemoteCommunication::PacketResult
831GDBRemoteCommunicationServerCommon::Handle_vFile_MD5 (StringExtractorGDBRemote &packet)
832{
833 packet.SetFilePos(::strlen("vFile:MD5:"));
834 std::string path;
835 packet.GetHexByteString(path);
836 if (!path.empty())
837 {
838 uint64_t a,b;
839 StreamGDBRemote response;
Oleksiy Vyalov6801be32015-02-25 22:15:44 +0000840 if (!FileSystem::CalculateMD5(FileSpec(path.c_str(), false), a, b))
Tamas Berghammere13c2732015-02-11 10:29:30 +0000841 {
842 response.PutCString("F,");
843 response.PutCString("x");
844 }
845 else
846 {
847 response.PutCString("F,");
848 response.PutHex64(a);
849 response.PutHex64(b);
850 }
851 return SendPacketNoLock(response.GetData(), response.GetSize());
852 }
853 return SendErrorResponse(25);
854}
855
856GDBRemoteCommunication::PacketResult
857GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet)
858{
859 packet.SetFilePos(::strlen("qPlatform_mkdir:"));
860 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
861 if (packet.GetChar() == ',')
862 {
863 std::string path;
864 packet.GetHexByteString(path);
865 Error error = FileSystem::MakeDirectory(path.c_str(), mode);
Tamas Berghammer0f86b742015-02-23 11:03:08 +0000866
867 StreamGDBRemote response;
868 response.Printf("F%u", error.GetError());
869
870 return SendPacketNoLock(response.GetData(), response.GetSize());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000871 }
872 return SendErrorResponse(20);
873}
874
875GDBRemoteCommunication::PacketResult
876GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet)
877{
878 packet.SetFilePos(::strlen("qPlatform_chmod:"));
879
880 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
881 if (packet.GetChar() == ',')
882 {
883 std::string path;
884 packet.GetHexByteString(path);
885 Error error = FileSystem::SetFilePermissions(path.c_str(), mode);
Tamas Berghammer0f86b742015-02-23 11:03:08 +0000886
887 StreamGDBRemote response;
888 response.Printf("F%u", error.GetError());
889
890 return SendPacketNoLock(response.GetData(), response.GetSize());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000891 }
892 return SendErrorResponse(19);
893}
894
895GDBRemoteCommunication::PacketResult
896GDBRemoteCommunicationServerCommon::Handle_qSupported (StringExtractorGDBRemote &packet)
897{
898 StreamGDBRemote response;
899
900 // Features common to lldb-platform and llgs.
901 uint32_t max_packet_size = 128 * 1024; // 128KBytes is a reasonable max packet size--debugger can always use less
902 response.Printf ("PacketSize=%x", max_packet_size);
903
904 response.PutCString (";QStartNoAckMode+");
905 response.PutCString (";QThreadSuffixSupported+");
906 response.PutCString (";QListThreadsInStopReply+");
907#if defined(__linux__)
908 response.PutCString (";qXfer:auxv:read+");
909#endif
910
911 return SendPacketNoLock(response.GetData(), response.GetSize());
912}
913
914GDBRemoteCommunication::PacketResult
915GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet)
916{
917 m_thread_suffix_supported = true;
918 return SendOKResponse();
919}
920
921GDBRemoteCommunication::PacketResult
922GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet)
923{
924 m_list_threads_in_stop_reply = true;
925 return SendOKResponse();
926}
927
928GDBRemoteCommunication::PacketResult
929GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError (StringExtractorGDBRemote &packet)
930{
931 packet.SetFilePos(::strlen ("QSetDetachOnError:"));
932 if (packet.GetU32(0))
933 m_process_launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
934 else
935 m_process_launch_info.GetFlags().Clear (eLaunchFlagDetachOnError);
936 return SendOKResponse ();
937}
938
939GDBRemoteCommunication::PacketResult
940GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet)
941{
942 // Send response first before changing m_send_acks to we ack this packet
943 PacketResult packet_result = SendOKResponse ();
944 m_send_acks = false;
945 return packet_result;
946}
947
948GDBRemoteCommunication::PacketResult
949GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN (StringExtractorGDBRemote &packet)
950{
951 packet.SetFilePos(::strlen ("QSetSTDIN:"));
952 FileAction file_action;
953 std::string path;
954 packet.GetHexByteString(path);
955 const bool read = false;
956 const bool write = true;
957 if (file_action.Open(STDIN_FILENO, path.c_str(), read, write))
958 {
959 m_process_launch_info.AppendFileAction(file_action);
960 return SendOKResponse ();
961 }
962 return SendErrorResponse (15);
963}
964
965GDBRemoteCommunication::PacketResult
966GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet)
967{
968 packet.SetFilePos(::strlen ("QSetSTDOUT:"));
969 FileAction file_action;
970 std::string path;
971 packet.GetHexByteString(path);
972 const bool read = true;
973 const bool write = false;
974 if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write))
975 {
976 m_process_launch_info.AppendFileAction(file_action);
977 return SendOKResponse ();
978 }
979 return SendErrorResponse (16);
980}
981
982GDBRemoteCommunication::PacketResult
983GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR (StringExtractorGDBRemote &packet)
984{
985 packet.SetFilePos(::strlen ("QSetSTDERR:"));
986 FileAction file_action;
987 std::string path;
988 packet.GetHexByteString(path);
989 const bool read = true;
990 const bool write = false;
991 if (file_action.Open(STDERR_FILENO, path.c_str(), read, write))
992 {
993 m_process_launch_info.AppendFileAction(file_action);
994 return SendOKResponse ();
995 }
996 return SendErrorResponse (17);
997}
998
999GDBRemoteCommunication::PacketResult
1000GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet)
1001{
1002 if (m_process_launch_error.Success())
1003 return SendOKResponse();
1004 StreamString response;
1005 response.PutChar('E');
1006 response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
1007 return SendPacketNoLock (response.GetData(), response.GetSize());
1008}
1009
1010GDBRemoteCommunication::PacketResult
1011GDBRemoteCommunicationServerCommon::Handle_QEnvironment (StringExtractorGDBRemote &packet)
1012{
1013 packet.SetFilePos(::strlen ("QEnvironment:"));
1014 const uint32_t bytes_left = packet.GetBytesLeft();
1015 if (bytes_left > 0)
1016 {
1017 m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek());
1018 return SendOKResponse ();
1019 }
1020 return SendErrorResponse (12);
1021}
1022
1023GDBRemoteCommunication::PacketResult
1024GDBRemoteCommunicationServerCommon::Handle_QLaunchArch (StringExtractorGDBRemote &packet)
1025{
1026 packet.SetFilePos(::strlen ("QLaunchArch:"));
1027 const uint32_t bytes_left = packet.GetBytesLeft();
1028 if (bytes_left > 0)
1029 {
1030 const char* arch_triple = packet.Peek();
1031 ArchSpec arch_spec(arch_triple,NULL);
1032 m_process_launch_info.SetArchitecture(arch_spec);
1033 return SendOKResponse();
1034 }
1035 return SendErrorResponse(13);
1036}
1037
1038GDBRemoteCommunication::PacketResult
1039GDBRemoteCommunicationServerCommon::Handle_A (StringExtractorGDBRemote &packet)
1040{
1041 // The 'A' packet is the most over designed packet ever here with
1042 // redundant argument indexes, redundant argument lengths and needed hex
1043 // encoded argument string values. Really all that is needed is a comma
1044 // separated hex encoded argument value list, but we will stay true to the
1045 // documented version of the 'A' packet here...
1046
1047 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1048 int actual_arg_index = 0;
1049
1050 packet.SetFilePos(1); // Skip the 'A'
1051 bool success = true;
1052 while (success && packet.GetBytesLeft() > 0)
1053 {
1054 // Decode the decimal argument string length. This length is the
1055 // number of hex nibbles in the argument string value.
1056 const uint32_t arg_len = packet.GetU32(UINT32_MAX);
1057 if (arg_len == UINT32_MAX)
1058 success = false;
1059 else
1060 {
1061 // Make sure the argument hex string length is followed by a comma
1062 if (packet.GetChar() != ',')
1063 success = false;
1064 else
1065 {
1066 // Decode the argument index. We ignore this really because
1067 // who would really send down the arguments in a random order???
1068 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1069 if (arg_idx == UINT32_MAX)
1070 success = false;
1071 else
1072 {
1073 // Make sure the argument index is followed by a comma
1074 if (packet.GetChar() != ',')
1075 success = false;
1076 else
1077 {
1078 // Decode the argument string value from hex bytes
1079 // back into a UTF8 string and make sure the length
1080 // matches the one supplied in the packet
1081 std::string arg;
1082 if (packet.GetHexByteStringFixedLength(arg, arg_len) != (arg_len / 2))
1083 success = false;
1084 else
1085 {
1086 // If there are any bytes left
1087 if (packet.GetBytesLeft())
1088 {
1089 if (packet.GetChar() != ',')
1090 success = false;
1091 }
1092
1093 if (success)
1094 {
1095 if (arg_idx == 0)
1096 m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false);
1097 m_process_launch_info.GetArguments().AppendArgument(arg.c_str());
1098 if (log)
1099 log->Printf ("LLGSPacketHandler::%s added arg %d: \"%s\"", __FUNCTION__, actual_arg_index, arg.c_str ());
1100 ++actual_arg_index;
1101 }
1102 }
1103 }
1104 }
1105 }
1106 }
1107 }
1108
1109 if (success)
1110 {
1111 m_process_launch_error = LaunchProcess ();
1112 if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1113 {
1114 return SendOKResponse ();
1115 }
1116 else
1117 {
1118 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1119 if (log)
1120 log->Printf("LLGSPacketHandler::%s failed to launch exe: %s",
1121 __FUNCTION__,
1122 m_process_launch_error.AsCString());
1123
1124 }
1125 }
1126 return SendErrorResponse (8);
1127}
1128
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001129GDBRemoteCommunication::PacketResult
1130GDBRemoteCommunicationServerCommon::Handle_qModuleInfo (StringExtractorGDBRemote &packet)
1131{
1132 packet.SetFilePos(::strlen ("qModuleInfo:"));
1133
1134 std::string module_path;
1135 packet.GetHexByteStringTerminatedBy(module_path, ';');
1136 if (module_path.empty())
1137 return SendErrorResponse (1);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001138
1139 if (packet.GetChar() != ';')
1140 return SendErrorResponse (2);
1141
1142 std::string triple;
1143 packet.GetHexByteString(triple);
Tamas Berghammerdad4db72015-03-13 11:16:08 +00001144 ArchSpec arch(triple.c_str());
1145
1146#ifdef __ANDROID__
1147 const FileSpec module_path_spec = HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1148#else
1149 const FileSpec module_path_spec(module_path.c_str(), true);
1150#endif
1151
1152 const ModuleSpec module_spec(module_path_spec, arch);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001153
1154 ModuleSpecList module_specs;
1155 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, module_specs))
1156 return SendErrorResponse (3);
1157
1158 ModuleSpec matched_module_spec;
1159 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1160 return SendErrorResponse (4);
1161
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001162 const auto file_offset = matched_module_spec.GetObjectOffset();
1163 const auto file_size = matched_module_spec.GetObjectSize();
1164 const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001165
1166 StreamGDBRemote response;
1167
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001168 if (uuid_str.empty())
1169 {
1170 std::string md5_hash;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001171 if (!FileSystem::CalculateMD5AsString(matched_module_spec.GetFileSpec(), file_offset, file_size, md5_hash))
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001172 return SendErrorResponse (5);
1173 response.PutCString ("md5:");
1174 response.PutCStringAsRawHex8(md5_hash.c_str());
1175 }
1176 else{
1177 response.PutCString ("uuid:");
1178 response.PutCStringAsRawHex8(uuid_str.c_str());
1179 }
1180 response.PutChar(';');
1181
1182 const auto &module_arch = matched_module_spec.GetArchitecture();
1183 response.PutCString("triple:");
1184 response.PutCStringAsRawHex8( module_arch.GetTriple().getTriple().c_str());
1185 response.PutChar(';');
1186
Tamas Berghammerdad4db72015-03-13 11:16:08 +00001187 response.PutCString("file_path:");
1188 response.PutCStringAsRawHex8(module_path_spec.GetPath().c_str());
1189 response.PutChar(';');
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001190 response.PutCString("file_offset:");
1191 response.PutHex64(file_offset);
1192 response.PutChar(';');
1193 response.PutCString("file_size:");
1194 response.PutHex64(file_size);
1195 response.PutChar(';');
1196
1197 return SendPacketNoLock(response.GetData(), response.GetSize());
1198}
1199
Tamas Berghammere13c2732015-02-11 10:29:30 +00001200void
1201GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info,
1202 StreamString &response)
1203{
1204 response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1205 proc_info.GetProcessID(),
1206 proc_info.GetParentProcessID(),
1207 proc_info.GetUserID(),
1208 proc_info.GetGroupID(),
1209 proc_info.GetEffectiveUserID(),
1210 proc_info.GetEffectiveGroupID());
1211 response.PutCString ("name:");
Oleksiy Vyalovbac75692015-03-11 18:13:37 +00001212 response.PutCStringAsRawHex8(proc_info.GetExecutableFile().GetPath().c_str());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001213 response.PutChar(';');
1214 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1215 if (proc_arch.IsValid())
1216 {
1217 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1218 response.PutCString("triple:");
1219 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1220 response.PutChar(';');
1221 }
1222}
1223
1224void
1225GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse_DebugServerStyle (
1226 const ProcessInstanceInfo &proc_info, StreamString &response)
1227{
1228 response.Printf ("pid:%" PRIx64 ";parent-pid:%" PRIx64 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1229 proc_info.GetProcessID(),
1230 proc_info.GetParentProcessID(),
1231 proc_info.GetUserID(),
1232 proc_info.GetGroupID(),
1233 proc_info.GetEffectiveUserID(),
1234 proc_info.GetEffectiveGroupID());
1235
1236 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1237 if (proc_arch.IsValid())
1238 {
1239 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1240#if defined(__APPLE__)
1241 // We'll send cputype/cpusubtype.
1242 const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1243 if (cpu_type != 0)
1244 response.Printf ("cputype:%" PRIx32 ";", cpu_type);
1245
1246 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1247 if (cpu_subtype != 0)
1248 response.Printf ("cpusubtype:%" PRIx32 ";", cpu_subtype);
1249
1250 const std::string vendor = proc_triple.getVendorName ();
1251 if (!vendor.empty ())
1252 response.Printf ("vendor:%s;", vendor.c_str ());
1253#else
1254 // We'll send the triple.
1255 response.PutCString("triple:");
1256 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1257 response.PutChar(';');
1258#endif
1259 std::string ostype = proc_triple.getOSName ();
1260 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1261 if (proc_triple.getVendor () == llvm::Triple::Apple)
1262 {
1263 switch (proc_triple.getArch ())
1264 {
1265 case llvm::Triple::arm:
1266 case llvm::Triple::aarch64:
1267 ostype = "ios";
1268 break;
1269 default:
1270 // No change.
1271 break;
1272 }
1273 }
1274 response.Printf ("ostype:%s;", ostype.c_str ());
1275
1276
1277 switch (proc_arch.GetByteOrder ())
1278 {
1279 case lldb::eByteOrderLittle: response.PutCString ("endian:little;"); break;
1280 case lldb::eByteOrderBig: response.PutCString ("endian:big;"); break;
1281 case lldb::eByteOrderPDP: response.PutCString ("endian:pdp;"); break;
1282 default:
1283 // Nothing.
1284 break;
1285 }
1286
1287 if (proc_triple.isArch64Bit ())
1288 response.PutCString ("ptrsize:8;");
1289 else if (proc_triple.isArch32Bit ())
1290 response.PutCString ("ptrsize:4;");
1291 else if (proc_triple.isArch16Bit ())
1292 response.PutCString ("ptrsize:2;");
1293 }
1294}