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