blob: 1b8791c6e74291eca6017414c425ccc94441ab55 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ProcessGDBRemote.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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000011#include "lldb/Host/Config.h"
Daniel Malea93a64302012-12-05 00:20:57 +000012
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013// C Includes
14#include <errno.h>
Stephen Wilsona78867b2011-03-25 18:16:28 +000015#include <stdlib.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000016#ifndef LLDB_DISABLE_POSIX
17#include <spawn.h>
Sean Callanan224f6f52012-07-19 18:07:36 +000018#include <netinet/in.h>
Greg Clayton2a48f522011-05-14 01:50:35 +000019#include <sys/mman.h> // for mmap
Virgile Bellob2f1fb22013-08-23 12:44:05 +000020#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include <sys/stat.h>
Greg Clayton2a48f522011-05-14 01:50:35 +000022#include <sys/types.h>
Stephen Wilsondc916862011-03-30 00:12:40 +000023#include <time.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25// C++ Includes
26#include <algorithm>
27#include <map>
28
29// Other libraries and framework includes
30
Johnny Chen01a67862011-10-14 00:42:25 +000031#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000032#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/ArchSpec.h"
34#include "lldb/Core/Debugger.h"
35#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton53239f02011-02-08 05:05:52 +000036#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Core/InputReader.h"
38#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000039#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/PluginManager.h"
41#include "lldb/Core/State.h"
Greg Claytond451c1a2012-04-13 21:24:18 +000042#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Core/StreamString.h"
44#include "lldb/Core/Timer.h"
Greg Clayton70b57652011-05-15 01:25:55 +000045#include "lldb/Core/Value.h"
Jason Molendad1fae142012-09-29 08:03:33 +000046#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Host/TimeValue.h"
Greg Clayton02686b82012-10-15 22:42:16 +000048#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000049#include "lldb/Interpreter/CommandObject.h"
50#include "lldb/Interpreter/CommandObjectMultiword.h"
Greg Clayton02686b82012-10-15 22:42:16 +000051#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytonef8180a2013-10-15 00:14:28 +000052#include "lldb/Interpreter/PythonDataObjects.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Symbol/ObjectFile.h"
54#include "lldb/Target/DynamicLoader.h"
55#include "lldb/Target/Target.h"
56#include "lldb/Target/TargetList.h"
Greg Clayton2a48f522011-05-14 01:50:35 +000057#include "lldb/Target/ThreadPlanCallFunction.h"
Jason Molendaa34a0c62010-06-09 21:28:42 +000058#include "lldb/Utility/PseudoTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
60// Project includes
61#include "lldb/Host/Host.h"
Peter Collingbourne99f9aa02011-06-03 20:40:38 +000062#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Jason Molendac42d2432012-07-25 03:40:06 +000063#include "Plugins/Process/Utility/StopInfoMachException.h"
Jim Ingham43c555d2012-07-04 00:35:43 +000064#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
Greg Claytonc982c762010-07-09 20:39:50 +000065#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066#include "GDBRemoteRegisterContext.h"
67#include "ProcessGDBRemote.h"
68#include "ProcessGDBRemoteLog.h"
69#include "ThreadGDBRemote.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000070
Jason Molenda5e8534e2012-10-03 01:29:34 +000071
Greg Claytonc1422c12012-04-09 22:46:21 +000072namespace lldb
73{
74 // Provide a function that can easily dump the packet history if we know a
75 // ProcessGDBRemote * value (which we can get from logs or from debugging).
76 // We need the function in the lldb namespace so it makes it into the final
77 // executable since the LLDB shared library only exports stuff in the lldb
78 // namespace. This allows you to attach with a debugger and call this
79 // function and get the packet history dumped to a file.
80 void
81 DumpProcessGDBRemotePacketHistory (void *p, const char *path)
82 {
Greg Claytond451c1a2012-04-13 21:24:18 +000083 lldb_private::StreamFile strm;
84 lldb_private::Error error (strm.GetFile().Open(path, lldb_private::File::eOpenOptionWrite | lldb_private::File::eOpenOptionCanCreate));
85 if (error.Success())
86 ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +000087 }
Filipe Cabecinhasc34f7762012-05-23 16:27:09 +000088}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090#define DEBUGSERVER_BASENAME "debugserver"
91using namespace lldb;
92using namespace lldb_private;
93
Greg Clayton7f982402013-07-15 22:54:20 +000094
95namespace {
96
97 static PropertyDefinition
98 g_properties[] =
99 {
100 { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." },
Greg Claytonef8180a2013-10-15 00:14:28 +0000101 { "target-definition-file" , OptionValue::eTypeFileSpec , true, 0 , NULL, NULL, "The file that provides the description for remote target registers." },
Greg Clayton7f982402013-07-15 22:54:20 +0000102 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
103 };
104
105 enum
106 {
Greg Claytonef8180a2013-10-15 00:14:28 +0000107 ePropertyPacketTimeout,
108 ePropertyTargetDefinitionFile
Greg Clayton7f982402013-07-15 22:54:20 +0000109 };
110
111 class PluginProperties : public Properties
112 {
113 public:
114
115 static ConstString
116 GetSettingName ()
117 {
118 return ProcessGDBRemote::GetPluginNameStatic();
119 }
120
121 PluginProperties() :
122 Properties ()
123 {
124 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
125 m_collection_sp->Initialize(g_properties);
126 }
127
128 virtual
129 ~PluginProperties()
130 {
131 }
132
133 uint64_t
134 GetPacketTimeout()
135 {
136 const uint32_t idx = ePropertyPacketTimeout;
137 return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
138 }
Greg Claytonef8180a2013-10-15 00:14:28 +0000139 FileSpec
140 GetTargetDefinitionFile () const
141 {
142 const uint32_t idx = ePropertyTargetDefinitionFile;
143 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
144 }
Greg Clayton7f982402013-07-15 22:54:20 +0000145 };
146
147 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
148
149 static const ProcessKDPPropertiesSP &
150 GetGlobalPluginProperties()
151 {
152 static ProcessKDPPropertiesSP g_settings_sp;
153 if (!g_settings_sp)
154 g_settings_sp.reset (new PluginProperties ());
155 return g_settings_sp;
156 }
157
158} // anonymous namespace end
159
Jim Ingham7572fa72011-03-29 21:45:47 +0000160static bool rand_initialized = false;
161
Sean Callanan224f6f52012-07-19 18:07:36 +0000162// TODO Randomly assigning a port is unsafe. We should get an unused
163// ephemeral port from the kernel and make sure we reserve it before passing
164// it to debugserver.
165
166#if defined (__APPLE__)
167#define LOW_PORT (IPPORT_RESERVED)
168#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
169#else
170#define LOW_PORT (1024u)
171#define HIGH_PORT (49151u)
172#endif
173
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174static inline uint16_t
175get_random_port ()
176{
Jim Ingham7572fa72011-03-29 21:45:47 +0000177 if (!rand_initialized)
178 {
Stephen Wilsondc916862011-03-30 00:12:40 +0000179 time_t seed = time(NULL);
180
Jim Ingham7572fa72011-03-29 21:45:47 +0000181 rand_initialized = true;
Stephen Wilsondc916862011-03-30 00:12:40 +0000182 srand(seed);
Jim Ingham7572fa72011-03-29 21:45:47 +0000183 }
Sean Callanan224f6f52012-07-19 18:07:36 +0000184 return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
187
Greg Clayton57abc5d2013-05-10 21:47:16 +0000188lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189ProcessGDBRemote::GetPluginNameStatic()
190{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000191 static ConstString g_name("gdb-remote");
192 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
195const char *
196ProcessGDBRemote::GetPluginDescriptionStatic()
197{
198 return "GDB Remote protocol based debugging plug-in.";
199}
200
201void
202ProcessGDBRemote::Terminate()
203{
204 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
205}
206
207
Greg Claytonc3776bf2012-02-09 06:16:32 +0000208lldb::ProcessSP
209ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210{
Greg Claytonc3776bf2012-02-09 06:16:32 +0000211 lldb::ProcessSP process_sp;
212 if (crash_file_path == NULL)
213 process_sp.reset (new ProcessGDBRemote (target, listener));
214 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
217bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000218ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219{
Greg Clayton596ed242011-10-21 21:41:45 +0000220 if (plugin_specified_by_name)
221 return true;
222
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +0000224 Module *exe_module = target.GetExecutableModulePointer();
225 if (exe_module)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000226 {
227 ObjectFile *exe_objfile = exe_module->GetObjectFile();
228 // We can't debug core files...
229 switch (exe_objfile->GetType())
230 {
231 case ObjectFile::eTypeInvalid:
232 case ObjectFile::eTypeCoreFile:
233 case ObjectFile::eTypeDebugInfo:
234 case ObjectFile::eTypeObjectFile:
235 case ObjectFile::eTypeSharedLibrary:
236 case ObjectFile::eTypeStubLibrary:
237 return false;
238 case ObjectFile::eTypeExecutable:
239 case ObjectFile::eTypeDynamicLinker:
240 case ObjectFile::eTypeUnknown:
241 break;
242 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000243 return exe_module->GetFileSpec().Exists();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000244 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000245 // However, if there is no executable module, we return true since we might be preparing to attach.
246 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
249//----------------------------------------------------------------------
250// ProcessGDBRemote constructor
251//----------------------------------------------------------------------
252ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
253 Process (target, listener),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 m_flags (0),
Greg Clayton8b82f082011-04-12 05:54:46 +0000255 m_gdb_comm(false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Claytonc982c762010-07-09 20:39:50 +0000257 m_last_stop_packet (),
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000258 m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259 m_register_info (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000260 m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham455fa5c2012-11-01 01:15:33 +0000262 m_async_thread_state(eAsyncThreadNotStarted),
263 m_async_thread_state_mutex(Mutex::eMutexTypeRecursive),
Greg Clayton9e920902012-04-10 02:25:43 +0000264 m_thread_ids (),
Greg Clayton71fc2a32011-02-12 06:28:37 +0000265 m_continue_c_tids (),
266 m_continue_C_tids (),
267 m_continue_s_tids (),
268 m_continue_S_tids (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS),
Greg Claytonc982c762010-07-09 20:39:50 +0000270 m_max_memory_size (512),
Greg Clayton4116e932012-05-15 02:33:01 +0000271 m_addr_to_mmap_size (),
272 m_thread_create_bp_sp (),
Jim Ingham43c555d2012-07-04 00:35:43 +0000273 m_waiting_for_attach (false),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000274 m_destroy_tried_resuming (false),
Greg Clayton998255b2012-10-13 02:07:45 +0000275 m_command_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276{
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000277 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
278 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Jim Inghamb1e2e842012-04-12 18:49:31 +0000279 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit");
Greg Clayton7f982402013-07-15 22:54:20 +0000280 const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
281 if (timeout_seconds > 0)
282 m_gdb_comm.SetPacketTimeout(timeout_seconds);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283}
284
285//----------------------------------------------------------------------
286// Destructor
287//----------------------------------------------------------------------
288ProcessGDBRemote::~ProcessGDBRemote()
289{
290 // m_mach_process.UnregisterNotificationCallbacks (this);
291 Clear();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000292 // We need to call finalize on the process before destroying ourselves
293 // to make sure all of the broadcaster cleanup goes as planned. If we
294 // destruct this class, then Process::~Process() might have problems
295 // trying to fully destroy the broadcaster.
296 Finalize();
Jim Ingham455fa5c2012-11-01 01:15:33 +0000297
298 // The general Finalize is going to try to destroy the process and that SHOULD
299 // shut down the async thread. However, if we don't kill it it will get stranded and
300 // its connection will go away so when it wakes up it will crash. So kill it for sure here.
301 StopAsyncThread();
302 KillDebugserverProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
305//----------------------------------------------------------------------
306// PluginInterface
307//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000308ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309ProcessGDBRemote::GetPluginName()
310{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 return GetPluginNameStatic();
312}
313
314uint32_t
315ProcessGDBRemote::GetPluginVersion()
316{
317 return 1;
318}
319
Greg Claytonef8180a2013-10-15 00:14:28 +0000320bool
321ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec)
322{
323 ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
324 Error error;
325 lldb::ScriptInterpreterObjectSP module_object_sp (interpreter->LoadPluginModule(target_definition_fspec, error));
326 if (module_object_sp)
327 {
328 lldb::ScriptInterpreterObjectSP target_definition_sp (interpreter->GetDynamicSettings(module_object_sp,
329 &GetTarget(),
330 "gdb-server-target-definition",
331 error));
332
333 PythonDictionary target_dict(target_definition_sp);
334
335 if (target_dict)
336 {
Greg Clayton312bcbe2013-10-17 01:10:23 +0000337 PythonDictionary host_info_dict (target_dict.GetItemForKey("host-info"));
338 if (host_info_dict)
339 {
340 ArchSpec host_arch (host_info_dict.GetItemForKeyAsString(PythonString("triple")));
341
342 if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture()))
343 {
344 GetTarget().SetArchitecture(host_arch);
345 }
346
347 }
348 if (m_register_info.SetRegisterInfo (target_dict, GetTarget().GetArchitecture().GetByteOrder()) > 0)
Greg Claytonef8180a2013-10-15 00:14:28 +0000349 {
350 return true;
351 }
352 }
353 }
354 return false;
355}
356
357
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358void
Greg Clayton513c26c2011-01-29 07:10:55 +0000359ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360{
Greg Clayton513c26c2011-01-29 07:10:55 +0000361 if (!force && m_register_info.GetNumRegisters() > 0)
362 return;
363
364 char packet[128];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 m_register_info.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 uint32_t reg_offset = 0;
367 uint32_t reg_num = 0;
Greg Clayton23f59502012-07-17 03:23:13 +0000368 for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse;
Greg Clayton576d8832011-03-22 04:00:09 +0000369 response_type == StringExtractorGDBRemote::eResponse;
370 ++reg_num)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 {
Greg Clayton513c26c2011-01-29 07:10:55 +0000372 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
Andy Gibbsa297a972013-06-19 19:04:53 +0000373 assert (packet_len < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +0000375 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 {
Greg Clayton576d8832011-03-22 04:00:09 +0000377 response_type = response.GetResponseType();
378 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 {
380 std::string name;
381 std::string value;
382 ConstString reg_name;
383 ConstString alt_name;
384 ConstString set_name;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000385 std::vector<uint32_t> value_regs;
386 std::vector<uint32_t> invalidate_regs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 RegisterInfo reg_info = { NULL, // Name
388 NULL, // Alt name
389 0, // byte size
390 reg_offset, // offset
391 eEncodingUint, // encoding
392 eFormatHex, // formate
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 {
394 LLDB_INVALID_REGNUM, // GCC reg num
395 LLDB_INVALID_REGNUM, // DWARF reg num
396 LLDB_INVALID_REGNUM, // generic reg num
Jason Molendafbcb7f22010-09-10 07:49:16 +0000397 reg_num, // GDB reg num
398 reg_num // native register number
Greg Clayton435d85a2012-02-29 19:27:27 +0000399 },
400 NULL,
401 NULL
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 };
403
404 while (response.GetNameColonValue(name, value))
405 {
406 if (name.compare("name") == 0)
407 {
408 reg_name.SetCString(value.c_str());
409 }
410 else if (name.compare("alt-name") == 0)
411 {
412 alt_name.SetCString(value.c_str());
413 }
414 else if (name.compare("bitsize") == 0)
415 {
416 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
417 }
418 else if (name.compare("offset") == 0)
419 {
420 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda743e86a2010-06-11 23:44:18 +0000421 if (reg_offset != offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 {
423 reg_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 }
425 }
426 else if (name.compare("encoding") == 0)
427 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000428 const Encoding encoding = Args::StringToEncoding (value.c_str());
429 if (encoding != eEncodingInvalid)
430 reg_info.encoding = encoding;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 }
432 else if (name.compare("format") == 0)
433 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000434 Format format = eFormatInvalid;
435 if (Args::StringToFormat (value.c_str(), format, NULL).Success())
436 reg_info.format = format;
437 else if (value.compare("binary") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 reg_info.format = eFormatBinary;
439 else if (value.compare("decimal") == 0)
440 reg_info.format = eFormatDecimal;
441 else if (value.compare("hex") == 0)
442 reg_info.format = eFormatHex;
443 else if (value.compare("float") == 0)
444 reg_info.format = eFormatFloat;
445 else if (value.compare("vector-sint8") == 0)
446 reg_info.format = eFormatVectorOfSInt8;
447 else if (value.compare("vector-uint8") == 0)
448 reg_info.format = eFormatVectorOfUInt8;
449 else if (value.compare("vector-sint16") == 0)
450 reg_info.format = eFormatVectorOfSInt16;
451 else if (value.compare("vector-uint16") == 0)
452 reg_info.format = eFormatVectorOfUInt16;
453 else if (value.compare("vector-sint32") == 0)
454 reg_info.format = eFormatVectorOfSInt32;
455 else if (value.compare("vector-uint32") == 0)
456 reg_info.format = eFormatVectorOfUInt32;
457 else if (value.compare("vector-float32") == 0)
458 reg_info.format = eFormatVectorOfFloat32;
459 else if (value.compare("vector-uint128") == 0)
460 reg_info.format = eFormatVectorOfUInt128;
461 }
462 else if (name.compare("set") == 0)
463 {
464 set_name.SetCString(value.c_str());
465 }
466 else if (name.compare("gcc") == 0)
467 {
468 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
469 }
470 else if (name.compare("dwarf") == 0)
471 {
472 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
473 }
474 else if (name.compare("generic") == 0)
475 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000476 reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 }
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000478 else if (name.compare("container-regs") == 0)
479 {
480 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
481 value_pair.second = value;
482 do
483 {
484 value_pair = value_pair.second.split(',');
485 if (!value_pair.first.empty())
486 {
Greg Clayton0ba20242013-01-21 23:32:42 +0000487 uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
488 if (reg != LLDB_INVALID_REGNUM)
489 value_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000490 }
491 } while (!value_pair.second.empty());
492 }
493 else if (name.compare("invalidate-regs") == 0)
494 {
495 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
496 value_pair.second = value;
497 do
498 {
499 value_pair = value_pair.second.split(',');
500 if (!value_pair.first.empty())
501 {
Greg Clayton0ba20242013-01-21 23:32:42 +0000502 uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
503 if (reg != LLDB_INVALID_REGNUM)
504 invalidate_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000505 }
506 } while (!value_pair.second.empty());
507 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 }
509
Jason Molenda743e86a2010-06-11 23:44:18 +0000510 reg_info.byte_offset = reg_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 assert (reg_info.byte_size != 0);
512 reg_offset += reg_info.byte_size;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000513 if (!value_regs.empty())
514 {
515 value_regs.push_back(LLDB_INVALID_REGNUM);
516 reg_info.value_regs = value_regs.data();
517 }
518 if (!invalidate_regs.empty())
519 {
520 invalidate_regs.push_back(LLDB_INVALID_REGNUM);
521 reg_info.invalidate_regs = invalidate_regs.data();
522 }
523
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
525 }
526 }
527 else
528 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000529 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
531 }
532
Greg Claytonef8180a2013-10-15 00:14:28 +0000533 if (reg_num == 0)
534 {
535 FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile ();
Greg Clayton312bcbe2013-10-17 01:10:23 +0000536
537 if (target_definition_fspec)
Greg Claytonef8180a2013-10-15 00:14:28 +0000538 {
Greg Clayton312bcbe2013-10-17 01:10:23 +0000539 // See if we can get register definitions from a python file
540 if (ParsePythonTargetDefinition (target_definition_fspec))
541 return;
Greg Claytonef8180a2013-10-15 00:14:28 +0000542 }
543 }
544
Johnny Chen2fa9de12012-05-14 18:44:23 +0000545 // We didn't get anything if the accumulated reg_num is zero. See if we are
546 // debugging ARM and fill with a hard coded register set until we can get an
547 // updated debugserver down on the devices.
548 // On the other hand, if the accumulated reg_num is positive, see if we can
549 // add composite registers to the existing primordial ones.
550 bool from_scratch = (reg_num == 0);
551
552 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000553 const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture();
554 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
555
556 // Use the process' architecture instead of the host arch, if available
557 ArchSpec remote_arch;
558 if (remote_process_arch.IsValid ())
559 remote_arch = remote_process_arch;
560 else
561 remote_arch = remote_host_arch;
562
Johnny Chen2fa9de12012-05-14 18:44:23 +0000563 if (!target_arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 {
Johnny Chen2fa9de12012-05-14 18:44:23 +0000565 if (remote_arch.IsValid()
566 && remote_arch.GetMachine() == llvm::Triple::arm
567 && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
568 m_register_info.HardcodeARMRegisters(from_scratch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 }
Johnny Chen2fa9de12012-05-14 18:44:23 +0000570 else if (target_arch.GetMachine() == llvm::Triple::arm)
571 {
572 m_register_info.HardcodeARMRegisters(from_scratch);
573 }
574
575 // At this point, we can finalize our register info.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 m_register_info.Finalize ();
577}
578
579Error
580ProcessGDBRemote::WillLaunch (Module* module)
581{
582 return WillLaunchOrAttach ();
583}
584
585Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000586ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587{
588 return WillLaunchOrAttach ();
589}
590
591Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000592ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593{
594 return WillLaunchOrAttach ();
595}
596
597Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000598ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +0000599{
600 Error error (WillLaunchOrAttach ());
601
602 if (error.Fail())
603 return error;
604
Greg Clayton2289fa42011-04-30 01:09:13 +0000605 error = ConnectToDebugserver (remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000606
607 if (error.Fail())
608 return error;
609 StartAsyncThread ();
610
Greg Claytonc574ede2011-03-10 02:26:48 +0000611 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonb766a732011-02-04 01:58:07 +0000612 if (pid == LLDB_INVALID_PROCESS_ID)
613 {
614 // We don't have a valid process ID, so note that we are connected
615 // and could now request to launch or attach, or get remote process
616 // listings...
617 SetPrivateState (eStateConnected);
618 }
619 else
620 {
621 // We have a valid process
622 SetID (pid);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000623 GetThreadList();
Greg Claytondd0e5a52011-06-02 22:22:38 +0000624 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytonb766a732011-02-04 01:58:07 +0000625 {
Carlo Kok74389122013-10-14 07:09:13 +0000626 if (!m_target.GetArchitecture().IsValid()) { // Make sure we have an architecture
627 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
628 }
629
630
Greg Claytondd0e5a52011-06-02 22:22:38 +0000631 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytonb766a732011-02-04 01:58:07 +0000632 if (state == eStateStopped)
633 {
634 SetPrivateState (state);
635 }
636 else
Daniel Malead01b2952012-11-29 21:49:15 +0000637 error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state));
Greg Claytonb766a732011-02-04 01:58:07 +0000638 }
639 else
Daniel Malead01b2952012-11-29 21:49:15 +0000640 error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000641 }
Jason Molenda16d127c2012-05-03 22:37:30 +0000642
643 if (error.Success()
644 && !GetTarget().GetArchitecture().IsValid()
645 && m_gdb_comm.GetHostArchitecture().IsValid())
646 {
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000647 // Prefer the *process'* architecture over that of the *host*, if available.
648 if (m_gdb_comm.GetProcessArchitecture().IsValid())
649 GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture());
650 else
651 GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture());
Jason Molenda16d127c2012-05-03 22:37:30 +0000652 }
653
Greg Claytonb766a732011-02-04 01:58:07 +0000654 return error;
655}
656
657Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658ProcessGDBRemote::WillLaunchOrAttach ()
659{
660 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661 m_stdio_communication.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 return error;
663}
664
665//----------------------------------------------------------------------
666// Process Control
667//----------------------------------------------------------------------
668Error
Greg Clayton982c9762011-11-03 21:22:33 +0000669ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670{
Greg Clayton4957bf62010-09-30 21:49:03 +0000671 Error error;
Greg Clayton982c9762011-11-03 21:22:33 +0000672
673 uint32_t launch_flags = launch_info.GetFlags().Get();
674 const char *stdin_path = NULL;
675 const char *stdout_path = NULL;
676 const char *stderr_path = NULL;
677 const char *working_dir = launch_info.GetWorkingDirectory();
678
679 const ProcessLaunchInfo::FileAction *file_action;
680 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
681 if (file_action)
682 {
683 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
684 stdin_path = file_action->GetPath();
685 }
686 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
687 if (file_action)
688 {
689 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
690 stdout_path = file_action->GetPath();
691 }
692 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
693 if (file_action)
694 {
695 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
696 stderr_path = file_action->GetPath();
697 }
698
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
700 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
701 // ::LogSetLogFile ("/dev/stdout");
Greg Clayton5160ce52013-03-27 23:08:40 +0000702 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703
Greg Clayton982c9762011-11-03 21:22:33 +0000704 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 if (object_file)
706 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 char host_port[128];
708 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytonb766a732011-02-04 01:58:07 +0000709 char connect_url[128];
710 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711
Greg Clayton71337622011-02-24 22:24:29 +0000712 // Make sure we aren't already connected?
713 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 {
Han Ming Ong84647042012-02-25 01:07:38 +0000715 error = StartDebugserverProcess (host_port, launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 if (error.Fail())
Greg Claytonc235ac72011-08-09 05:20:29 +0000717 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000718 if (log)
719 log->Printf("failed to start debugserver process: %s", error.AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 return error;
Greg Claytonc235ac72011-08-09 05:20:29 +0000721 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722
Greg Claytonb766a732011-02-04 01:58:07 +0000723 error = ConnectToDebugserver (connect_url);
Greg Clayton71337622011-02-24 22:24:29 +0000724 }
725
726 if (error.Success())
727 {
728 lldb_utility::PseudoTerminal pty;
729 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000730
731 // If the debugserver is local and we aren't disabling STDIO, lets use
732 // a pseudo terminal to instead of relying on the 'O' packets for stdio
733 // since 'O' packets can really slow down debugging if the inferior
734 // does a lot of output.
Greg Claytonf58c2692011-06-24 22:32:10 +0000735 PlatformSP platform_sp (m_target.GetPlatform());
736 if (platform_sp && platform_sp->IsHost() && !disable_stdio)
Greg Clayton71337622011-02-24 22:24:29 +0000737 {
738 const char *slave_name = NULL;
739 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 {
Greg Clayton71337622011-02-24 22:24:29 +0000741 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
742 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 }
Greg Clayton71337622011-02-24 22:24:29 +0000744 if (stdin_path == NULL)
745 stdin_path = slave_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746
Greg Clayton71337622011-02-24 22:24:29 +0000747 if (stdout_path == NULL)
748 stdout_path = slave_name;
749
750 if (stderr_path == NULL)
751 stderr_path = slave_name;
752 }
753
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000754 // Set STDIN to /dev/null if we want STDIO disabled or if either
755 // STDOUT or STDERR have been set to something and STDIN hasn't
756 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000757 stdin_path = "/dev/null";
758
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000759 // Set STDOUT to /dev/null if we want STDIO disabled or if either
760 // STDIN or STDERR have been set to something and STDOUT hasn't
761 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000762 stdout_path = "/dev/null";
763
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000764 // Set STDERR to /dev/null if we want STDIO disabled or if either
765 // STDIN or STDOUT have been set to something and STDERR hasn't
766 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000767 stderr_path = "/dev/null";
768
769 if (stdin_path)
770 m_gdb_comm.SetSTDIN (stdin_path);
771 if (stdout_path)
772 m_gdb_comm.SetSTDOUT (stdout_path);
773 if (stderr_path)
774 m_gdb_comm.SetSTDERR (stderr_path);
775
776 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
777
Greg Claytonc4103b32011-05-08 04:53:50 +0000778 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Clayton71337622011-02-24 22:24:29 +0000779
780 if (working_dir && working_dir[0])
781 {
782 m_gdb_comm.SetWorkingDir (working_dir);
783 }
784
785 // Send the environment and the program + arguments after we connect
Greg Clayton982c9762011-11-03 21:22:33 +0000786 const Args &environment = launch_info.GetEnvironmentEntries();
787 if (environment.GetArgumentCount())
Greg Clayton71337622011-02-24 22:24:29 +0000788 {
Greg Clayton982c9762011-11-03 21:22:33 +0000789 size_t num_environment_entries = environment.GetArgumentCount();
790 for (size_t i=0; i<num_environment_entries; ++i)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000791 {
Greg Clayton982c9762011-11-03 21:22:33 +0000792 const char *env_entry = environment.GetArgumentAtIndex(i);
793 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Clayton71337622011-02-24 22:24:29 +0000794 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000795 }
Greg Clayton71337622011-02-24 22:24:29 +0000796 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000797
Greg Claytonc574ede2011-03-10 02:26:48 +0000798 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10);
Greg Clayton982c9762011-11-03 21:22:33 +0000799 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector());
Greg Clayton71337622011-02-24 22:24:29 +0000800 if (arg_packet_err == 0)
801 {
802 std::string error_str;
Greg Claytonc574ede2011-03-10 02:26:48 +0000803 if (m_gdb_comm.GetLaunchSuccess (error_str))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 {
Greg Claytonc574ede2011-03-10 02:26:48 +0000805 SetID (m_gdb_comm.GetCurrentProcessID ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 }
807 else
808 {
Greg Clayton71337622011-02-24 22:24:29 +0000809 error.SetErrorString (error_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 }
Greg Clayton71337622011-02-24 22:24:29 +0000811 }
812 else
813 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000814 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Greg Clayton71337622011-02-24 22:24:29 +0000815 }
Greg Clayton8b45bee2011-08-10 22:05:39 +0000816
817 m_gdb_comm.SetPacketTimeout (old_packet_timeout);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818
Greg Clayton71337622011-02-24 22:24:29 +0000819 if (GetID() == LLDB_INVALID_PROCESS_ID)
820 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000821 if (log)
822 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton71337622011-02-24 22:24:29 +0000823 KillDebugserverProcess ();
824 return error;
825 }
826
Greg Claytondd0e5a52011-06-02 22:22:38 +0000827 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Clayton71337622011-02-24 22:24:29 +0000828 {
Carlo Kok74389122013-10-14 07:09:13 +0000829 if (!m_target.GetArchitecture().IsValid()) { // Make sure we have an architecture
830 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
831 }
832
Greg Claytondd0e5a52011-06-02 22:22:38 +0000833 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Clayton71337622011-02-24 22:24:29 +0000834
835 if (!disable_stdio)
836 {
837 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Claytonee95ed52011-11-17 22:14:31 +0000838 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Clayton71337622011-02-24 22:24:29 +0000839 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840 }
841 }
Greg Claytonc235ac72011-08-09 05:20:29 +0000842 else
843 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000844 if (log)
845 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytonc235ac72011-08-09 05:20:29 +0000846 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 }
848 else
849 {
850 // Set our user ID to an invalid process ID.
851 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton982c9762011-11-03 21:22:33 +0000852 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
853 exe_module->GetFileSpec().GetFilename().AsCString(),
854 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 return error;
Greg Clayton4957bf62010-09-30 21:49:03 +0000857
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858}
859
860
861Error
Greg Claytonb766a732011-02-04 01:58:07 +0000862ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863{
864 Error error;
865 // Sleep and wait a bit for debugserver to start to listen...
Greg Clayton7b0992d2013-04-18 22:45:39 +0000866 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 if (conn_ap.get())
868 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 const uint32_t max_retry_count = 50;
870 uint32_t retry_count = 0;
871 while (!m_gdb_comm.IsConnected())
872 {
Greg Claytonb766a732011-02-04 01:58:07 +0000873 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 {
875 m_gdb_comm.SetConnection (conn_ap.release());
876 break;
877 }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000878 else if (error.WasInterrupted())
879 {
880 // If we were interrupted, don't keep retrying.
881 break;
882 }
883
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 retry_count++;
Jim Ingham4e5c8212013-06-07 22:09:53 +0000885
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 if (retry_count >= max_retry_count)
887 break;
888
889 usleep (100000);
890 }
891 }
892
893 if (!m_gdb_comm.IsConnected())
894 {
895 if (error.Success())
896 error.SetErrorString("not connected to remote gdb server");
897 return error;
898 }
899
Greg Clayton32e0a752011-03-30 18:16:51 +0000900 // We always seem to be able to open a connection to a local port
901 // so we need to make sure we can then send data to it. If we can't
902 // then we aren't actually connected to anything, so try and do the
903 // handshake with the remote GDB server and make sure that goes
904 // alright.
905 if (!m_gdb_comm.HandshakeWithServer (NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000907 m_gdb_comm.Disconnect();
908 if (error.Success())
909 error.SetErrorString("not connected to remote gdb server");
910 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000912 m_gdb_comm.ResetDiscoverableSettings();
913 m_gdb_comm.QueryNoAckModeSupported ();
914 m_gdb_comm.GetThreadSuffixSupported ();
Greg Clayton44633992012-04-10 03:22:03 +0000915 m_gdb_comm.GetListThreadsInStopReplySupported ();
Greg Clayton32e0a752011-03-30 18:16:51 +0000916 m_gdb_comm.GetHostInfo ();
917 m_gdb_comm.GetVContSupported ('c');
Jim Inghamcd16df92012-07-20 21:37:13 +0000918 m_gdb_comm.GetVAttachOrWaitSupported();
Jim Ingham03afad82012-07-02 05:40:07 +0000919
920 size_t num_cmds = GetExtraStartupCommands().GetArgumentCount();
921 for (size_t idx = 0; idx < num_cmds; idx++)
922 {
923 StringExtractorGDBRemote response;
Jim Ingham03afad82012-07-02 05:40:07 +0000924 m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false);
925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 return error;
927}
928
929void
930ProcessGDBRemote::DidLaunchOrAttach ()
931{
Greg Clayton5160ce52013-03-27 23:08:40 +0000932 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +0000933 if (log)
934 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton93d3c8332011-02-16 04:46:07 +0000935 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 {
937 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
938
Greg Clayton513c26c2011-01-29 07:10:55 +0000939 BuildDynamicRegisterInfo (false);
Greg Clayton3af9ea52010-11-18 05:57:03 +0000940
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 // See if the GDB server supports the qHostInfo information
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000942
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000943 ArchSpec gdb_remote_arch = m_gdb_comm.GetHostArchitecture();
944
945 // See if the GDB server supports the qProcessInfo packet, if so
946 // prefer that over the Host information as it will be more specific
947 // to our process.
948
949 if (m_gdb_comm.GetProcessArchitecture().IsValid())
950 gdb_remote_arch = m_gdb_comm.GetProcessArchitecture();
951
Greg Claytond314e812011-03-23 00:09:55 +0000952 if (gdb_remote_arch.IsValid())
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000953 {
Greg Claytond314e812011-03-23 00:09:55 +0000954 ArchSpec &target_arch = GetTarget().GetArchitecture();
955
956 if (target_arch.IsValid())
957 {
958 // If the remote host is ARM and we have apple as the vendor, then
959 // ARM executables and shared libraries can have mixed ARM architectures.
960 // You can have an armv6 executable, and if the host is armv7, then the
961 // system will load the best possible architecture for all shared libraries
962 // it has, so we really need to take the remote host architecture as our
963 // defacto architecture in this case.
964
965 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
966 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
967 {
968 target_arch = gdb_remote_arch;
969 }
970 else
971 {
972 // Fill in what is missing in the triple
973 const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple();
974 llvm::Triple &target_triple = target_arch.GetTriple();
Greg Clayton70b57652011-05-15 01:25:55 +0000975 if (target_triple.getVendorName().size() == 0)
976 {
Greg Claytond314e812011-03-23 00:09:55 +0000977 target_triple.setVendor (remote_triple.getVendor());
978
Greg Clayton70b57652011-05-15 01:25:55 +0000979 if (target_triple.getOSName().size() == 0)
980 {
981 target_triple.setOS (remote_triple.getOS());
Greg Claytond314e812011-03-23 00:09:55 +0000982
Greg Clayton70b57652011-05-15 01:25:55 +0000983 if (target_triple.getEnvironmentName().size() == 0)
984 target_triple.setEnvironment (remote_triple.getEnvironment());
985 }
986 }
Greg Claytond314e812011-03-23 00:09:55 +0000987 }
988 }
989 else
990 {
991 // The target doesn't have a valid architecture yet, set it from
992 // the architecture we got from the remote GDB server
993 target_arch = gdb_remote_arch;
994 }
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 }
997}
998
999void
1000ProcessGDBRemote::DidLaunch ()
1001{
1002 DidLaunchOrAttach ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003}
1004
1005Error
Greg Claytonc982c762010-07-09 20:39:50 +00001006ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007{
Han Ming Ong84647042012-02-25 01:07:38 +00001008 ProcessAttachInfo attach_info;
1009 return DoAttachToProcessWithID(attach_pid, attach_info);
1010}
1011
1012Error
1013ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
1014{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 Error error;
1016 // Clear out and clean up from any current state
1017 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 if (attach_pid != LLDB_INVALID_PROCESS_ID)
1019 {
Greg Clayton71337622011-02-24 22:24:29 +00001020 // Make sure we aren't already connected?
1021 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 {
Greg Clayton71337622011-02-24 22:24:29 +00001023 char host_port[128];
1024 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
1025 char connect_url[128];
1026 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027
Han Ming Ong84647042012-02-25 01:07:38 +00001028 error = StartDebugserverProcess (host_port, attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001029
1030 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 {
Greg Clayton71337622011-02-24 22:24:29 +00001032 const char *error_string = error.AsCString();
1033 if (error_string == NULL)
1034 error_string = "unable to launch " DEBUGSERVER_BASENAME;
1035
1036 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 }
Greg Clayton71337622011-02-24 22:24:29 +00001038 else
1039 {
1040 error = ConnectToDebugserver (connect_url);
1041 }
1042 }
1043
1044 if (error.Success())
1045 {
1046 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001047 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid);
Greg Clayton3b608422011-11-19 02:11:30 +00001048 SetID (attach_pid);
Greg Clayton71337622011-02-24 22:24:29 +00001049 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 }
1051 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 return error;
1053}
1054
1055size_t
1056ProcessGDBRemote::AttachInputReaderCallback
1057(
1058 void *baton,
1059 InputReader *reader,
1060 lldb::InputReaderAction notification,
1061 const char *bytes,
1062 size_t bytes_len
1063)
1064{
1065 if (notification == eInputReaderGotToken)
1066 {
1067 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
1068 if (gdb_process->m_waiting_for_attach)
1069 gdb_process->m_waiting_for_attach = false;
1070 reader->SetIsDone(true);
1071 return 1;
1072 }
1073 return 0;
1074}
1075
1076Error
Han Ming Ong84647042012-02-25 01:07:38 +00001077ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078{
1079 Error error;
1080 // Clear out and clean up from any current state
1081 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 if (process_name && process_name[0])
1084 {
Greg Clayton71337622011-02-24 22:24:29 +00001085 // Make sure we aren't already connected?
1086 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087 {
Greg Clayton71337622011-02-24 22:24:29 +00001088 char host_port[128];
1089 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
1090 char connect_url[128];
1091 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
1092
Han Ming Ong84647042012-02-25 01:07:38 +00001093 error = StartDebugserverProcess (host_port, attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001094 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 {
Greg Clayton71337622011-02-24 22:24:29 +00001096 const char *error_string = error.AsCString();
1097 if (error_string == NULL)
1098 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099
Greg Clayton71337622011-02-24 22:24:29 +00001100 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 }
Greg Clayton71337622011-02-24 22:24:29 +00001102 else
1103 {
1104 error = ConnectToDebugserver (connect_url);
1105 }
1106 }
1107
1108 if (error.Success())
1109 {
1110 StreamString packet;
1111
1112 if (wait_for_launch)
Jim Inghamcd16df92012-07-20 21:37:13 +00001113 {
1114 if (!m_gdb_comm.GetVAttachOrWaitSupported())
1115 {
1116 packet.PutCString ("vAttachWait");
1117 }
1118 else
1119 {
1120 if (attach_info.GetIgnoreExisting())
1121 packet.PutCString("vAttachWait");
1122 else
1123 packet.PutCString ("vAttachOrWait");
1124 }
1125 }
Greg Clayton71337622011-02-24 22:24:29 +00001126 else
1127 packet.PutCString("vAttachName");
1128 packet.PutChar(';');
1129 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
1130
1131 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
1132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 }
1134 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135 return error;
1136}
1137
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138
1139void
1140ProcessGDBRemote::DidAttach ()
1141{
Greg Claytonb766a732011-02-04 01:58:07 +00001142 DidLaunchOrAttach ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143}
1144
Greg Clayton90ba8112012-12-05 00:16:59 +00001145
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146Error
1147ProcessGDBRemote::WillResume ()
1148{
Greg Clayton71fc2a32011-02-12 06:28:37 +00001149 m_continue_c_tids.clear();
1150 m_continue_C_tids.clear();
1151 m_continue_s_tids.clear();
1152 m_continue_S_tids.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 return Error();
1154}
1155
1156Error
1157ProcessGDBRemote::DoResume ()
1158{
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001159 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001160 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001161 if (log)
1162 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytone5219662010-12-03 06:02:24 +00001163
1164 Listener listener ("gdb-remote.resume-packet-sent");
1165 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
1166 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00001167 listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit);
1168
Greg Claytond1d06e42013-04-20 00:27:58 +00001169 const size_t num_threads = GetThreadList().GetSize();
1170
Greg Clayton71fc2a32011-02-12 06:28:37 +00001171 StreamString continue_packet;
1172 bool continue_packet_error = false;
1173 if (m_gdb_comm.HasAnyVContSupport ())
1174 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001175 if (m_continue_c_tids.size() == num_threads)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001176 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001177 // All threads are continuing, just send a "c" packet
1178 continue_packet.PutCString ("c");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001179 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001180 else
1181 {
1182 continue_packet.PutCString ("vCont");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001183
Greg Claytond1d06e42013-04-20 00:27:58 +00001184 if (!m_continue_c_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001185 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001186 if (m_gdb_comm.GetVContSupported ('c'))
1187 {
1188 for (tid_collection::const_iterator t_pos = m_continue_c_tids.begin(), t_end = m_continue_c_tids.end(); t_pos != t_end; ++t_pos)
1189 continue_packet.Printf(";c:%4.4" PRIx64, *t_pos);
1190 }
1191 else
1192 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001193 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001194
1195 if (!continue_packet_error && !m_continue_C_tids.empty())
1196 {
1197 if (m_gdb_comm.GetVContSupported ('C'))
1198 {
1199 for (tid_sig_collection::const_iterator s_pos = m_continue_C_tids.begin(), s_end = m_continue_C_tids.end(); s_pos != s_end; ++s_pos)
1200 continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1201 }
1202 else
1203 continue_packet_error = true;
1204 }
Greg Claytone5219662010-12-03 06:02:24 +00001205
Greg Claytond1d06e42013-04-20 00:27:58 +00001206 if (!continue_packet_error && !m_continue_s_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001207 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001208 if (m_gdb_comm.GetVContSupported ('s'))
1209 {
1210 for (tid_collection::const_iterator t_pos = m_continue_s_tids.begin(), t_end = m_continue_s_tids.end(); t_pos != t_end; ++t_pos)
1211 continue_packet.Printf(";s:%4.4" PRIx64, *t_pos);
1212 }
1213 else
1214 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001215 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001216
1217 if (!continue_packet_error && !m_continue_S_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001218 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001219 if (m_gdb_comm.GetVContSupported ('S'))
1220 {
1221 for (tid_sig_collection::const_iterator s_pos = m_continue_S_tids.begin(), s_end = m_continue_S_tids.end(); s_pos != s_end; ++s_pos)
1222 continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1223 }
1224 else
1225 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001226 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001227
1228 if (continue_packet_error)
1229 continue_packet.GetString().clear();
Greg Clayton71fc2a32011-02-12 06:28:37 +00001230 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001231 }
1232 else
1233 continue_packet_error = true;
1234
1235 if (continue_packet_error)
1236 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001237 // Either no vCont support, or we tried to use part of the vCont
1238 // packet that wasn't supported by the remote GDB server.
1239 // We need to try and make a simple packet that can do our continue
Greg Clayton71fc2a32011-02-12 06:28:37 +00001240 const size_t num_continue_c_tids = m_continue_c_tids.size();
1241 const size_t num_continue_C_tids = m_continue_C_tids.size();
1242 const size_t num_continue_s_tids = m_continue_s_tids.size();
1243 const size_t num_continue_S_tids = m_continue_S_tids.size();
1244 if (num_continue_c_tids > 0)
1245 {
1246 if (num_continue_c_tids == num_threads)
1247 {
1248 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001249 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001250 continue_packet.PutChar ('c');
1251 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001252 }
1253 else if (num_continue_c_tids == 1 &&
1254 num_continue_C_tids == 0 &&
1255 num_continue_s_tids == 0 &&
1256 num_continue_S_tids == 0 )
1257 {
1258 // Only one thread is continuing
Greg Clayton8b82f082011-04-12 05:54:46 +00001259 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001260 continue_packet.PutChar ('c');
Greg Clayton0c74e782011-06-24 03:21:43 +00001261 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001262 }
1263 }
1264
Greg Clayton0c74e782011-06-24 03:21:43 +00001265 if (continue_packet_error && num_continue_C_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001266 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001267 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1268 num_continue_C_tids > 0 &&
1269 num_continue_s_tids == 0 &&
1270 num_continue_S_tids == 0 )
Greg Clayton71fc2a32011-02-12 06:28:37 +00001271 {
1272 const int continue_signo = m_continue_C_tids.front().second;
Greg Clayton0c74e782011-06-24 03:21:43 +00001273 // Only one thread is continuing
Greg Clayton71fc2a32011-02-12 06:28:37 +00001274 if (num_continue_C_tids > 1)
1275 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001276 // More that one thread with a signal, yet we don't have
1277 // vCont support and we are being asked to resume each
1278 // thread with a signal, we need to make sure they are
1279 // all the same signal, or we can't issue the continue
1280 // accurately with the current support...
1281 if (num_continue_C_tids > 1)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001282 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001283 continue_packet_error = false;
1284 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1285 {
1286 if (m_continue_C_tids[i].second != continue_signo)
1287 continue_packet_error = true;
1288 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001289 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001290 if (!continue_packet_error)
1291 m_gdb_comm.SetCurrentThreadForRun (-1);
1292 }
1293 else
1294 {
1295 // Set the continue thread ID
1296 continue_packet_error = false;
1297 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001298 }
1299 if (!continue_packet_error)
1300 {
1301 // Add threads continuing with the same signo...
Greg Clayton71fc2a32011-02-12 06:28:37 +00001302 continue_packet.Printf("C%2.2x", continue_signo);
1303 }
1304 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001305 }
1306
Greg Clayton0c74e782011-06-24 03:21:43 +00001307 if (continue_packet_error && num_continue_s_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001308 {
1309 if (num_continue_s_tids == num_threads)
1310 {
1311 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001312 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001313 continue_packet.PutChar ('s');
1314 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001315 }
1316 else if (num_continue_c_tids == 0 &&
1317 num_continue_C_tids == 0 &&
1318 num_continue_s_tids == 1 &&
1319 num_continue_S_tids == 0 )
1320 {
1321 // Only one thread is stepping
Greg Clayton8b82f082011-04-12 05:54:46 +00001322 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001323 continue_packet.PutChar ('s');
Greg Clayton0c74e782011-06-24 03:21:43 +00001324 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001325 }
1326 }
1327
1328 if (!continue_packet_error && num_continue_S_tids > 0)
1329 {
1330 if (num_continue_S_tids == num_threads)
1331 {
1332 const int step_signo = m_continue_S_tids.front().second;
1333 // Are all threads trying to step with the same signal?
Greg Clayton0c74e782011-06-24 03:21:43 +00001334 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001335 if (num_continue_S_tids > 1)
1336 {
1337 for (size_t i=1; i<num_threads; ++i)
1338 {
1339 if (m_continue_S_tids[i].second != step_signo)
1340 continue_packet_error = true;
1341 }
1342 }
1343 if (!continue_packet_error)
1344 {
1345 // Add threads stepping with the same signo...
Greg Clayton8b82f082011-04-12 05:54:46 +00001346 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001347 continue_packet.Printf("S%2.2x", step_signo);
1348 }
1349 }
1350 else if (num_continue_c_tids == 0 &&
1351 num_continue_C_tids == 0 &&
1352 num_continue_s_tids == 0 &&
1353 num_continue_S_tids == 1 )
1354 {
1355 // Only one thread is stepping with signal
Greg Clayton8b82f082011-04-12 05:54:46 +00001356 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001357 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Clayton0c74e782011-06-24 03:21:43 +00001358 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001359 }
1360 }
1361 }
1362
1363 if (continue_packet_error)
1364 {
1365 error.SetErrorString ("can't make continue packet for this resume");
1366 }
1367 else
1368 {
1369 EventSP event_sp;
1370 TimeValue timeout;
1371 timeout = TimeValue::Now();
1372 timeout.OffsetWithSeconds (5);
Jim Inghamb1e2e842012-04-12 18:49:31 +00001373 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
1374 {
1375 error.SetErrorString ("Trying to resume but the async thread is dead.");
1376 if (log)
1377 log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead.");
1378 return error;
1379 }
1380
Greg Clayton71fc2a32011-02-12 06:28:37 +00001381 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1382
1383 if (listener.WaitForEvent (&timeout, event_sp) == false)
Jim Inghamb1e2e842012-04-12 18:49:31 +00001384 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001385 error.SetErrorString("Resume timed out.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00001386 if (log)
1387 log->Printf ("ProcessGDBRemote::DoResume: Resume timed out.");
1388 }
1389 else if (event_sp->BroadcasterIs (&m_async_broadcaster))
1390 {
1391 error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back.");
1392 if (log)
1393 log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back.");
1394 return error;
1395 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001396 }
Greg Claytone5219662010-12-03 06:02:24 +00001397 }
1398
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001399 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400}
1401
Greg Clayton9e920902012-04-10 02:25:43 +00001402void
1403ProcessGDBRemote::ClearThreadIDList ()
1404{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001405 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001406 m_thread_ids.clear();
1407}
1408
1409bool
1410ProcessGDBRemote::UpdateThreadIDList ()
1411{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001412 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001413 bool sequence_mutex_unavailable = false;
1414 m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable);
1415 if (sequence_mutex_unavailable)
1416 {
Greg Clayton9e920902012-04-10 02:25:43 +00001417 return false; // We just didn't get the list
1418 }
1419 return true;
1420}
1421
Greg Clayton9fc13552012-04-10 00:18:59 +00001422bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001423ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001424{
1425 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +00001426 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Clayton73b472d2010-10-27 03:32:59 +00001427 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +00001428 log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Clayton9e920902012-04-10 02:25:43 +00001429
1430 size_t num_thread_ids = m_thread_ids.size();
1431 // The "m_thread_ids" thread ID list should always be updated after each stop
1432 // reply packet, but in case it isn't, update it here.
1433 if (num_thread_ids == 0)
1434 {
1435 if (!UpdateThreadIDList ())
1436 return false;
1437 num_thread_ids = m_thread_ids.size();
1438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001440 ThreadList old_thread_list_copy(old_thread_list);
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001441 if (num_thread_ids > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001443 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444 {
Greg Clayton9e920902012-04-10 02:25:43 +00001445 tid_t tid = m_thread_ids[i];
Greg Clayton160c9d82013-05-01 21:54:04 +00001446 ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001447 if (!thread_sp)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001448 {
Jim Ingham4f465cf2012-10-10 18:32:14 +00001449 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001450 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1451 log->Printf(
1452 "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1453 __FUNCTION__,
1454 thread_sp.get(),
1455 thread_sp->GetID());
1456 }
1457 else
1458 {
1459 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1460 log->Printf(
1461 "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n",
1462 __FUNCTION__,
1463 thread_sp.get(),
1464 thread_sp->GetID());
1465 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001466 new_thread_list.AddThread(thread_sp);
Greg Claytonadc00cb2011-05-20 23:38:13 +00001467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001469
1470 // Whatever that is left in old_thread_list_copy are not
1471 // present in new_thread_list. Remove non-existent threads from internal id table.
1472 size_t old_num_thread_ids = old_thread_list_copy.GetSize(false);
1473 for (size_t i=0; i<old_num_thread_ids; i++)
1474 {
1475 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false));
1476 if (old_thread_sp)
1477 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001478 lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID();
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001479 m_thread_id_to_index_id_map.erase(old_thread_id);
1480 }
1481 }
1482
Greg Clayton9fc13552012-04-10 00:18:59 +00001483 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484}
1485
1486
1487StateType
1488ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1489{
Greg Claytondd0e5a52011-06-02 22:22:38 +00001490 stop_packet.SetFilePos (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 const char stop_type = stop_packet.GetChar();
1492 switch (stop_type)
1493 {
1494 case 'T':
1495 case 'S':
1496 {
Greg Clayton15fc2be2013-05-21 01:00:52 +00001497 // This is a bit of a hack, but is is required. If we did exec, we
1498 // need to clear our thread lists and also know to rebuild our dynamic
1499 // register info before we lookup and threads and populate the expedited
1500 // register values so we need to know this right away so we can cleanup
1501 // and update our registers.
Greg Clayton8cda7f02013-05-21 21:55:59 +00001502 const uint32_t stop_id = GetStopID();
1503 if (stop_id == 0)
Greg Claytone576ab22011-02-15 00:19:15 +00001504 {
1505 // Our first stop, make sure we have a process ID, and also make
1506 // sure we know about our registers
1507 if (GetID() == LLDB_INVALID_PROCESS_ID)
1508 {
Greg Claytonc574ede2011-03-10 02:26:48 +00001509 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone576ab22011-02-15 00:19:15 +00001510 if (pid != LLDB_INVALID_PROCESS_ID)
1511 SetID (pid);
1512 }
1513 BuildDynamicRegisterInfo (true);
1514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 // Stop with signal and thread info
1516 const uint8_t signo = stop_packet.GetHexU8();
1517 std::string name;
1518 std::string value;
1519 std::string thread_name;
Greg Claytona658fd22011-06-04 01:26:29 +00001520 std::string reason;
1521 std::string description;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522 uint32_t exc_type = 0;
Greg Clayton896dff62010-07-23 16:45:51 +00001523 std::vector<addr_t> exc_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001525 ThreadSP thread_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001526 ThreadGDBRemote *gdb_thread = NULL;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001527
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 while (stop_packet.GetNameColonValue(name, value))
1529 {
1530 if (name.compare("metype") == 0)
1531 {
1532 // exception type in big endian hex
1533 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1534 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 else if (name.compare("medata") == 0)
1536 {
1537 // exception data in big endian hex
1538 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1539 }
1540 else if (name.compare("thread") == 0)
1541 {
1542 // thread in big endian hex
Greg Clayton9e920902012-04-10 02:25:43 +00001543 lldb::tid_t tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001544 // m_thread_list_real does have its own mutex, but we need to
1545 // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...)
1546 // and the m_thread_list_real.AddThread(...) so it doesn't change on us
1547 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1548 thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false);
Greg Clayton160c9d82013-05-01 21:54:04 +00001549
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001550 if (!thread_sp)
Greg Claytond1d06e42013-04-20 00:27:58 +00001551 {
Greg Claytone576ab22011-02-15 00:19:15 +00001552 // Create the thread if we need to
Jim Ingham4f465cf2012-10-10 18:32:14 +00001553 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001554 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
1555 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1556 log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1557 __FUNCTION__,
1558 thread_sp.get(),
1559 thread_sp->GetID());
1560
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001561 m_thread_list_real.AddThread(thread_sp);
Greg Claytone576ab22011-02-15 00:19:15 +00001562 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001563 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1564
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565 }
Greg Clayton9e920902012-04-10 02:25:43 +00001566 else if (name.compare("threads") == 0)
1567 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001568 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001569 m_thread_ids.clear();
Greg Clayton44633992012-04-10 03:22:03 +00001570 // A comma separated list of all threads in the current
1571 // process that includes the thread for this stop reply
1572 // packet
Greg Clayton9e920902012-04-10 02:25:43 +00001573 size_t comma_pos;
1574 lldb::tid_t tid;
1575 while ((comma_pos = value.find(',')) != std::string::npos)
1576 {
1577 value[comma_pos] = '\0';
1578 // thread in big endian hex
1579 tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
1580 if (tid != LLDB_INVALID_THREAD_ID)
1581 m_thread_ids.push_back (tid);
1582 value.erase(0, comma_pos + 1);
1583
1584 }
1585 tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
1586 if (tid != LLDB_INVALID_THREAD_ID)
1587 m_thread_ids.push_back (tid);
1588 }
Greg Claytonde9d0492011-01-08 03:17:57 +00001589 else if (name.compare("hexname") == 0)
1590 {
1591 StringExtractor name_extractor;
1592 // Swap "value" over into "name_extractor"
1593 name_extractor.GetStringRef().swap(value);
1594 // Now convert the HEX bytes into a string value
1595 name_extractor.GetHexByteString (value);
1596 thread_name.swap (value);
1597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598 else if (name.compare("name") == 0)
1599 {
1600 thread_name.swap (value);
1601 }
Greg Clayton6f35f5c2010-09-09 06:32:46 +00001602 else if (name.compare("qaddr") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001603 {
1604 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1605 }
Greg Claytona658fd22011-06-04 01:26:29 +00001606 else if (name.compare("reason") == 0)
1607 {
1608 reason.swap(value);
1609 }
1610 else if (name.compare("description") == 0)
1611 {
1612 StringExtractor desc_extractor;
1613 // Swap "value" over into "name_extractor"
1614 desc_extractor.GetStringRef().swap(value);
1615 // Now convert the HEX bytes into a string value
1616 desc_extractor.GetHexByteString (thread_name);
1617 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001618 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1619 {
1620 // We have a register number that contains an expedited
1621 // register value. Lets supply this register to our thread
1622 // so it won't have to go and read it.
Greg Clayton160c9d82013-05-01 21:54:04 +00001623 if (gdb_thread)
Greg Clayton3e06bd92011-01-09 21:07:35 +00001624 {
1625 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1626
1627 if (reg != UINT32_MAX)
1628 {
1629 StringExtractor reg_value_extractor;
1630 // Swap "value" over into "reg_value_extractor"
1631 reg_value_extractor.GetStringRef().swap(value);
Greg Clayton160c9d82013-05-01 21:54:04 +00001632 if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor))
Greg Claytone576ab22011-02-15 00:19:15 +00001633 {
1634 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1635 name.c_str(),
1636 reg,
1637 reg,
1638 reg_value_extractor.GetStringRef().c_str(),
1639 stop_packet.GetStringRef().c_str());
1640 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001641 }
1642 }
1643 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001645
1646 if (thread_sp)
1647 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001648 // Clear the stop info just in case we don't set it to anything
1649 thread_sp->SetStopInfo (StopInfoSP());
1650
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Inghamdd2fe7a2011-01-28 02:23:12 +00001652 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653 if (exc_type != 0)
1654 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00001655 const size_t exc_data_size = exc_data.size();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001656
Greg Clayton160c9d82013-05-01 21:54:04 +00001657 thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1658 exc_type,
1659 exc_data_size,
1660 exc_data_size >= 1 ? exc_data[0] : 0,
1661 exc_data_size >= 2 ? exc_data[1] : 0,
1662 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663 }
Greg Claytona658fd22011-06-04 01:26:29 +00001664 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665 {
Greg Claytona658fd22011-06-04 01:26:29 +00001666 bool handled = false;
Greg Clayton8cda7f02013-05-21 21:55:59 +00001667 bool did_exec = false;
Greg Claytona658fd22011-06-04 01:26:29 +00001668 if (!reason.empty())
1669 {
1670 if (reason.compare("trace") == 0)
1671 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001672 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Greg Claytona658fd22011-06-04 01:26:29 +00001673 handled = true;
1674 }
1675 else if (reason.compare("breakpoint") == 0)
1676 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001677 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1678 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Claytona658fd22011-06-04 01:26:29 +00001679 if (bp_site_sp)
1680 {
1681 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1682 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1683 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
Jim Ingham54cc6e42012-07-11 21:41:19 +00001684 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001685 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001686 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001687 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001688 }
1689 else
1690 {
1691 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001692 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001693 }
1694 }
1695
Greg Claytona658fd22011-06-04 01:26:29 +00001696 }
1697 else if (reason.compare("trap") == 0)
1698 {
1699 // Let the trap just use the standard signal stop reason below...
1700 }
1701 else if (reason.compare("watchpoint") == 0)
1702 {
1703 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1704 // TODO: locate the watchpoint somehow...
Greg Clayton160c9d82013-05-01 21:54:04 +00001705 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
Greg Claytona658fd22011-06-04 01:26:29 +00001706 handled = true;
1707 }
1708 else if (reason.compare("exception") == 0)
1709 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001710 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001711 handled = true;
1712 }
Greg Clayton15fc2be2013-05-21 01:00:52 +00001713 else if (reason.compare("exec") == 0)
1714 {
Greg Clayton8cda7f02013-05-21 21:55:59 +00001715 did_exec = true;
Greg Clayton15fc2be2013-05-21 01:00:52 +00001716 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp));
1717 handled = true;
1718 }
Greg Claytona658fd22011-06-04 01:26:29 +00001719 }
1720
Greg Clayton15fc2be2013-05-21 01:00:52 +00001721 if (signo && did_exec == false)
Greg Claytona658fd22011-06-04 01:26:29 +00001722 {
1723 if (signo == SIGTRAP)
1724 {
1725 // Currently we are going to assume SIGTRAP means we are either
1726 // hitting a breakpoint or hardware single stepping.
Jim Ingham54cc6e42012-07-11 21:41:19 +00001727 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001728 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1729 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Jim Ingham54cc6e42012-07-11 21:41:19 +00001730
Greg Claytona658fd22011-06-04 01:26:29 +00001731 if (bp_site_sp)
1732 {
1733 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1734 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1735 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
Greg Clayton160c9d82013-05-01 21:54:04 +00001736 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001737 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001738 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001739 }
1740 else
1741 {
1742 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001743 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001744 }
1745 }
Jim Ingham54cc6e42012-07-11 21:41:19 +00001746 else
Greg Claytona658fd22011-06-04 01:26:29 +00001747 {
Jim Ingham4dc613b2012-10-27 02:52:04 +00001748 // If we were stepping then assume the stop was the result of the trace. If we were
1749 // not stepping then report the SIGTRAP.
1750 // FIXME: We are still missing the case where we single step over a trap instruction.
Greg Clayton160c9d82013-05-01 21:54:04 +00001751 if (thread_sp->GetTemporaryResumeState() == eStateStepping)
1752 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Jim Ingham4dc613b2012-10-27 02:52:04 +00001753 else
Greg Clayton160c9d82013-05-01 21:54:04 +00001754 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo));
Greg Claytona658fd22011-06-04 01:26:29 +00001755 }
1756 }
1757 if (!handled)
Greg Clayton160c9d82013-05-01 21:54:04 +00001758 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Jason Molenda8214b012013-04-25 01:33:46 +00001759 }
Greg Claytona658fd22011-06-04 01:26:29 +00001760
1761 if (!description.empty())
1762 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001763 lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
Greg Claytona658fd22011-06-04 01:26:29 +00001764 if (stop_info_sp)
1765 {
1766 stop_info_sp->SetDescription (description.c_str());
Greg Clayton3418c852011-08-10 02:10:13 +00001767 }
Greg Claytona658fd22011-06-04 01:26:29 +00001768 else
1769 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001770 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001771 }
1772 }
1773 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774 }
1775 return eStateStopped;
1776 }
1777 break;
1778
1779 case 'W':
1780 // process exited
1781 return eStateExited;
1782
1783 default:
1784 break;
1785 }
1786 return eStateInvalid;
1787}
1788
1789void
1790ProcessGDBRemote::RefreshStateAfterStop ()
1791{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001792 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001793 m_thread_ids.clear();
1794 // Set the thread stop info. It might have a "threads" key whose value is
1795 // a list of all thread IDs in the current process, so m_thread_ids might
1796 // get set.
1797 SetThreadStopInfo (m_last_stop_packet);
1798 // Check to see if SetThreadStopInfo() filled in m_thread_ids?
1799 if (m_thread_ids.empty())
1800 {
1801 // No, we need to fetch the thread list manually
1802 UpdateThreadIDList();
1803 }
1804
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805 // Let all threads recover from stopping and do any clean up based
1806 // on the previous thread state (if any).
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001807 m_thread_list_real.RefreshStateAfterStop();
Greg Clayton9e920902012-04-10 02:25:43 +00001808
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809}
1810
1811Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001812ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813{
1814 Error error;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001815
Greg Clayton6ed95942011-01-22 07:12:45 +00001816 bool timed_out = false;
1817 Mutex::Locker locker;
Greg Clayton513c26c2011-01-29 07:10:55 +00001818
1819 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton3af9ea52010-11-18 05:57:03 +00001820 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001821 // We are being asked to halt during an attach. We need to just close
1822 // our file handle and debugserver will go away, and we can be done...
1823 m_gdb_comm.Disconnect();
Greg Clayton3af9ea52010-11-18 05:57:03 +00001824 }
Greg Clayton513c26c2011-01-29 07:10:55 +00001825 else
1826 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001827 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton513c26c2011-01-29 07:10:55 +00001828 {
1829 if (timed_out)
1830 error.SetErrorString("timed out sending interrupt packet");
1831 else
1832 error.SetErrorString("unknown error sending interrupt packet");
1833 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001834
1835 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton513c26c2011-01-29 07:10:55 +00001836 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837 return error;
1838}
1839
1840Error
Jim Inghamacff8952013-05-02 00:27:30 +00001841ProcessGDBRemote::DoDetach(bool keep_stopped)
Greg Clayton594e5ed2010-09-27 21:07:38 +00001842{
1843 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001844 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton594e5ed2010-09-27 21:07:38 +00001845 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +00001846 log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped);
1847
Greg Clayton594e5ed2010-09-27 21:07:38 +00001848 DisableAllBreakpointSites ();
1849
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001850 m_thread_list.DiscardThreadPlans();
Greg Clayton594e5ed2010-09-27 21:07:38 +00001851
Jim Inghamacff8952013-05-02 00:27:30 +00001852 error = m_gdb_comm.Detach (keep_stopped);
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001853 if (log)
Greg Clayton594e5ed2010-09-27 21:07:38 +00001854 {
Jim Inghamacff8952013-05-02 00:27:30 +00001855 if (error.Success())
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001856 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1857 else
Jim Inghamacff8952013-05-02 00:27:30 +00001858 log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>");
Greg Clayton594e5ed2010-09-27 21:07:38 +00001859 }
Jim Inghamacff8952013-05-02 00:27:30 +00001860
1861 if (!error.Success())
1862 return error;
1863
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001864 // Sleep for one second to let the process get all detached...
Greg Clayton594e5ed2010-09-27 21:07:38 +00001865 StopAsyncThread ();
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001866
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001867 SetPrivateState (eStateDetached);
1868 ResumePrivateStateThread();
1869
1870 //KillDebugserverProcess ();
Greg Clayton594e5ed2010-09-27 21:07:38 +00001871 return error;
1872}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873
Jim Ingham43c555d2012-07-04 00:35:43 +00001874
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875Error
1876ProcessGDBRemote::DoDestroy ()
1877{
1878 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001879 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880 if (log)
1881 log->Printf ("ProcessGDBRemote::DoDestroy()");
1882
Jim Ingham43c555d2012-07-04 00:35:43 +00001883 // There is a bug in older iOS debugservers where they don't shut down the process
1884 // they are debugging properly. If the process is sitting at a breakpoint or an exception,
1885 // this can cause problems with restarting. So we check to see if any of our threads are stopped
1886 // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN
1887 // destroy it again.
1888 //
1889 // Note, we don't have a good way to test the version of debugserver, but I happen to know that
1890 // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of
1891 // the debugservers with this bug are equal. There really should be a better way to test this!
1892 //
1893 // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and
1894 // get called here to destroy again and we're still at a breakpoint or exception, then we should
1895 // just do the straight-forward kill.
1896 //
1897 // And of course, if we weren't able to stop the process by the time we get here, it isn't
1898 // necessary (or helpful) to do any of this.
1899
1900 if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning)
1901 {
1902 PlatformSP platform_sp = GetTarget().GetPlatform();
1903
1904 // FIXME: These should be ConstStrings so we aren't doing strcmp'ing.
1905 if (platform_sp
1906 && platform_sp->GetName()
Greg Clayton57abc5d2013-05-10 21:47:16 +00001907 && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic())
Jim Ingham43c555d2012-07-04 00:35:43 +00001908 {
1909 if (m_destroy_tried_resuming)
1910 {
1911 if (log)
1912 log->PutCString ("ProcessGDBRemote::DoDestroy()Tried resuming to destroy once already, not doing it again.");
1913 }
1914 else
1915 {
1916 // At present, the plans are discarded and the breakpoints disabled Process::Destroy,
1917 // but we really need it to happen here and it doesn't matter if we do it twice.
1918 m_thread_list.DiscardThreadPlans();
1919 DisableAllBreakpointSites();
1920
1921 bool stop_looks_like_crash = false;
1922 ThreadList &threads = GetThreadList();
1923
1924 {
Jim Ingham45350372012-09-11 00:08:52 +00001925 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00001926
1927 size_t num_threads = threads.GetSize();
1928 for (size_t i = 0; i < num_threads; i++)
1929 {
1930 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001931 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00001932 StopReason reason = eStopReasonInvalid;
1933 if (stop_info_sp)
1934 reason = stop_info_sp->GetStopReason();
1935 if (reason == eStopReasonBreakpoint
1936 || reason == eStopReasonException)
1937 {
1938 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001939 log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.",
1940 thread_sp->GetProtocolID(),
Jim Ingham43c555d2012-07-04 00:35:43 +00001941 stop_info_sp->GetDescription());
1942 stop_looks_like_crash = true;
1943 break;
1944 }
1945 }
1946 }
1947
1948 if (stop_looks_like_crash)
1949 {
1950 if (log)
1951 log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill.");
1952 m_destroy_tried_resuming = true;
1953
1954 // If we are going to run again before killing, it would be good to suspend all the threads
1955 // before resuming so they won't get into more trouble. Sadly, for the threads stopped with
1956 // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do
1957 // have to run the risk of letting those threads proceed a bit.
1958
1959 {
Jim Ingham45350372012-09-11 00:08:52 +00001960 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00001961
1962 size_t num_threads = threads.GetSize();
1963 for (size_t i = 0; i < num_threads; i++)
1964 {
1965 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001966 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00001967 StopReason reason = eStopReasonInvalid;
1968 if (stop_info_sp)
1969 reason = stop_info_sp->GetStopReason();
1970 if (reason != eStopReasonBreakpoint
1971 && reason != eStopReasonException)
1972 {
1973 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001974 log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.",
1975 thread_sp->GetProtocolID());
Jim Ingham43c555d2012-07-04 00:35:43 +00001976 thread_sp->SetResumeState(eStateSuspended);
1977 }
1978 }
1979 }
1980 Resume ();
1981 return Destroy();
1982 }
1983 }
1984 }
1985 }
1986
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 // Interrupt if our inferior is running...
Jim Inghambabfc382012-06-06 00:32:39 +00001988 int exit_status = SIGABRT;
1989 std::string exit_string;
1990
Greg Clayton6ed95942011-01-22 07:12:45 +00001991 if (m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992 {
Jim Inghamaab78372011-10-28 01:11:35 +00001993 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton6779606a2011-01-22 23:43:18 +00001994 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001995
1996 StringExtractorGDBRemote response;
1997 bool send_async = true;
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00001998 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (3);
1999
Greg Claytonc574ede2011-03-10 02:26:48 +00002000 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton513c26c2011-01-29 07:10:55 +00002001 {
2002 char packet_cmd = response.GetChar(0);
2003
2004 if (packet_cmd == 'W' || packet_cmd == 'X')
2005 {
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002006 SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00002007 ClearThreadIDList ();
Jim Inghambabfc382012-06-06 00:32:39 +00002008 exit_status = response.GetHexU8();
2009 }
2010 else
2011 {
2012 if (log)
2013 log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str());
2014 exit_string.assign("got unexpected response to k packet: ");
2015 exit_string.append(response.GetStringRef());
Greg Clayton513c26c2011-01-29 07:10:55 +00002016 }
2017 }
2018 else
2019 {
Jim Inghambabfc382012-06-06 00:32:39 +00002020 if (log)
2021 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
2022 exit_string.assign("failed to send the k packet");
Greg Clayton513c26c2011-01-29 07:10:55 +00002023 }
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00002024
2025 m_gdb_comm.SetPacketTimeout(old_packet_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +00002026 }
Jim Inghambabfc382012-06-06 00:32:39 +00002027 else
2028 {
2029 if (log)
2030 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
Jim Inghamcfc09352012-07-27 23:57:19 +00002031 exit_string.assign ("killed or interrupted while attaching.");
Jim Inghambabfc382012-06-06 00:32:39 +00002032 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002033 }
Jim Inghambabfc382012-06-06 00:32:39 +00002034 else
2035 {
2036 // If we missed setting the exit status on the way out, do it here.
2037 // NB set exit status can be called multiple times, the first one sets the status.
2038 exit_string.assign("destroying when not connected to debugserver");
2039 }
2040
2041 SetExitStatus(exit_status, exit_string.c_str());
2042
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043 StopAsyncThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002044 KillDebugserverProcess ();
2045 return error;
2046}
2047
Greg Clayton8cda7f02013-05-21 21:55:59 +00002048void
2049ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response)
2050{
2051 lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
2052 const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos;
2053 if (did_exec)
2054 {
2055 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
2056 if (log)
2057 log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec");
2058
2059 m_thread_list_real.Clear();
2060 m_thread_list.Clear();
2061 BuildDynamicRegisterInfo (true);
2062 m_gdb_comm.ResetDiscoverableSettings();
2063 }
2064 m_last_stop_packet = response;
2065}
2066
2067
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002068//------------------------------------------------------------------
2069// Process Queries
2070//------------------------------------------------------------------
2071
2072bool
2073ProcessGDBRemote::IsAlive ()
2074{
Greg Clayton10177aa2010-12-08 05:08:21 +00002075 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076}
2077
2078addr_t
2079ProcessGDBRemote::GetImageInfoAddress()
2080{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +00002081 return m_gdb_comm.GetShlibInfoAddr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082}
2083
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084//------------------------------------------------------------------
2085// Process Memory
2086//------------------------------------------------------------------
2087size_t
2088ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2089{
2090 if (size > m_max_memory_size)
2091 {
2092 // Keep memory read sizes down to a sane limit. This function will be
2093 // called multiple times in order to complete the task by
2094 // lldb_private::Process so it is ok to do this.
2095 size = m_max_memory_size;
2096 }
2097
2098 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00002099 const int packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size);
Andy Gibbsa297a972013-06-19 19:04:53 +00002100 assert (packet_len + 1 < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002101 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +00002102 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103 {
Greg Clayton576d8832011-03-22 04:00:09 +00002104 if (response.IsNormalResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105 {
2106 error.Clear();
2107 return response.GetHexBytes(buf, size, '\xdd');
2108 }
Greg Clayton576d8832011-03-22 04:00:09 +00002109 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002110 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002111 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002112 error.SetErrorStringWithFormat("GDB server does not support reading memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002114 error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002115 }
2116 else
2117 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002118 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119 }
2120 return 0;
2121}
2122
2123size_t
2124ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2125{
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00002126 if (size > m_max_memory_size)
2127 {
2128 // Keep memory read sizes down to a sane limit. This function will be
2129 // called multiple times in order to complete the task by
2130 // lldb_private::Process so it is ok to do this.
2131 size = m_max_memory_size;
2132 }
2133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134 StreamString packet;
Daniel Malead01b2952012-11-29 21:49:15 +00002135 packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size);
Greg Clayton7fb56d02011-02-01 01:31:41 +00002136 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002137 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +00002138 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139 {
Greg Clayton576d8832011-03-22 04:00:09 +00002140 if (response.IsOKResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141 {
2142 error.Clear();
2143 return size;
2144 }
Greg Clayton576d8832011-03-22 04:00:09 +00002145 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002146 error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002147 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002148 error.SetErrorStringWithFormat("GDB server does not support writing memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002149 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002150 error.SetErrorStringWithFormat("unexpected response to GDB server memory write packet '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002151 }
2152 else
2153 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002154 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002155 }
2156 return 0;
2157}
2158
2159lldb::addr_t
2160ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
2161{
Greg Clayton2a48f522011-05-14 01:50:35 +00002162 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
2163
Greg Clayton70b57652011-05-15 01:25:55 +00002164 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton2a48f522011-05-14 01:50:35 +00002165 switch (supported)
2166 {
2167 case eLazyBoolCalculate:
2168 case eLazyBoolYes:
2169 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
2170 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
2171 return allocated_addr;
2172
2173 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002174 // Call mmap() to create memory in the inferior..
2175 unsigned prot = 0;
2176 if (permissions & lldb::ePermissionsReadable)
2177 prot |= eMmapProtRead;
2178 if (permissions & lldb::ePermissionsWritable)
2179 prot |= eMmapProtWrite;
2180 if (permissions & lldb::ePermissionsExecutable)
2181 prot |= eMmapProtExec;
Greg Clayton2a48f522011-05-14 01:50:35 +00002182
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002183 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
2184 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
2185 m_addr_to_mmap_size[allocated_addr] = size;
2186 else
2187 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton2a48f522011-05-14 01:50:35 +00002188 break;
2189 }
2190
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191 if (allocated_addr == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +00002192 error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002193 else
2194 error.Clear();
2195 return allocated_addr;
2196}
2197
2198Error
Greg Clayton46fb5582011-11-18 07:03:08 +00002199ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
2200 MemoryRegionInfo &region_info)
2201{
2202
2203 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
2204 return error;
2205}
2206
2207Error
Johnny Chen64637202012-05-23 21:09:52 +00002208ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num)
2209{
2210
2211 Error error (m_gdb_comm.GetWatchpointSupportInfo (num));
2212 return error;
2213}
2214
2215Error
Enrico Granataf04a2192012-07-13 23:18:48 +00002216ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2217{
2218 Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after));
2219 return error;
2220}
2221
2222Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002223ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
2224{
2225 Error error;
Greg Clayton70b57652011-05-15 01:25:55 +00002226 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
2227
2228 switch (supported)
2229 {
2230 case eLazyBoolCalculate:
2231 // We should never be deallocating memory without allocating memory
2232 // first so we should never get eLazyBoolCalculate
2233 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
2234 break;
2235
2236 case eLazyBoolYes:
2237 if (!m_gdb_comm.DeallocateMemory (addr))
Daniel Malead01b2952012-11-29 21:49:15 +00002238 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002239 break;
2240
2241 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002242 // Call munmap() to deallocate memory in the inferior..
Greg Clayton70b57652011-05-15 01:25:55 +00002243 {
2244 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002245 if (pos != m_addr_to_mmap_size.end() &&
2246 InferiorCallMunmap(this, addr, pos->second))
2247 m_addr_to_mmap_size.erase (pos);
2248 else
Daniel Malead01b2952012-11-29 21:49:15 +00002249 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002250 }
2251 break;
2252 }
2253
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254 return error;
2255}
2256
2257
2258//------------------------------------------------------------------
2259// Process STDIO
2260//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261size_t
2262ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
2263{
2264 if (m_stdio_communication.IsConnected())
2265 {
2266 ConnectionStatus status;
2267 m_stdio_communication.Write(src, src_len, status, NULL);
2268 }
2269 return 0;
2270}
2271
2272Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002273ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274{
2275 Error error;
2276 assert (bp_site != NULL);
2277
Greg Clayton5160ce52013-03-27 23:08:40 +00002278 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002279 user_id_t site_id = bp_site->GetID();
2280 const addr_t addr = bp_site->GetLoadAddress();
2281 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002282 log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002283
2284 if (bp_site->IsEnabled())
2285 {
2286 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002287 log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002288 return error;
2289 }
2290 else
2291 {
2292 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2293
Greg Claytoneb023e72013-10-11 19:48:25 +00002294 if (bp_site->HardwareRequired())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002295 {
2296 // Try and set hardware breakpoint, and if that fails, fall through
2297 // and set a software breakpoint?
Greg Clayton8b82f082011-04-12 05:54:46 +00002298 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002299 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002300 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301 {
2302 bp_site->SetEnabled(true);
Greg Clayton8b82f082011-04-12 05:54:46 +00002303 bp_site->SetType (BreakpointSite::eHardware);
Greg Claytoneb023e72013-10-11 19:48:25 +00002304 }
2305 else
2306 {
2307 error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002308 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002309 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002310 else
2311 {
2312 error.SetErrorString("hardware breakpoints are not supported");
2313 }
2314 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002315 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002316 else if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002317 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002318 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
2319 {
2320 bp_site->SetEnabled(true);
2321 bp_site->SetType (BreakpointSite::eExternal);
2322 return error;
2323 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002324 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002325
2326 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002327 }
2328
2329 if (log)
2330 {
2331 const char *err_string = error.AsCString();
Jim Ingham299c0c12013-02-15 02:06:30 +00002332 log->Printf ("ProcessGDBRemote::EnableBreakpointSite () error for breakpoint at 0x%8.8" PRIx64 ": %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333 bp_site->GetLoadAddress(),
2334 err_string ? err_string : "NULL");
2335 }
2336 // We shouldn't reach here on a successful breakpoint enable...
2337 if (error.Success())
2338 error.SetErrorToGenericError();
2339 return error;
2340}
2341
2342Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002343ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344{
2345 Error error;
2346 assert (bp_site != NULL);
2347 addr_t addr = bp_site->GetLoadAddress();
2348 user_id_t site_id = bp_site->GetID();
Greg Clayton5160ce52013-03-27 23:08:40 +00002349 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002350 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002351 log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002352
2353 if (bp_site->IsEnabled())
2354 {
2355 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2356
Greg Clayton8b82f082011-04-12 05:54:46 +00002357 BreakpointSite::Type bp_type = bp_site->GetType();
2358 switch (bp_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002359 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002360 case BreakpointSite::eSoftware:
2361 error = DisableSoftwareBreakpoint (bp_site);
2362 break;
2363
2364 case BreakpointSite::eHardware:
2365 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
2366 error.SetErrorToGenericError();
2367 break;
2368
2369 case BreakpointSite::eExternal:
2370 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
2371 error.SetErrorToGenericError();
2372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002373 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002374 if (error.Success())
2375 bp_site->SetEnabled(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002376 }
2377 else
2378 {
2379 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002380 log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002381 return error;
2382 }
2383
2384 if (error.Success())
2385 error.SetErrorToGenericError();
2386 return error;
2387}
2388
Johnny Chen11309a32011-09-06 22:38:36 +00002389// Pre-requisite: wp != NULL.
2390static GDBStoppointType
Johnny Chen01a67862011-10-14 00:42:25 +00002391GetGDBStoppointType (Watchpoint *wp)
Johnny Chen11309a32011-09-06 22:38:36 +00002392{
2393 assert(wp);
2394 bool watch_read = wp->WatchpointRead();
2395 bool watch_write = wp->WatchpointWrite();
2396
2397 // watch_read and watch_write cannot both be false.
2398 assert(watch_read || watch_write);
2399 if (watch_read && watch_write)
2400 return eWatchpointReadWrite;
Johnny Chen6d487a92011-09-09 20:35:15 +00002401 else if (watch_read)
Johnny Chen11309a32011-09-06 22:38:36 +00002402 return eWatchpointRead;
Johnny Chen6d487a92011-09-09 20:35:15 +00002403 else // Must be watch_write, then.
Johnny Chen11309a32011-09-06 22:38:36 +00002404 return eWatchpointWrite;
2405}
2406
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002407Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002408ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002409{
2410 Error error;
2411 if (wp)
2412 {
2413 user_id_t watchID = wp->GetID();
2414 addr_t addr = wp->GetLoadAddress();
Greg Clayton5160ce52013-03-27 23:08:40 +00002415 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002416 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002417 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002418 if (wp->IsEnabled())
2419 {
2420 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002421 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", watchID, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002422 return error;
2423 }
Johnny Chen11309a32011-09-06 22:38:36 +00002424
2425 GDBStoppointType type = GetGDBStoppointType(wp);
2426 // Pass down an appropriate z/Z packet...
2427 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002428 {
Johnny Chen11309a32011-09-06 22:38:36 +00002429 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
2430 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002431 wp->SetEnabled(true, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002432 return error;
2433 }
2434 else
2435 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002436 }
Johnny Chen11309a32011-09-06 22:38:36 +00002437 else
2438 error.SetErrorString("watchpoints not supported");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002439 }
2440 else
2441 {
Johnny Chen01a67862011-10-14 00:42:25 +00002442 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002443 }
2444 if (error.Success())
2445 error.SetErrorToGenericError();
2446 return error;
2447}
2448
2449Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002450ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002451{
2452 Error error;
2453 if (wp)
2454 {
2455 user_id_t watchID = wp->GetID();
2456
Greg Clayton5160ce52013-03-27 23:08:40 +00002457 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002458
2459 addr_t addr = wp->GetLoadAddress();
Jim Ingham1b5792e2012-12-18 02:03:49 +00002460
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002461 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002462 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002463
Johnny Chen11309a32011-09-06 22:38:36 +00002464 if (!wp->IsEnabled())
2465 {
2466 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002467 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", watchID, (uint64_t)addr);
Johnny Chen892943f2012-08-23 22:28:26 +00002468 // See also 'class WatchpointSentry' within StopInfo.cpp.
2469 // This disabling attempt might come from the user-supplied actions, we'll route it in order for
2470 // the watchpoint object to intelligently process this action.
Jim Ingham1b5792e2012-12-18 02:03:49 +00002471 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002472 return error;
2473 }
2474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002475 if (wp->IsHardware())
2476 {
Johnny Chen11309a32011-09-06 22:38:36 +00002477 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002478 // Pass down an appropriate z/Z packet...
Johnny Chen11309a32011-09-06 22:38:36 +00002479 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2480 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002481 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002482 return error;
2483 }
2484 else
2485 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002486 }
2487 // TODO: clear software watchpoints if we implement them
2488 }
2489 else
2490 {
Johnny Chen01a67862011-10-14 00:42:25 +00002491 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002492 }
2493 if (error.Success())
2494 error.SetErrorToGenericError();
2495 return error;
2496}
2497
2498void
2499ProcessGDBRemote::Clear()
2500{
2501 m_flags = 0;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00002502 m_thread_list_real.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002503 m_thread_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002504}
2505
2506Error
2507ProcessGDBRemote::DoSignal (int signo)
2508{
2509 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002510 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002511 if (log)
2512 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2513
2514 if (!m_gdb_comm.SendAsyncSignal (signo))
2515 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2516 return error;
2517}
2518
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002519Error
Han Ming Ong84647042012-02-25 01:07:38 +00002520ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2521{
2522 ProcessLaunchInfo launch_info;
2523 return StartDebugserverProcess(debugserver_url, launch_info);
2524}
2525
2526Error
2527ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url, const ProcessInfo &process_info) // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002528{
2529 Error error;
2530 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2531 {
2532 // If we locate debugserver, keep that located version around
2533 static FileSpec g_debugserver_file_spec;
2534
Han Ming Ong84647042012-02-25 01:07:38 +00002535 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002536 char debugserver_path[PATH_MAX];
Han Ming Ong84647042012-02-25 01:07:38 +00002537 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002538
2539 // Always check to see if we have an environment override for the path
2540 // to the debugserver to use and use it if we do.
2541 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2542 if (env_debugserver_path)
Greg Clayton274060b2010-10-20 20:54:39 +00002543 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002544 else
2545 debugserver_file_spec = g_debugserver_file_spec;
2546 bool debugserver_exists = debugserver_file_spec.Exists();
2547 if (!debugserver_exists)
2548 {
2549 // The debugserver binary is in the LLDB.framework/Resources
2550 // directory.
Greg Claytondd36def2010-10-17 22:03:32 +00002551 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002552 {
Greg Claytondd36def2010-10-17 22:03:32 +00002553 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002554 debugserver_exists = debugserver_file_spec.Exists();
Greg Claytondd36def2010-10-17 22:03:32 +00002555 if (debugserver_exists)
2556 {
2557 g_debugserver_file_spec = debugserver_file_spec;
2558 }
2559 else
2560 {
2561 g_debugserver_file_spec.Clear();
2562 debugserver_file_spec.Clear();
2563 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002564 }
2565 }
2566
2567 if (debugserver_exists)
2568 {
2569 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2570
2571 m_stdio_communication.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002572
Greg Clayton5160ce52013-03-27 23:08:40 +00002573 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002574
Han Ming Ong84647042012-02-25 01:07:38 +00002575 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002576 char arg_cstr[PATH_MAX];
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002577
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002578 // Start args with "debugserver /file/path -r --"
2579 debugserver_args.AppendArgument(debugserver_path);
2580 debugserver_args.AppendArgument(debugserver_url);
Greg Claytondd36def2010-10-17 22:03:32 +00002581 // use native registers, not the GDB registers
2582 debugserver_args.AppendArgument("--native-regs");
2583 // make debugserver run in its own session so signals generated by
2584 // special terminal key sequences (^C) don't affect debugserver
2585 debugserver_args.AppendArgument("--setsid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002586
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002587 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2588 if (env_debugserver_log_file)
2589 {
2590 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2591 debugserver_args.AppendArgument(arg_cstr);
2592 }
2593
2594 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2595 if (env_debugserver_log_flags)
2596 {
2597 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2598 debugserver_args.AppendArgument(arg_cstr);
2599 }
Jim Inghama0cc6b22012-10-03 22:31:30 +00002600// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
2601// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002602
Greg Clayton8b82f082011-04-12 05:54:46 +00002603 // We currently send down all arguments, attach pids, or attach
2604 // process names in dedicated GDB server packets, so we don't need
2605 // to pass them as arguments. This is currently because of all the
2606 // things we need to setup prior to launching: the environment,
2607 // current working dir, file actions, etc.
2608#if 0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002609 // Now append the program arguments
Greg Clayton71337622011-02-24 22:24:29 +00002610 if (inferior_argv)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002611 {
Greg Clayton71337622011-02-24 22:24:29 +00002612 // Terminate the debugserver args so we can now append the inferior args
2613 debugserver_args.AppendArgument("--");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002614
Greg Clayton71337622011-02-24 22:24:29 +00002615 for (int i = 0; inferior_argv[i] != NULL; ++i)
2616 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002617 }
2618 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2619 {
2620 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2621 debugserver_args.AppendArgument (arg_cstr);
2622 }
2623 else if (attach_name && attach_name[0])
2624 {
2625 if (wait_for_launch)
2626 debugserver_args.AppendArgument ("--waitfor");
2627 else
2628 debugserver_args.AppendArgument ("--attach");
2629 debugserver_args.AppendArgument (attach_name);
2630 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002631#endif
Greg Clayton8b82f082011-04-12 05:54:46 +00002632
2633 ProcessLaunchInfo::FileAction file_action;
2634
2635 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2636 // to "/dev/null" if we run into any problems.
2637 file_action.Close (STDIN_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002638 debugserver_launch_info.AppendFileAction (file_action);
Greg Clayton8b82f082011-04-12 05:54:46 +00002639 file_action.Close (STDOUT_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002640 debugserver_launch_info.AppendFileAction (file_action);
Greg Clayton8b82f082011-04-12 05:54:46 +00002641 file_action.Close (STDERR_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002642 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002643
2644 if (log)
2645 {
2646 StreamString strm;
2647 debugserver_args.Dump (&strm);
2648 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2649 }
2650
Han Ming Ong84647042012-02-25 01:07:38 +00002651 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2652 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Claytone4e45922011-11-16 05:37:56 +00002653
Han Ming Ong84647042012-02-25 01:07:38 +00002654 error = Host::LaunchProcess(debugserver_launch_info);
Greg Clayton0b42ac32010-07-02 01:29:13 +00002655
Greg Clayton8b82f082011-04-12 05:54:46 +00002656 if (error.Success ())
Han Ming Ong84647042012-02-25 01:07:38 +00002657 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Clayton8b82f082011-04-12 05:54:46 +00002658 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002659 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2660
2661 if (error.Fail() || log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002662 error.PutToLog(log, "Host::LaunchProcess (launch_info) => pid=%" PRIu64 ", path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002663 }
2664 else
2665 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002666 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002667 }
2668
2669 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2670 StartAsyncThread ();
2671 }
2672 return error;
2673}
2674
2675bool
2676ProcessGDBRemote::MonitorDebugserverProcess
2677(
2678 void *callback_baton,
2679 lldb::pid_t debugserver_pid,
Greg Claytone4e45922011-11-16 05:37:56 +00002680 bool exited, // True if the process did exit
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002681 int signo, // Zero for no signal
2682 int exit_status // Exit value of process if signal is zero
2683)
2684{
Greg Claytone4e45922011-11-16 05:37:56 +00002685 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2686 // and might not exist anymore, so we need to carefully try to get the
2687 // target for this process first since we have a race condition when
2688 // we are done running between getting the notice that the inferior
2689 // process has died and the debugserver that was debugging this process.
2690 // In our test suite, we are also continually running process after
2691 // process, so we must be very careful to make sure:
2692 // 1 - process object hasn't been deleted already
2693 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002694
2695 // "debugserver_pid" argument passed in is the process ID for
2696 // debugserver that we are tracking...
Greg Clayton5160ce52013-03-27 23:08:40 +00002697 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002698
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002699 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton6779606a2011-01-22 23:43:18 +00002700
Greg Claytone4e45922011-11-16 05:37:56 +00002701 // Get a shared pointer to the target that has a matching process pointer.
2702 // This target could be gone, or the target could already have a new process
2703 // object inside of it
2704 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2705
Greg Clayton6779606a2011-01-22 23:43:18 +00002706 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002707 log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status);
Greg Clayton6779606a2011-01-22 23:43:18 +00002708
Greg Claytone4e45922011-11-16 05:37:56 +00002709 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002710 {
Greg Claytone4e45922011-11-16 05:37:56 +00002711 // We found a process in a target that matches, but another thread
2712 // might be in the process of launching a new process that will
2713 // soon replace it, so get a shared pointer to the process so we
2714 // can keep it alive.
2715 ProcessSP process_sp (target_sp->GetProcessSP());
2716 // Now we have a shared pointer to the process that can't go away on us
2717 // so we now make sure it was the same as the one passed in, and also make
2718 // sure that our previous "process *" didn't get deleted and have a new
2719 // "process *" created in its place with the same pointer. To verify this
2720 // we make sure the process has our debugserver process ID. If we pass all
2721 // of these tests, then we are sure that this process is the one we were
2722 // looking for.
2723 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002724 {
Greg Claytone4e45922011-11-16 05:37:56 +00002725 // Sleep for a half a second to make sure our inferior process has
2726 // time to set its exit status before we set it incorrectly when
2727 // both the debugserver and the inferior process shut down.
2728 usleep (500000);
2729 // If our process hasn't yet exited, debugserver might have died.
2730 // If the process did exit, the we are reaping it.
2731 const StateType state = process->GetState();
2732
2733 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2734 state != eStateInvalid &&
2735 state != eStateUnloaded &&
2736 state != eStateExited &&
2737 state != eStateDetached)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002738 {
Greg Claytone4e45922011-11-16 05:37:56 +00002739 char error_str[1024];
2740 if (signo)
2741 {
2742 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2743 if (signal_cstr)
2744 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2745 else
2746 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002748 else
Greg Claytone4e45922011-11-16 05:37:56 +00002749 {
2750 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2751 }
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002752
Greg Claytone4e45922011-11-16 05:37:56 +00002753 process->SetExitStatus (-1, error_str);
2754 }
2755 // Debugserver has exited we need to let our ProcessGDBRemote
2756 // know that it no longer has a debugserver instance
2757 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002759 }
2760 return true;
2761}
2762
2763void
2764ProcessGDBRemote::KillDebugserverProcess ()
2765{
2766 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2767 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002768 Host::Kill (m_debugserver_pid, SIGINT);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002769 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2770 }
2771}
2772
2773void
2774ProcessGDBRemote::Initialize()
2775{
2776 static bool g_initialized = false;
2777
2778 if (g_initialized == false)
2779 {
2780 g_initialized = true;
2781 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2782 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +00002783 CreateInstance,
2784 DebuggerInitialize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002785
2786 Log::Callbacks log_callbacks = {
2787 ProcessGDBRemoteLog::DisableLog,
2788 ProcessGDBRemoteLog::EnableLog,
2789 ProcessGDBRemoteLog::ListLogCategories
2790 };
2791
2792 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2793 }
2794}
2795
Greg Clayton7f982402013-07-15 22:54:20 +00002796void
2797ProcessGDBRemote::DebuggerInitialize (lldb_private::Debugger &debugger)
2798{
2799 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
2800 {
2801 const bool is_global_setting = true;
2802 PluginManager::CreateSettingForProcessPlugin (debugger,
2803 GetGlobalPluginProperties()->GetValueProperties(),
2804 ConstString ("Properties for the gdb-remote process plug-in."),
2805 is_global_setting);
2806 }
2807}
2808
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002809bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810ProcessGDBRemote::StartAsyncThread ()
2811{
Greg Clayton5160ce52013-03-27 23:08:40 +00002812 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002813
2814 if (log)
2815 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Jim Ingham455fa5c2012-11-01 01:15:33 +00002816
2817 Mutex::Locker start_locker(m_async_thread_state_mutex);
2818 if (m_async_thread_state == eAsyncThreadNotStarted)
2819 {
2820 // Create a thread that watches our internal state and controls which
2821 // events make it to clients (into the DCProcess event queue).
2822 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
2823 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
2824 {
2825 m_async_thread_state = eAsyncThreadRunning;
2826 return true;
2827 }
2828 else
2829 return false;
2830 }
2831 else
2832 {
2833 // Somebody tried to start the async thread while it was either being started or stopped. If the former, and
2834 // it started up successfully, then say all's well. Otherwise it is an error, since we aren't going to restart it.
2835 if (log)
2836 log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
2837 if (m_async_thread_state == eAsyncThreadRunning)
2838 return true;
2839 else
2840 return false;
2841 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002842}
2843
2844void
2845ProcessGDBRemote::StopAsyncThread ()
2846{
Greg Clayton5160ce52013-03-27 23:08:40 +00002847 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002848
2849 if (log)
2850 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2851
Jim Ingham455fa5c2012-11-01 01:15:33 +00002852 Mutex::Locker start_locker(m_async_thread_state_mutex);
2853 if (m_async_thread_state == eAsyncThreadRunning)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002854 {
Jim Ingham455fa5c2012-11-01 01:15:33 +00002855 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
2856
2857 // This will shut down the async thread.
2858 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
2859
2860 // Stop the stdio thread
2861 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
2862 {
2863 Host::ThreadJoin (m_async_thread, NULL, NULL);
2864 }
2865 m_async_thread_state = eAsyncThreadDone;
2866 }
2867 else
2868 {
2869 if (log)
2870 log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002871 }
2872}
2873
2874
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002875thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002876ProcessGDBRemote::AsyncThread (void *arg)
2877{
2878 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2879
Greg Clayton5160ce52013-03-27 23:08:40 +00002880 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002881 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002882 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002883
2884 Listener listener ("ProcessGDBRemote::AsyncThread");
2885 EventSP event_sp;
2886 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2887 eBroadcastBitAsyncThreadShouldExit;
2888
2889 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2890 {
Greg Clayton71337622011-02-24 22:24:29 +00002891 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2892
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002893 bool done = false;
2894 while (!done)
2895 {
2896 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002897 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002898 if (listener.WaitForEvent (NULL, event_sp))
2899 {
2900 const uint32_t event_type = event_sp->GetType();
Greg Clayton71337622011-02-24 22:24:29 +00002901 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902 {
Greg Clayton71337622011-02-24 22:24:29 +00002903 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002904 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002905
Greg Clayton71337622011-02-24 22:24:29 +00002906 switch (event_type)
2907 {
2908 case eBroadcastBitAsyncContinue:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002909 {
Greg Clayton71337622011-02-24 22:24:29 +00002910 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002911
Greg Clayton71337622011-02-24 22:24:29 +00002912 if (continue_packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002913 {
Greg Clayton71337622011-02-24 22:24:29 +00002914 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2915 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2916 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002917 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002918
Greg Clayton71337622011-02-24 22:24:29 +00002919 if (::strstr (continue_cstr, "vAttach") == NULL)
2920 process->SetPrivateState(eStateRunning);
2921 StringExtractorGDBRemote response;
2922 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923
Greg Clayton0772ded2012-05-16 02:48:06 +00002924 // We need to immediately clear the thread ID list so we are sure to get a valid list of threads.
2925 // The thread ID list might be contained within the "response", or the stop reply packet that
2926 // caused the stop. So clear it now before we give the stop reply packet to the process
2927 // using the process->SetLastStopPacket()...
2928 process->ClearThreadIDList ();
2929
Greg Clayton71337622011-02-24 22:24:29 +00002930 switch (stop_state)
2931 {
2932 case eStateStopped:
2933 case eStateCrashed:
2934 case eStateSuspended:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002935 process->SetLastStopPacket (response);
Greg Clayton71337622011-02-24 22:24:29 +00002936 process->SetPrivateState (stop_state);
2937 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002938
Greg Clayton71337622011-02-24 22:24:29 +00002939 case eStateExited:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002940 process->SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00002941 process->ClearThreadIDList();
Greg Clayton71337622011-02-24 22:24:29 +00002942 response.SetFilePos(1);
2943 process->SetExitStatus(response.GetHexU8(), NULL);
2944 done = true;
2945 break;
2946
2947 case eStateInvalid:
2948 process->SetExitStatus(-1, "lost connection");
2949 break;
2950
2951 default:
2952 process->SetPrivateState (stop_state);
2953 break;
2954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002955 }
2956 }
Greg Clayton71337622011-02-24 22:24:29 +00002957 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002958
Greg Clayton71337622011-02-24 22:24:29 +00002959 case eBroadcastBitAsyncThreadShouldExit:
2960 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002961 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton71337622011-02-24 22:24:29 +00002962 done = true;
2963 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964
Greg Clayton71337622011-02-24 22:24:29 +00002965 default:
2966 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002967 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type);
Greg Clayton71337622011-02-24 22:24:29 +00002968 done = true;
2969 break;
2970 }
2971 }
2972 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2973 {
2974 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2975 {
2976 process->SetExitStatus (-1, "lost connection");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002977 done = true;
Greg Clayton71337622011-02-24 22:24:29 +00002978 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002979 }
2980 }
2981 else
2982 {
2983 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002984 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002985 done = true;
2986 }
2987 }
2988 }
2989
2990 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002991 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002992
2993 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2994 return NULL;
2995}
2996
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002997const char *
2998ProcessGDBRemote::GetDispatchQueueNameForThread
2999(
3000 addr_t thread_dispatch_qaddr,
3001 std::string &dispatch_queue_name
3002)
3003{
3004 dispatch_queue_name.clear();
3005 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
3006 {
3007 // Cache the dispatch_queue_offsets_addr value so we don't always have
3008 // to look it up
3009 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
3010 {
Greg Clayton897f96a2010-10-12 17:33:06 +00003011 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
3012 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Claytonb9a01b32012-02-26 05:51:37 +00003013 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
3014 ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
Greg Clayton897f96a2010-10-12 17:33:06 +00003015 if (module_sp)
3016 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
3017
3018 if (dispatch_queue_offsets_symbol == NULL)
3019 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00003020 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
3021 module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
Greg Clayton897f96a2010-10-12 17:33:06 +00003022 if (module_sp)
3023 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
3024 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003025 if (dispatch_queue_offsets_symbol)
Greg Claytone7612132012-03-07 21:03:09 +00003026 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027
3028 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
3029 return NULL;
3030 }
3031
3032 uint8_t memory_buffer[8];
Greg Clayton514487e2011-02-15 21:59:32 +00003033 DataExtractor data (memory_buffer,
3034 sizeof(memory_buffer),
3035 m_target.GetArchitecture().GetByteOrder(),
3036 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037
3038 // Excerpt from src/queue_private.h
3039 struct dispatch_queue_offsets_s
3040 {
3041 uint16_t dqo_version;
Jason Molendae424a9b2012-11-10 06:54:30 +00003042 uint16_t dqo_label; // in version 1-3, offset to string; in version 4+, offset to a pointer to a string
3043 uint16_t dqo_label_size; // in version 1-3, length of string; in version 4+, size of a (void*) in this process
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044 } dispatch_queue_offsets;
3045
3046
3047 Error error;
3048 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
3049 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003050 lldb::offset_t data_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
3052 {
3053 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
3054 {
3055 data_offset = 0;
3056 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
Jason Molendae424a9b2012-11-10 06:54:30 +00003057 if (dispatch_queue_offsets.dqo_version >= 4)
3058 {
3059 // libdispatch versions 4+, pointer to dispatch name is in the
3060 // queue structure.
3061 lldb::addr_t pointer_to_label_address = queue_addr + dispatch_queue_offsets.dqo_label;
3062 if (ReadMemory (pointer_to_label_address, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
3063 {
3064 data_offset = 0;
3065 lldb::addr_t label_addr = data.GetAddress(&data_offset);
3066 ReadCStringFromMemory (label_addr, dispatch_queue_name, error);
3067 }
3068 }
3069 else
3070 {
3071 // libdispatch versions 1-3, dispatch name is a fixed width char array
3072 // in the queue structure.
3073 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
3074 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
3075 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
3076 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
3077 dispatch_queue_name.erase (bytes_read);
3078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003079 }
3080 }
3081 }
3082 }
3083 if (dispatch_queue_name.empty())
3084 return NULL;
3085 return dispatch_queue_name.c_str();
3086}
3087
Greg Claytone996fd32011-03-08 22:40:15 +00003088//uint32_t
3089//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3090//{
3091// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
3092// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
3093// if (m_local_debugserver)
3094// {
3095// return Host::ListProcessesMatchingName (name, matches, pids);
3096// }
3097// else
3098// {
3099// // FIXME: Implement talking to the remote debugserver.
3100// return 0;
3101// }
3102//
3103//}
3104//
Jim Ingham1c823b42011-01-22 01:33:44 +00003105bool
3106ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
3107 lldb_private::StoppointCallbackContext *context,
3108 lldb::user_id_t break_id,
3109 lldb::user_id_t break_loc_id)
3110{
3111 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
3112 // run so I can stop it if that's what I want to do.
Greg Clayton5160ce52013-03-27 23:08:40 +00003113 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham1c823b42011-01-22 01:33:44 +00003114 if (log)
3115 log->Printf("Hit New Thread Notification breakpoint.");
3116 return false;
3117}
3118
3119
3120bool
3121ProcessGDBRemote::StartNoticingNewThreads()
3122{
Greg Clayton5160ce52013-03-27 23:08:40 +00003123 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton4116e932012-05-15 02:33:01 +00003124 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003125 {
Greg Clayton4116e932012-05-15 02:33:01 +00003126 if (log && log->GetVerbose())
3127 log->Printf("Enabled noticing new thread breakpoint.");
3128 m_thread_create_bp_sp->SetEnabled(true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003129 }
Greg Clayton4116e932012-05-15 02:33:01 +00003130 else
Jim Ingham1c823b42011-01-22 01:33:44 +00003131 {
Greg Clayton4116e932012-05-15 02:33:01 +00003132 PlatformSP platform_sp (m_target.GetPlatform());
3133 if (platform_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003134 {
Greg Clayton4116e932012-05-15 02:33:01 +00003135 m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target);
3136 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003137 {
Jim Ingham37cfeab2011-10-15 00:21:37 +00003138 if (log && log->GetVerbose())
Greg Clayton4116e932012-05-15 02:33:01 +00003139 log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID());
3140 m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003141 }
3142 else
3143 {
3144 if (log)
3145 log->Printf("Failed to create new thread notification breakpoint.");
Jim Ingham1c823b42011-01-22 01:33:44 +00003146 }
3147 }
3148 }
Greg Clayton4116e932012-05-15 02:33:01 +00003149 return m_thread_create_bp_sp.get() != NULL;
Jim Ingham1c823b42011-01-22 01:33:44 +00003150}
3151
3152bool
3153ProcessGDBRemote::StopNoticingNewThreads()
3154{
Greg Clayton5160ce52013-03-27 23:08:40 +00003155 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham37cfeab2011-10-15 00:21:37 +00003156 if (log && log->GetVerbose())
Jim Ingham0e97cbc2011-02-08 05:19:01 +00003157 log->Printf ("Disabling new thread notification breakpoint.");
Greg Clayton4116e932012-05-15 02:33:01 +00003158
3159 if (m_thread_create_bp_sp)
3160 m_thread_create_bp_sp->SetEnabled(false);
3161
Jim Ingham1c823b42011-01-22 01:33:44 +00003162 return true;
3163}
3164
Jason Molenda5e8534e2012-10-03 01:29:34 +00003165lldb_private::DynamicLoader *
3166ProcessGDBRemote::GetDynamicLoader ()
3167{
3168 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +00003169 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Jason Molenda5e8534e2012-10-03 01:29:34 +00003170 return m_dyld_ap.get();
3171}
Jim Ingham1c823b42011-01-22 01:33:44 +00003172
Greg Clayton998255b2012-10-13 02:07:45 +00003173
Greg Clayton02686b82012-10-15 22:42:16 +00003174class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed
Greg Clayton998255b2012-10-13 02:07:45 +00003175{
3176private:
3177
3178public:
Greg Clayton02686b82012-10-15 22:42:16 +00003179 CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) :
Greg Clayton998255b2012-10-13 02:07:45 +00003180 CommandObjectParsed (interpreter,
Greg Clayton02686b82012-10-15 22:42:16 +00003181 "process plugin packet history",
3182 "Dumps the packet history buffer. ",
Greg Clayton998255b2012-10-13 02:07:45 +00003183 NULL)
3184 {
3185 }
3186
Greg Clayton02686b82012-10-15 22:42:16 +00003187 ~CommandObjectProcessGDBRemotePacketHistory ()
Greg Clayton998255b2012-10-13 02:07:45 +00003188 {
3189 }
3190
3191 bool
3192 DoExecute (Args& command, CommandReturnObject &result)
3193 {
Greg Clayton02686b82012-10-15 22:42:16 +00003194 const size_t argc = command.GetArgumentCount();
3195 if (argc == 0)
3196 {
3197 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3198 if (process)
3199 {
3200 process->GetGDBRemote().DumpHistory(result.GetOutputStream());
3201 result.SetStatus (eReturnStatusSuccessFinishResult);
3202 return true;
3203 }
3204 }
3205 else
3206 {
3207 result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str());
3208 }
3209 result.SetStatus (eReturnStatusFailed);
3210 return false;
3211 }
3212};
3213
3214class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed
3215{
3216private:
3217
3218public:
3219 CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) :
3220 CommandObjectParsed (interpreter,
3221 "process plugin packet send",
3222 "Send a custom packet through the GDB remote protocol and print the answer. "
3223 "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.",
3224 NULL)
3225 {
3226 }
3227
3228 ~CommandObjectProcessGDBRemotePacketSend ()
3229 {
3230 }
3231
3232 bool
3233 DoExecute (Args& command, CommandReturnObject &result)
3234 {
3235 const size_t argc = command.GetArgumentCount();
3236 if (argc == 0)
3237 {
3238 result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str());
3239 result.SetStatus (eReturnStatusFailed);
3240 return false;
3241 }
3242
3243 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3244 if (process)
3245 {
Han Ming Ong84145852012-11-26 20:42:03 +00003246 for (size_t i=0; i<argc; ++ i)
Greg Clayton02686b82012-10-15 22:42:16 +00003247 {
Han Ming Ong84145852012-11-26 20:42:03 +00003248 const char *packet_cstr = command.GetArgumentAtIndex(0);
3249 bool send_async = true;
3250 StringExtractorGDBRemote response;
3251 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3252 result.SetStatus (eReturnStatusSuccessFinishResult);
3253 Stream &output_strm = result.GetOutputStream();
3254 output_strm.Printf (" packet: %s\n", packet_cstr);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003255 std::string &response_str = response.GetStringRef();
3256
Han Ming Ong399289e2013-06-21 19:56:59 +00003257 if (strstr(packet_cstr, "qGetProfileData") != NULL)
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003258 {
3259 response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response);
3260 }
3261
Han Ming Ong84145852012-11-26 20:42:03 +00003262 if (response_str.empty())
3263 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3264 else
3265 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
Greg Clayton02686b82012-10-15 22:42:16 +00003266 }
Greg Clayton02686b82012-10-15 22:42:16 +00003267 }
Greg Clayton998255b2012-10-13 02:07:45 +00003268 return true;
3269 }
3270};
3271
Greg Claytonba4a0a52013-02-01 23:03:47 +00003272class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw
3273{
3274private:
3275
3276public:
3277 CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) :
3278 CommandObjectRaw (interpreter,
3279 "process plugin packet monitor",
Greg Claytoneee5e982013-02-14 18:39:30 +00003280 "Send a qRcmd packet through the GDB remote protocol and print the response."
3281 "The argument passed to this command will be hex encoded into a valid 'qRcmd' packet, sent and the response will be printed.",
Greg Claytonba4a0a52013-02-01 23:03:47 +00003282 NULL)
3283 {
3284 }
3285
3286 ~CommandObjectProcessGDBRemotePacketMonitor ()
3287 {
3288 }
3289
3290 bool
3291 DoExecute (const char *command, CommandReturnObject &result)
3292 {
3293 if (command == NULL || command[0] == '\0')
3294 {
3295 result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str());
3296 result.SetStatus (eReturnStatusFailed);
3297 return false;
3298 }
3299
3300 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3301 if (process)
3302 {
3303 StreamString packet;
Greg Claytoneee5e982013-02-14 18:39:30 +00003304 packet.PutCString("qRcmd,");
Greg Claytonba4a0a52013-02-01 23:03:47 +00003305 packet.PutBytesAsRawHex8(command, strlen(command));
3306 const char *packet_cstr = packet.GetString().c_str();
3307
3308 bool send_async = true;
3309 StringExtractorGDBRemote response;
3310 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3311 result.SetStatus (eReturnStatusSuccessFinishResult);
3312 Stream &output_strm = result.GetOutputStream();
3313 output_strm.Printf (" packet: %s\n", packet_cstr);
3314 const std::string &response_str = response.GetStringRef();
3315
3316 if (response_str.empty())
3317 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3318 else
3319 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
3320 }
3321 return true;
3322 }
3323};
3324
Greg Clayton02686b82012-10-15 22:42:16 +00003325class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword
3326{
3327private:
3328
3329public:
3330 CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) :
3331 CommandObjectMultiword (interpreter,
3332 "process plugin packet",
3333 "Commands that deal with GDB remote packets.",
3334 NULL)
3335 {
3336 LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter)));
3337 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter)));
Greg Claytonba4a0a52013-02-01 23:03:47 +00003338 LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter)));
Greg Clayton02686b82012-10-15 22:42:16 +00003339 }
3340
3341 ~CommandObjectProcessGDBRemotePacket ()
3342 {
3343 }
3344};
Greg Clayton998255b2012-10-13 02:07:45 +00003345
3346class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword
3347{
3348public:
3349 CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) :
3350 CommandObjectMultiword (interpreter,
3351 "process plugin",
3352 "A set of commands for operating on a ProcessGDBRemote process.",
3353 "process plugin <subcommand> [<subcommand-options>]")
3354 {
3355 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter)));
3356 }
3357
3358 ~CommandObjectMultiwordProcessGDBRemote ()
3359 {
3360 }
3361};
3362
Greg Clayton998255b2012-10-13 02:07:45 +00003363CommandObject *
3364ProcessGDBRemote::GetPluginCommandObject()
3365{
3366 if (!m_command_sp)
3367 m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter()));
3368 return m_command_sp.get();
3369}