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