blob: 3e9ad7c3fdb5cdafb9ac0fcc852701a894436203 [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 {
Robert Flackebc56092015-03-18 13:55:48 +0000570 uint32_t flags = File::ConvertOpenOptionsForPOSIXOpen(
571 packet.GetHexMaxU32(false, 0));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000572 if (packet.GetChar() == ',')
573 {
574 mode_t mode = packet.GetHexMaxU32(false, 0600);
575 Error error;
576 int fd = ::open (path.c_str(), flags, mode);
577 const int save_errno = fd == -1 ? errno : 0;
578 StreamString response;
579 response.PutChar('F');
580 response.Printf("%i", fd);
581 if (save_errno)
582 response.Printf(",%i", save_errno);
583 return SendPacketNoLock(response.GetData(), response.GetSize());
584 }
585 }
586 }
587 return SendErrorResponse(18);
588}
589
590GDBRemoteCommunication::PacketResult
591GDBRemoteCommunicationServerCommon::Handle_vFile_Close (StringExtractorGDBRemote &packet)
592{
593 packet.SetFilePos(::strlen("vFile:close:"));
594 int fd = packet.GetS32(-1);
595 Error error;
596 int err = -1;
597 int save_errno = 0;
598 if (fd >= 0)
599 {
600 err = close(fd);
601 save_errno = err == -1 ? errno : 0;
602 }
603 else
604 {
605 save_errno = EINVAL;
606 }
607 StreamString response;
608 response.PutChar('F');
609 response.Printf("%i", err);
610 if (save_errno)
611 response.Printf(",%i", save_errno);
612 return SendPacketNoLock(response.GetData(), response.GetSize());
613}
614
615GDBRemoteCommunication::PacketResult
616GDBRemoteCommunicationServerCommon::Handle_vFile_pRead (StringExtractorGDBRemote &packet)
617{
618#ifdef _WIN32
619 // Not implemented on Windows
620 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pRead() unimplemented");
621#else
622 StreamGDBRemote response;
623 packet.SetFilePos(::strlen("vFile:pread:"));
624 int fd = packet.GetS32(-1);
625 if (packet.GetChar() == ',')
626 {
627 uint64_t count = packet.GetU64(UINT64_MAX);
628 if (packet.GetChar() == ',')
629 {
630 uint64_t offset = packet.GetU64(UINT32_MAX);
631 if (count == UINT64_MAX)
632 {
633 response.Printf("F-1:%i", EINVAL);
634 return SendPacketNoLock(response.GetData(), response.GetSize());
635 }
636
637 std::string buffer(count, 0);
638 const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset);
639 const int save_errno = bytes_read == -1 ? errno : 0;
640 response.PutChar('F');
641 response.Printf("%zi", bytes_read);
642 if (save_errno)
643 response.Printf(",%i", save_errno);
644 else
645 {
646 response.PutChar(';');
647 response.PutEscapedBytes(&buffer[0], bytes_read);
648 }
649 return SendPacketNoLock(response.GetData(), response.GetSize());
650 }
651 }
652 return SendErrorResponse(21);
653
654#endif
655}
656
657GDBRemoteCommunication::PacketResult
658GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite (StringExtractorGDBRemote &packet)
659{
660#ifdef _WIN32
661 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite() unimplemented");
662#else
663 packet.SetFilePos(::strlen("vFile:pwrite:"));
664
665 StreamGDBRemote response;
666 response.PutChar('F');
667
668 int fd = packet.GetU32(UINT32_MAX);
669 if (packet.GetChar() == ',')
670 {
671 off_t offset = packet.GetU64(UINT32_MAX);
672 if (packet.GetChar() == ',')
673 {
674 std::string buffer;
675 if (packet.GetEscapedBinaryData(buffer))
676 {
677 const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset);
678 const int save_errno = bytes_written == -1 ? errno : 0;
679 response.Printf("%zi", bytes_written);
680 if (save_errno)
681 response.Printf(",%i", save_errno);
682 }
683 else
684 {
685 response.Printf ("-1,%i", EINVAL);
686 }
687 return SendPacketNoLock(response.GetData(), response.GetSize());
688 }
689 }
690 return SendErrorResponse(27);
691#endif
692}
693
694GDBRemoteCommunication::PacketResult
695GDBRemoteCommunicationServerCommon::Handle_vFile_Size (StringExtractorGDBRemote &packet)
696{
697 packet.SetFilePos(::strlen("vFile:size:"));
698 std::string path;
699 packet.GetHexByteString(path);
700 if (!path.empty())
701 {
702 lldb::user_id_t retcode = FileSystem::GetFileSize(FileSpec(path.c_str(), false));
703 StreamString response;
704 response.PutChar('F');
705 response.PutHex64(retcode);
706 if (retcode == UINT64_MAX)
707 {
708 response.PutChar(',');
709 response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode()
710 }
711 return SendPacketNoLock(response.GetData(), response.GetSize());
712 }
713 return SendErrorResponse(22);
714}
715
716GDBRemoteCommunication::PacketResult
717GDBRemoteCommunicationServerCommon::Handle_vFile_Mode (StringExtractorGDBRemote &packet)
718{
719 packet.SetFilePos(::strlen("vFile:mode:"));
720 std::string path;
721 packet.GetHexByteString(path);
722 if (!path.empty())
723 {
724 Error error;
725 const uint32_t mode = File::GetPermissions(path.c_str(), error);
726 StreamString response;
727 response.Printf("F%u", mode);
728 if (mode == 0 || error.Fail())
729 response.Printf(",%i", (int)error.GetError());
730 return SendPacketNoLock(response.GetData(), response.GetSize());
731 }
732 return SendErrorResponse(23);
733}
734
735GDBRemoteCommunication::PacketResult
736GDBRemoteCommunicationServerCommon::Handle_vFile_Exists (StringExtractorGDBRemote &packet)
737{
738 packet.SetFilePos(::strlen("vFile:exists:"));
739 std::string path;
740 packet.GetHexByteString(path);
741 if (!path.empty())
742 {
743 bool retcode = FileSystem::GetFileExists(FileSpec(path.c_str(), false));
744 StreamString response;
745 response.PutChar('F');
746 response.PutChar(',');
747 if (retcode)
748 response.PutChar('1');
749 else
750 response.PutChar('0');
751 return SendPacketNoLock(response.GetData(), response.GetSize());
752 }
753 return SendErrorResponse(24);
754}
755
756GDBRemoteCommunication::PacketResult
757GDBRemoteCommunicationServerCommon::Handle_vFile_symlink (StringExtractorGDBRemote &packet)
758{
759 packet.SetFilePos(::strlen("vFile:symlink:"));
760 std::string dst, src;
761 packet.GetHexByteStringTerminatedBy(dst, ',');
762 packet.GetChar(); // Skip ',' char
763 packet.GetHexByteString(src);
764 Error error = FileSystem::Symlink(src.c_str(), dst.c_str());
765 StreamString response;
766 response.Printf("F%u,%u", error.GetError(), error.GetError());
767 return SendPacketNoLock(response.GetData(), response.GetSize());
768}
769
770GDBRemoteCommunication::PacketResult
771GDBRemoteCommunicationServerCommon::Handle_vFile_unlink (StringExtractorGDBRemote &packet)
772{
773 packet.SetFilePos(::strlen("vFile:unlink:"));
774 std::string path;
775 packet.GetHexByteString(path);
776 Error error = FileSystem::Unlink(path.c_str());
777 StreamString response;
778 response.Printf("F%u,%u", error.GetError(), error.GetError());
779 return SendPacketNoLock(response.GetData(), response.GetSize());
780}
781
782GDBRemoteCommunication::PacketResult
783GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell (StringExtractorGDBRemote &packet)
784{
785 packet.SetFilePos(::strlen("qPlatform_shell:"));
786 std::string path;
787 std::string working_dir;
788 packet.GetHexByteStringTerminatedBy(path,',');
789 if (!path.empty())
790 {
791 if (packet.GetChar() == ',')
792 {
793 // FIXME: add timeout to qPlatform_shell packet
794 // uint32_t timeout = packet.GetHexMaxU32(false, 32);
795 uint32_t timeout = 10;
796 if (packet.GetChar() == ',')
797 packet.GetHexByteString(working_dir);
798 int status, signo;
799 std::string output;
800 Error err = Host::RunShellCommand(path.c_str(),
801 working_dir.empty() ? NULL : working_dir.c_str(),
802 &status, &signo, &output, timeout);
803 StreamGDBRemote response;
804 if (err.Fail())
805 {
806 response.PutCString("F,");
807 response.PutHex32(UINT32_MAX);
808 }
809 else
810 {
811 response.PutCString("F,");
812 response.PutHex32(status);
813 response.PutChar(',');
814 response.PutHex32(signo);
815 response.PutChar(',');
816 response.PutEscapedBytes(output.c_str(), output.size());
817 }
818 return SendPacketNoLock(response.GetData(), response.GetSize());
819 }
820 }
821 return SendErrorResponse(24);
822}
823
824
825GDBRemoteCommunication::PacketResult
826GDBRemoteCommunicationServerCommon::Handle_vFile_Stat (StringExtractorGDBRemote &packet)
827{
828 return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
829}
830
831GDBRemoteCommunication::PacketResult
832GDBRemoteCommunicationServerCommon::Handle_vFile_MD5 (StringExtractorGDBRemote &packet)
833{
834 packet.SetFilePos(::strlen("vFile:MD5:"));
835 std::string path;
836 packet.GetHexByteString(path);
837 if (!path.empty())
838 {
839 uint64_t a,b;
840 StreamGDBRemote response;
Oleksiy Vyalov6801be32015-02-25 22:15:44 +0000841 if (!FileSystem::CalculateMD5(FileSpec(path.c_str(), false), a, b))
Tamas Berghammere13c2732015-02-11 10:29:30 +0000842 {
843 response.PutCString("F,");
844 response.PutCString("x");
845 }
846 else
847 {
848 response.PutCString("F,");
849 response.PutHex64(a);
850 response.PutHex64(b);
851 }
852 return SendPacketNoLock(response.GetData(), response.GetSize());
853 }
854 return SendErrorResponse(25);
855}
856
857GDBRemoteCommunication::PacketResult
858GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet)
859{
860 packet.SetFilePos(::strlen("qPlatform_mkdir:"));
861 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
862 if (packet.GetChar() == ',')
863 {
864 std::string path;
865 packet.GetHexByteString(path);
866 Error error = FileSystem::MakeDirectory(path.c_str(), mode);
Tamas Berghammer0f86b742015-02-23 11:03:08 +0000867
868 StreamGDBRemote response;
869 response.Printf("F%u", error.GetError());
870
871 return SendPacketNoLock(response.GetData(), response.GetSize());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000872 }
873 return SendErrorResponse(20);
874}
875
876GDBRemoteCommunication::PacketResult
877GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet)
878{
879 packet.SetFilePos(::strlen("qPlatform_chmod:"));
880
881 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
882 if (packet.GetChar() == ',')
883 {
884 std::string path;
885 packet.GetHexByteString(path);
886 Error error = FileSystem::SetFilePermissions(path.c_str(), mode);
Tamas Berghammer0f86b742015-02-23 11:03:08 +0000887
888 StreamGDBRemote response;
889 response.Printf("F%u", error.GetError());
890
891 return SendPacketNoLock(response.GetData(), response.GetSize());
Tamas Berghammere13c2732015-02-11 10:29:30 +0000892 }
893 return SendErrorResponse(19);
894}
895
896GDBRemoteCommunication::PacketResult
897GDBRemoteCommunicationServerCommon::Handle_qSupported (StringExtractorGDBRemote &packet)
898{
899 StreamGDBRemote response;
900
901 // Features common to lldb-platform and llgs.
902 uint32_t max_packet_size = 128 * 1024; // 128KBytes is a reasonable max packet size--debugger can always use less
903 response.Printf ("PacketSize=%x", max_packet_size);
904
905 response.PutCString (";QStartNoAckMode+");
906 response.PutCString (";QThreadSuffixSupported+");
907 response.PutCString (";QListThreadsInStopReply+");
908#if defined(__linux__)
909 response.PutCString (";qXfer:auxv:read+");
910#endif
911
912 return SendPacketNoLock(response.GetData(), response.GetSize());
913}
914
915GDBRemoteCommunication::PacketResult
916GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet)
917{
918 m_thread_suffix_supported = true;
919 return SendOKResponse();
920}
921
922GDBRemoteCommunication::PacketResult
923GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet)
924{
925 m_list_threads_in_stop_reply = true;
926 return SendOKResponse();
927}
928
929GDBRemoteCommunication::PacketResult
930GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError (StringExtractorGDBRemote &packet)
931{
932 packet.SetFilePos(::strlen ("QSetDetachOnError:"));
933 if (packet.GetU32(0))
934 m_process_launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
935 else
936 m_process_launch_info.GetFlags().Clear (eLaunchFlagDetachOnError);
937 return SendOKResponse ();
938}
939
940GDBRemoteCommunication::PacketResult
941GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet)
942{
943 // Send response first before changing m_send_acks to we ack this packet
944 PacketResult packet_result = SendOKResponse ();
945 m_send_acks = false;
946 return packet_result;
947}
948
949GDBRemoteCommunication::PacketResult
950GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN (StringExtractorGDBRemote &packet)
951{
952 packet.SetFilePos(::strlen ("QSetSTDIN:"));
953 FileAction file_action;
954 std::string path;
955 packet.GetHexByteString(path);
956 const bool read = false;
957 const bool write = true;
958 if (file_action.Open(STDIN_FILENO, path.c_str(), read, write))
959 {
960 m_process_launch_info.AppendFileAction(file_action);
961 return SendOKResponse ();
962 }
963 return SendErrorResponse (15);
964}
965
966GDBRemoteCommunication::PacketResult
967GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet)
968{
969 packet.SetFilePos(::strlen ("QSetSTDOUT:"));
970 FileAction file_action;
971 std::string path;
972 packet.GetHexByteString(path);
973 const bool read = true;
974 const bool write = false;
975 if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write))
976 {
977 m_process_launch_info.AppendFileAction(file_action);
978 return SendOKResponse ();
979 }
980 return SendErrorResponse (16);
981}
982
983GDBRemoteCommunication::PacketResult
984GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR (StringExtractorGDBRemote &packet)
985{
986 packet.SetFilePos(::strlen ("QSetSTDERR:"));
987 FileAction file_action;
988 std::string path;
989 packet.GetHexByteString(path);
990 const bool read = true;
991 const bool write = false;
992 if (file_action.Open(STDERR_FILENO, path.c_str(), read, write))
993 {
994 m_process_launch_info.AppendFileAction(file_action);
995 return SendOKResponse ();
996 }
997 return SendErrorResponse (17);
998}
999
1000GDBRemoteCommunication::PacketResult
1001GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet)
1002{
1003 if (m_process_launch_error.Success())
1004 return SendOKResponse();
1005 StreamString response;
1006 response.PutChar('E');
1007 response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
1008 return SendPacketNoLock (response.GetData(), response.GetSize());
1009}
1010
1011GDBRemoteCommunication::PacketResult
1012GDBRemoteCommunicationServerCommon::Handle_QEnvironment (StringExtractorGDBRemote &packet)
1013{
1014 packet.SetFilePos(::strlen ("QEnvironment:"));
1015 const uint32_t bytes_left = packet.GetBytesLeft();
1016 if (bytes_left > 0)
1017 {
1018 m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek());
1019 return SendOKResponse ();
1020 }
1021 return SendErrorResponse (12);
1022}
1023
1024GDBRemoteCommunication::PacketResult
1025GDBRemoteCommunicationServerCommon::Handle_QLaunchArch (StringExtractorGDBRemote &packet)
1026{
1027 packet.SetFilePos(::strlen ("QLaunchArch:"));
1028 const uint32_t bytes_left = packet.GetBytesLeft();
1029 if (bytes_left > 0)
1030 {
1031 const char* arch_triple = packet.Peek();
1032 ArchSpec arch_spec(arch_triple,NULL);
1033 m_process_launch_info.SetArchitecture(arch_spec);
1034 return SendOKResponse();
1035 }
1036 return SendErrorResponse(13);
1037}
1038
1039GDBRemoteCommunication::PacketResult
1040GDBRemoteCommunicationServerCommon::Handle_A (StringExtractorGDBRemote &packet)
1041{
1042 // The 'A' packet is the most over designed packet ever here with
1043 // redundant argument indexes, redundant argument lengths and needed hex
1044 // encoded argument string values. Really all that is needed is a comma
1045 // separated hex encoded argument value list, but we will stay true to the
1046 // documented version of the 'A' packet here...
1047
1048 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1049 int actual_arg_index = 0;
1050
1051 packet.SetFilePos(1); // Skip the 'A'
1052 bool success = true;
1053 while (success && packet.GetBytesLeft() > 0)
1054 {
1055 // Decode the decimal argument string length. This length is the
1056 // number of hex nibbles in the argument string value.
1057 const uint32_t arg_len = packet.GetU32(UINT32_MAX);
1058 if (arg_len == UINT32_MAX)
1059 success = false;
1060 else
1061 {
1062 // Make sure the argument hex string length is followed by a comma
1063 if (packet.GetChar() != ',')
1064 success = false;
1065 else
1066 {
1067 // Decode the argument index. We ignore this really because
1068 // who would really send down the arguments in a random order???
1069 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1070 if (arg_idx == UINT32_MAX)
1071 success = false;
1072 else
1073 {
1074 // Make sure the argument index is followed by a comma
1075 if (packet.GetChar() != ',')
1076 success = false;
1077 else
1078 {
1079 // Decode the argument string value from hex bytes
1080 // back into a UTF8 string and make sure the length
1081 // matches the one supplied in the packet
1082 std::string arg;
1083 if (packet.GetHexByteStringFixedLength(arg, arg_len) != (arg_len / 2))
1084 success = false;
1085 else
1086 {
1087 // If there are any bytes left
1088 if (packet.GetBytesLeft())
1089 {
1090 if (packet.GetChar() != ',')
1091 success = false;
1092 }
1093
1094 if (success)
1095 {
1096 if (arg_idx == 0)
1097 m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false);
1098 m_process_launch_info.GetArguments().AppendArgument(arg.c_str());
1099 if (log)
1100 log->Printf ("LLGSPacketHandler::%s added arg %d: \"%s\"", __FUNCTION__, actual_arg_index, arg.c_str ());
1101 ++actual_arg_index;
1102 }
1103 }
1104 }
1105 }
1106 }
1107 }
1108 }
1109
1110 if (success)
1111 {
1112 m_process_launch_error = LaunchProcess ();
1113 if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1114 {
1115 return SendOKResponse ();
1116 }
1117 else
1118 {
1119 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1120 if (log)
1121 log->Printf("LLGSPacketHandler::%s failed to launch exe: %s",
1122 __FUNCTION__,
1123 m_process_launch_error.AsCString());
1124
1125 }
1126 }
1127 return SendErrorResponse (8);
1128}
1129
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001130GDBRemoteCommunication::PacketResult
1131GDBRemoteCommunicationServerCommon::Handle_qModuleInfo (StringExtractorGDBRemote &packet)
1132{
1133 packet.SetFilePos(::strlen ("qModuleInfo:"));
1134
1135 std::string module_path;
1136 packet.GetHexByteStringTerminatedBy(module_path, ';');
1137 if (module_path.empty())
1138 return SendErrorResponse (1);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001139
1140 if (packet.GetChar() != ';')
1141 return SendErrorResponse (2);
1142
1143 std::string triple;
1144 packet.GetHexByteString(triple);
Tamas Berghammerdad4db72015-03-13 11:16:08 +00001145 ArchSpec arch(triple.c_str());
1146
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001147 const FileSpec module_path_spec = FindModuleFile(module_path, arch);
Tamas Berghammerdad4db72015-03-13 11:16:08 +00001148 const ModuleSpec module_spec(module_path_spec, arch);
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001149
1150 ModuleSpecList module_specs;
1151 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, module_specs))
1152 return SendErrorResponse (3);
1153
1154 ModuleSpec matched_module_spec;
1155 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1156 return SendErrorResponse (4);
1157
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001158 const auto file_offset = matched_module_spec.GetObjectOffset();
1159 const auto file_size = matched_module_spec.GetObjectSize();
1160 const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001161
1162 StreamGDBRemote response;
1163
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001164 if (uuid_str.empty())
1165 {
1166 std::string md5_hash;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001167 if (!FileSystem::CalculateMD5AsString(matched_module_spec.GetFileSpec(), file_offset, file_size, md5_hash))
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001168 return SendErrorResponse (5);
1169 response.PutCString ("md5:");
1170 response.PutCStringAsRawHex8(md5_hash.c_str());
1171 }
1172 else{
1173 response.PutCString ("uuid:");
1174 response.PutCStringAsRawHex8(uuid_str.c_str());
1175 }
1176 response.PutChar(';');
1177
1178 const auto &module_arch = matched_module_spec.GetArchitecture();
1179 response.PutCString("triple:");
1180 response.PutCStringAsRawHex8( module_arch.GetTriple().getTriple().c_str());
1181 response.PutChar(';');
1182
Tamas Berghammerdad4db72015-03-13 11:16:08 +00001183 response.PutCString("file_path:");
1184 response.PutCStringAsRawHex8(module_path_spec.GetPath().c_str());
1185 response.PutChar(';');
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00001186 response.PutCString("file_offset:");
1187 response.PutHex64(file_offset);
1188 response.PutChar(';');
1189 response.PutCString("file_size:");
1190 response.PutHex64(file_size);
1191 response.PutChar(';');
1192
1193 return SendPacketNoLock(response.GetData(), response.GetSize());
1194}
1195
Tamas Berghammere13c2732015-02-11 10:29:30 +00001196void
1197GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info,
1198 StreamString &response)
1199{
1200 response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1201 proc_info.GetProcessID(),
1202 proc_info.GetParentProcessID(),
1203 proc_info.GetUserID(),
1204 proc_info.GetGroupID(),
1205 proc_info.GetEffectiveUserID(),
1206 proc_info.GetEffectiveGroupID());
1207 response.PutCString ("name:");
Oleksiy Vyalovbac75692015-03-11 18:13:37 +00001208 response.PutCStringAsRawHex8(proc_info.GetExecutableFile().GetPath().c_str());
Tamas Berghammere13c2732015-02-11 10:29:30 +00001209 response.PutChar(';');
1210 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1211 if (proc_arch.IsValid())
1212 {
1213 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1214 response.PutCString("triple:");
1215 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1216 response.PutChar(';');
1217 }
1218}
1219
1220void
1221GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse_DebugServerStyle (
1222 const ProcessInstanceInfo &proc_info, StreamString &response)
1223{
1224 response.Printf ("pid:%" PRIx64 ";parent-pid:%" PRIx64 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1225 proc_info.GetProcessID(),
1226 proc_info.GetParentProcessID(),
1227 proc_info.GetUserID(),
1228 proc_info.GetGroupID(),
1229 proc_info.GetEffectiveUserID(),
1230 proc_info.GetEffectiveGroupID());
1231
1232 const ArchSpec &proc_arch = proc_info.GetArchitecture();
1233 if (proc_arch.IsValid())
1234 {
1235 const llvm::Triple &proc_triple = proc_arch.GetTriple();
1236#if defined(__APPLE__)
1237 // We'll send cputype/cpusubtype.
1238 const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1239 if (cpu_type != 0)
1240 response.Printf ("cputype:%" PRIx32 ";", cpu_type);
1241
1242 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1243 if (cpu_subtype != 0)
1244 response.Printf ("cpusubtype:%" PRIx32 ";", cpu_subtype);
1245
1246 const std::string vendor = proc_triple.getVendorName ();
1247 if (!vendor.empty ())
1248 response.Printf ("vendor:%s;", vendor.c_str ());
1249#else
1250 // We'll send the triple.
1251 response.PutCString("triple:");
1252 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1253 response.PutChar(';');
1254#endif
1255 std::string ostype = proc_triple.getOSName ();
1256 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1257 if (proc_triple.getVendor () == llvm::Triple::Apple)
1258 {
1259 switch (proc_triple.getArch ())
1260 {
1261 case llvm::Triple::arm:
1262 case llvm::Triple::aarch64:
1263 ostype = "ios";
1264 break;
1265 default:
1266 // No change.
1267 break;
1268 }
1269 }
1270 response.Printf ("ostype:%s;", ostype.c_str ());
1271
1272
1273 switch (proc_arch.GetByteOrder ())
1274 {
1275 case lldb::eByteOrderLittle: response.PutCString ("endian:little;"); break;
1276 case lldb::eByteOrderBig: response.PutCString ("endian:big;"); break;
1277 case lldb::eByteOrderPDP: response.PutCString ("endian:pdp;"); break;
1278 default:
1279 // Nothing.
1280 break;
1281 }
1282
1283 if (proc_triple.isArch64Bit ())
1284 response.PutCString ("ptrsize:8;");
1285 else if (proc_triple.isArch32Bit ())
1286 response.PutCString ("ptrsize:4;");
1287 else if (proc_triple.isArch16Bit ())
1288 response.PutCString ("ptrsize:2;");
1289 }
1290}
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001291
1292FileSpec
1293GDBRemoteCommunicationServerCommon::FindModuleFile(const std::string& module_path,
1294 const ArchSpec& arch)
1295{
1296#ifdef __ANDROID__
1297 return HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1298#else
1299 return FileSpec(module_path.c_str(), true);
1300#endif
1301}