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