blob: e44d7a355f374d049c428d789e6c2a6c12229244 [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 {
337 if (m_register_info.SetRegisterInfo (target_dict) > 0)
338 {
339 return true;
340 }
341 }
342 }
343 return false;
344}
345
346
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347void
Greg Clayton513c26c2011-01-29 07:10:55 +0000348ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
Greg Clayton513c26c2011-01-29 07:10:55 +0000350 if (!force && m_register_info.GetNumRegisters() > 0)
351 return;
352
353 char packet[128];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 m_register_info.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 uint32_t reg_offset = 0;
356 uint32_t reg_num = 0;
Greg Clayton23f59502012-07-17 03:23:13 +0000357 for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse;
Greg Clayton576d8832011-03-22 04:00:09 +0000358 response_type == StringExtractorGDBRemote::eResponse;
359 ++reg_num)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 {
Greg Clayton513c26c2011-01-29 07:10:55 +0000361 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
Andy Gibbsa297a972013-06-19 19:04:53 +0000362 assert (packet_len < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +0000364 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 {
Greg Clayton576d8832011-03-22 04:00:09 +0000366 response_type = response.GetResponseType();
367 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 {
369 std::string name;
370 std::string value;
371 ConstString reg_name;
372 ConstString alt_name;
373 ConstString set_name;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000374 std::vector<uint32_t> value_regs;
375 std::vector<uint32_t> invalidate_regs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 RegisterInfo reg_info = { NULL, // Name
377 NULL, // Alt name
378 0, // byte size
379 reg_offset, // offset
380 eEncodingUint, // encoding
381 eFormatHex, // formate
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
383 LLDB_INVALID_REGNUM, // GCC reg num
384 LLDB_INVALID_REGNUM, // DWARF reg num
385 LLDB_INVALID_REGNUM, // generic reg num
Jason Molendafbcb7f22010-09-10 07:49:16 +0000386 reg_num, // GDB reg num
387 reg_num // native register number
Greg Clayton435d85a2012-02-29 19:27:27 +0000388 },
389 NULL,
390 NULL
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 };
392
393 while (response.GetNameColonValue(name, value))
394 {
395 if (name.compare("name") == 0)
396 {
397 reg_name.SetCString(value.c_str());
398 }
399 else if (name.compare("alt-name") == 0)
400 {
401 alt_name.SetCString(value.c_str());
402 }
403 else if (name.compare("bitsize") == 0)
404 {
405 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
406 }
407 else if (name.compare("offset") == 0)
408 {
409 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda743e86a2010-06-11 23:44:18 +0000410 if (reg_offset != offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 {
412 reg_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 }
414 }
415 else if (name.compare("encoding") == 0)
416 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000417 const Encoding encoding = Args::StringToEncoding (value.c_str());
418 if (encoding != eEncodingInvalid)
419 reg_info.encoding = encoding;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 }
421 else if (name.compare("format") == 0)
422 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000423 Format format = eFormatInvalid;
424 if (Args::StringToFormat (value.c_str(), format, NULL).Success())
425 reg_info.format = format;
426 else if (value.compare("binary") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 reg_info.format = eFormatBinary;
428 else if (value.compare("decimal") == 0)
429 reg_info.format = eFormatDecimal;
430 else if (value.compare("hex") == 0)
431 reg_info.format = eFormatHex;
432 else if (value.compare("float") == 0)
433 reg_info.format = eFormatFloat;
434 else if (value.compare("vector-sint8") == 0)
435 reg_info.format = eFormatVectorOfSInt8;
436 else if (value.compare("vector-uint8") == 0)
437 reg_info.format = eFormatVectorOfUInt8;
438 else if (value.compare("vector-sint16") == 0)
439 reg_info.format = eFormatVectorOfSInt16;
440 else if (value.compare("vector-uint16") == 0)
441 reg_info.format = eFormatVectorOfUInt16;
442 else if (value.compare("vector-sint32") == 0)
443 reg_info.format = eFormatVectorOfSInt32;
444 else if (value.compare("vector-uint32") == 0)
445 reg_info.format = eFormatVectorOfUInt32;
446 else if (value.compare("vector-float32") == 0)
447 reg_info.format = eFormatVectorOfFloat32;
448 else if (value.compare("vector-uint128") == 0)
449 reg_info.format = eFormatVectorOfUInt128;
450 }
451 else if (name.compare("set") == 0)
452 {
453 set_name.SetCString(value.c_str());
454 }
455 else if (name.compare("gcc") == 0)
456 {
457 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
458 }
459 else if (name.compare("dwarf") == 0)
460 {
461 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
462 }
463 else if (name.compare("generic") == 0)
464 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000465 reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 }
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000467 else if (name.compare("container-regs") == 0)
468 {
469 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
470 value_pair.second = value;
471 do
472 {
473 value_pair = value_pair.second.split(',');
474 if (!value_pair.first.empty())
475 {
Greg Clayton0ba20242013-01-21 23:32:42 +0000476 uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
477 if (reg != LLDB_INVALID_REGNUM)
478 value_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000479 }
480 } while (!value_pair.second.empty());
481 }
482 else if (name.compare("invalidate-regs") == 0)
483 {
484 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
485 value_pair.second = value;
486 do
487 {
488 value_pair = value_pair.second.split(',');
489 if (!value_pair.first.empty())
490 {
Greg Clayton0ba20242013-01-21 23:32:42 +0000491 uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
492 if (reg != LLDB_INVALID_REGNUM)
493 invalidate_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000494 }
495 } while (!value_pair.second.empty());
496 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 }
498
Jason Molenda743e86a2010-06-11 23:44:18 +0000499 reg_info.byte_offset = reg_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 assert (reg_info.byte_size != 0);
501 reg_offset += reg_info.byte_size;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000502 if (!value_regs.empty())
503 {
504 value_regs.push_back(LLDB_INVALID_REGNUM);
505 reg_info.value_regs = value_regs.data();
506 }
507 if (!invalidate_regs.empty())
508 {
509 invalidate_regs.push_back(LLDB_INVALID_REGNUM);
510 reg_info.invalidate_regs = invalidate_regs.data();
511 }
512
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
514 }
515 }
516 else
517 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000518 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 }
520 }
521
Greg Claytonef8180a2013-10-15 00:14:28 +0000522 if (reg_num == 0)
523 {
524 FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile ();
525
526 // See if we can get register definitions from a python file
527 if (ParsePythonTargetDefinition (target_definition_fspec))
528 {
529 m_register_info.Finalize ();
530 return;
531 }
532 }
533
Johnny Chen2fa9de12012-05-14 18:44:23 +0000534 // We didn't get anything if the accumulated reg_num is zero. See if we are
535 // debugging ARM and fill with a hard coded register set until we can get an
536 // updated debugserver down on the devices.
537 // On the other hand, if the accumulated reg_num is positive, see if we can
538 // add composite registers to the existing primordial ones.
539 bool from_scratch = (reg_num == 0);
540
541 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000542 const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture();
543 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
544
545 // Use the process' architecture instead of the host arch, if available
546 ArchSpec remote_arch;
547 if (remote_process_arch.IsValid ())
548 remote_arch = remote_process_arch;
549 else
550 remote_arch = remote_host_arch;
551
Johnny Chen2fa9de12012-05-14 18:44:23 +0000552 if (!target_arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 {
Johnny Chen2fa9de12012-05-14 18:44:23 +0000554 if (remote_arch.IsValid()
555 && remote_arch.GetMachine() == llvm::Triple::arm
556 && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
557 m_register_info.HardcodeARMRegisters(from_scratch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 }
Johnny Chen2fa9de12012-05-14 18:44:23 +0000559 else if (target_arch.GetMachine() == llvm::Triple::arm)
560 {
561 m_register_info.HardcodeARMRegisters(from_scratch);
562 }
563
564 // At this point, we can finalize our register info.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 m_register_info.Finalize ();
566}
567
568Error
569ProcessGDBRemote::WillLaunch (Module* module)
570{
571 return WillLaunchOrAttach ();
572}
573
574Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000575ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576{
577 return WillLaunchOrAttach ();
578}
579
580Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000581ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582{
583 return WillLaunchOrAttach ();
584}
585
586Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000587ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +0000588{
589 Error error (WillLaunchOrAttach ());
590
591 if (error.Fail())
592 return error;
593
Greg Clayton2289fa42011-04-30 01:09:13 +0000594 error = ConnectToDebugserver (remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000595
596 if (error.Fail())
597 return error;
598 StartAsyncThread ();
599
Greg Claytonc574ede2011-03-10 02:26:48 +0000600 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonb766a732011-02-04 01:58:07 +0000601 if (pid == LLDB_INVALID_PROCESS_ID)
602 {
603 // We don't have a valid process ID, so note that we are connected
604 // and could now request to launch or attach, or get remote process
605 // listings...
606 SetPrivateState (eStateConnected);
607 }
608 else
609 {
610 // We have a valid process
611 SetID (pid);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000612 GetThreadList();
Greg Claytondd0e5a52011-06-02 22:22:38 +0000613 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytonb766a732011-02-04 01:58:07 +0000614 {
Carlo Kok74389122013-10-14 07:09:13 +0000615 if (!m_target.GetArchitecture().IsValid()) { // Make sure we have an architecture
616 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
617 }
618
619
Greg Claytondd0e5a52011-06-02 22:22:38 +0000620 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytonb766a732011-02-04 01:58:07 +0000621 if (state == eStateStopped)
622 {
623 SetPrivateState (state);
624 }
625 else
Daniel Malead01b2952012-11-29 21:49:15 +0000626 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 +0000627 }
628 else
Daniel Malead01b2952012-11-29 21:49:15 +0000629 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 +0000630 }
Jason Molenda16d127c2012-05-03 22:37:30 +0000631
632 if (error.Success()
633 && !GetTarget().GetArchitecture().IsValid()
634 && m_gdb_comm.GetHostArchitecture().IsValid())
635 {
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000636 // Prefer the *process'* architecture over that of the *host*, if available.
637 if (m_gdb_comm.GetProcessArchitecture().IsValid())
638 GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture());
639 else
640 GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture());
Jason Molenda16d127c2012-05-03 22:37:30 +0000641 }
642
Greg Claytonb766a732011-02-04 01:58:07 +0000643 return error;
644}
645
646Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647ProcessGDBRemote::WillLaunchOrAttach ()
648{
649 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 m_stdio_communication.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 return error;
652}
653
654//----------------------------------------------------------------------
655// Process Control
656//----------------------------------------------------------------------
657Error
Greg Clayton982c9762011-11-03 21:22:33 +0000658ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659{
Greg Clayton4957bf62010-09-30 21:49:03 +0000660 Error error;
Greg Clayton982c9762011-11-03 21:22:33 +0000661
662 uint32_t launch_flags = launch_info.GetFlags().Get();
663 const char *stdin_path = NULL;
664 const char *stdout_path = NULL;
665 const char *stderr_path = NULL;
666 const char *working_dir = launch_info.GetWorkingDirectory();
667
668 const ProcessLaunchInfo::FileAction *file_action;
669 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
670 if (file_action)
671 {
672 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
673 stdin_path = file_action->GetPath();
674 }
675 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
676 if (file_action)
677 {
678 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
679 stdout_path = file_action->GetPath();
680 }
681 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
682 if (file_action)
683 {
684 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
685 stderr_path = file_action->GetPath();
686 }
687
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
689 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
690 // ::LogSetLogFile ("/dev/stdout");
Greg Clayton5160ce52013-03-27 23:08:40 +0000691 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692
Greg Clayton982c9762011-11-03 21:22:33 +0000693 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 if (object_file)
695 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 char host_port[128];
697 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytonb766a732011-02-04 01:58:07 +0000698 char connect_url[128];
699 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700
Greg Clayton71337622011-02-24 22:24:29 +0000701 // Make sure we aren't already connected?
702 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 {
Han Ming Ong84647042012-02-25 01:07:38 +0000704 error = StartDebugserverProcess (host_port, launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 if (error.Fail())
Greg Claytonc235ac72011-08-09 05:20:29 +0000706 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000707 if (log)
708 log->Printf("failed to start debugserver process: %s", error.AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 return error;
Greg Claytonc235ac72011-08-09 05:20:29 +0000710 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711
Greg Claytonb766a732011-02-04 01:58:07 +0000712 error = ConnectToDebugserver (connect_url);
Greg Clayton71337622011-02-24 22:24:29 +0000713 }
714
715 if (error.Success())
716 {
717 lldb_utility::PseudoTerminal pty;
718 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000719
720 // If the debugserver is local and we aren't disabling STDIO, lets use
721 // a pseudo terminal to instead of relying on the 'O' packets for stdio
722 // since 'O' packets can really slow down debugging if the inferior
723 // does a lot of output.
Greg Claytonf58c2692011-06-24 22:32:10 +0000724 PlatformSP platform_sp (m_target.GetPlatform());
725 if (platform_sp && platform_sp->IsHost() && !disable_stdio)
Greg Clayton71337622011-02-24 22:24:29 +0000726 {
727 const char *slave_name = NULL;
728 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 {
Greg Clayton71337622011-02-24 22:24:29 +0000730 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
731 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
Greg Clayton71337622011-02-24 22:24:29 +0000733 if (stdin_path == NULL)
734 stdin_path = slave_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735
Greg Clayton71337622011-02-24 22:24:29 +0000736 if (stdout_path == NULL)
737 stdout_path = slave_name;
738
739 if (stderr_path == NULL)
740 stderr_path = slave_name;
741 }
742
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000743 // Set STDIN to /dev/null if we want STDIO disabled or if either
744 // STDOUT or STDERR have been set to something and STDIN hasn't
745 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000746 stdin_path = "/dev/null";
747
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000748 // Set STDOUT to /dev/null if we want STDIO disabled or if either
749 // STDIN or STDERR have been set to something and STDOUT hasn't
750 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000751 stdout_path = "/dev/null";
752
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000753 // Set STDERR to /dev/null if we want STDIO disabled or if either
754 // STDIN or STDOUT have been set to something and STDERR hasn't
755 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Clayton71337622011-02-24 22:24:29 +0000756 stderr_path = "/dev/null";
757
758 if (stdin_path)
759 m_gdb_comm.SetSTDIN (stdin_path);
760 if (stdout_path)
761 m_gdb_comm.SetSTDOUT (stdout_path);
762 if (stderr_path)
763 m_gdb_comm.SetSTDERR (stderr_path);
764
765 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
766
Greg Claytonc4103b32011-05-08 04:53:50 +0000767 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Clayton71337622011-02-24 22:24:29 +0000768
769 if (working_dir && working_dir[0])
770 {
771 m_gdb_comm.SetWorkingDir (working_dir);
772 }
773
774 // Send the environment and the program + arguments after we connect
Greg Clayton982c9762011-11-03 21:22:33 +0000775 const Args &environment = launch_info.GetEnvironmentEntries();
776 if (environment.GetArgumentCount())
Greg Clayton71337622011-02-24 22:24:29 +0000777 {
Greg Clayton982c9762011-11-03 21:22:33 +0000778 size_t num_environment_entries = environment.GetArgumentCount();
779 for (size_t i=0; i<num_environment_entries; ++i)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000780 {
Greg Clayton982c9762011-11-03 21:22:33 +0000781 const char *env_entry = environment.GetArgumentAtIndex(i);
782 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Clayton71337622011-02-24 22:24:29 +0000783 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000784 }
Greg Clayton71337622011-02-24 22:24:29 +0000785 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000786
Greg Claytonc574ede2011-03-10 02:26:48 +0000787 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10);
Greg Clayton982c9762011-11-03 21:22:33 +0000788 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector());
Greg Clayton71337622011-02-24 22:24:29 +0000789 if (arg_packet_err == 0)
790 {
791 std::string error_str;
Greg Claytonc574ede2011-03-10 02:26:48 +0000792 if (m_gdb_comm.GetLaunchSuccess (error_str))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 {
Greg Claytonc574ede2011-03-10 02:26:48 +0000794 SetID (m_gdb_comm.GetCurrentProcessID ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 }
796 else
797 {
Greg Clayton71337622011-02-24 22:24:29 +0000798 error.SetErrorString (error_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 }
Greg Clayton71337622011-02-24 22:24:29 +0000800 }
801 else
802 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000803 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Greg Clayton71337622011-02-24 22:24:29 +0000804 }
Greg Clayton8b45bee2011-08-10 22:05:39 +0000805
806 m_gdb_comm.SetPacketTimeout (old_packet_timeout);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807
Greg Clayton71337622011-02-24 22:24:29 +0000808 if (GetID() == LLDB_INVALID_PROCESS_ID)
809 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000810 if (log)
811 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton71337622011-02-24 22:24:29 +0000812 KillDebugserverProcess ();
813 return error;
814 }
815
Greg Claytondd0e5a52011-06-02 22:22:38 +0000816 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Clayton71337622011-02-24 22:24:29 +0000817 {
Carlo Kok74389122013-10-14 07:09:13 +0000818 if (!m_target.GetArchitecture().IsValid()) { // Make sure we have an architecture
819 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
820 }
821
Greg Claytondd0e5a52011-06-02 22:22:38 +0000822 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Clayton71337622011-02-24 22:24:29 +0000823
824 if (!disable_stdio)
825 {
826 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Claytonee95ed52011-11-17 22:14:31 +0000827 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Clayton71337622011-02-24 22:24:29 +0000828 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 }
830 }
Greg Claytonc235ac72011-08-09 05:20:29 +0000831 else
832 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000833 if (log)
834 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytonc235ac72011-08-09 05:20:29 +0000835 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 }
837 else
838 {
839 // Set our user ID to an invalid process ID.
840 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton982c9762011-11-03 21:22:33 +0000841 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
842 exe_module->GetFileSpec().GetFilename().AsCString(),
843 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 return error;
Greg Clayton4957bf62010-09-30 21:49:03 +0000846
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847}
848
849
850Error
Greg Claytonb766a732011-02-04 01:58:07 +0000851ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852{
853 Error error;
854 // Sleep and wait a bit for debugserver to start to listen...
Greg Clayton7b0992d2013-04-18 22:45:39 +0000855 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 if (conn_ap.get())
857 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 const uint32_t max_retry_count = 50;
859 uint32_t retry_count = 0;
860 while (!m_gdb_comm.IsConnected())
861 {
Greg Claytonb766a732011-02-04 01:58:07 +0000862 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 {
864 m_gdb_comm.SetConnection (conn_ap.release());
865 break;
866 }
Jim Ingham4e5c8212013-06-07 22:09:53 +0000867 else if (error.WasInterrupted())
868 {
869 // If we were interrupted, don't keep retrying.
870 break;
871 }
872
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 retry_count++;
Jim Ingham4e5c8212013-06-07 22:09:53 +0000874
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 if (retry_count >= max_retry_count)
876 break;
877
878 usleep (100000);
879 }
880 }
881
882 if (!m_gdb_comm.IsConnected())
883 {
884 if (error.Success())
885 error.SetErrorString("not connected to remote gdb server");
886 return error;
887 }
888
Greg Clayton32e0a752011-03-30 18:16:51 +0000889 // We always seem to be able to open a connection to a local port
890 // so we need to make sure we can then send data to it. If we can't
891 // then we aren't actually connected to anything, so try and do the
892 // handshake with the remote GDB server and make sure that goes
893 // alright.
894 if (!m_gdb_comm.HandshakeWithServer (NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000896 m_gdb_comm.Disconnect();
897 if (error.Success())
898 error.SetErrorString("not connected to remote gdb server");
899 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000901 m_gdb_comm.ResetDiscoverableSettings();
902 m_gdb_comm.QueryNoAckModeSupported ();
903 m_gdb_comm.GetThreadSuffixSupported ();
Greg Clayton44633992012-04-10 03:22:03 +0000904 m_gdb_comm.GetListThreadsInStopReplySupported ();
Greg Clayton32e0a752011-03-30 18:16:51 +0000905 m_gdb_comm.GetHostInfo ();
906 m_gdb_comm.GetVContSupported ('c');
Jim Inghamcd16df92012-07-20 21:37:13 +0000907 m_gdb_comm.GetVAttachOrWaitSupported();
Jim Ingham03afad82012-07-02 05:40:07 +0000908
909 size_t num_cmds = GetExtraStartupCommands().GetArgumentCount();
910 for (size_t idx = 0; idx < num_cmds; idx++)
911 {
912 StringExtractorGDBRemote response;
Jim Ingham03afad82012-07-02 05:40:07 +0000913 m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false);
914 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 return error;
916}
917
918void
919ProcessGDBRemote::DidLaunchOrAttach ()
920{
Greg Clayton5160ce52013-03-27 23:08:40 +0000921 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +0000922 if (log)
923 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton93d3c8332011-02-16 04:46:07 +0000924 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 {
926 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
927
Greg Clayton513c26c2011-01-29 07:10:55 +0000928 BuildDynamicRegisterInfo (false);
Greg Clayton3af9ea52010-11-18 05:57:03 +0000929
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 // See if the GDB server supports the qHostInfo information
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000931
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000932 ArchSpec gdb_remote_arch = m_gdb_comm.GetHostArchitecture();
933
934 // See if the GDB server supports the qProcessInfo packet, if so
935 // prefer that over the Host information as it will be more specific
936 // to our process.
937
938 if (m_gdb_comm.GetProcessArchitecture().IsValid())
939 gdb_remote_arch = m_gdb_comm.GetProcessArchitecture();
940
Greg Claytond314e812011-03-23 00:09:55 +0000941 if (gdb_remote_arch.IsValid())
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000942 {
Greg Claytond314e812011-03-23 00:09:55 +0000943 ArchSpec &target_arch = GetTarget().GetArchitecture();
944
945 if (target_arch.IsValid())
946 {
947 // If the remote host is ARM and we have apple as the vendor, then
948 // ARM executables and shared libraries can have mixed ARM architectures.
949 // You can have an armv6 executable, and if the host is armv7, then the
950 // system will load the best possible architecture for all shared libraries
951 // it has, so we really need to take the remote host architecture as our
952 // defacto architecture in this case.
953
954 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
955 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
956 {
957 target_arch = gdb_remote_arch;
958 }
959 else
960 {
961 // Fill in what is missing in the triple
962 const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple();
963 llvm::Triple &target_triple = target_arch.GetTriple();
Greg Clayton70b57652011-05-15 01:25:55 +0000964 if (target_triple.getVendorName().size() == 0)
965 {
Greg Claytond314e812011-03-23 00:09:55 +0000966 target_triple.setVendor (remote_triple.getVendor());
967
Greg Clayton70b57652011-05-15 01:25:55 +0000968 if (target_triple.getOSName().size() == 0)
969 {
970 target_triple.setOS (remote_triple.getOS());
Greg Claytond314e812011-03-23 00:09:55 +0000971
Greg Clayton70b57652011-05-15 01:25:55 +0000972 if (target_triple.getEnvironmentName().size() == 0)
973 target_triple.setEnvironment (remote_triple.getEnvironment());
974 }
975 }
Greg Claytond314e812011-03-23 00:09:55 +0000976 }
977 }
978 else
979 {
980 // The target doesn't have a valid architecture yet, set it from
981 // the architecture we got from the remote GDB server
982 target_arch = gdb_remote_arch;
983 }
Greg Clayton0d0c12a2011-02-09 03:09:55 +0000984 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 }
986}
987
988void
989ProcessGDBRemote::DidLaunch ()
990{
991 DidLaunchOrAttach ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992}
993
994Error
Greg Claytonc982c762010-07-09 20:39:50 +0000995ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996{
Han Ming Ong84647042012-02-25 01:07:38 +0000997 ProcessAttachInfo attach_info;
998 return DoAttachToProcessWithID(attach_pid, attach_info);
999}
1000
1001Error
1002ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
1003{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 Error error;
1005 // Clear out and clean up from any current state
1006 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007 if (attach_pid != LLDB_INVALID_PROCESS_ID)
1008 {
Greg Clayton71337622011-02-24 22:24:29 +00001009 // Make sure we aren't already connected?
1010 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 {
Greg Clayton71337622011-02-24 22:24:29 +00001012 char host_port[128];
1013 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
1014 char connect_url[128];
1015 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016
Han Ming Ong84647042012-02-25 01:07:38 +00001017 error = StartDebugserverProcess (host_port, attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001018
1019 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 {
Greg Clayton71337622011-02-24 22:24:29 +00001021 const char *error_string = error.AsCString();
1022 if (error_string == NULL)
1023 error_string = "unable to launch " DEBUGSERVER_BASENAME;
1024
1025 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 }
Greg Clayton71337622011-02-24 22:24:29 +00001027 else
1028 {
1029 error = ConnectToDebugserver (connect_url);
1030 }
1031 }
1032
1033 if (error.Success())
1034 {
1035 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001036 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid);
Greg Clayton3b608422011-11-19 02:11:30 +00001037 SetID (attach_pid);
Greg Clayton71337622011-02-24 22:24:29 +00001038 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 }
1040 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 return error;
1042}
1043
1044size_t
1045ProcessGDBRemote::AttachInputReaderCallback
1046(
1047 void *baton,
1048 InputReader *reader,
1049 lldb::InputReaderAction notification,
1050 const char *bytes,
1051 size_t bytes_len
1052)
1053{
1054 if (notification == eInputReaderGotToken)
1055 {
1056 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
1057 if (gdb_process->m_waiting_for_attach)
1058 gdb_process->m_waiting_for_attach = false;
1059 reader->SetIsDone(true);
1060 return 1;
1061 }
1062 return 0;
1063}
1064
1065Error
Han Ming Ong84647042012-02-25 01:07:38 +00001066ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067{
1068 Error error;
1069 // Clear out and clean up from any current state
1070 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 if (process_name && process_name[0])
1073 {
Greg Clayton71337622011-02-24 22:24:29 +00001074 // Make sure we aren't already connected?
1075 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 {
Greg Clayton71337622011-02-24 22:24:29 +00001077 char host_port[128];
1078 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
1079 char connect_url[128];
1080 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
1081
Han Ming Ong84647042012-02-25 01:07:38 +00001082 error = StartDebugserverProcess (host_port, attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001083 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 {
Greg Clayton71337622011-02-24 22:24:29 +00001085 const char *error_string = error.AsCString();
1086 if (error_string == NULL)
1087 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088
Greg Clayton71337622011-02-24 22:24:29 +00001089 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 }
Greg Clayton71337622011-02-24 22:24:29 +00001091 else
1092 {
1093 error = ConnectToDebugserver (connect_url);
1094 }
1095 }
1096
1097 if (error.Success())
1098 {
1099 StreamString packet;
1100
1101 if (wait_for_launch)
Jim Inghamcd16df92012-07-20 21:37:13 +00001102 {
1103 if (!m_gdb_comm.GetVAttachOrWaitSupported())
1104 {
1105 packet.PutCString ("vAttachWait");
1106 }
1107 else
1108 {
1109 if (attach_info.GetIgnoreExisting())
1110 packet.PutCString("vAttachWait");
1111 else
1112 packet.PutCString ("vAttachOrWait");
1113 }
1114 }
Greg Clayton71337622011-02-24 22:24:29 +00001115 else
1116 packet.PutCString("vAttachName");
1117 packet.PutChar(';');
1118 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
1119
1120 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
1121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 }
1123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124 return error;
1125}
1126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127
1128void
1129ProcessGDBRemote::DidAttach ()
1130{
Greg Claytonb766a732011-02-04 01:58:07 +00001131 DidLaunchOrAttach ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132}
1133
Greg Clayton90ba8112012-12-05 00:16:59 +00001134
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135Error
1136ProcessGDBRemote::WillResume ()
1137{
Greg Clayton71fc2a32011-02-12 06:28:37 +00001138 m_continue_c_tids.clear();
1139 m_continue_C_tids.clear();
1140 m_continue_s_tids.clear();
1141 m_continue_S_tids.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 return Error();
1143}
1144
1145Error
1146ProcessGDBRemote::DoResume ()
1147{
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001148 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001149 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001150 if (log)
1151 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytone5219662010-12-03 06:02:24 +00001152
1153 Listener listener ("gdb-remote.resume-packet-sent");
1154 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
1155 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00001156 listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit);
1157
Greg Claytond1d06e42013-04-20 00:27:58 +00001158 const size_t num_threads = GetThreadList().GetSize();
1159
Greg Clayton71fc2a32011-02-12 06:28:37 +00001160 StreamString continue_packet;
1161 bool continue_packet_error = false;
1162 if (m_gdb_comm.HasAnyVContSupport ())
1163 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001164 if (m_continue_c_tids.size() == num_threads)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001165 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001166 // All threads are continuing, just send a "c" packet
1167 continue_packet.PutCString ("c");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001168 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001169 else
1170 {
1171 continue_packet.PutCString ("vCont");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001172
Greg Claytond1d06e42013-04-20 00:27:58 +00001173 if (!m_continue_c_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001174 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001175 if (m_gdb_comm.GetVContSupported ('c'))
1176 {
1177 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)
1178 continue_packet.Printf(";c:%4.4" PRIx64, *t_pos);
1179 }
1180 else
1181 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001182 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001183
1184 if (!continue_packet_error && !m_continue_C_tids.empty())
1185 {
1186 if (m_gdb_comm.GetVContSupported ('C'))
1187 {
1188 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)
1189 continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1190 }
1191 else
1192 continue_packet_error = true;
1193 }
Greg Claytone5219662010-12-03 06:02:24 +00001194
Greg Claytond1d06e42013-04-20 00:27:58 +00001195 if (!continue_packet_error && !m_continue_s_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001196 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001197 if (m_gdb_comm.GetVContSupported ('s'))
1198 {
1199 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)
1200 continue_packet.Printf(";s:%4.4" PRIx64, *t_pos);
1201 }
1202 else
1203 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001204 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001205
1206 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_sig_collection::const_iterator s_pos = m_continue_S_tids.begin(), s_end = m_continue_S_tids.end(); s_pos != s_end; ++s_pos)
1211 continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
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)
1218 continue_packet.GetString().clear();
Greg Clayton71fc2a32011-02-12 06:28:37 +00001219 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001220 }
1221 else
1222 continue_packet_error = true;
1223
1224 if (continue_packet_error)
1225 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001226 // Either no vCont support, or we tried to use part of the vCont
1227 // packet that wasn't supported by the remote GDB server.
1228 // We need to try and make a simple packet that can do our continue
Greg Clayton71fc2a32011-02-12 06:28:37 +00001229 const size_t num_continue_c_tids = m_continue_c_tids.size();
1230 const size_t num_continue_C_tids = m_continue_C_tids.size();
1231 const size_t num_continue_s_tids = m_continue_s_tids.size();
1232 const size_t num_continue_S_tids = m_continue_S_tids.size();
1233 if (num_continue_c_tids > 0)
1234 {
1235 if (num_continue_c_tids == num_threads)
1236 {
1237 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001238 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001239 continue_packet.PutChar ('c');
1240 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001241 }
1242 else if (num_continue_c_tids == 1 &&
1243 num_continue_C_tids == 0 &&
1244 num_continue_s_tids == 0 &&
1245 num_continue_S_tids == 0 )
1246 {
1247 // Only one thread is continuing
Greg Clayton8b82f082011-04-12 05:54:46 +00001248 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001249 continue_packet.PutChar ('c');
Greg Clayton0c74e782011-06-24 03:21:43 +00001250 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001251 }
1252 }
1253
Greg Clayton0c74e782011-06-24 03:21:43 +00001254 if (continue_packet_error && num_continue_C_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001255 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001256 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1257 num_continue_C_tids > 0 &&
1258 num_continue_s_tids == 0 &&
1259 num_continue_S_tids == 0 )
Greg Clayton71fc2a32011-02-12 06:28:37 +00001260 {
1261 const int continue_signo = m_continue_C_tids.front().second;
Greg Clayton0c74e782011-06-24 03:21:43 +00001262 // Only one thread is continuing
Greg Clayton71fc2a32011-02-12 06:28:37 +00001263 if (num_continue_C_tids > 1)
1264 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001265 // More that one thread with a signal, yet we don't have
1266 // vCont support and we are being asked to resume each
1267 // thread with a signal, we need to make sure they are
1268 // all the same signal, or we can't issue the continue
1269 // accurately with the current support...
1270 if (num_continue_C_tids > 1)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001271 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001272 continue_packet_error = false;
1273 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1274 {
1275 if (m_continue_C_tids[i].second != continue_signo)
1276 continue_packet_error = true;
1277 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001278 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001279 if (!continue_packet_error)
1280 m_gdb_comm.SetCurrentThreadForRun (-1);
1281 }
1282 else
1283 {
1284 // Set the continue thread ID
1285 continue_packet_error = false;
1286 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001287 }
1288 if (!continue_packet_error)
1289 {
1290 // Add threads continuing with the same signo...
Greg Clayton71fc2a32011-02-12 06:28:37 +00001291 continue_packet.Printf("C%2.2x", continue_signo);
1292 }
1293 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001294 }
1295
Greg Clayton0c74e782011-06-24 03:21:43 +00001296 if (continue_packet_error && num_continue_s_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001297 {
1298 if (num_continue_s_tids == num_threads)
1299 {
1300 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001301 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001302 continue_packet.PutChar ('s');
1303 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001304 }
1305 else if (num_continue_c_tids == 0 &&
1306 num_continue_C_tids == 0 &&
1307 num_continue_s_tids == 1 &&
1308 num_continue_S_tids == 0 )
1309 {
1310 // Only one thread is stepping
Greg Clayton8b82f082011-04-12 05:54:46 +00001311 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001312 continue_packet.PutChar ('s');
Greg Clayton0c74e782011-06-24 03:21:43 +00001313 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001314 }
1315 }
1316
1317 if (!continue_packet_error && num_continue_S_tids > 0)
1318 {
1319 if (num_continue_S_tids == num_threads)
1320 {
1321 const int step_signo = m_continue_S_tids.front().second;
1322 // Are all threads trying to step with the same signal?
Greg Clayton0c74e782011-06-24 03:21:43 +00001323 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001324 if (num_continue_S_tids > 1)
1325 {
1326 for (size_t i=1; i<num_threads; ++i)
1327 {
1328 if (m_continue_S_tids[i].second != step_signo)
1329 continue_packet_error = true;
1330 }
1331 }
1332 if (!continue_packet_error)
1333 {
1334 // Add threads stepping with the same signo...
Greg Clayton8b82f082011-04-12 05:54:46 +00001335 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001336 continue_packet.Printf("S%2.2x", step_signo);
1337 }
1338 }
1339 else if (num_continue_c_tids == 0 &&
1340 num_continue_C_tids == 0 &&
1341 num_continue_s_tids == 0 &&
1342 num_continue_S_tids == 1 )
1343 {
1344 // Only one thread is stepping with signal
Greg Clayton8b82f082011-04-12 05:54:46 +00001345 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001346 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Clayton0c74e782011-06-24 03:21:43 +00001347 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001348 }
1349 }
1350 }
1351
1352 if (continue_packet_error)
1353 {
1354 error.SetErrorString ("can't make continue packet for this resume");
1355 }
1356 else
1357 {
1358 EventSP event_sp;
1359 TimeValue timeout;
1360 timeout = TimeValue::Now();
1361 timeout.OffsetWithSeconds (5);
Jim Inghamb1e2e842012-04-12 18:49:31 +00001362 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
1363 {
1364 error.SetErrorString ("Trying to resume but the async thread is dead.");
1365 if (log)
1366 log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead.");
1367 return error;
1368 }
1369
Greg Clayton71fc2a32011-02-12 06:28:37 +00001370 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1371
1372 if (listener.WaitForEvent (&timeout, event_sp) == false)
Jim Inghamb1e2e842012-04-12 18:49:31 +00001373 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001374 error.SetErrorString("Resume timed out.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00001375 if (log)
1376 log->Printf ("ProcessGDBRemote::DoResume: Resume timed out.");
1377 }
1378 else if (event_sp->BroadcasterIs (&m_async_broadcaster))
1379 {
1380 error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back.");
1381 if (log)
1382 log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back.");
1383 return error;
1384 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001385 }
Greg Claytone5219662010-12-03 06:02:24 +00001386 }
1387
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001388 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Greg Clayton9e920902012-04-10 02:25:43 +00001391void
1392ProcessGDBRemote::ClearThreadIDList ()
1393{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001394 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001395 m_thread_ids.clear();
1396}
1397
1398bool
1399ProcessGDBRemote::UpdateThreadIDList ()
1400{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001401 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001402 bool sequence_mutex_unavailable = false;
1403 m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable);
1404 if (sequence_mutex_unavailable)
1405 {
Greg Clayton9e920902012-04-10 02:25:43 +00001406 return false; // We just didn't get the list
1407 }
1408 return true;
1409}
1410
Greg Clayton9fc13552012-04-10 00:18:59 +00001411bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001412ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413{
1414 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +00001415 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Clayton73b472d2010-10-27 03:32:59 +00001416 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +00001417 log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Clayton9e920902012-04-10 02:25:43 +00001418
1419 size_t num_thread_ids = m_thread_ids.size();
1420 // The "m_thread_ids" thread ID list should always be updated after each stop
1421 // reply packet, but in case it isn't, update it here.
1422 if (num_thread_ids == 0)
1423 {
1424 if (!UpdateThreadIDList ())
1425 return false;
1426 num_thread_ids = m_thread_ids.size();
1427 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001429 ThreadList old_thread_list_copy(old_thread_list);
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001430 if (num_thread_ids > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001432 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 {
Greg Clayton9e920902012-04-10 02:25:43 +00001434 tid_t tid = m_thread_ids[i];
Greg Clayton160c9d82013-05-01 21:54:04 +00001435 ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001436 if (!thread_sp)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001437 {
Jim Ingham4f465cf2012-10-10 18:32:14 +00001438 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001439 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1440 log->Printf(
1441 "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1442 __FUNCTION__,
1443 thread_sp.get(),
1444 thread_sp->GetID());
1445 }
1446 else
1447 {
1448 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1449 log->Printf(
1450 "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n",
1451 __FUNCTION__,
1452 thread_sp.get(),
1453 thread_sp->GetID());
1454 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001455 new_thread_list.AddThread(thread_sp);
Greg Claytonadc00cb2011-05-20 23:38:13 +00001456 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 }
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001458
1459 // Whatever that is left in old_thread_list_copy are not
1460 // present in new_thread_list. Remove non-existent threads from internal id table.
1461 size_t old_num_thread_ids = old_thread_list_copy.GetSize(false);
1462 for (size_t i=0; i<old_num_thread_ids; i++)
1463 {
1464 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false));
1465 if (old_thread_sp)
1466 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001467 lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID();
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001468 m_thread_id_to_index_id_map.erase(old_thread_id);
1469 }
1470 }
1471
Greg Clayton9fc13552012-04-10 00:18:59 +00001472 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473}
1474
1475
1476StateType
1477ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1478{
Greg Claytondd0e5a52011-06-02 22:22:38 +00001479 stop_packet.SetFilePos (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 const char stop_type = stop_packet.GetChar();
1481 switch (stop_type)
1482 {
1483 case 'T':
1484 case 'S':
1485 {
Greg Clayton15fc2be2013-05-21 01:00:52 +00001486 // This is a bit of a hack, but is is required. If we did exec, we
1487 // need to clear our thread lists and also know to rebuild our dynamic
1488 // register info before we lookup and threads and populate the expedited
1489 // register values so we need to know this right away so we can cleanup
1490 // and update our registers.
Greg Clayton8cda7f02013-05-21 21:55:59 +00001491 const uint32_t stop_id = GetStopID();
1492 if (stop_id == 0)
Greg Claytone576ab22011-02-15 00:19:15 +00001493 {
1494 // Our first stop, make sure we have a process ID, and also make
1495 // sure we know about our registers
1496 if (GetID() == LLDB_INVALID_PROCESS_ID)
1497 {
Greg Claytonc574ede2011-03-10 02:26:48 +00001498 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone576ab22011-02-15 00:19:15 +00001499 if (pid != LLDB_INVALID_PROCESS_ID)
1500 SetID (pid);
1501 }
1502 BuildDynamicRegisterInfo (true);
1503 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 // Stop with signal and thread info
1505 const uint8_t signo = stop_packet.GetHexU8();
1506 std::string name;
1507 std::string value;
1508 std::string thread_name;
Greg Claytona658fd22011-06-04 01:26:29 +00001509 std::string reason;
1510 std::string description;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 uint32_t exc_type = 0;
Greg Clayton896dff62010-07-23 16:45:51 +00001512 std::vector<addr_t> exc_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001514 ThreadSP thread_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001515 ThreadGDBRemote *gdb_thread = NULL;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001516
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517 while (stop_packet.GetNameColonValue(name, value))
1518 {
1519 if (name.compare("metype") == 0)
1520 {
1521 // exception type in big endian hex
1522 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1523 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 else if (name.compare("medata") == 0)
1525 {
1526 // exception data in big endian hex
1527 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1528 }
1529 else if (name.compare("thread") == 0)
1530 {
1531 // thread in big endian hex
Greg Clayton9e920902012-04-10 02:25:43 +00001532 lldb::tid_t tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001533 // m_thread_list_real does have its own mutex, but we need to
1534 // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...)
1535 // and the m_thread_list_real.AddThread(...) so it doesn't change on us
1536 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1537 thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false);
Greg Clayton160c9d82013-05-01 21:54:04 +00001538
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001539 if (!thread_sp)
Greg Claytond1d06e42013-04-20 00:27:58 +00001540 {
Greg Claytone576ab22011-02-15 00:19:15 +00001541 // Create the thread if we need to
Jim Ingham4f465cf2012-10-10 18:32:14 +00001542 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001543 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
1544 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1545 log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1546 __FUNCTION__,
1547 thread_sp.get(),
1548 thread_sp->GetID());
1549
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001550 m_thread_list_real.AddThread(thread_sp);
Greg Claytone576ab22011-02-15 00:19:15 +00001551 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001552 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1553
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 }
Greg Clayton9e920902012-04-10 02:25:43 +00001555 else if (name.compare("threads") == 0)
1556 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001557 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001558 m_thread_ids.clear();
Greg Clayton44633992012-04-10 03:22:03 +00001559 // A comma separated list of all threads in the current
1560 // process that includes the thread for this stop reply
1561 // packet
Greg Clayton9e920902012-04-10 02:25:43 +00001562 size_t comma_pos;
1563 lldb::tid_t tid;
1564 while ((comma_pos = value.find(',')) != std::string::npos)
1565 {
1566 value[comma_pos] = '\0';
1567 // thread in big endian hex
1568 tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
1569 if (tid != LLDB_INVALID_THREAD_ID)
1570 m_thread_ids.push_back (tid);
1571 value.erase(0, comma_pos + 1);
1572
1573 }
1574 tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
1575 if (tid != LLDB_INVALID_THREAD_ID)
1576 m_thread_ids.push_back (tid);
1577 }
Greg Claytonde9d0492011-01-08 03:17:57 +00001578 else if (name.compare("hexname") == 0)
1579 {
1580 StringExtractor name_extractor;
1581 // Swap "value" over into "name_extractor"
1582 name_extractor.GetStringRef().swap(value);
1583 // Now convert the HEX bytes into a string value
1584 name_extractor.GetHexByteString (value);
1585 thread_name.swap (value);
1586 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587 else if (name.compare("name") == 0)
1588 {
1589 thread_name.swap (value);
1590 }
Greg Clayton6f35f5c2010-09-09 06:32:46 +00001591 else if (name.compare("qaddr") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592 {
1593 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1594 }
Greg Claytona658fd22011-06-04 01:26:29 +00001595 else if (name.compare("reason") == 0)
1596 {
1597 reason.swap(value);
1598 }
1599 else if (name.compare("description") == 0)
1600 {
1601 StringExtractor desc_extractor;
1602 // Swap "value" over into "name_extractor"
1603 desc_extractor.GetStringRef().swap(value);
1604 // Now convert the HEX bytes into a string value
1605 desc_extractor.GetHexByteString (thread_name);
1606 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001607 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1608 {
1609 // We have a register number that contains an expedited
1610 // register value. Lets supply this register to our thread
1611 // so it won't have to go and read it.
Greg Clayton160c9d82013-05-01 21:54:04 +00001612 if (gdb_thread)
Greg Clayton3e06bd92011-01-09 21:07:35 +00001613 {
1614 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1615
1616 if (reg != UINT32_MAX)
1617 {
1618 StringExtractor reg_value_extractor;
1619 // Swap "value" over into "reg_value_extractor"
1620 reg_value_extractor.GetStringRef().swap(value);
Greg Clayton160c9d82013-05-01 21:54:04 +00001621 if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor))
Greg Claytone576ab22011-02-15 00:19:15 +00001622 {
1623 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1624 name.c_str(),
1625 reg,
1626 reg,
1627 reg_value_extractor.GetStringRef().c_str(),
1628 stop_packet.GetStringRef().c_str());
1629 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001630 }
1631 }
1632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
1635 if (thread_sp)
1636 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001637 // Clear the stop info just in case we don't set it to anything
1638 thread_sp->SetStopInfo (StopInfoSP());
1639
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001640 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Inghamdd2fe7a2011-01-28 02:23:12 +00001641 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642 if (exc_type != 0)
1643 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00001644 const size_t exc_data_size = exc_data.size();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001645
Greg Clayton160c9d82013-05-01 21:54:04 +00001646 thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1647 exc_type,
1648 exc_data_size,
1649 exc_data_size >= 1 ? exc_data[0] : 0,
1650 exc_data_size >= 2 ? exc_data[1] : 0,
1651 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652 }
Greg Claytona658fd22011-06-04 01:26:29 +00001653 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654 {
Greg Claytona658fd22011-06-04 01:26:29 +00001655 bool handled = false;
Greg Clayton8cda7f02013-05-21 21:55:59 +00001656 bool did_exec = false;
Greg Claytona658fd22011-06-04 01:26:29 +00001657 if (!reason.empty())
1658 {
1659 if (reason.compare("trace") == 0)
1660 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001661 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Greg Claytona658fd22011-06-04 01:26:29 +00001662 handled = true;
1663 }
1664 else if (reason.compare("breakpoint") == 0)
1665 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001666 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1667 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Claytona658fd22011-06-04 01:26:29 +00001668 if (bp_site_sp)
1669 {
1670 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1671 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1672 // 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 +00001673 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001674 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001675 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001676 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001677 }
1678 else
1679 {
1680 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001681 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001682 }
1683 }
1684
Greg Claytona658fd22011-06-04 01:26:29 +00001685 }
1686 else if (reason.compare("trap") == 0)
1687 {
1688 // Let the trap just use the standard signal stop reason below...
1689 }
1690 else if (reason.compare("watchpoint") == 0)
1691 {
1692 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1693 // TODO: locate the watchpoint somehow...
Greg Clayton160c9d82013-05-01 21:54:04 +00001694 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
Greg Claytona658fd22011-06-04 01:26:29 +00001695 handled = true;
1696 }
1697 else if (reason.compare("exception") == 0)
1698 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001699 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001700 handled = true;
1701 }
Greg Clayton15fc2be2013-05-21 01:00:52 +00001702 else if (reason.compare("exec") == 0)
1703 {
Greg Clayton8cda7f02013-05-21 21:55:59 +00001704 did_exec = true;
Greg Clayton15fc2be2013-05-21 01:00:52 +00001705 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp));
1706 handled = true;
1707 }
Greg Claytona658fd22011-06-04 01:26:29 +00001708 }
1709
Greg Clayton15fc2be2013-05-21 01:00:52 +00001710 if (signo && did_exec == false)
Greg Claytona658fd22011-06-04 01:26:29 +00001711 {
1712 if (signo == SIGTRAP)
1713 {
1714 // Currently we are going to assume SIGTRAP means we are either
1715 // hitting a breakpoint or hardware single stepping.
Jim Ingham54cc6e42012-07-11 21:41:19 +00001716 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001717 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1718 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Jim Ingham54cc6e42012-07-11 21:41:19 +00001719
Greg Claytona658fd22011-06-04 01:26:29 +00001720 if (bp_site_sp)
1721 {
1722 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1723 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1724 // 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 +00001725 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001726 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001727 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001728 }
1729 else
1730 {
1731 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001732 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001733 }
1734 }
Jim Ingham54cc6e42012-07-11 21:41:19 +00001735 else
Greg Claytona658fd22011-06-04 01:26:29 +00001736 {
Jim Ingham4dc613b2012-10-27 02:52:04 +00001737 // If we were stepping then assume the stop was the result of the trace. If we were
1738 // not stepping then report the SIGTRAP.
1739 // FIXME: We are still missing the case where we single step over a trap instruction.
Greg Clayton160c9d82013-05-01 21:54:04 +00001740 if (thread_sp->GetTemporaryResumeState() == eStateStepping)
1741 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Jim Ingham4dc613b2012-10-27 02:52:04 +00001742 else
Greg Clayton160c9d82013-05-01 21:54:04 +00001743 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo));
Greg Claytona658fd22011-06-04 01:26:29 +00001744 }
1745 }
1746 if (!handled)
Greg Clayton160c9d82013-05-01 21:54:04 +00001747 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Jason Molenda8214b012013-04-25 01:33:46 +00001748 }
Greg Claytona658fd22011-06-04 01:26:29 +00001749
1750 if (!description.empty())
1751 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001752 lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
Greg Claytona658fd22011-06-04 01:26:29 +00001753 if (stop_info_sp)
1754 {
1755 stop_info_sp->SetDescription (description.c_str());
Greg Clayton3418c852011-08-10 02:10:13 +00001756 }
Greg Claytona658fd22011-06-04 01:26:29 +00001757 else
1758 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001759 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001760 }
1761 }
1762 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763 }
1764 return eStateStopped;
1765 }
1766 break;
1767
1768 case 'W':
1769 // process exited
1770 return eStateExited;
1771
1772 default:
1773 break;
1774 }
1775 return eStateInvalid;
1776}
1777
1778void
1779ProcessGDBRemote::RefreshStateAfterStop ()
1780{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001781 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001782 m_thread_ids.clear();
1783 // Set the thread stop info. It might have a "threads" key whose value is
1784 // a list of all thread IDs in the current process, so m_thread_ids might
1785 // get set.
1786 SetThreadStopInfo (m_last_stop_packet);
1787 // Check to see if SetThreadStopInfo() filled in m_thread_ids?
1788 if (m_thread_ids.empty())
1789 {
1790 // No, we need to fetch the thread list manually
1791 UpdateThreadIDList();
1792 }
1793
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001794 // Let all threads recover from stopping and do any clean up based
1795 // on the previous thread state (if any).
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001796 m_thread_list_real.RefreshStateAfterStop();
Greg Clayton9e920902012-04-10 02:25:43 +00001797
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798}
1799
1800Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001801ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802{
1803 Error error;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001804
Greg Clayton6ed95942011-01-22 07:12:45 +00001805 bool timed_out = false;
1806 Mutex::Locker locker;
Greg Clayton513c26c2011-01-29 07:10:55 +00001807
1808 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton3af9ea52010-11-18 05:57:03 +00001809 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001810 // We are being asked to halt during an attach. We need to just close
1811 // our file handle and debugserver will go away, and we can be done...
1812 m_gdb_comm.Disconnect();
Greg Clayton3af9ea52010-11-18 05:57:03 +00001813 }
Greg Clayton513c26c2011-01-29 07:10:55 +00001814 else
1815 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001816 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton513c26c2011-01-29 07:10:55 +00001817 {
1818 if (timed_out)
1819 error.SetErrorString("timed out sending interrupt packet");
1820 else
1821 error.SetErrorString("unknown error sending interrupt packet");
1822 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001823
1824 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton513c26c2011-01-29 07:10:55 +00001825 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 return error;
1827}
1828
1829Error
Jim Inghamacff8952013-05-02 00:27:30 +00001830ProcessGDBRemote::DoDetach(bool keep_stopped)
Greg Clayton594e5ed2010-09-27 21:07:38 +00001831{
1832 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001833 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton594e5ed2010-09-27 21:07:38 +00001834 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +00001835 log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped);
1836
Greg Clayton594e5ed2010-09-27 21:07:38 +00001837 DisableAllBreakpointSites ();
1838
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001839 m_thread_list.DiscardThreadPlans();
Greg Clayton594e5ed2010-09-27 21:07:38 +00001840
Jim Inghamacff8952013-05-02 00:27:30 +00001841 error = m_gdb_comm.Detach (keep_stopped);
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001842 if (log)
Greg Clayton594e5ed2010-09-27 21:07:38 +00001843 {
Jim Inghamacff8952013-05-02 00:27:30 +00001844 if (error.Success())
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001845 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1846 else
Jim Inghamacff8952013-05-02 00:27:30 +00001847 log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>");
Greg Clayton594e5ed2010-09-27 21:07:38 +00001848 }
Jim Inghamacff8952013-05-02 00:27:30 +00001849
1850 if (!error.Success())
1851 return error;
1852
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001853 // Sleep for one second to let the process get all detached...
Greg Clayton594e5ed2010-09-27 21:07:38 +00001854 StopAsyncThread ();
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001855
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001856 SetPrivateState (eStateDetached);
1857 ResumePrivateStateThread();
1858
1859 //KillDebugserverProcess ();
Greg Clayton594e5ed2010-09-27 21:07:38 +00001860 return error;
1861}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862
Jim Ingham43c555d2012-07-04 00:35:43 +00001863
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864Error
1865ProcessGDBRemote::DoDestroy ()
1866{
1867 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001868 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869 if (log)
1870 log->Printf ("ProcessGDBRemote::DoDestroy()");
1871
Jim Ingham43c555d2012-07-04 00:35:43 +00001872 // There is a bug in older iOS debugservers where they don't shut down the process
1873 // they are debugging properly. If the process is sitting at a breakpoint or an exception,
1874 // this can cause problems with restarting. So we check to see if any of our threads are stopped
1875 // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN
1876 // destroy it again.
1877 //
1878 // Note, we don't have a good way to test the version of debugserver, but I happen to know that
1879 // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of
1880 // the debugservers with this bug are equal. There really should be a better way to test this!
1881 //
1882 // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and
1883 // get called here to destroy again and we're still at a breakpoint or exception, then we should
1884 // just do the straight-forward kill.
1885 //
1886 // And of course, if we weren't able to stop the process by the time we get here, it isn't
1887 // necessary (or helpful) to do any of this.
1888
1889 if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning)
1890 {
1891 PlatformSP platform_sp = GetTarget().GetPlatform();
1892
1893 // FIXME: These should be ConstStrings so we aren't doing strcmp'ing.
1894 if (platform_sp
1895 && platform_sp->GetName()
Greg Clayton57abc5d2013-05-10 21:47:16 +00001896 && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic())
Jim Ingham43c555d2012-07-04 00:35:43 +00001897 {
1898 if (m_destroy_tried_resuming)
1899 {
1900 if (log)
1901 log->PutCString ("ProcessGDBRemote::DoDestroy()Tried resuming to destroy once already, not doing it again.");
1902 }
1903 else
1904 {
1905 // At present, the plans are discarded and the breakpoints disabled Process::Destroy,
1906 // but we really need it to happen here and it doesn't matter if we do it twice.
1907 m_thread_list.DiscardThreadPlans();
1908 DisableAllBreakpointSites();
1909
1910 bool stop_looks_like_crash = false;
1911 ThreadList &threads = GetThreadList();
1912
1913 {
Jim Ingham45350372012-09-11 00:08:52 +00001914 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00001915
1916 size_t num_threads = threads.GetSize();
1917 for (size_t i = 0; i < num_threads; i++)
1918 {
1919 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001920 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00001921 StopReason reason = eStopReasonInvalid;
1922 if (stop_info_sp)
1923 reason = stop_info_sp->GetStopReason();
1924 if (reason == eStopReasonBreakpoint
1925 || reason == eStopReasonException)
1926 {
1927 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001928 log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.",
1929 thread_sp->GetProtocolID(),
Jim Ingham43c555d2012-07-04 00:35:43 +00001930 stop_info_sp->GetDescription());
1931 stop_looks_like_crash = true;
1932 break;
1933 }
1934 }
1935 }
1936
1937 if (stop_looks_like_crash)
1938 {
1939 if (log)
1940 log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill.");
1941 m_destroy_tried_resuming = true;
1942
1943 // If we are going to run again before killing, it would be good to suspend all the threads
1944 // before resuming so they won't get into more trouble. Sadly, for the threads stopped with
1945 // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do
1946 // have to run the risk of letting those threads proceed a bit.
1947
1948 {
Jim Ingham45350372012-09-11 00:08:52 +00001949 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00001950
1951 size_t num_threads = threads.GetSize();
1952 for (size_t i = 0; i < num_threads; i++)
1953 {
1954 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001955 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00001956 StopReason reason = eStopReasonInvalid;
1957 if (stop_info_sp)
1958 reason = stop_info_sp->GetStopReason();
1959 if (reason != eStopReasonBreakpoint
1960 && reason != eStopReasonException)
1961 {
1962 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001963 log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.",
1964 thread_sp->GetProtocolID());
Jim Ingham43c555d2012-07-04 00:35:43 +00001965 thread_sp->SetResumeState(eStateSuspended);
1966 }
1967 }
1968 }
1969 Resume ();
1970 return Destroy();
1971 }
1972 }
1973 }
1974 }
1975
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976 // Interrupt if our inferior is running...
Jim Inghambabfc382012-06-06 00:32:39 +00001977 int exit_status = SIGABRT;
1978 std::string exit_string;
1979
Greg Clayton6ed95942011-01-22 07:12:45 +00001980 if (m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981 {
Jim Inghamaab78372011-10-28 01:11:35 +00001982 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton6779606a2011-01-22 23:43:18 +00001983 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001984
1985 StringExtractorGDBRemote response;
1986 bool send_async = true;
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00001987 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (3);
1988
Greg Claytonc574ede2011-03-10 02:26:48 +00001989 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton513c26c2011-01-29 07:10:55 +00001990 {
1991 char packet_cmd = response.GetChar(0);
1992
1993 if (packet_cmd == 'W' || packet_cmd == 'X')
1994 {
Greg Clayton09c3e3d2011-12-06 04:51:14 +00001995 SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00001996 ClearThreadIDList ();
Jim Inghambabfc382012-06-06 00:32:39 +00001997 exit_status = response.GetHexU8();
1998 }
1999 else
2000 {
2001 if (log)
2002 log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str());
2003 exit_string.assign("got unexpected response to k packet: ");
2004 exit_string.append(response.GetStringRef());
Greg Clayton513c26c2011-01-29 07:10:55 +00002005 }
2006 }
2007 else
2008 {
Jim Inghambabfc382012-06-06 00:32:39 +00002009 if (log)
2010 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
2011 exit_string.assign("failed to send the k packet");
Greg Clayton513c26c2011-01-29 07:10:55 +00002012 }
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00002013
2014 m_gdb_comm.SetPacketTimeout(old_packet_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +00002015 }
Jim Inghambabfc382012-06-06 00:32:39 +00002016 else
2017 {
2018 if (log)
2019 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
Jim Inghamcfc09352012-07-27 23:57:19 +00002020 exit_string.assign ("killed or interrupted while attaching.");
Jim Inghambabfc382012-06-06 00:32:39 +00002021 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002022 }
Jim Inghambabfc382012-06-06 00:32:39 +00002023 else
2024 {
2025 // If we missed setting the exit status on the way out, do it here.
2026 // NB set exit status can be called multiple times, the first one sets the status.
2027 exit_string.assign("destroying when not connected to debugserver");
2028 }
2029
2030 SetExitStatus(exit_status, exit_string.c_str());
2031
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032 StopAsyncThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002033 KillDebugserverProcess ();
2034 return error;
2035}
2036
Greg Clayton8cda7f02013-05-21 21:55:59 +00002037void
2038ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response)
2039{
2040 lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
2041 const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos;
2042 if (did_exec)
2043 {
2044 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
2045 if (log)
2046 log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec");
2047
2048 m_thread_list_real.Clear();
2049 m_thread_list.Clear();
2050 BuildDynamicRegisterInfo (true);
2051 m_gdb_comm.ResetDiscoverableSettings();
2052 }
2053 m_last_stop_packet = response;
2054}
2055
2056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002057//------------------------------------------------------------------
2058// Process Queries
2059//------------------------------------------------------------------
2060
2061bool
2062ProcessGDBRemote::IsAlive ()
2063{
Greg Clayton10177aa2010-12-08 05:08:21 +00002064 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065}
2066
2067addr_t
2068ProcessGDBRemote::GetImageInfoAddress()
2069{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +00002070 return m_gdb_comm.GetShlibInfoAddr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071}
2072
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073//------------------------------------------------------------------
2074// Process Memory
2075//------------------------------------------------------------------
2076size_t
2077ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2078{
2079 if (size > m_max_memory_size)
2080 {
2081 // Keep memory read sizes down to a sane limit. This function will be
2082 // called multiple times in order to complete the task by
2083 // lldb_private::Process so it is ok to do this.
2084 size = m_max_memory_size;
2085 }
2086
2087 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00002088 const int packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size);
Andy Gibbsa297a972013-06-19 19:04:53 +00002089 assert (packet_len + 1 < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002090 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +00002091 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092 {
Greg Clayton576d8832011-03-22 04:00:09 +00002093 if (response.IsNormalResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094 {
2095 error.Clear();
2096 return response.GetHexBytes(buf, size, '\xdd');
2097 }
Greg Clayton576d8832011-03-22 04:00:09 +00002098 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002099 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002100 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002101 error.SetErrorStringWithFormat("GDB server does not support reading memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002102 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002103 error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104 }
2105 else
2106 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002107 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002108 }
2109 return 0;
2110}
2111
2112size_t
2113ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2114{
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00002115 if (size > m_max_memory_size)
2116 {
2117 // Keep memory read sizes down to a sane limit. This function will be
2118 // called multiple times in order to complete the task by
2119 // lldb_private::Process so it is ok to do this.
2120 size = m_max_memory_size;
2121 }
2122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002123 StreamString packet;
Daniel Malead01b2952012-11-29 21:49:15 +00002124 packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size);
Greg Clayton7fb56d02011-02-01 01:31:41 +00002125 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126 StringExtractorGDBRemote response;
Greg Claytonc574ede2011-03-10 02:26:48 +00002127 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128 {
Greg Clayton576d8832011-03-22 04:00:09 +00002129 if (response.IsOKResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130 {
2131 error.Clear();
2132 return size;
2133 }
Greg Clayton576d8832011-03-22 04:00:09 +00002134 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002135 error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002136 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002137 error.SetErrorStringWithFormat("GDB server does not support writing memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002138 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002139 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 +00002140 }
2141 else
2142 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002143 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002144 }
2145 return 0;
2146}
2147
2148lldb::addr_t
2149ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
2150{
Greg Clayton2a48f522011-05-14 01:50:35 +00002151 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
2152
Greg Clayton70b57652011-05-15 01:25:55 +00002153 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton2a48f522011-05-14 01:50:35 +00002154 switch (supported)
2155 {
2156 case eLazyBoolCalculate:
2157 case eLazyBoolYes:
2158 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
2159 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
2160 return allocated_addr;
2161
2162 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002163 // Call mmap() to create memory in the inferior..
2164 unsigned prot = 0;
2165 if (permissions & lldb::ePermissionsReadable)
2166 prot |= eMmapProtRead;
2167 if (permissions & lldb::ePermissionsWritable)
2168 prot |= eMmapProtWrite;
2169 if (permissions & lldb::ePermissionsExecutable)
2170 prot |= eMmapProtExec;
Greg Clayton2a48f522011-05-14 01:50:35 +00002171
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002172 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
2173 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
2174 m_addr_to_mmap_size[allocated_addr] = size;
2175 else
2176 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton2a48f522011-05-14 01:50:35 +00002177 break;
2178 }
2179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180 if (allocated_addr == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +00002181 error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002182 else
2183 error.Clear();
2184 return allocated_addr;
2185}
2186
2187Error
Greg Clayton46fb5582011-11-18 07:03:08 +00002188ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
2189 MemoryRegionInfo &region_info)
2190{
2191
2192 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
2193 return error;
2194}
2195
2196Error
Johnny Chen64637202012-05-23 21:09:52 +00002197ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num)
2198{
2199
2200 Error error (m_gdb_comm.GetWatchpointSupportInfo (num));
2201 return error;
2202}
2203
2204Error
Enrico Granataf04a2192012-07-13 23:18:48 +00002205ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2206{
2207 Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after));
2208 return error;
2209}
2210
2211Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
2213{
2214 Error error;
Greg Clayton70b57652011-05-15 01:25:55 +00002215 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
2216
2217 switch (supported)
2218 {
2219 case eLazyBoolCalculate:
2220 // We should never be deallocating memory without allocating memory
2221 // first so we should never get eLazyBoolCalculate
2222 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
2223 break;
2224
2225 case eLazyBoolYes:
2226 if (!m_gdb_comm.DeallocateMemory (addr))
Daniel Malead01b2952012-11-29 21:49:15 +00002227 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002228 break;
2229
2230 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002231 // Call munmap() to deallocate memory in the inferior..
Greg Clayton70b57652011-05-15 01:25:55 +00002232 {
2233 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002234 if (pos != m_addr_to_mmap_size.end() &&
2235 InferiorCallMunmap(this, addr, pos->second))
2236 m_addr_to_mmap_size.erase (pos);
2237 else
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 }
2240 break;
2241 }
2242
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243 return error;
2244}
2245
2246
2247//------------------------------------------------------------------
2248// Process STDIO
2249//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250size_t
2251ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
2252{
2253 if (m_stdio_communication.IsConnected())
2254 {
2255 ConnectionStatus status;
2256 m_stdio_communication.Write(src, src_len, status, NULL);
2257 }
2258 return 0;
2259}
2260
2261Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002262ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002263{
2264 Error error;
2265 assert (bp_site != NULL);
2266
Greg Clayton5160ce52013-03-27 23:08:40 +00002267 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002268 user_id_t site_id = bp_site->GetID();
2269 const addr_t addr = bp_site->GetLoadAddress();
2270 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002271 log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002272
2273 if (bp_site->IsEnabled())
2274 {
2275 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002276 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 +00002277 return error;
2278 }
2279 else
2280 {
2281 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2282
Greg Claytoneb023e72013-10-11 19:48:25 +00002283 if (bp_site->HardwareRequired())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002284 {
2285 // Try and set hardware breakpoint, and if that fails, fall through
2286 // and set a software breakpoint?
Greg Clayton8b82f082011-04-12 05:54:46 +00002287 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002288 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002289 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002290 {
2291 bp_site->SetEnabled(true);
Greg Clayton8b82f082011-04-12 05:54:46 +00002292 bp_site->SetType (BreakpointSite::eHardware);
Greg Claytoneb023e72013-10-11 19:48:25 +00002293 }
2294 else
2295 {
2296 error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002297 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002299 else
2300 {
2301 error.SetErrorString("hardware breakpoints are not supported");
2302 }
2303 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002305 else if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002307 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
2308 {
2309 bp_site->SetEnabled(true);
2310 bp_site->SetType (BreakpointSite::eExternal);
2311 return error;
2312 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002313 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002314
2315 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002316 }
2317
2318 if (log)
2319 {
2320 const char *err_string = error.AsCString();
Jim Ingham299c0c12013-02-15 02:06:30 +00002321 log->Printf ("ProcessGDBRemote::EnableBreakpointSite () error for breakpoint at 0x%8.8" PRIx64 ": %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002322 bp_site->GetLoadAddress(),
2323 err_string ? err_string : "NULL");
2324 }
2325 // We shouldn't reach here on a successful breakpoint enable...
2326 if (error.Success())
2327 error.SetErrorToGenericError();
2328 return error;
2329}
2330
2331Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002332ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333{
2334 Error error;
2335 assert (bp_site != NULL);
2336 addr_t addr = bp_site->GetLoadAddress();
2337 user_id_t site_id = bp_site->GetID();
Greg Clayton5160ce52013-03-27 23:08:40 +00002338 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002339 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002340 log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002341
2342 if (bp_site->IsEnabled())
2343 {
2344 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2345
Greg Clayton8b82f082011-04-12 05:54:46 +00002346 BreakpointSite::Type bp_type = bp_site->GetType();
2347 switch (bp_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002348 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002349 case BreakpointSite::eSoftware:
2350 error = DisableSoftwareBreakpoint (bp_site);
2351 break;
2352
2353 case BreakpointSite::eHardware:
2354 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
2355 error.SetErrorToGenericError();
2356 break;
2357
2358 case BreakpointSite::eExternal:
2359 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
2360 error.SetErrorToGenericError();
2361 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002362 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002363 if (error.Success())
2364 bp_site->SetEnabled(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002365 }
2366 else
2367 {
2368 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002369 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 +00002370 return error;
2371 }
2372
2373 if (error.Success())
2374 error.SetErrorToGenericError();
2375 return error;
2376}
2377
Johnny Chen11309a32011-09-06 22:38:36 +00002378// Pre-requisite: wp != NULL.
2379static GDBStoppointType
Johnny Chen01a67862011-10-14 00:42:25 +00002380GetGDBStoppointType (Watchpoint *wp)
Johnny Chen11309a32011-09-06 22:38:36 +00002381{
2382 assert(wp);
2383 bool watch_read = wp->WatchpointRead();
2384 bool watch_write = wp->WatchpointWrite();
2385
2386 // watch_read and watch_write cannot both be false.
2387 assert(watch_read || watch_write);
2388 if (watch_read && watch_write)
2389 return eWatchpointReadWrite;
Johnny Chen6d487a92011-09-09 20:35:15 +00002390 else if (watch_read)
Johnny Chen11309a32011-09-06 22:38:36 +00002391 return eWatchpointRead;
Johnny Chen6d487a92011-09-09 20:35:15 +00002392 else // Must be watch_write, then.
Johnny Chen11309a32011-09-06 22:38:36 +00002393 return eWatchpointWrite;
2394}
2395
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002396Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002397ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002398{
2399 Error error;
2400 if (wp)
2401 {
2402 user_id_t watchID = wp->GetID();
2403 addr_t addr = wp->GetLoadAddress();
Greg Clayton5160ce52013-03-27 23:08:40 +00002404 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002405 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002406 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002407 if (wp->IsEnabled())
2408 {
2409 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002410 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 +00002411 return error;
2412 }
Johnny Chen11309a32011-09-06 22:38:36 +00002413
2414 GDBStoppointType type = GetGDBStoppointType(wp);
2415 // Pass down an appropriate z/Z packet...
2416 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 {
Johnny Chen11309a32011-09-06 22:38:36 +00002418 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
2419 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002420 wp->SetEnabled(true, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002421 return error;
2422 }
2423 else
2424 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002425 }
Johnny Chen11309a32011-09-06 22:38:36 +00002426 else
2427 error.SetErrorString("watchpoints not supported");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002428 }
2429 else
2430 {
Johnny Chen01a67862011-10-14 00:42:25 +00002431 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002432 }
2433 if (error.Success())
2434 error.SetErrorToGenericError();
2435 return error;
2436}
2437
2438Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002439ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002440{
2441 Error error;
2442 if (wp)
2443 {
2444 user_id_t watchID = wp->GetID();
2445
Greg Clayton5160ce52013-03-27 23:08:40 +00002446 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002447
2448 addr_t addr = wp->GetLoadAddress();
Jim Ingham1b5792e2012-12-18 02:03:49 +00002449
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002450 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002451 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002452
Johnny Chen11309a32011-09-06 22:38:36 +00002453 if (!wp->IsEnabled())
2454 {
2455 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002456 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 +00002457 // See also 'class WatchpointSentry' within StopInfo.cpp.
2458 // This disabling attempt might come from the user-supplied actions, we'll route it in order for
2459 // the watchpoint object to intelligently process this action.
Jim Ingham1b5792e2012-12-18 02:03:49 +00002460 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002461 return error;
2462 }
2463
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002464 if (wp->IsHardware())
2465 {
Johnny Chen11309a32011-09-06 22:38:36 +00002466 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002467 // Pass down an appropriate z/Z packet...
Johnny Chen11309a32011-09-06 22:38:36 +00002468 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2469 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002470 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002471 return error;
2472 }
2473 else
2474 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002475 }
2476 // TODO: clear software watchpoints if we implement them
2477 }
2478 else
2479 {
Johnny Chen01a67862011-10-14 00:42:25 +00002480 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002481 }
2482 if (error.Success())
2483 error.SetErrorToGenericError();
2484 return error;
2485}
2486
2487void
2488ProcessGDBRemote::Clear()
2489{
2490 m_flags = 0;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00002491 m_thread_list_real.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002492 m_thread_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002493}
2494
2495Error
2496ProcessGDBRemote::DoSignal (int signo)
2497{
2498 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002499 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002500 if (log)
2501 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2502
2503 if (!m_gdb_comm.SendAsyncSignal (signo))
2504 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2505 return error;
2506}
2507
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002508Error
Han Ming Ong84647042012-02-25 01:07:38 +00002509ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2510{
2511 ProcessLaunchInfo launch_info;
2512 return StartDebugserverProcess(debugserver_url, launch_info);
2513}
2514
2515Error
2516ProcessGDBRemote::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 +00002517{
2518 Error error;
2519 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2520 {
2521 // If we locate debugserver, keep that located version around
2522 static FileSpec g_debugserver_file_spec;
2523
Han Ming Ong84647042012-02-25 01:07:38 +00002524 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002525 char debugserver_path[PATH_MAX];
Han Ming Ong84647042012-02-25 01:07:38 +00002526 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002527
2528 // Always check to see if we have an environment override for the path
2529 // to the debugserver to use and use it if we do.
2530 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2531 if (env_debugserver_path)
Greg Clayton274060b2010-10-20 20:54:39 +00002532 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002533 else
2534 debugserver_file_spec = g_debugserver_file_spec;
2535 bool debugserver_exists = debugserver_file_spec.Exists();
2536 if (!debugserver_exists)
2537 {
2538 // The debugserver binary is in the LLDB.framework/Resources
2539 // directory.
Greg Claytondd36def2010-10-17 22:03:32 +00002540 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002541 {
Greg Claytondd36def2010-10-17 22:03:32 +00002542 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002543 debugserver_exists = debugserver_file_spec.Exists();
Greg Claytondd36def2010-10-17 22:03:32 +00002544 if (debugserver_exists)
2545 {
2546 g_debugserver_file_spec = debugserver_file_spec;
2547 }
2548 else
2549 {
2550 g_debugserver_file_spec.Clear();
2551 debugserver_file_spec.Clear();
2552 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002553 }
2554 }
2555
2556 if (debugserver_exists)
2557 {
2558 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2559
2560 m_stdio_communication.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002561
Greg Clayton5160ce52013-03-27 23:08:40 +00002562 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002563
Han Ming Ong84647042012-02-25 01:07:38 +00002564 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002565 char arg_cstr[PATH_MAX];
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002566
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002567 // Start args with "debugserver /file/path -r --"
2568 debugserver_args.AppendArgument(debugserver_path);
2569 debugserver_args.AppendArgument(debugserver_url);
Greg Claytondd36def2010-10-17 22:03:32 +00002570 // use native registers, not the GDB registers
2571 debugserver_args.AppendArgument("--native-regs");
2572 // make debugserver run in its own session so signals generated by
2573 // special terminal key sequences (^C) don't affect debugserver
2574 debugserver_args.AppendArgument("--setsid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002575
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002576 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2577 if (env_debugserver_log_file)
2578 {
2579 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2580 debugserver_args.AppendArgument(arg_cstr);
2581 }
2582
2583 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2584 if (env_debugserver_log_flags)
2585 {
2586 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2587 debugserver_args.AppendArgument(arg_cstr);
2588 }
Jim Inghama0cc6b22012-10-03 22:31:30 +00002589// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
2590// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002591
Greg Clayton8b82f082011-04-12 05:54:46 +00002592 // We currently send down all arguments, attach pids, or attach
2593 // process names in dedicated GDB server packets, so we don't need
2594 // to pass them as arguments. This is currently because of all the
2595 // things we need to setup prior to launching: the environment,
2596 // current working dir, file actions, etc.
2597#if 0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002598 // Now append the program arguments
Greg Clayton71337622011-02-24 22:24:29 +00002599 if (inferior_argv)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002600 {
Greg Clayton71337622011-02-24 22:24:29 +00002601 // Terminate the debugserver args so we can now append the inferior args
2602 debugserver_args.AppendArgument("--");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002603
Greg Clayton71337622011-02-24 22:24:29 +00002604 for (int i = 0; inferior_argv[i] != NULL; ++i)
2605 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002606 }
2607 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2608 {
2609 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2610 debugserver_args.AppendArgument (arg_cstr);
2611 }
2612 else if (attach_name && attach_name[0])
2613 {
2614 if (wait_for_launch)
2615 debugserver_args.AppendArgument ("--waitfor");
2616 else
2617 debugserver_args.AppendArgument ("--attach");
2618 debugserver_args.AppendArgument (attach_name);
2619 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002620#endif
Greg Clayton8b82f082011-04-12 05:54:46 +00002621
2622 ProcessLaunchInfo::FileAction file_action;
2623
2624 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2625 // to "/dev/null" if we run into any problems.
2626 file_action.Close (STDIN_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002627 debugserver_launch_info.AppendFileAction (file_action);
Greg Clayton8b82f082011-04-12 05:54:46 +00002628 file_action.Close (STDOUT_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002629 debugserver_launch_info.AppendFileAction (file_action);
Greg Clayton8b82f082011-04-12 05:54:46 +00002630 file_action.Close (STDERR_FILENO);
Han Ming Ong84647042012-02-25 01:07:38 +00002631 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002632
2633 if (log)
2634 {
2635 StreamString strm;
2636 debugserver_args.Dump (&strm);
2637 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2638 }
2639
Han Ming Ong84647042012-02-25 01:07:38 +00002640 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2641 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Claytone4e45922011-11-16 05:37:56 +00002642
Han Ming Ong84647042012-02-25 01:07:38 +00002643 error = Host::LaunchProcess(debugserver_launch_info);
Greg Clayton0b42ac32010-07-02 01:29:13 +00002644
Greg Clayton8b82f082011-04-12 05:54:46 +00002645 if (error.Success ())
Han Ming Ong84647042012-02-25 01:07:38 +00002646 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Clayton8b82f082011-04-12 05:54:46 +00002647 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002648 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2649
2650 if (error.Fail() || log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002651 error.PutToLog(log, "Host::LaunchProcess (launch_info) => pid=%" PRIu64 ", path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002652 }
2653 else
2654 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002655 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002656 }
2657
2658 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2659 StartAsyncThread ();
2660 }
2661 return error;
2662}
2663
2664bool
2665ProcessGDBRemote::MonitorDebugserverProcess
2666(
2667 void *callback_baton,
2668 lldb::pid_t debugserver_pid,
Greg Claytone4e45922011-11-16 05:37:56 +00002669 bool exited, // True if the process did exit
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002670 int signo, // Zero for no signal
2671 int exit_status // Exit value of process if signal is zero
2672)
2673{
Greg Claytone4e45922011-11-16 05:37:56 +00002674 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2675 // and might not exist anymore, so we need to carefully try to get the
2676 // target for this process first since we have a race condition when
2677 // we are done running between getting the notice that the inferior
2678 // process has died and the debugserver that was debugging this process.
2679 // In our test suite, we are also continually running process after
2680 // process, so we must be very careful to make sure:
2681 // 1 - process object hasn't been deleted already
2682 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002683
2684 // "debugserver_pid" argument passed in is the process ID for
2685 // debugserver that we are tracking...
Greg Clayton5160ce52013-03-27 23:08:40 +00002686 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002687
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002688 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton6779606a2011-01-22 23:43:18 +00002689
Greg Claytone4e45922011-11-16 05:37:56 +00002690 // Get a shared pointer to the target that has a matching process pointer.
2691 // This target could be gone, or the target could already have a new process
2692 // object inside of it
2693 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2694
Greg Clayton6779606a2011-01-22 23:43:18 +00002695 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002696 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 +00002697
Greg Claytone4e45922011-11-16 05:37:56 +00002698 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002699 {
Greg Claytone4e45922011-11-16 05:37:56 +00002700 // We found a process in a target that matches, but another thread
2701 // might be in the process of launching a new process that will
2702 // soon replace it, so get a shared pointer to the process so we
2703 // can keep it alive.
2704 ProcessSP process_sp (target_sp->GetProcessSP());
2705 // Now we have a shared pointer to the process that can't go away on us
2706 // so we now make sure it was the same as the one passed in, and also make
2707 // sure that our previous "process *" didn't get deleted and have a new
2708 // "process *" created in its place with the same pointer. To verify this
2709 // we make sure the process has our debugserver process ID. If we pass all
2710 // of these tests, then we are sure that this process is the one we were
2711 // looking for.
2712 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002713 {
Greg Claytone4e45922011-11-16 05:37:56 +00002714 // Sleep for a half a second to make sure our inferior process has
2715 // time to set its exit status before we set it incorrectly when
2716 // both the debugserver and the inferior process shut down.
2717 usleep (500000);
2718 // If our process hasn't yet exited, debugserver might have died.
2719 // If the process did exit, the we are reaping it.
2720 const StateType state = process->GetState();
2721
2722 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2723 state != eStateInvalid &&
2724 state != eStateUnloaded &&
2725 state != eStateExited &&
2726 state != eStateDetached)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002727 {
Greg Claytone4e45922011-11-16 05:37:56 +00002728 char error_str[1024];
2729 if (signo)
2730 {
2731 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2732 if (signal_cstr)
2733 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2734 else
2735 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2736 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737 else
Greg Claytone4e45922011-11-16 05:37:56 +00002738 {
2739 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2740 }
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002741
Greg Claytone4e45922011-11-16 05:37:56 +00002742 process->SetExitStatus (-1, error_str);
2743 }
2744 // Debugserver has exited we need to let our ProcessGDBRemote
2745 // know that it no longer has a debugserver instance
2746 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002748 }
2749 return true;
2750}
2751
2752void
2753ProcessGDBRemote::KillDebugserverProcess ()
2754{
2755 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2756 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002757 Host::Kill (m_debugserver_pid, SIGINT);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002758 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2759 }
2760}
2761
2762void
2763ProcessGDBRemote::Initialize()
2764{
2765 static bool g_initialized = false;
2766
2767 if (g_initialized == false)
2768 {
2769 g_initialized = true;
2770 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2771 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +00002772 CreateInstance,
2773 DebuggerInitialize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002774
2775 Log::Callbacks log_callbacks = {
2776 ProcessGDBRemoteLog::DisableLog,
2777 ProcessGDBRemoteLog::EnableLog,
2778 ProcessGDBRemoteLog::ListLogCategories
2779 };
2780
2781 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2782 }
2783}
2784
Greg Clayton7f982402013-07-15 22:54:20 +00002785void
2786ProcessGDBRemote::DebuggerInitialize (lldb_private::Debugger &debugger)
2787{
2788 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
2789 {
2790 const bool is_global_setting = true;
2791 PluginManager::CreateSettingForProcessPlugin (debugger,
2792 GetGlobalPluginProperties()->GetValueProperties(),
2793 ConstString ("Properties for the gdb-remote process plug-in."),
2794 is_global_setting);
2795 }
2796}
2797
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002798bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002799ProcessGDBRemote::StartAsyncThread ()
2800{
Greg Clayton5160ce52013-03-27 23:08:40 +00002801 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002802
2803 if (log)
2804 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Jim Ingham455fa5c2012-11-01 01:15:33 +00002805
2806 Mutex::Locker start_locker(m_async_thread_state_mutex);
2807 if (m_async_thread_state == eAsyncThreadNotStarted)
2808 {
2809 // Create a thread that watches our internal state and controls which
2810 // events make it to clients (into the DCProcess event queue).
2811 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
2812 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
2813 {
2814 m_async_thread_state = eAsyncThreadRunning;
2815 return true;
2816 }
2817 else
2818 return false;
2819 }
2820 else
2821 {
2822 // Somebody tried to start the async thread while it was either being started or stopped. If the former, and
2823 // it started up successfully, then say all's well. Otherwise it is an error, since we aren't going to restart it.
2824 if (log)
2825 log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
2826 if (m_async_thread_state == eAsyncThreadRunning)
2827 return true;
2828 else
2829 return false;
2830 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002831}
2832
2833void
2834ProcessGDBRemote::StopAsyncThread ()
2835{
Greg Clayton5160ce52013-03-27 23:08:40 +00002836 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002837
2838 if (log)
2839 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2840
Jim Ingham455fa5c2012-11-01 01:15:33 +00002841 Mutex::Locker start_locker(m_async_thread_state_mutex);
2842 if (m_async_thread_state == eAsyncThreadRunning)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002843 {
Jim Ingham455fa5c2012-11-01 01:15:33 +00002844 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
2845
2846 // This will shut down the async thread.
2847 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
2848
2849 // Stop the stdio thread
2850 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
2851 {
2852 Host::ThreadJoin (m_async_thread, NULL, NULL);
2853 }
2854 m_async_thread_state = eAsyncThreadDone;
2855 }
2856 else
2857 {
2858 if (log)
2859 log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002860 }
2861}
2862
2863
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002864thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002865ProcessGDBRemote::AsyncThread (void *arg)
2866{
2867 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2868
Greg Clayton5160ce52013-03-27 23:08:40 +00002869 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002870 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002871 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002872
2873 Listener listener ("ProcessGDBRemote::AsyncThread");
2874 EventSP event_sp;
2875 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2876 eBroadcastBitAsyncThreadShouldExit;
2877
2878 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2879 {
Greg Clayton71337622011-02-24 22:24:29 +00002880 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2881
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002882 bool done = false;
2883 while (!done)
2884 {
2885 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002886 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002887 if (listener.WaitForEvent (NULL, event_sp))
2888 {
2889 const uint32_t event_type = event_sp->GetType();
Greg Clayton71337622011-02-24 22:24:29 +00002890 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002891 {
Greg Clayton71337622011-02-24 22:24:29 +00002892 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002893 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 +00002894
Greg Clayton71337622011-02-24 22:24:29 +00002895 switch (event_type)
2896 {
2897 case eBroadcastBitAsyncContinue:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002898 {
Greg Clayton71337622011-02-24 22:24:29 +00002899 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002900
Greg Clayton71337622011-02-24 22:24:29 +00002901 if (continue_packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902 {
Greg Clayton71337622011-02-24 22:24:29 +00002903 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2904 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2905 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002906 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002907
Greg Clayton71337622011-02-24 22:24:29 +00002908 if (::strstr (continue_cstr, "vAttach") == NULL)
2909 process->SetPrivateState(eStateRunning);
2910 StringExtractorGDBRemote response;
2911 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002912
Greg Clayton0772ded2012-05-16 02:48:06 +00002913 // We need to immediately clear the thread ID list so we are sure to get a valid list of threads.
2914 // The thread ID list might be contained within the "response", or the stop reply packet that
2915 // caused the stop. So clear it now before we give the stop reply packet to the process
2916 // using the process->SetLastStopPacket()...
2917 process->ClearThreadIDList ();
2918
Greg Clayton71337622011-02-24 22:24:29 +00002919 switch (stop_state)
2920 {
2921 case eStateStopped:
2922 case eStateCrashed:
2923 case eStateSuspended:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002924 process->SetLastStopPacket (response);
Greg Clayton71337622011-02-24 22:24:29 +00002925 process->SetPrivateState (stop_state);
2926 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002927
Greg Clayton71337622011-02-24 22:24:29 +00002928 case eStateExited:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002929 process->SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00002930 process->ClearThreadIDList();
Greg Clayton71337622011-02-24 22:24:29 +00002931 response.SetFilePos(1);
2932 process->SetExitStatus(response.GetHexU8(), NULL);
2933 done = true;
2934 break;
2935
2936 case eStateInvalid:
2937 process->SetExitStatus(-1, "lost connection");
2938 break;
2939
2940 default:
2941 process->SetPrivateState (stop_state);
2942 break;
2943 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002944 }
2945 }
Greg Clayton71337622011-02-24 22:24:29 +00002946 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002947
Greg Clayton71337622011-02-24 22:24:29 +00002948 case eBroadcastBitAsyncThreadShouldExit:
2949 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002950 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton71337622011-02-24 22:24:29 +00002951 done = true;
2952 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002953
Greg Clayton71337622011-02-24 22:24:29 +00002954 default:
2955 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002956 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 +00002957 done = true;
2958 break;
2959 }
2960 }
2961 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2962 {
2963 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2964 {
2965 process->SetExitStatus (-1, "lost connection");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002966 done = true;
Greg Clayton71337622011-02-24 22:24:29 +00002967 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002968 }
2969 }
2970 else
2971 {
2972 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002973 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 +00002974 done = true;
2975 }
2976 }
2977 }
2978
2979 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002980 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002981
2982 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2983 return NULL;
2984}
2985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002986const char *
2987ProcessGDBRemote::GetDispatchQueueNameForThread
2988(
2989 addr_t thread_dispatch_qaddr,
2990 std::string &dispatch_queue_name
2991)
2992{
2993 dispatch_queue_name.clear();
2994 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2995 {
2996 // Cache the dispatch_queue_offsets_addr value so we don't always have
2997 // to look it up
2998 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2999 {
Greg Clayton897f96a2010-10-12 17:33:06 +00003000 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
3001 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Claytonb9a01b32012-02-26 05:51:37 +00003002 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
3003 ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
Greg Clayton897f96a2010-10-12 17:33:06 +00003004 if (module_sp)
3005 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
3006
3007 if (dispatch_queue_offsets_symbol == NULL)
3008 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00003009 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
3010 module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
Greg Clayton897f96a2010-10-12 17:33:06 +00003011 if (module_sp)
3012 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
3013 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003014 if (dispatch_queue_offsets_symbol)
Greg Claytone7612132012-03-07 21:03:09 +00003015 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003016
3017 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
3018 return NULL;
3019 }
3020
3021 uint8_t memory_buffer[8];
Greg Clayton514487e2011-02-15 21:59:32 +00003022 DataExtractor data (memory_buffer,
3023 sizeof(memory_buffer),
3024 m_target.GetArchitecture().GetByteOrder(),
3025 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026
3027 // Excerpt from src/queue_private.h
3028 struct dispatch_queue_offsets_s
3029 {
3030 uint16_t dqo_version;
Jason Molendae424a9b2012-11-10 06:54:30 +00003031 uint16_t dqo_label; // in version 1-3, offset to string; in version 4+, offset to a pointer to a string
3032 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 +00003033 } dispatch_queue_offsets;
3034
3035
3036 Error error;
3037 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
3038 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003039 lldb::offset_t data_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
3041 {
3042 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
3043 {
3044 data_offset = 0;
3045 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
Jason Molendae424a9b2012-11-10 06:54:30 +00003046 if (dispatch_queue_offsets.dqo_version >= 4)
3047 {
3048 // libdispatch versions 4+, pointer to dispatch name is in the
3049 // queue structure.
3050 lldb::addr_t pointer_to_label_address = queue_addr + dispatch_queue_offsets.dqo_label;
3051 if (ReadMemory (pointer_to_label_address, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
3052 {
3053 data_offset = 0;
3054 lldb::addr_t label_addr = data.GetAddress(&data_offset);
3055 ReadCStringFromMemory (label_addr, dispatch_queue_name, error);
3056 }
3057 }
3058 else
3059 {
3060 // libdispatch versions 1-3, dispatch name is a fixed width char array
3061 // in the queue structure.
3062 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
3063 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
3064 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
3065 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
3066 dispatch_queue_name.erase (bytes_read);
3067 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 }
3069 }
3070 }
3071 }
3072 if (dispatch_queue_name.empty())
3073 return NULL;
3074 return dispatch_queue_name.c_str();
3075}
3076
Greg Claytone996fd32011-03-08 22:40:15 +00003077//uint32_t
3078//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3079//{
3080// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
3081// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
3082// if (m_local_debugserver)
3083// {
3084// return Host::ListProcessesMatchingName (name, matches, pids);
3085// }
3086// else
3087// {
3088// // FIXME: Implement talking to the remote debugserver.
3089// return 0;
3090// }
3091//
3092//}
3093//
Jim Ingham1c823b42011-01-22 01:33:44 +00003094bool
3095ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
3096 lldb_private::StoppointCallbackContext *context,
3097 lldb::user_id_t break_id,
3098 lldb::user_id_t break_loc_id)
3099{
3100 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
3101 // run so I can stop it if that's what I want to do.
Greg Clayton5160ce52013-03-27 23:08:40 +00003102 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham1c823b42011-01-22 01:33:44 +00003103 if (log)
3104 log->Printf("Hit New Thread Notification breakpoint.");
3105 return false;
3106}
3107
3108
3109bool
3110ProcessGDBRemote::StartNoticingNewThreads()
3111{
Greg Clayton5160ce52013-03-27 23:08:40 +00003112 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton4116e932012-05-15 02:33:01 +00003113 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003114 {
Greg Clayton4116e932012-05-15 02:33:01 +00003115 if (log && log->GetVerbose())
3116 log->Printf("Enabled noticing new thread breakpoint.");
3117 m_thread_create_bp_sp->SetEnabled(true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003118 }
Greg Clayton4116e932012-05-15 02:33:01 +00003119 else
Jim Ingham1c823b42011-01-22 01:33:44 +00003120 {
Greg Clayton4116e932012-05-15 02:33:01 +00003121 PlatformSP platform_sp (m_target.GetPlatform());
3122 if (platform_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003123 {
Greg Clayton4116e932012-05-15 02:33:01 +00003124 m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target);
3125 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003126 {
Jim Ingham37cfeab2011-10-15 00:21:37 +00003127 if (log && log->GetVerbose())
Greg Clayton4116e932012-05-15 02:33:01 +00003128 log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID());
3129 m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003130 }
3131 else
3132 {
3133 if (log)
3134 log->Printf("Failed to create new thread notification breakpoint.");
Jim Ingham1c823b42011-01-22 01:33:44 +00003135 }
3136 }
3137 }
Greg Clayton4116e932012-05-15 02:33:01 +00003138 return m_thread_create_bp_sp.get() != NULL;
Jim Ingham1c823b42011-01-22 01:33:44 +00003139}
3140
3141bool
3142ProcessGDBRemote::StopNoticingNewThreads()
3143{
Greg Clayton5160ce52013-03-27 23:08:40 +00003144 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham37cfeab2011-10-15 00:21:37 +00003145 if (log && log->GetVerbose())
Jim Ingham0e97cbc2011-02-08 05:19:01 +00003146 log->Printf ("Disabling new thread notification breakpoint.");
Greg Clayton4116e932012-05-15 02:33:01 +00003147
3148 if (m_thread_create_bp_sp)
3149 m_thread_create_bp_sp->SetEnabled(false);
3150
Jim Ingham1c823b42011-01-22 01:33:44 +00003151 return true;
3152}
3153
Jason Molenda5e8534e2012-10-03 01:29:34 +00003154lldb_private::DynamicLoader *
3155ProcessGDBRemote::GetDynamicLoader ()
3156{
3157 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +00003158 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Jason Molenda5e8534e2012-10-03 01:29:34 +00003159 return m_dyld_ap.get();
3160}
Jim Ingham1c823b42011-01-22 01:33:44 +00003161
Greg Clayton998255b2012-10-13 02:07:45 +00003162
Greg Clayton02686b82012-10-15 22:42:16 +00003163class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed
Greg Clayton998255b2012-10-13 02:07:45 +00003164{
3165private:
3166
3167public:
Greg Clayton02686b82012-10-15 22:42:16 +00003168 CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) :
Greg Clayton998255b2012-10-13 02:07:45 +00003169 CommandObjectParsed (interpreter,
Greg Clayton02686b82012-10-15 22:42:16 +00003170 "process plugin packet history",
3171 "Dumps the packet history buffer. ",
Greg Clayton998255b2012-10-13 02:07:45 +00003172 NULL)
3173 {
3174 }
3175
Greg Clayton02686b82012-10-15 22:42:16 +00003176 ~CommandObjectProcessGDBRemotePacketHistory ()
Greg Clayton998255b2012-10-13 02:07:45 +00003177 {
3178 }
3179
3180 bool
3181 DoExecute (Args& command, CommandReturnObject &result)
3182 {
Greg Clayton02686b82012-10-15 22:42:16 +00003183 const size_t argc = command.GetArgumentCount();
3184 if (argc == 0)
3185 {
3186 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3187 if (process)
3188 {
3189 process->GetGDBRemote().DumpHistory(result.GetOutputStream());
3190 result.SetStatus (eReturnStatusSuccessFinishResult);
3191 return true;
3192 }
3193 }
3194 else
3195 {
3196 result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str());
3197 }
3198 result.SetStatus (eReturnStatusFailed);
3199 return false;
3200 }
3201};
3202
3203class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed
3204{
3205private:
3206
3207public:
3208 CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) :
3209 CommandObjectParsed (interpreter,
3210 "process plugin packet send",
3211 "Send a custom packet through the GDB remote protocol and print the answer. "
3212 "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.",
3213 NULL)
3214 {
3215 }
3216
3217 ~CommandObjectProcessGDBRemotePacketSend ()
3218 {
3219 }
3220
3221 bool
3222 DoExecute (Args& command, CommandReturnObject &result)
3223 {
3224 const size_t argc = command.GetArgumentCount();
3225 if (argc == 0)
3226 {
3227 result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str());
3228 result.SetStatus (eReturnStatusFailed);
3229 return false;
3230 }
3231
3232 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3233 if (process)
3234 {
Han Ming Ong84145852012-11-26 20:42:03 +00003235 for (size_t i=0; i<argc; ++ i)
Greg Clayton02686b82012-10-15 22:42:16 +00003236 {
Han Ming Ong84145852012-11-26 20:42:03 +00003237 const char *packet_cstr = command.GetArgumentAtIndex(0);
3238 bool send_async = true;
3239 StringExtractorGDBRemote response;
3240 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3241 result.SetStatus (eReturnStatusSuccessFinishResult);
3242 Stream &output_strm = result.GetOutputStream();
3243 output_strm.Printf (" packet: %s\n", packet_cstr);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003244 std::string &response_str = response.GetStringRef();
3245
Han Ming Ong399289e2013-06-21 19:56:59 +00003246 if (strstr(packet_cstr, "qGetProfileData") != NULL)
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003247 {
3248 response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response);
3249 }
3250
Han Ming Ong84145852012-11-26 20:42:03 +00003251 if (response_str.empty())
3252 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3253 else
3254 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
Greg Clayton02686b82012-10-15 22:42:16 +00003255 }
Greg Clayton02686b82012-10-15 22:42:16 +00003256 }
Greg Clayton998255b2012-10-13 02:07:45 +00003257 return true;
3258 }
3259};
3260
Greg Claytonba4a0a52013-02-01 23:03:47 +00003261class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw
3262{
3263private:
3264
3265public:
3266 CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) :
3267 CommandObjectRaw (interpreter,
3268 "process plugin packet monitor",
Greg Claytoneee5e982013-02-14 18:39:30 +00003269 "Send a qRcmd packet through the GDB remote protocol and print the response."
3270 "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 +00003271 NULL)
3272 {
3273 }
3274
3275 ~CommandObjectProcessGDBRemotePacketMonitor ()
3276 {
3277 }
3278
3279 bool
3280 DoExecute (const char *command, CommandReturnObject &result)
3281 {
3282 if (command == NULL || command[0] == '\0')
3283 {
3284 result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str());
3285 result.SetStatus (eReturnStatusFailed);
3286 return false;
3287 }
3288
3289 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3290 if (process)
3291 {
3292 StreamString packet;
Greg Claytoneee5e982013-02-14 18:39:30 +00003293 packet.PutCString("qRcmd,");
Greg Claytonba4a0a52013-02-01 23:03:47 +00003294 packet.PutBytesAsRawHex8(command, strlen(command));
3295 const char *packet_cstr = packet.GetString().c_str();
3296
3297 bool send_async = true;
3298 StringExtractorGDBRemote response;
3299 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3300 result.SetStatus (eReturnStatusSuccessFinishResult);
3301 Stream &output_strm = result.GetOutputStream();
3302 output_strm.Printf (" packet: %s\n", packet_cstr);
3303 const std::string &response_str = response.GetStringRef();
3304
3305 if (response_str.empty())
3306 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3307 else
3308 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
3309 }
3310 return true;
3311 }
3312};
3313
Greg Clayton02686b82012-10-15 22:42:16 +00003314class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword
3315{
3316private:
3317
3318public:
3319 CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) :
3320 CommandObjectMultiword (interpreter,
3321 "process plugin packet",
3322 "Commands that deal with GDB remote packets.",
3323 NULL)
3324 {
3325 LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter)));
3326 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter)));
Greg Claytonba4a0a52013-02-01 23:03:47 +00003327 LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter)));
Greg Clayton02686b82012-10-15 22:42:16 +00003328 }
3329
3330 ~CommandObjectProcessGDBRemotePacket ()
3331 {
3332 }
3333};
Greg Clayton998255b2012-10-13 02:07:45 +00003334
3335class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword
3336{
3337public:
3338 CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) :
3339 CommandObjectMultiword (interpreter,
3340 "process plugin",
3341 "A set of commands for operating on a ProcessGDBRemote process.",
3342 "process plugin <subcommand> [<subcommand-options>]")
3343 {
3344 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter)));
3345 }
3346
3347 ~CommandObjectMultiwordProcessGDBRemote ()
3348 {
3349 }
3350};
3351
Greg Clayton998255b2012-10-13 02:07:45 +00003352CommandObject *
3353ProcessGDBRemote::GetPluginCommandObject()
3354{
3355 if (!m_command_sp)
3356 m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter()));
3357 return m_command_sp.get();
3358}