blob: 00cbb20aa815b1bb5739cc50b3ccefae1fb4bab5 [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
Sean Callanan224f6f52012-07-19 18:07:36 +000017#include <netinet/in.h>
Greg Clayton2a48f522011-05-14 01:50:35 +000018#include <sys/mman.h> // for mmap
Virgile Bellob2f1fb22013-08-23 12:44:05 +000019#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include <sys/stat.h>
Greg Clayton2a48f522011-05-14 01:50:35 +000021#include <sys/types.h>
Stephen Wilsondc916862011-03-30 00:12:40 +000022#include <time.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24// C++ Includes
25#include <algorithm>
26#include <map>
27
28// Other libraries and framework includes
29
Johnny Chen01a67862011-10-14 00:42:25 +000030#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000031#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/ArchSpec.h"
33#include "lldb/Core/Debugger.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000034#include "lldb/Host/ConnectionFileDescriptor.h"
Greg Clayton53239f02011-02-08 05:05:52 +000035#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Core/PluginManager.h"
39#include "lldb/Core/State.h"
Greg Claytond451c1a2012-04-13 21:24:18 +000040#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Core/StreamString.h"
42#include "lldb/Core/Timer.h"
Greg Clayton70b57652011-05-15 01:25:55 +000043#include "lldb/Core/Value.h"
Zachary Turner39de3112014-09-09 20:54:56 +000044#include "lldb/Host/HostThread.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000045#include "lldb/Host/StringConvert.h"
Jason Molendad1fae142012-09-29 08:03:33 +000046#include "lldb/Host/Symbols.h"
Zachary Turner39de3112014-09-09 20:54:56 +000047#include "lldb/Host/ThreadLauncher.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Host/TimeValue.h"
Greg Clayton02686b82012-10-15 22:42:16 +000049#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000050#include "lldb/Interpreter/CommandObject.h"
51#include "lldb/Interpreter/CommandObjectMultiword.h"
Greg Clayton02686b82012-10-15 22:42:16 +000052#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000053#include "lldb/Interpreter/OptionValueProperties.h"
54#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055#include "lldb/Symbol/ObjectFile.h"
56#include "lldb/Target/DynamicLoader.h"
57#include "lldb/Target/Target.h"
58#include "lldb/Target/TargetList.h"
Greg Clayton2a48f522011-05-14 01:50:35 +000059#include "lldb/Target/ThreadPlanCallFunction.h"
Jason Molenda705b1802014-06-13 02:37:02 +000060#include "lldb/Target/SystemRuntime.h"
Jason Molendaa34a0c62010-06-09 21:28:42 +000061#include "lldb/Utility/PseudoTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
63// Project includes
64#include "lldb/Host/Host.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000065#include "Plugins/Process/Utility/FreeBSDSignals.h"
Peter Collingbourne99f9aa02011-06-03 20:40:38 +000066#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000067#include "Plugins/Process/Utility/LinuxSignals.h"
Jason Molendac42d2432012-07-25 03:40:06 +000068#include "Plugins/Process/Utility/StopInfoMachException.h"
Jim Ingham43c555d2012-07-04 00:35:43 +000069#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
Greg Claytonc982c762010-07-09 20:39:50 +000070#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071#include "GDBRemoteRegisterContext.h"
72#include "ProcessGDBRemote.h"
73#include "ProcessGDBRemoteLog.h"
74#include "ThreadGDBRemote.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000075
Tamas Berghammerdb264a62015-03-31 09:52:22 +000076#define DEBUGSERVER_BASENAME "debugserver"
77using namespace lldb;
78using namespace lldb_private;
79using namespace lldb_private::process_gdb_remote;
Jason Molenda5e8534e2012-10-03 01:29:34 +000080
Greg Claytonc1422c12012-04-09 22:46:21 +000081namespace lldb
82{
83 // Provide a function that can easily dump the packet history if we know a
84 // ProcessGDBRemote * value (which we can get from logs or from debugging).
85 // We need the function in the lldb namespace so it makes it into the final
86 // executable since the LLDB shared library only exports stuff in the lldb
87 // namespace. This allows you to attach with a debugger and call this
88 // function and get the packet history dumped to a file.
89 void
90 DumpProcessGDBRemotePacketHistory (void *p, const char *path)
91 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +000092 StreamFile strm;
93 Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate));
Greg Claytond451c1a2012-04-13 21:24:18 +000094 if (error.Success())
95 ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +000096 }
Filipe Cabecinhasc34f7762012-05-23 16:27:09 +000097}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Greg Clayton7f982402013-07-15 22:54:20 +000099namespace {
Steve Pucci5ec012d2014-01-16 22:18:14 +0000100
Greg Clayton7f982402013-07-15 22:54:20 +0000101 static PropertyDefinition
102 g_properties[] =
103 {
104 { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." },
Greg Claytonef8180a2013-10-15 00:14:28 +0000105 { "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 +0000106 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
107 };
108
109 enum
110 {
Greg Claytonef8180a2013-10-15 00:14:28 +0000111 ePropertyPacketTimeout,
112 ePropertyTargetDefinitionFile
Greg Clayton7f982402013-07-15 22:54:20 +0000113 };
114
115 class PluginProperties : public Properties
116 {
117 public:
118
119 static ConstString
120 GetSettingName ()
121 {
122 return ProcessGDBRemote::GetPluginNameStatic();
123 }
124
125 PluginProperties() :
126 Properties ()
127 {
128 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
129 m_collection_sp->Initialize(g_properties);
130 }
131
132 virtual
133 ~PluginProperties()
134 {
135 }
136
137 uint64_t
138 GetPacketTimeout()
139 {
140 const uint32_t idx = ePropertyPacketTimeout;
141 return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
142 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000143
144 bool
145 SetPacketTimeout(uint64_t timeout)
146 {
147 const uint32_t idx = ePropertyPacketTimeout;
148 return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout);
149 }
150
Greg Claytonef8180a2013-10-15 00:14:28 +0000151 FileSpec
152 GetTargetDefinitionFile () const
153 {
154 const uint32_t idx = ePropertyTargetDefinitionFile;
155 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
156 }
Greg Clayton7f982402013-07-15 22:54:20 +0000157 };
158
159 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
160
161 static const ProcessKDPPropertiesSP &
162 GetGlobalPluginProperties()
163 {
164 static ProcessKDPPropertiesSP g_settings_sp;
165 if (!g_settings_sp)
166 g_settings_sp.reset (new PluginProperties ());
167 return g_settings_sp;
168 }
169
170} // anonymous namespace end
171
Greg Claytonfda4fab2014-01-10 22:24:11 +0000172// TODO Randomly assigning a port is unsafe. We should get an unused
173// ephemeral port from the kernel and make sure we reserve it before passing
174// it to debugserver.
175
176#if defined (__APPLE__)
177#define LOW_PORT (IPPORT_RESERVED)
178#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
179#else
180#define LOW_PORT (1024u)
181#define HIGH_PORT (49151u)
182#endif
183
Todd Fiala013434e2014-07-09 01:29:05 +0000184#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000185static bool rand_initialized = false;
186
Greg Claytonfda4fab2014-01-10 22:24:11 +0000187static inline uint16_t
188get_random_port ()
189{
190 if (!rand_initialized)
191 {
192 time_t seed = time(NULL);
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000193
Greg Claytonfda4fab2014-01-10 22:24:11 +0000194 rand_initialized = true;
195 srand(seed);
196 }
197 return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT;
198}
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000199#endif
Greg Claytonfda4fab2014-01-10 22:24:11 +0000200
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000201ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202ProcessGDBRemote::GetPluginNameStatic()
203{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000204 static ConstString g_name("gdb-remote");
205 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
208const char *
209ProcessGDBRemote::GetPluginDescriptionStatic()
210{
211 return "GDB Remote protocol based debugging plug-in.";
212}
213
214void
215ProcessGDBRemote::Terminate()
216{
217 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
218}
219
220
Greg Claytonc3776bf2012-02-09 06:16:32 +0000221lldb::ProcessSP
222ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223{
Greg Claytonc3776bf2012-02-09 06:16:32 +0000224 lldb::ProcessSP process_sp;
225 if (crash_file_path == NULL)
226 process_sp.reset (new ProcessGDBRemote (target, listener));
227 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
230bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000231ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232{
Greg Clayton596ed242011-10-21 21:41:45 +0000233 if (plugin_specified_by_name)
234 return true;
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +0000237 Module *exe_module = target.GetExecutableModulePointer();
238 if (exe_module)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000239 {
240 ObjectFile *exe_objfile = exe_module->GetObjectFile();
241 // We can't debug core files...
242 switch (exe_objfile->GetType())
243 {
244 case ObjectFile::eTypeInvalid:
245 case ObjectFile::eTypeCoreFile:
246 case ObjectFile::eTypeDebugInfo:
247 case ObjectFile::eTypeObjectFile:
248 case ObjectFile::eTypeSharedLibrary:
249 case ObjectFile::eTypeStubLibrary:
Greg Clayton23f8c952014-03-24 23:10:19 +0000250 case ObjectFile::eTypeJIT:
Greg Claytonc3776bf2012-02-09 06:16:32 +0000251 return false;
252 case ObjectFile::eTypeExecutable:
253 case ObjectFile::eTypeDynamicLinker:
254 case ObjectFile::eTypeUnknown:
255 break;
256 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000257 return exe_module->GetFileSpec().Exists();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000258 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000259 // However, if there is no executable module, we return true since we might be preparing to attach.
260 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261}
262
263//----------------------------------------------------------------------
264// ProcessGDBRemote constructor
265//----------------------------------------------------------------------
266ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
267 Process (target, listener),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268 m_flags (0),
Tamas Berghammere13c2732015-02-11 10:29:30 +0000269 m_gdb_comm (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Claytonc982c762010-07-09 20:39:50 +0000271 m_last_stop_packet (),
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000272 m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 m_register_info (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000274 m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
Jim Ingham455fa5c2012-11-01 01:15:33 +0000275 m_async_thread_state_mutex(Mutex::eMutexTypeRecursive),
Greg Clayton9e920902012-04-10 02:25:43 +0000276 m_thread_ids (),
Greg Clayton71fc2a32011-02-12 06:28:37 +0000277 m_continue_c_tids (),
278 m_continue_C_tids (),
279 m_continue_s_tids (),
280 m_continue_S_tids (),
Jason Molenda6076bf42014-05-06 04:34:52 +0000281 m_max_memory_size (0),
282 m_remote_stub_max_memory_size (0),
Greg Clayton4116e932012-05-15 02:33:01 +0000283 m_addr_to_mmap_size (),
284 m_thread_create_bp_sp (),
Jim Ingham43c555d2012-07-04 00:35:43 +0000285 m_waiting_for_attach (false),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000286 m_destroy_tried_resuming (false),
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000287 m_command_sp (),
Todd Fiala4ceced32014-08-29 17:35:57 +0000288 m_breakpoint_pc_offset (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289{
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000290 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
291 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Jim Inghamb1e2e842012-04-12 18:49:31 +0000292 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit");
Greg Clayton7f982402013-07-15 22:54:20 +0000293 const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
294 if (timeout_seconds > 0)
295 m_gdb_comm.SetPacketTimeout(timeout_seconds);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296}
297
298//----------------------------------------------------------------------
299// Destructor
300//----------------------------------------------------------------------
301ProcessGDBRemote::~ProcessGDBRemote()
302{
303 // m_mach_process.UnregisterNotificationCallbacks (this);
304 Clear();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000305 // We need to call finalize on the process before destroying ourselves
306 // to make sure all of the broadcaster cleanup goes as planned. If we
307 // destruct this class, then Process::~Process() might have problems
308 // trying to fully destroy the broadcaster.
309 Finalize();
Jim Ingham455fa5c2012-11-01 01:15:33 +0000310
311 // The general Finalize is going to try to destroy the process and that SHOULD
312 // shut down the async thread. However, if we don't kill it it will get stranded and
313 // its connection will go away so when it wakes up it will crash. So kill it for sure here.
314 StopAsyncThread();
315 KillDebugserverProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316}
317
318//----------------------------------------------------------------------
319// PluginInterface
320//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000321ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322ProcessGDBRemote::GetPluginName()
323{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 return GetPluginNameStatic();
325}
326
327uint32_t
328ProcessGDBRemote::GetPluginVersion()
329{
330 return 1;
331}
332
Greg Claytonef8180a2013-10-15 00:14:28 +0000333bool
334ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec)
335{
336 ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
337 Error error;
Zachary Turner0641ca12015-03-17 20:04:04 +0000338 StructuredData::ObjectSP module_object_sp(interpreter->LoadPluginModule(target_definition_fspec, error));
Greg Claytonef8180a2013-10-15 00:14:28 +0000339 if (module_object_sp)
340 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000341 StructuredData::DictionarySP target_definition_sp(
342 interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), "gdb-server-target-definition", error));
Greg Claytonef8180a2013-10-15 00:14:28 +0000343
Zachary Turner0641ca12015-03-17 20:04:04 +0000344 if (target_definition_sp)
Greg Claytonef8180a2013-10-15 00:14:28 +0000345 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000346 StructuredData::ObjectSP target_object(target_definition_sp->GetValueForKey("host-info"));
347 if (target_object)
Greg Clayton312bcbe2013-10-17 01:10:23 +0000348 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000349 if (auto host_info_dict = target_object->GetAsDictionary())
Greg Clayton312bcbe2013-10-17 01:10:23 +0000350 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000351 StructuredData::ObjectSP triple_value = host_info_dict->GetValueForKey("triple");
352 if (auto triple_string_value = triple_value->GetAsString())
353 {
354 std::string triple_string = triple_string_value->GetValue();
355 ArchSpec host_arch(triple_string.c_str());
356 if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture()))
357 {
358 GetTarget().SetArchitecture(host_arch);
359 }
360 }
Greg Clayton312bcbe2013-10-17 01:10:23 +0000361 }
Greg Clayton312bcbe2013-10-17 01:10:23 +0000362 }
Zachary Turner0641ca12015-03-17 20:04:04 +0000363 m_breakpoint_pc_offset = 0;
364 StructuredData::ObjectSP breakpoint_pc_offset_value = target_definition_sp->GetValueForKey("breakpoint-pc-offset");
365 if (breakpoint_pc_offset_value)
366 {
367 if (auto breakpoint_pc_int_value = breakpoint_pc_offset_value->GetAsInteger())
368 m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue();
369 }
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000370
Zachary Turner0641ca12015-03-17 20:04:04 +0000371 if (m_register_info.SetRegisterInfo(*target_definition_sp, GetTarget().GetArchitecture().GetByteOrder()) > 0)
Greg Claytonef8180a2013-10-15 00:14:28 +0000372 {
373 return true;
374 }
375 }
376 }
377 return false;
378}
379
380
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381void
Greg Clayton513c26c2011-01-29 07:10:55 +0000382ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383{
Greg Clayton513c26c2011-01-29 07:10:55 +0000384 if (!force && m_register_info.GetNumRegisters() > 0)
385 return;
386
387 char packet[128];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 m_register_info.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 uint32_t reg_offset = 0;
390 uint32_t reg_num = 0;
Greg Clayton23f59502012-07-17 03:23:13 +0000391 for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse;
Greg Clayton576d8832011-03-22 04:00:09 +0000392 response_type == StringExtractorGDBRemote::eResponse;
393 ++reg_num)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 {
Greg Clayton513c26c2011-01-29 07:10:55 +0000395 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
Andy Gibbsa297a972013-06-19 19:04:53 +0000396 assert (packet_len < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000398 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 {
Greg Clayton576d8832011-03-22 04:00:09 +0000400 response_type = response.GetResponseType();
401 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
403 std::string name;
404 std::string value;
405 ConstString reg_name;
406 ConstString alt_name;
407 ConstString set_name;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000408 std::vector<uint32_t> value_regs;
409 std::vector<uint32_t> invalidate_regs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 RegisterInfo reg_info = { NULL, // Name
411 NULL, // Alt name
412 0, // byte size
413 reg_offset, // offset
414 eEncodingUint, // encoding
415 eFormatHex, // formate
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 {
417 LLDB_INVALID_REGNUM, // GCC reg num
418 LLDB_INVALID_REGNUM, // DWARF reg num
419 LLDB_INVALID_REGNUM, // generic reg num
Jason Molendafbcb7f22010-09-10 07:49:16 +0000420 reg_num, // GDB reg num
421 reg_num // native register number
Greg Clayton435d85a2012-02-29 19:27:27 +0000422 },
423 NULL,
424 NULL
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 };
426
427 while (response.GetNameColonValue(name, value))
428 {
429 if (name.compare("name") == 0)
430 {
431 reg_name.SetCString(value.c_str());
432 }
433 else if (name.compare("alt-name") == 0)
434 {
435 alt_name.SetCString(value.c_str());
436 }
437 else if (name.compare("bitsize") == 0)
438 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000439 reg_info.byte_size = StringConvert::ToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 }
441 else if (name.compare("offset") == 0)
442 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000443 uint32_t offset = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda743e86a2010-06-11 23:44:18 +0000444 if (reg_offset != offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 {
446 reg_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 }
448 }
449 else if (name.compare("encoding") == 0)
450 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000451 const Encoding encoding = Args::StringToEncoding (value.c_str());
452 if (encoding != eEncodingInvalid)
453 reg_info.encoding = encoding;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 }
455 else if (name.compare("format") == 0)
456 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000457 Format format = eFormatInvalid;
458 if (Args::StringToFormat (value.c_str(), format, NULL).Success())
459 reg_info.format = format;
460 else if (value.compare("binary") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 reg_info.format = eFormatBinary;
462 else if (value.compare("decimal") == 0)
463 reg_info.format = eFormatDecimal;
464 else if (value.compare("hex") == 0)
465 reg_info.format = eFormatHex;
466 else if (value.compare("float") == 0)
467 reg_info.format = eFormatFloat;
468 else if (value.compare("vector-sint8") == 0)
469 reg_info.format = eFormatVectorOfSInt8;
470 else if (value.compare("vector-uint8") == 0)
471 reg_info.format = eFormatVectorOfUInt8;
472 else if (value.compare("vector-sint16") == 0)
473 reg_info.format = eFormatVectorOfSInt16;
474 else if (value.compare("vector-uint16") == 0)
475 reg_info.format = eFormatVectorOfUInt16;
476 else if (value.compare("vector-sint32") == 0)
477 reg_info.format = eFormatVectorOfSInt32;
478 else if (value.compare("vector-uint32") == 0)
479 reg_info.format = eFormatVectorOfUInt32;
480 else if (value.compare("vector-float32") == 0)
481 reg_info.format = eFormatVectorOfFloat32;
482 else if (value.compare("vector-uint128") == 0)
483 reg_info.format = eFormatVectorOfUInt128;
484 }
485 else if (name.compare("set") == 0)
486 {
487 set_name.SetCString(value.c_str());
488 }
489 else if (name.compare("gcc") == 0)
490 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000491 reg_info.kinds[eRegisterKindGCC] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 }
493 else if (name.compare("dwarf") == 0)
494 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000495 reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 }
497 else if (name.compare("generic") == 0)
498 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000499 reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000501 else if (name.compare("container-regs") == 0)
502 {
503 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
504 value_pair.second = value;
505 do
506 {
507 value_pair = value_pair.second.split(',');
508 if (!value_pair.first.empty())
509 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000510 uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
Greg Clayton0ba20242013-01-21 23:32:42 +0000511 if (reg != LLDB_INVALID_REGNUM)
512 value_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000513 }
514 } while (!value_pair.second.empty());
515 }
516 else if (name.compare("invalidate-regs") == 0)
517 {
518 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
519 value_pair.second = value;
520 do
521 {
522 value_pair = value_pair.second.split(',');
523 if (!value_pair.first.empty())
524 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000525 uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
Greg Clayton0ba20242013-01-21 23:32:42 +0000526 if (reg != LLDB_INVALID_REGNUM)
527 invalidate_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000528 }
529 } while (!value_pair.second.empty());
530 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532
Jason Molenda743e86a2010-06-11 23:44:18 +0000533 reg_info.byte_offset = reg_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 assert (reg_info.byte_size != 0);
535 reg_offset += reg_info.byte_size;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000536 if (!value_regs.empty())
537 {
538 value_regs.push_back(LLDB_INVALID_REGNUM);
539 reg_info.value_regs = value_regs.data();
540 }
541 if (!invalidate_regs.empty())
542 {
543 invalidate_regs.push_back(LLDB_INVALID_REGNUM);
544 reg_info.invalidate_regs = invalidate_regs.data();
545 }
546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
548 }
Todd Fiala1a634402014-01-08 07:52:40 +0000549 else
550 {
551 break; // ensure exit before reg_num is incremented
552 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 }
554 else
555 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000556 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 }
558 }
559
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000560 // Check if qHostInfo specified a specific packet timeout for this connection.
561 // If so then lets update our setting so the user knows what the timeout is
562 // and can see it.
563 const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout();
564 if (host_packet_timeout)
565 {
566 GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout);
567 }
568
569
Greg Claytonef8180a2013-10-15 00:14:28 +0000570 if (reg_num == 0)
571 {
572 FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile ();
Greg Clayton312bcbe2013-10-17 01:10:23 +0000573
574 if (target_definition_fspec)
Greg Claytonef8180a2013-10-15 00:14:28 +0000575 {
Greg Clayton312bcbe2013-10-17 01:10:23 +0000576 // See if we can get register definitions from a python file
577 if (ParsePythonTargetDefinition (target_definition_fspec))
578 return;
Greg Claytonef8180a2013-10-15 00:14:28 +0000579 }
580 }
581
Johnny Chen2fa9de12012-05-14 18:44:23 +0000582 // We didn't get anything if the accumulated reg_num is zero. See if we are
583 // debugging ARM and fill with a hard coded register set until we can get an
584 // updated debugserver down on the devices.
585 // On the other hand, if the accumulated reg_num is positive, see if we can
586 // add composite registers to the existing primordial ones.
587 bool from_scratch = (reg_num == 0);
588
589 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000590 const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture();
591 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
592
593 // Use the process' architecture instead of the host arch, if available
594 ArchSpec remote_arch;
595 if (remote_process_arch.IsValid ())
596 remote_arch = remote_process_arch;
597 else
598 remote_arch = remote_host_arch;
599
Johnny Chen2fa9de12012-05-14 18:44:23 +0000600 if (!target_arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 {
Johnny Chen2fa9de12012-05-14 18:44:23 +0000602 if (remote_arch.IsValid()
603 && remote_arch.GetMachine() == llvm::Triple::arm
604 && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
605 m_register_info.HardcodeARMRegisters(from_scratch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 }
Johnny Chen2fa9de12012-05-14 18:44:23 +0000607 else if (target_arch.GetMachine() == llvm::Triple::arm)
608 {
609 m_register_info.HardcodeARMRegisters(from_scratch);
610 }
611
612 // At this point, we can finalize our register info.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 m_register_info.Finalize ();
614}
615
616Error
617ProcessGDBRemote::WillLaunch (Module* module)
618{
619 return WillLaunchOrAttach ();
620}
621
622Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000623ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624{
625 return WillLaunchOrAttach ();
626}
627
628Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000629ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630{
631 return WillLaunchOrAttach ();
632}
633
634Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000635ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +0000636{
Todd Fialaaf245d12014-06-30 21:05:18 +0000637 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonb766a732011-02-04 01:58:07 +0000638 Error error (WillLaunchOrAttach ());
639
640 if (error.Fail())
641 return error;
642
Greg Clayton2289fa42011-04-30 01:09:13 +0000643 error = ConnectToDebugserver (remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000644
645 if (error.Fail())
646 return error;
647 StartAsyncThread ();
648
Greg Claytonc574ede2011-03-10 02:26:48 +0000649 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonb766a732011-02-04 01:58:07 +0000650 if (pid == LLDB_INVALID_PROCESS_ID)
651 {
652 // We don't have a valid process ID, so note that we are connected
653 // and could now request to launch or attach, or get remote process
654 // listings...
655 SetPrivateState (eStateConnected);
656 }
657 else
658 {
659 // We have a valid process
660 SetID (pid);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000661 GetThreadList();
Greg Clayton3dedae12013-12-06 21:45:27 +0000662 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success)
Greg Claytonb766a732011-02-04 01:58:07 +0000663 {
Jason Molendac62bd7b2013-12-21 05:20:36 +0000664 if (!m_target.GetArchitecture().IsValid())
665 {
666 if (m_gdb_comm.GetProcessArchitecture().IsValid())
667 {
668 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
669 }
670 else
671 {
672 m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture());
673 }
Carlo Kok74389122013-10-14 07:09:13 +0000674 }
675
Greg Claytondd0e5a52011-06-02 22:22:38 +0000676 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytonb766a732011-02-04 01:58:07 +0000677 if (state == eStateStopped)
678 {
679 SetPrivateState (state);
680 }
681 else
Daniel Malead01b2952012-11-29 21:49:15 +0000682 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 +0000683 }
684 else
Daniel Malead01b2952012-11-29 21:49:15 +0000685 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 +0000686 }
Jason Molenda16d127c2012-05-03 22:37:30 +0000687
Todd Fialaaf245d12014-06-30 21:05:18 +0000688 if (log)
689 log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalizing target architecture initial triple: %s (GetTarget().GetArchitecture().IsValid() %s, m_gdb_comm.GetHostArchitecture().IsValid(): %s)", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str (), GetTarget ().GetArchitecture ().IsValid () ? "true" : "false", m_gdb_comm.GetHostArchitecture ().IsValid () ? "true" : "false");
690
691
692 if (error.Success()
Jason Molenda16d127c2012-05-03 22:37:30 +0000693 && !GetTarget().GetArchitecture().IsValid()
694 && m_gdb_comm.GetHostArchitecture().IsValid())
695 {
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000696 // Prefer the *process'* architecture over that of the *host*, if available.
697 if (m_gdb_comm.GetProcessArchitecture().IsValid())
698 GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture());
699 else
700 GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture());
Jason Molenda16d127c2012-05-03 22:37:30 +0000701 }
702
Todd Fialaaf245d12014-06-30 21:05:18 +0000703 if (log)
704 log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalized target architecture triple: %s", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str ());
705
706 // Set the Unix signals properly for the target.
707 // FIXME Add a gdb-remote packet to discover dynamically.
708 if (error.Success ())
709 {
Tamas Berghammerac839822015-03-04 11:34:10 +0000710 const ArchSpec arch_spec = m_gdb_comm.GetHostArchitecture();
Todd Fialaaf245d12014-06-30 21:05:18 +0000711 if (arch_spec.IsValid ())
712 {
713 if (log)
714 log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": determining unix signals type based on architecture %s, triple %s", __FUNCTION__, GetID (), arch_spec.GetArchitectureName () ? arch_spec.GetArchitectureName () : "<null>", arch_spec.GetTriple ().getTriple ().c_str ());
715
716 switch (arch_spec.GetTriple ().getOS ())
717 {
718 case llvm::Triple::Linux:
Todd Fiala4ceced32014-08-29 17:35:57 +0000719 SetUnixSignals (UnixSignalsSP (new process_linux::LinuxSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000720 if (log)
721 log->Printf ("ProcessGDBRemote::%s using Linux unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
722 break;
723 case llvm::Triple::OpenBSD:
724 case llvm::Triple::FreeBSD:
725 case llvm::Triple::NetBSD:
Todd Fiala4ceced32014-08-29 17:35:57 +0000726 SetUnixSignals (UnixSignalsSP (new FreeBSDSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000727 if (log)
728 log->Printf ("ProcessGDBRemote::%s using *BSD unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
729 break;
730 default:
Todd Fiala4ceced32014-08-29 17:35:57 +0000731 SetUnixSignals (UnixSignalsSP (new UnixSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000732 if (log)
733 log->Printf ("ProcessGDBRemote::%s using generic unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
734 break;
735 }
736 }
737 }
738
Greg Claytonb766a732011-02-04 01:58:07 +0000739 return error;
740}
741
742Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743ProcessGDBRemote::WillLaunchOrAttach ()
744{
745 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 m_stdio_communication.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 return error;
748}
749
750//----------------------------------------------------------------------
751// Process Control
752//----------------------------------------------------------------------
753Error
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000754ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755{
Todd Fiala75f47c32014-10-11 21:42:09 +0000756 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton4957bf62010-09-30 21:49:03 +0000757 Error error;
Greg Clayton982c9762011-11-03 21:22:33 +0000758
Todd Fiala75f47c32014-10-11 21:42:09 +0000759 if (log)
760 log->Printf ("ProcessGDBRemote::%s() entered", __FUNCTION__);
761
Greg Clayton982c9762011-11-03 21:22:33 +0000762 uint32_t launch_flags = launch_info.GetFlags().Get();
763 const char *stdin_path = NULL;
764 const char *stdout_path = NULL;
765 const char *stderr_path = NULL;
766 const char *working_dir = launch_info.GetWorkingDirectory();
767
Zachary Turner696b5282014-08-14 16:01:25 +0000768 const FileAction *file_action;
Greg Clayton982c9762011-11-03 21:22:33 +0000769 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
770 if (file_action)
771 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000772 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000773 stdin_path = file_action->GetPath();
774 }
775 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
776 if (file_action)
777 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000778 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000779 stdout_path = file_action->GetPath();
780 }
781 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
782 if (file_action)
783 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000784 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000785 stderr_path = file_action->GetPath();
786 }
787
Todd Fiala75f47c32014-10-11 21:42:09 +0000788 if (log)
789 {
790 if (stdin_path || stdout_path || stderr_path)
Vince Harron4a8abd32015-02-13 19:15:24 +0000791 log->Printf ("ProcessGDBRemote::%s provided with STDIO paths via launch_info: stdin=%s, stdout=%s, stderr=%s",
Todd Fiala75f47c32014-10-11 21:42:09 +0000792 __FUNCTION__,
793 stdin_path ? stdin_path : "<null>",
794 stdout_path ? stdout_path : "<null>",
795 stderr_path ? stderr_path : "<null>");
796 else
797 log->Printf ("ProcessGDBRemote::%s no STDIO paths given via launch_info", __FUNCTION__);
798 }
799
Vince Harrondf3f00f2015-02-10 21:09:04 +0000800 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
801 if (stdin_path || disable_stdio)
802 {
803 // the inferior will be reading stdin from the specified file
804 // or stdio is completely disabled
805 m_stdin_forward = false;
806 }
807 else
808 {
809 m_stdin_forward = true;
810 }
811
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
813 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
814 // ::LogSetLogFile ("/dev/stdout");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815
Greg Clayton982c9762011-11-03 21:22:33 +0000816 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 if (object_file)
818 {
Greg Clayton71337622011-02-24 22:24:29 +0000819 // Make sure we aren't already connected?
820 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 {
Greg Clayton91a9b2472013-12-04 19:19:12 +0000822 error = LaunchAndConnectToDebugserver (launch_info);
Greg Clayton71337622011-02-24 22:24:29 +0000823 }
824
825 if (error.Success())
826 {
827 lldb_utility::PseudoTerminal pty;
828 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000829
Greg Claytonf58c2692011-06-24 22:32:10 +0000830 PlatformSP platform_sp (m_target.GetPlatform());
Vince Harrondf3f00f2015-02-10 21:09:04 +0000831 if (disable_stdio)
Greg Clayton71337622011-02-24 22:24:29 +0000832 {
Vince Harrondf3f00f2015-02-10 21:09:04 +0000833 // set to /dev/null unless redirected to a file above
834 if (!stdin_path)
835 stdin_path = "/dev/null";
836 if (!stdout_path)
837 stdout_path = "/dev/null";
838 if (!stderr_path)
839 stderr_path = "/dev/null";
840 }
841 else if (platform_sp && platform_sp->IsHost())
842 {
843 // If the debugserver is local and we aren't disabling STDIO, lets use
844 // a pseudo terminal to instead of relying on the 'O' packets for stdio
845 // since 'O' packets can really slow down debugging if the inferior
846 // does a lot of output.
Greg Clayton71337622011-02-24 22:24:29 +0000847 const char *slave_name = NULL;
848 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 {
Greg Clayton71337622011-02-24 22:24:29 +0000850 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
851 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 }
Greg Clayton71337622011-02-24 22:24:29 +0000853 if (stdin_path == NULL)
854 stdin_path = slave_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855
Greg Clayton71337622011-02-24 22:24:29 +0000856 if (stdout_path == NULL)
857 stdout_path = slave_name;
858
859 if (stderr_path == NULL)
860 stderr_path = slave_name;
Todd Fiala75f47c32014-10-11 21:42:09 +0000861
862 if (log)
Vince Harron4a8abd32015-02-13 19:15:24 +0000863 log->Printf ("ProcessGDBRemote::%s adjusted STDIO paths for local platform (IsHost() is true) using slave: stdin=%s, stdout=%s, stderr=%s",
Todd Fiala75f47c32014-10-11 21:42:09 +0000864 __FUNCTION__,
865 stdin_path ? stdin_path : "<null>",
866 stdout_path ? stdout_path : "<null>",
867 stderr_path ? stderr_path : "<null>");
Greg Clayton71337622011-02-24 22:24:29 +0000868 }
869
Todd Fiala75f47c32014-10-11 21:42:09 +0000870 if (log)
Vince Harron4a8abd32015-02-13 19:15:24 +0000871 log->Printf ("ProcessGDBRemote::%s final STDIO paths after all adjustments: stdin=%s, stdout=%s, stderr=%s",
Todd Fiala75f47c32014-10-11 21:42:09 +0000872 __FUNCTION__,
873 stdin_path ? stdin_path : "<null>",
874 stdout_path ? stdout_path : "<null>",
875 stderr_path ? stderr_path : "<null>");
876
877 if (stdin_path)
Greg Clayton71337622011-02-24 22:24:29 +0000878 m_gdb_comm.SetSTDIN (stdin_path);
879 if (stdout_path)
880 m_gdb_comm.SetSTDOUT (stdout_path);
881 if (stderr_path)
882 m_gdb_comm.SetSTDERR (stderr_path);
883
884 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
Jim Ingham106d0282014-06-25 02:32:56 +0000885 m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError);
Greg Clayton71337622011-02-24 22:24:29 +0000886
Greg Claytonc4103b32011-05-08 04:53:50 +0000887 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Clayton71337622011-02-24 22:24:29 +0000888
Jason Molendaa3329782014-03-29 18:54:20 +0000889 const char * launch_event_data = launch_info.GetLaunchEventData();
890 if (launch_event_data != NULL && *launch_event_data != '\0')
891 m_gdb_comm.SendLaunchEventDataPacket (launch_event_data);
892
Greg Clayton71337622011-02-24 22:24:29 +0000893 if (working_dir && working_dir[0])
894 {
895 m_gdb_comm.SetWorkingDir (working_dir);
896 }
897
898 // Send the environment and the program + arguments after we connect
Greg Clayton982c9762011-11-03 21:22:33 +0000899 const Args &environment = launch_info.GetEnvironmentEntries();
900 if (environment.GetArgumentCount())
Greg Clayton71337622011-02-24 22:24:29 +0000901 {
Greg Clayton982c9762011-11-03 21:22:33 +0000902 size_t num_environment_entries = environment.GetArgumentCount();
903 for (size_t i=0; i<num_environment_entries; ++i)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000904 {
Greg Clayton982c9762011-11-03 21:22:33 +0000905 const char *env_entry = environment.GetArgumentAtIndex(i);
906 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Clayton71337622011-02-24 22:24:29 +0000907 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000908 }
Greg Clayton71337622011-02-24 22:24:29 +0000909 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000910
Greg Clayton71337622011-02-24 22:24:29 +0000911 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000912 // Scope for the scoped timeout object
913 GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10);
914
915 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info);
916 if (arg_packet_err == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000918 std::string error_str;
919 if (m_gdb_comm.GetLaunchSuccess (error_str))
920 {
921 SetID (m_gdb_comm.GetCurrentProcessID ());
922 }
923 else
924 {
925 error.SetErrorString (error_str.c_str());
926 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 }
928 else
929 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000930 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 }
Greg Clayton71337622011-02-24 22:24:29 +0000932 }
Tamas Berghammer912800c2015-02-24 10:23:39 +0000933
Greg Clayton71337622011-02-24 22:24:29 +0000934 if (GetID() == LLDB_INVALID_PROCESS_ID)
935 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000936 if (log)
937 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton71337622011-02-24 22:24:29 +0000938 KillDebugserverProcess ();
939 return error;
940 }
941
Greg Clayton3dedae12013-12-06 21:45:27 +0000942 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success)
Greg Clayton71337622011-02-24 22:24:29 +0000943 {
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +0000944 const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture();
945
946 if (process_arch.IsValid())
Jason Molendac62bd7b2013-12-21 05:20:36 +0000947 {
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +0000948 m_target.MergeArchitecture(process_arch);
949 }
950 else
951 {
952 const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture();
953 if (host_arch.IsValid())
954 m_target.MergeArchitecture(host_arch);
Carlo Kok74389122013-10-14 07:09:13 +0000955 }
956
Greg Claytondd0e5a52011-06-02 22:22:38 +0000957 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Clayton71337622011-02-24 22:24:29 +0000958
959 if (!disable_stdio)
960 {
961 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Claytonee95ed52011-11-17 22:14:31 +0000962 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Clayton71337622011-02-24 22:24:29 +0000963 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964 }
965 }
Greg Claytonc235ac72011-08-09 05:20:29 +0000966 else
967 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000968 if (log)
969 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytonc235ac72011-08-09 05:20:29 +0000970 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 }
972 else
973 {
974 // Set our user ID to an invalid process ID.
975 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton982c9762011-11-03 21:22:33 +0000976 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
977 exe_module->GetFileSpec().GetFilename().AsCString(),
978 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 return error;
Greg Clayton4957bf62010-09-30 21:49:03 +0000981
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982}
983
984
985Error
Greg Claytonb766a732011-02-04 01:58:07 +0000986ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987{
988 Error error;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000989 // Only connect if we have a valid connect URL
Vince Harron1b5a74e2015-01-21 22:42:49 +0000990 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton00fe87b2013-12-05 22:58:22 +0000991
992 if (connect_url && connect_url[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993 {
Vince Harron1b5a74e2015-01-21 22:42:49 +0000994 if (log)
995 log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, connect_url);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000996 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
997 if (conn_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000999 const uint32_t max_retry_count = 50;
1000 uint32_t retry_count = 0;
1001 while (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002 {
Greg Clayton00fe87b2013-12-05 22:58:22 +00001003 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
1004 {
1005 m_gdb_comm.SetConnection (conn_ap.release());
1006 break;
1007 }
1008 else if (error.WasInterrupted())
1009 {
1010 // If we were interrupted, don't keep retrying.
1011 break;
1012 }
1013
1014 retry_count++;
1015
1016 if (retry_count >= max_retry_count)
1017 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018
Greg Clayton00fe87b2013-12-05 22:58:22 +00001019 usleep (100000);
1020 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 }
1022 }
1023
1024 if (!m_gdb_comm.IsConnected())
1025 {
1026 if (error.Success())
1027 error.SetErrorString("not connected to remote gdb server");
1028 return error;
1029 }
1030
Greg Clayton32e0a752011-03-30 18:16:51 +00001031 // We always seem to be able to open a connection to a local port
1032 // so we need to make sure we can then send data to it. If we can't
1033 // then we aren't actually connected to anything, so try and do the
1034 // handshake with the remote GDB server and make sure that goes
1035 // alright.
Greg Claytonfb909312013-11-23 01:58:15 +00001036 if (!m_gdb_comm.HandshakeWithServer (&error))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001038 m_gdb_comm.Disconnect();
1039 if (error.Success())
1040 error.SetErrorString("not connected to remote gdb server");
1041 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001043 m_gdb_comm.GetThreadSuffixSupported ();
Greg Clayton44633992012-04-10 03:22:03 +00001044 m_gdb_comm.GetListThreadsInStopReplySupported ();
Greg Clayton32e0a752011-03-30 18:16:51 +00001045 m_gdb_comm.GetHostInfo ();
1046 m_gdb_comm.GetVContSupported ('c');
Jim Inghamcd16df92012-07-20 21:37:13 +00001047 m_gdb_comm.GetVAttachOrWaitSupported();
Jim Ingham03afad82012-07-02 05:40:07 +00001048
1049 size_t num_cmds = GetExtraStartupCommands().GetArgumentCount();
1050 for (size_t idx = 0; idx < num_cmds; idx++)
1051 {
1052 StringExtractorGDBRemote response;
Jim Ingham03afad82012-07-02 05:40:07 +00001053 m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false);
1054 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055 return error;
1056}
1057
1058void
Jim Inghambb006ce2014-08-02 00:33:35 +00001059ProcessGDBRemote::DidLaunchOrAttach (ArchSpec& process_arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060{
Greg Clayton5160ce52013-03-27 23:08:40 +00001061 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001062 if (log)
1063 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton93d3c8332011-02-16 04:46:07 +00001064 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001066 BuildDynamicRegisterInfo (false);
Greg Clayton3af9ea52010-11-18 05:57:03 +00001067
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 // See if the GDB server supports the qHostInfo information
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001069
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001070
1071 // See if the GDB server supports the qProcessInfo packet, if so
1072 // prefer that over the Host information as it will be more specific
1073 // to our process.
1074
Todd Fiala75f47c32014-10-11 21:42:09 +00001075 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
1076 if (remote_process_arch.IsValid())
1077 {
1078 process_arch = remote_process_arch;
1079 if (log)
1080 log->Printf ("ProcessGDBRemote::%s gdb-remote had process architecture, using %s %s",
1081 __FUNCTION__,
1082 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1083 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
1084 }
Jim Inghambb006ce2014-08-02 00:33:35 +00001085 else
Todd Fiala75f47c32014-10-11 21:42:09 +00001086 {
Jim Inghambb006ce2014-08-02 00:33:35 +00001087 process_arch = m_gdb_comm.GetHostArchitecture();
Todd Fiala75f47c32014-10-11 21:42:09 +00001088 if (log)
1089 log->Printf ("ProcessGDBRemote::%s gdb-remote did not have process architecture, using gdb-remote host architecture %s %s",
1090 __FUNCTION__,
1091 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1092 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
1093 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001094
Jim Inghambb006ce2014-08-02 00:33:35 +00001095 if (process_arch.IsValid())
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001096 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001097 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Greg Claytond314e812011-03-23 00:09:55 +00001098 if (target_arch.IsValid())
1099 {
Todd Fiala75f47c32014-10-11 21:42:09 +00001100 if (log)
1101 log->Printf ("ProcessGDBRemote::%s analyzing target arch, currently %s %s",
1102 __FUNCTION__,
1103 target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>",
1104 target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>");
1105
1106 // If the remote host is ARM and we have apple as the vendor, then
Greg Claytond314e812011-03-23 00:09:55 +00001107 // ARM executables and shared libraries can have mixed ARM architectures.
1108 // You can have an armv6 executable, and if the host is armv7, then the
1109 // system will load the best possible architecture for all shared libraries
1110 // it has, so we really need to take the remote host architecture as our
1111 // defacto architecture in this case.
1112
Jim Inghambb006ce2014-08-02 00:33:35 +00001113 if (process_arch.GetMachine() == llvm::Triple::arm &&
1114 process_arch.GetTriple().getVendor() == llvm::Triple::Apple)
Greg Claytond314e812011-03-23 00:09:55 +00001115 {
Jason Molenda921c01b2014-08-03 21:42:52 +00001116 GetTarget().SetArchitecture (process_arch);
Todd Fiala75f47c32014-10-11 21:42:09 +00001117 if (log)
1118 log->Printf ("ProcessGDBRemote::%s remote process is ARM/Apple, setting target arch to %s %s",
1119 __FUNCTION__,
1120 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1121 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
Greg Claytond314e812011-03-23 00:09:55 +00001122 }
1123 else
1124 {
1125 // Fill in what is missing in the triple
Jim Inghambb006ce2014-08-02 00:33:35 +00001126 const llvm::Triple &remote_triple = process_arch.GetTriple();
Tamas Berghammere724af12015-03-13 10:32:37 +00001127 llvm::Triple new_target_triple = target_arch.GetTriple();
1128 if (new_target_triple.getVendorName().size() == 0)
Greg Clayton70b57652011-05-15 01:25:55 +00001129 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001130 new_target_triple.setVendor (remote_triple.getVendor());
Greg Claytond314e812011-03-23 00:09:55 +00001131
Tamas Berghammere724af12015-03-13 10:32:37 +00001132 if (new_target_triple.getOSName().size() == 0)
Greg Clayton70b57652011-05-15 01:25:55 +00001133 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001134 new_target_triple.setOS (remote_triple.getOS());
Greg Claytond314e812011-03-23 00:09:55 +00001135
Tamas Berghammere724af12015-03-13 10:32:37 +00001136 if (new_target_triple.getEnvironmentName().size() == 0)
1137 new_target_triple.setEnvironment (remote_triple.getEnvironment());
Greg Clayton70b57652011-05-15 01:25:55 +00001138 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001139
Tamas Berghammere724af12015-03-13 10:32:37 +00001140 ArchSpec new_target_arch = target_arch;
1141 new_target_arch.SetTriple(new_target_triple);
1142 GetTarget().SetArchitecture(new_target_arch);
1143 }
Greg Claytond314e812011-03-23 00:09:55 +00001144 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001145
1146 if (log)
1147 log->Printf ("ProcessGDBRemote::%s final target arch after adjustments for remote architecture: %s %s",
1148 __FUNCTION__,
1149 target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>",
1150 target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>");
Greg Claytond314e812011-03-23 00:09:55 +00001151 }
1152 else
1153 {
1154 // The target doesn't have a valid architecture yet, set it from
1155 // the architecture we got from the remote GDB server
Jason Molenda921c01b2014-08-03 21:42:52 +00001156 GetTarget().SetArchitecture (process_arch);
Greg Claytond314e812011-03-23 00:09:55 +00001157 }
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001158 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 }
1160}
1161
1162void
1163ProcessGDBRemote::DidLaunch ()
1164{
Jim Inghambb006ce2014-08-02 00:33:35 +00001165 ArchSpec process_arch;
1166 DidLaunchOrAttach (process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167}
1168
1169Error
Greg Claytonc982c762010-07-09 20:39:50 +00001170ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171{
Han Ming Ong84647042012-02-25 01:07:38 +00001172 ProcessAttachInfo attach_info;
1173 return DoAttachToProcessWithID(attach_pid, attach_info);
1174}
1175
1176Error
1177ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
1178{
Todd Fiala75f47c32014-10-11 21:42:09 +00001179 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 Error error;
Todd Fiala75f47c32014-10-11 21:42:09 +00001181
1182 if (log)
1183 log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__);
1184
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 // Clear out and clean up from any current state
1186 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 if (attach_pid != LLDB_INVALID_PROCESS_ID)
1188 {
Greg Clayton71337622011-02-24 22:24:29 +00001189 // Make sure we aren't already connected?
1190 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 {
Greg Clayton91a9b2472013-12-04 19:19:12 +00001192 error = LaunchAndConnectToDebugserver (attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001193
1194 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 {
Greg Clayton71337622011-02-24 22:24:29 +00001196 const char *error_string = error.AsCString();
1197 if (error_string == NULL)
1198 error_string = "unable to launch " DEBUGSERVER_BASENAME;
1199
1200 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 }
Greg Clayton71337622011-02-24 22:24:29 +00001202 }
1203
1204 if (error.Success())
1205 {
Jim Ingham106d0282014-06-25 02:32:56 +00001206 m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError());
Todd Fiala75f47c32014-10-11 21:42:09 +00001207
Greg Clayton71337622011-02-24 22:24:29 +00001208 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001209 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid);
Greg Clayton3b608422011-11-19 02:11:30 +00001210 SetID (attach_pid);
Greg Clayton71337622011-02-24 22:24:29 +00001211 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 }
1213 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 return error;
1216}
1217
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218Error
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00001219ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220{
1221 Error error;
1222 // Clear out and clean up from any current state
1223 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 if (process_name && process_name[0])
1226 {
Greg Clayton71337622011-02-24 22:24:29 +00001227 // Make sure we aren't already connected?
1228 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229 {
Greg Clayton91a9b2472013-12-04 19:19:12 +00001230 error = LaunchAndConnectToDebugserver (attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001231
Greg Clayton71337622011-02-24 22:24:29 +00001232 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 {
Greg Clayton71337622011-02-24 22:24:29 +00001234 const char *error_string = error.AsCString();
1235 if (error_string == NULL)
1236 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237
Greg Clayton71337622011-02-24 22:24:29 +00001238 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 }
Greg Clayton71337622011-02-24 22:24:29 +00001240 }
1241
1242 if (error.Success())
1243 {
1244 StreamString packet;
1245
Jim Ingham106d0282014-06-25 02:32:56 +00001246 m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError());
1247
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00001248 if (attach_info.GetWaitForLaunch())
Jim Inghamcd16df92012-07-20 21:37:13 +00001249 {
1250 if (!m_gdb_comm.GetVAttachOrWaitSupported())
1251 {
1252 packet.PutCString ("vAttachWait");
1253 }
1254 else
1255 {
1256 if (attach_info.GetIgnoreExisting())
1257 packet.PutCString("vAttachWait");
1258 else
1259 packet.PutCString ("vAttachOrWait");
1260 }
1261 }
Greg Clayton71337622011-02-24 22:24:29 +00001262 else
1263 packet.PutCString("vAttachName");
1264 packet.PutChar(';');
1265 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
1266
1267 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
1268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269 }
1270 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 return error;
1272}
1273
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274
Greg Claytonfbb76342013-11-20 21:07:01 +00001275bool
1276ProcessGDBRemote::SetExitStatus (int exit_status, const char *cstr)
1277{
1278 m_gdb_comm.Disconnect();
1279 return Process::SetExitStatus (exit_status, cstr);
1280}
1281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282void
Jim Inghambb006ce2014-08-02 00:33:35 +00001283ProcessGDBRemote::DidAttach (ArchSpec &process_arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284{
Jim Inghambb006ce2014-08-02 00:33:35 +00001285 // If you can figure out what the architecture is, fill it in here.
1286 process_arch.Clear();
1287 DidLaunchOrAttach (process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288}
1289
Greg Clayton90ba8112012-12-05 00:16:59 +00001290
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291Error
1292ProcessGDBRemote::WillResume ()
1293{
Greg Clayton71fc2a32011-02-12 06:28:37 +00001294 m_continue_c_tids.clear();
1295 m_continue_C_tids.clear();
1296 m_continue_s_tids.clear();
1297 m_continue_S_tids.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298 return Error();
1299}
1300
1301Error
1302ProcessGDBRemote::DoResume ()
1303{
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001304 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001305 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001306 if (log)
1307 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytone5219662010-12-03 06:02:24 +00001308
1309 Listener listener ("gdb-remote.resume-packet-sent");
1310 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
1311 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00001312 listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit);
1313
Greg Claytond1d06e42013-04-20 00:27:58 +00001314 const size_t num_threads = GetThreadList().GetSize();
1315
Greg Clayton71fc2a32011-02-12 06:28:37 +00001316 StreamString continue_packet;
1317 bool continue_packet_error = false;
1318 if (m_gdb_comm.HasAnyVContSupport ())
1319 {
Greg Claytone98008c2014-02-13 23:34:38 +00001320 if (m_continue_c_tids.size() == num_threads ||
1321 (m_continue_c_tids.empty() &&
1322 m_continue_C_tids.empty() &&
1323 m_continue_s_tids.empty() &&
1324 m_continue_S_tids.empty()))
Greg Clayton71fc2a32011-02-12 06:28:37 +00001325 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001326 // All threads are continuing, just send a "c" packet
1327 continue_packet.PutCString ("c");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001328 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001329 else
1330 {
1331 continue_packet.PutCString ("vCont");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001332
Greg Claytond1d06e42013-04-20 00:27:58 +00001333 if (!m_continue_c_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001334 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001335 if (m_gdb_comm.GetVContSupported ('c'))
1336 {
1337 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)
1338 continue_packet.Printf(";c:%4.4" PRIx64, *t_pos);
1339 }
1340 else
1341 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001342 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001343
1344 if (!continue_packet_error && !m_continue_C_tids.empty())
1345 {
1346 if (m_gdb_comm.GetVContSupported ('C'))
1347 {
1348 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)
1349 continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1350 }
1351 else
1352 continue_packet_error = true;
1353 }
Greg Claytone5219662010-12-03 06:02:24 +00001354
Greg Claytond1d06e42013-04-20 00:27:58 +00001355 if (!continue_packet_error && !m_continue_s_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001356 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001357 if (m_gdb_comm.GetVContSupported ('s'))
1358 {
1359 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)
1360 continue_packet.Printf(";s:%4.4" PRIx64, *t_pos);
1361 }
1362 else
1363 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001364 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001365
1366 if (!continue_packet_error && !m_continue_S_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001367 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001368 if (m_gdb_comm.GetVContSupported ('S'))
1369 {
1370 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)
1371 continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1372 }
1373 else
1374 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001375 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001376
1377 if (continue_packet_error)
1378 continue_packet.GetString().clear();
Greg Clayton71fc2a32011-02-12 06:28:37 +00001379 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001380 }
1381 else
1382 continue_packet_error = true;
1383
1384 if (continue_packet_error)
1385 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001386 // Either no vCont support, or we tried to use part of the vCont
1387 // packet that wasn't supported by the remote GDB server.
1388 // We need to try and make a simple packet that can do our continue
Greg Clayton71fc2a32011-02-12 06:28:37 +00001389 const size_t num_continue_c_tids = m_continue_c_tids.size();
1390 const size_t num_continue_C_tids = m_continue_C_tids.size();
1391 const size_t num_continue_s_tids = m_continue_s_tids.size();
1392 const size_t num_continue_S_tids = m_continue_S_tids.size();
1393 if (num_continue_c_tids > 0)
1394 {
1395 if (num_continue_c_tids == num_threads)
1396 {
1397 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001398 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001399 continue_packet.PutChar ('c');
1400 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001401 }
1402 else if (num_continue_c_tids == 1 &&
1403 num_continue_C_tids == 0 &&
1404 num_continue_s_tids == 0 &&
1405 num_continue_S_tids == 0 )
1406 {
1407 // Only one thread is continuing
Greg Clayton8b82f082011-04-12 05:54:46 +00001408 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001409 continue_packet.PutChar ('c');
Greg Clayton0c74e782011-06-24 03:21:43 +00001410 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001411 }
1412 }
1413
Greg Clayton0c74e782011-06-24 03:21:43 +00001414 if (continue_packet_error && num_continue_C_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001415 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001416 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1417 num_continue_C_tids > 0 &&
1418 num_continue_s_tids == 0 &&
1419 num_continue_S_tids == 0 )
Greg Clayton71fc2a32011-02-12 06:28:37 +00001420 {
1421 const int continue_signo = m_continue_C_tids.front().second;
Greg Clayton0c74e782011-06-24 03:21:43 +00001422 // Only one thread is continuing
Greg Clayton71fc2a32011-02-12 06:28:37 +00001423 if (num_continue_C_tids > 1)
1424 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001425 // More that one thread with a signal, yet we don't have
1426 // vCont support and we are being asked to resume each
1427 // thread with a signal, we need to make sure they are
1428 // all the same signal, or we can't issue the continue
1429 // accurately with the current support...
1430 if (num_continue_C_tids > 1)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001431 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001432 continue_packet_error = false;
1433 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1434 {
1435 if (m_continue_C_tids[i].second != continue_signo)
1436 continue_packet_error = true;
1437 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001438 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001439 if (!continue_packet_error)
1440 m_gdb_comm.SetCurrentThreadForRun (-1);
1441 }
1442 else
1443 {
1444 // Set the continue thread ID
1445 continue_packet_error = false;
1446 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001447 }
1448 if (!continue_packet_error)
1449 {
1450 // Add threads continuing with the same signo...
Greg Clayton71fc2a32011-02-12 06:28:37 +00001451 continue_packet.Printf("C%2.2x", continue_signo);
1452 }
1453 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001454 }
1455
Greg Clayton0c74e782011-06-24 03:21:43 +00001456 if (continue_packet_error && num_continue_s_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001457 {
1458 if (num_continue_s_tids == num_threads)
1459 {
1460 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001461 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001462 continue_packet.PutChar ('s');
1463 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001464 }
1465 else if (num_continue_c_tids == 0 &&
1466 num_continue_C_tids == 0 &&
1467 num_continue_s_tids == 1 &&
1468 num_continue_S_tids == 0 )
1469 {
1470 // Only one thread is stepping
Greg Clayton8b82f082011-04-12 05:54:46 +00001471 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001472 continue_packet.PutChar ('s');
Greg Clayton0c74e782011-06-24 03:21:43 +00001473 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001474 }
1475 }
1476
1477 if (!continue_packet_error && num_continue_S_tids > 0)
1478 {
1479 if (num_continue_S_tids == num_threads)
1480 {
1481 const int step_signo = m_continue_S_tids.front().second;
1482 // Are all threads trying to step with the same signal?
Greg Clayton0c74e782011-06-24 03:21:43 +00001483 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001484 if (num_continue_S_tids > 1)
1485 {
1486 for (size_t i=1; i<num_threads; ++i)
1487 {
1488 if (m_continue_S_tids[i].second != step_signo)
1489 continue_packet_error = true;
1490 }
1491 }
1492 if (!continue_packet_error)
1493 {
1494 // Add threads stepping with the same signo...
Greg Clayton8b82f082011-04-12 05:54:46 +00001495 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001496 continue_packet.Printf("S%2.2x", step_signo);
1497 }
1498 }
1499 else if (num_continue_c_tids == 0 &&
1500 num_continue_C_tids == 0 &&
1501 num_continue_s_tids == 0 &&
1502 num_continue_S_tids == 1 )
1503 {
1504 // Only one thread is stepping with signal
Greg Clayton8b82f082011-04-12 05:54:46 +00001505 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001506 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Clayton0c74e782011-06-24 03:21:43 +00001507 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001508 }
1509 }
1510 }
1511
1512 if (continue_packet_error)
1513 {
1514 error.SetErrorString ("can't make continue packet for this resume");
1515 }
1516 else
1517 {
1518 EventSP event_sp;
1519 TimeValue timeout;
1520 timeout = TimeValue::Now();
1521 timeout.OffsetWithSeconds (5);
Zachary Turneracee96a2014-09-23 18:32:09 +00001522 if (!m_async_thread.IsJoinable())
Jim Inghamb1e2e842012-04-12 18:49:31 +00001523 {
1524 error.SetErrorString ("Trying to resume but the async thread is dead.");
1525 if (log)
1526 log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead.");
1527 return error;
1528 }
1529
Greg Clayton71fc2a32011-02-12 06:28:37 +00001530 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1531
1532 if (listener.WaitForEvent (&timeout, event_sp) == false)
Jim Inghamb1e2e842012-04-12 18:49:31 +00001533 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001534 error.SetErrorString("Resume timed out.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00001535 if (log)
1536 log->Printf ("ProcessGDBRemote::DoResume: Resume timed out.");
1537 }
1538 else if (event_sp->BroadcasterIs (&m_async_broadcaster))
1539 {
1540 error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back.");
1541 if (log)
1542 log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back.");
1543 return error;
1544 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001545 }
Greg Claytone5219662010-12-03 06:02:24 +00001546 }
1547
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001548 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549}
1550
Greg Clayton9e920902012-04-10 02:25:43 +00001551void
1552ProcessGDBRemote::ClearThreadIDList ()
1553{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001554 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001555 m_thread_ids.clear();
1556}
1557
1558bool
1559ProcessGDBRemote::UpdateThreadIDList ()
1560{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001561 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001562 bool sequence_mutex_unavailable = false;
1563 m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable);
1564 if (sequence_mutex_unavailable)
1565 {
Greg Clayton9e920902012-04-10 02:25:43 +00001566 return false; // We just didn't get the list
1567 }
1568 return true;
1569}
1570
Greg Clayton9fc13552012-04-10 00:18:59 +00001571bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001572ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573{
1574 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +00001575 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Clayton73b472d2010-10-27 03:32:59 +00001576 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +00001577 log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Clayton9e920902012-04-10 02:25:43 +00001578
1579 size_t num_thread_ids = m_thread_ids.size();
1580 // The "m_thread_ids" thread ID list should always be updated after each stop
1581 // reply packet, but in case it isn't, update it here.
1582 if (num_thread_ids == 0)
1583 {
1584 if (!UpdateThreadIDList ())
1585 return false;
1586 num_thread_ids = m_thread_ids.size();
1587 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001589 ThreadList old_thread_list_copy(old_thread_list);
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001590 if (num_thread_ids > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001591 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001592 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001593 {
Greg Clayton9e920902012-04-10 02:25:43 +00001594 tid_t tid = m_thread_ids[i];
Greg Clayton160c9d82013-05-01 21:54:04 +00001595 ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001596 if (!thread_sp)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001597 {
Jim Ingham4f465cf2012-10-10 18:32:14 +00001598 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001599 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1600 log->Printf(
1601 "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001602 __FUNCTION__, static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001603 thread_sp->GetID());
1604 }
1605 else
1606 {
1607 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1608 log->Printf(
1609 "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001610 __FUNCTION__, static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001611 thread_sp->GetID());
1612 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001613 new_thread_list.AddThread(thread_sp);
Greg Claytonadc00cb2011-05-20 23:38:13 +00001614 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001615 }
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001616
1617 // Whatever that is left in old_thread_list_copy are not
1618 // present in new_thread_list. Remove non-existent threads from internal id table.
1619 size_t old_num_thread_ids = old_thread_list_copy.GetSize(false);
1620 for (size_t i=0; i<old_num_thread_ids; i++)
1621 {
1622 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false));
1623 if (old_thread_sp)
1624 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001625 lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID();
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001626 m_thread_id_to_index_id_map.erase(old_thread_id);
1627 }
1628 }
1629
Greg Clayton9fc13552012-04-10 00:18:59 +00001630 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631}
1632
1633
1634StateType
1635ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1636{
Greg Claytondd0e5a52011-06-02 22:22:38 +00001637 stop_packet.SetFilePos (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638 const char stop_type = stop_packet.GetChar();
1639 switch (stop_type)
1640 {
1641 case 'T':
1642 case 'S':
1643 {
Greg Clayton15fc2be2013-05-21 01:00:52 +00001644 // This is a bit of a hack, but is is required. If we did exec, we
1645 // need to clear our thread lists and also know to rebuild our dynamic
1646 // register info before we lookup and threads and populate the expedited
1647 // register values so we need to know this right away so we can cleanup
1648 // and update our registers.
Greg Clayton8cda7f02013-05-21 21:55:59 +00001649 const uint32_t stop_id = GetStopID();
1650 if (stop_id == 0)
Greg Claytone576ab22011-02-15 00:19:15 +00001651 {
1652 // Our first stop, make sure we have a process ID, and also make
1653 // sure we know about our registers
1654 if (GetID() == LLDB_INVALID_PROCESS_ID)
1655 {
Greg Claytonc574ede2011-03-10 02:26:48 +00001656 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone576ab22011-02-15 00:19:15 +00001657 if (pid != LLDB_INVALID_PROCESS_ID)
1658 SetID (pid);
1659 }
1660 BuildDynamicRegisterInfo (true);
1661 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662 // Stop with signal and thread info
1663 const uint8_t signo = stop_packet.GetHexU8();
1664 std::string name;
1665 std::string value;
1666 std::string thread_name;
Greg Claytona658fd22011-06-04 01:26:29 +00001667 std::string reason;
1668 std::string description;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001669 uint32_t exc_type = 0;
Greg Clayton896dff62010-07-23 16:45:51 +00001670 std::vector<addr_t> exc_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001672 ThreadSP thread_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001673 ThreadGDBRemote *gdb_thread = NULL;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001674
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675 while (stop_packet.GetNameColonValue(name, value))
1676 {
1677 if (name.compare("metype") == 0)
1678 {
1679 // exception type in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001680 exc_type = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682 else if (name.compare("medata") == 0)
1683 {
1684 // exception data in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001685 exc_data.push_back(StringConvert::ToUInt64 (value.c_str(), 0, 16));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 }
1687 else if (name.compare("thread") == 0)
1688 {
1689 // thread in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001690 lldb::tid_t tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001691 // m_thread_list_real does have its own mutex, but we need to
1692 // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...)
1693 // and the m_thread_list_real.AddThread(...) so it doesn't change on us
1694 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1695 thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false);
Greg Clayton160c9d82013-05-01 21:54:04 +00001696
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001697 if (!thread_sp)
Greg Claytond1d06e42013-04-20 00:27:58 +00001698 {
Greg Claytone576ab22011-02-15 00:19:15 +00001699 // Create the thread if we need to
Jim Ingham4f465cf2012-10-10 18:32:14 +00001700 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001701 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
1702 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1703 log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1704 __FUNCTION__,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001705 static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001706 thread_sp->GetID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001707
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001708 m_thread_list_real.AddThread(thread_sp);
Greg Claytone576ab22011-02-15 00:19:15 +00001709 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001710 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1711
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 }
Greg Clayton9e920902012-04-10 02:25:43 +00001713 else if (name.compare("threads") == 0)
1714 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001715 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001716 m_thread_ids.clear();
Greg Clayton44633992012-04-10 03:22:03 +00001717 // A comma separated list of all threads in the current
1718 // process that includes the thread for this stop reply
1719 // packet
Greg Clayton9e920902012-04-10 02:25:43 +00001720 size_t comma_pos;
1721 lldb::tid_t tid;
1722 while ((comma_pos = value.find(',')) != std::string::npos)
1723 {
1724 value[comma_pos] = '\0';
1725 // thread in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001726 tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Greg Clayton9e920902012-04-10 02:25:43 +00001727 if (tid != LLDB_INVALID_THREAD_ID)
1728 m_thread_ids.push_back (tid);
1729 value.erase(0, comma_pos + 1);
Greg Clayton9e920902012-04-10 02:25:43 +00001730 }
Vince Harron5275aaa2015-01-15 20:08:35 +00001731 tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Greg Clayton9e920902012-04-10 02:25:43 +00001732 if (tid != LLDB_INVALID_THREAD_ID)
1733 m_thread_ids.push_back (tid);
1734 }
Greg Claytonde9d0492011-01-08 03:17:57 +00001735 else if (name.compare("hexname") == 0)
1736 {
1737 StringExtractor name_extractor;
1738 // Swap "value" over into "name_extractor"
1739 name_extractor.GetStringRef().swap(value);
1740 // Now convert the HEX bytes into a string value
1741 name_extractor.GetHexByteString (value);
1742 thread_name.swap (value);
1743 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001744 else if (name.compare("name") == 0)
1745 {
1746 thread_name.swap (value);
1747 }
Greg Clayton6f35f5c2010-09-09 06:32:46 +00001748 else if (name.compare("qaddr") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001750 thread_dispatch_qaddr = StringConvert::ToUInt64 (value.c_str(), 0, 16);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751 }
Greg Claytona658fd22011-06-04 01:26:29 +00001752 else if (name.compare("reason") == 0)
1753 {
1754 reason.swap(value);
1755 }
1756 else if (name.compare("description") == 0)
1757 {
1758 StringExtractor desc_extractor;
1759 // Swap "value" over into "name_extractor"
1760 desc_extractor.GetStringRef().swap(value);
1761 // Now convert the HEX bytes into a string value
Chaoren Lin28e57422015-02-03 01:51:25 +00001762 desc_extractor.GetHexByteString (value);
1763 description.swap(value);
Greg Claytona658fd22011-06-04 01:26:29 +00001764 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001765 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1766 {
1767 // We have a register number that contains an expedited
1768 // register value. Lets supply this register to our thread
1769 // so it won't have to go and read it.
Greg Clayton160c9d82013-05-01 21:54:04 +00001770 if (gdb_thread)
Greg Clayton3e06bd92011-01-09 21:07:35 +00001771 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001772 uint32_t reg = StringConvert::ToUInt32 (name.c_str(), UINT32_MAX, 16);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001773
1774 if (reg != UINT32_MAX)
1775 {
1776 StringExtractor reg_value_extractor;
1777 // Swap "value" over into "reg_value_extractor"
1778 reg_value_extractor.GetStringRef().swap(value);
Greg Clayton160c9d82013-05-01 21:54:04 +00001779 if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor))
Greg Claytone576ab22011-02-15 00:19:15 +00001780 {
1781 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1782 name.c_str(),
1783 reg,
1784 reg,
1785 reg_value_extractor.GetStringRef().c_str(),
1786 stop_packet.GetStringRef().c_str());
1787 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001788 }
1789 }
1790 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792
Hafiz Abid Qadeer673b4a32014-02-20 10:23:20 +00001793 // If the response is old style 'S' packet which does not provide us with thread information
1794 // then update the thread list and choose the first one.
1795 if (!thread_sp)
1796 {
1797 UpdateThreadIDList ();
1798
1799 if (!m_thread_ids.empty ())
1800 {
1801 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1802 thread_sp = m_thread_list_real.FindThreadByProtocolID (m_thread_ids.front (), false);
1803 if (thread_sp)
1804 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get ());
1805 }
1806 }
1807
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001808 if (thread_sp)
1809 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001810 // Clear the stop info just in case we don't set it to anything
1811 thread_sp->SetStopInfo (StopInfoSP());
1812
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Inghamdd2fe7a2011-01-28 02:23:12 +00001814 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815 if (exc_type != 0)
1816 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00001817 const size_t exc_data_size = exc_data.size();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001818
Greg Clayton160c9d82013-05-01 21:54:04 +00001819 thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1820 exc_type,
1821 exc_data_size,
1822 exc_data_size >= 1 ? exc_data[0] : 0,
1823 exc_data_size >= 2 ? exc_data[1] : 0,
1824 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825 }
Greg Claytona658fd22011-06-04 01:26:29 +00001826 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827 {
Greg Claytona658fd22011-06-04 01:26:29 +00001828 bool handled = false;
Greg Clayton8cda7f02013-05-21 21:55:59 +00001829 bool did_exec = false;
Greg Claytona658fd22011-06-04 01:26:29 +00001830 if (!reason.empty())
1831 {
1832 if (reason.compare("trace") == 0)
1833 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001834 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Greg Claytona658fd22011-06-04 01:26:29 +00001835 handled = true;
1836 }
1837 else if (reason.compare("breakpoint") == 0)
1838 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001839 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1840 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Claytona658fd22011-06-04 01:26:29 +00001841 if (bp_site_sp)
1842 {
1843 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1844 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1845 // 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 +00001846 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001847 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001848 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001849 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001850 }
1851 else
1852 {
1853 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001854 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001855 }
1856 }
Greg Claytona658fd22011-06-04 01:26:29 +00001857 }
1858 else if (reason.compare("trap") == 0)
1859 {
1860 // Let the trap just use the standard signal stop reason below...
1861 }
1862 else if (reason.compare("watchpoint") == 0)
1863 {
Chaoren Lin18fe6402015-02-03 01:51:47 +00001864 StringExtractor desc_extractor(description.c_str());
1865 addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS);
1866 uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32);
1867 watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
1868 if (wp_addr != LLDB_INVALID_ADDRESS)
1869 {
1870 WatchpointSP wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr);
1871 if (wp_sp)
1872 {
1873 wp_sp->SetHardwareIndex(wp_index);
1874 watch_id = wp_sp->GetID();
1875 }
1876 }
1877 if (watch_id == LLDB_INVALID_WATCH_ID)
1878 {
1879 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_WATCHPOINTS));
1880 if (log) log->Printf ("failed to find watchpoint");
1881 }
Greg Clayton160c9d82013-05-01 21:54:04 +00001882 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
Greg Claytona658fd22011-06-04 01:26:29 +00001883 handled = true;
1884 }
1885 else if (reason.compare("exception") == 0)
1886 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001887 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001888 handled = true;
1889 }
Greg Clayton15fc2be2013-05-21 01:00:52 +00001890 else if (reason.compare("exec") == 0)
1891 {
Greg Clayton8cda7f02013-05-21 21:55:59 +00001892 did_exec = true;
Greg Clayton15fc2be2013-05-21 01:00:52 +00001893 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp));
1894 handled = true;
1895 }
Greg Claytona658fd22011-06-04 01:26:29 +00001896 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001897
Jim Ingham40083a42014-02-24 19:49:46 +00001898 if (!handled && signo && did_exec == false)
Greg Claytona658fd22011-06-04 01:26:29 +00001899 {
1900 if (signo == SIGTRAP)
1901 {
1902 // Currently we are going to assume SIGTRAP means we are either
1903 // hitting a breakpoint or hardware single stepping.
Jim Ingham54cc6e42012-07-11 21:41:19 +00001904 handled = true;
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +00001905 addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset;
Greg Clayton160c9d82013-05-01 21:54:04 +00001906 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001907
Greg Claytona658fd22011-06-04 01:26:29 +00001908 if (bp_site_sp)
1909 {
1910 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1911 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1912 // 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 +00001913 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001914 {
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +00001915 if(m_breakpoint_pc_offset != 0)
1916 thread_sp->GetRegisterContext()->SetPC(pc);
Greg Clayton160c9d82013-05-01 21:54:04 +00001917 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001918 }
1919 else
1920 {
1921 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001922 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001923 }
1924 }
Jim Ingham54cc6e42012-07-11 21:41:19 +00001925 else
Greg Claytona658fd22011-06-04 01:26:29 +00001926 {
Jim Ingham4dc613b2012-10-27 02:52:04 +00001927 // If we were stepping then assume the stop was the result of the trace. If we were
1928 // not stepping then report the SIGTRAP.
1929 // FIXME: We are still missing the case where we single step over a trap instruction.
Greg Clayton160c9d82013-05-01 21:54:04 +00001930 if (thread_sp->GetTemporaryResumeState() == eStateStepping)
1931 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Jim Ingham4dc613b2012-10-27 02:52:04 +00001932 else
Greg Clayton160c9d82013-05-01 21:54:04 +00001933 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo));
Greg Claytona658fd22011-06-04 01:26:29 +00001934 }
1935 }
1936 if (!handled)
Greg Clayton160c9d82013-05-01 21:54:04 +00001937 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Jason Molenda8214b012013-04-25 01:33:46 +00001938 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001939
Greg Claytona658fd22011-06-04 01:26:29 +00001940 if (!description.empty())
1941 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001942 lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
Greg Claytona658fd22011-06-04 01:26:29 +00001943 if (stop_info_sp)
1944 {
1945 stop_info_sp->SetDescription (description.c_str());
Greg Clayton3418c852011-08-10 02:10:13 +00001946 }
Greg Claytona658fd22011-06-04 01:26:29 +00001947 else
1948 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001949 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001950 }
1951 }
1952 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953 }
1954 return eStateStopped;
1955 }
1956 break;
1957
1958 case 'W':
Todd Fialaff6131a2014-05-19 04:57:23 +00001959 case 'X':
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 // process exited
1961 return eStateExited;
1962
1963 default:
1964 break;
1965 }
1966 return eStateInvalid;
1967}
1968
1969void
1970ProcessGDBRemote::RefreshStateAfterStop ()
1971{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001972 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001973 m_thread_ids.clear();
1974 // Set the thread stop info. It might have a "threads" key whose value is
1975 // a list of all thread IDs in the current process, so m_thread_ids might
1976 // get set.
1977 SetThreadStopInfo (m_last_stop_packet);
1978 // Check to see if SetThreadStopInfo() filled in m_thread_ids?
1979 if (m_thread_ids.empty())
1980 {
1981 // No, we need to fetch the thread list manually
1982 UpdateThreadIDList();
1983 }
1984
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985 // Let all threads recover from stopping and do any clean up based
1986 // on the previous thread state (if any).
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001987 m_thread_list_real.RefreshStateAfterStop();
Greg Clayton9e920902012-04-10 02:25:43 +00001988
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989}
1990
1991Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001992ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993{
1994 Error error;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001995
Greg Clayton6ed95942011-01-22 07:12:45 +00001996 bool timed_out = false;
1997 Mutex::Locker locker;
Greg Clayton513c26c2011-01-29 07:10:55 +00001998
1999 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton3af9ea52010-11-18 05:57:03 +00002000 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002001 // We are being asked to halt during an attach. We need to just close
2002 // our file handle and debugserver will go away, and we can be done...
2003 m_gdb_comm.Disconnect();
Greg Clayton3af9ea52010-11-18 05:57:03 +00002004 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002005 else
2006 {
Greg Clayton2687cd12012-03-29 01:55:41 +00002007 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton513c26c2011-01-29 07:10:55 +00002008 {
2009 if (timed_out)
2010 error.SetErrorString("timed out sending interrupt packet");
2011 else
2012 error.SetErrorString("unknown error sending interrupt packet");
2013 }
Greg Clayton2687cd12012-03-29 01:55:41 +00002014
2015 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton513c26c2011-01-29 07:10:55 +00002016 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017 return error;
2018}
2019
2020Error
Jim Inghamacff8952013-05-02 00:27:30 +00002021ProcessGDBRemote::DoDetach(bool keep_stopped)
Greg Clayton594e5ed2010-09-27 21:07:38 +00002022{
2023 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002024 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton594e5ed2010-09-27 21:07:38 +00002025 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +00002026 log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped);
2027
Jim Inghamacff8952013-05-02 00:27:30 +00002028 error = m_gdb_comm.Detach (keep_stopped);
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002029 if (log)
Greg Clayton594e5ed2010-09-27 21:07:38 +00002030 {
Jim Inghamacff8952013-05-02 00:27:30 +00002031 if (error.Success())
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002032 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
2033 else
Jim Inghamacff8952013-05-02 00:27:30 +00002034 log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>");
Greg Clayton594e5ed2010-09-27 21:07:38 +00002035 }
Jim Inghamacff8952013-05-02 00:27:30 +00002036
2037 if (!error.Success())
2038 return error;
2039
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002040 // Sleep for one second to let the process get all detached...
Greg Clayton594e5ed2010-09-27 21:07:38 +00002041 StopAsyncThread ();
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002042
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002043 SetPrivateState (eStateDetached);
2044 ResumePrivateStateThread();
2045
2046 //KillDebugserverProcess ();
Greg Clayton594e5ed2010-09-27 21:07:38 +00002047 return error;
2048}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049
Jim Ingham43c555d2012-07-04 00:35:43 +00002050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051Error
2052ProcessGDBRemote::DoDestroy ()
2053{
2054 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002055 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056 if (log)
2057 log->Printf ("ProcessGDBRemote::DoDestroy()");
2058
Jim Ingham43c555d2012-07-04 00:35:43 +00002059 // There is a bug in older iOS debugservers where they don't shut down the process
2060 // they are debugging properly. If the process is sitting at a breakpoint or an exception,
2061 // this can cause problems with restarting. So we check to see if any of our threads are stopped
2062 // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN
2063 // destroy it again.
2064 //
2065 // Note, we don't have a good way to test the version of debugserver, but I happen to know that
2066 // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of
2067 // the debugservers with this bug are equal. There really should be a better way to test this!
2068 //
2069 // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and
2070 // get called here to destroy again and we're still at a breakpoint or exception, then we should
2071 // just do the straight-forward kill.
2072 //
2073 // And of course, if we weren't able to stop the process by the time we get here, it isn't
2074 // necessary (or helpful) to do any of this.
2075
2076 if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning)
2077 {
2078 PlatformSP platform_sp = GetTarget().GetPlatform();
2079
2080 // FIXME: These should be ConstStrings so we aren't doing strcmp'ing.
2081 if (platform_sp
2082 && platform_sp->GetName()
Greg Clayton57abc5d2013-05-10 21:47:16 +00002083 && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic())
Jim Ingham43c555d2012-07-04 00:35:43 +00002084 {
2085 if (m_destroy_tried_resuming)
2086 {
2087 if (log)
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002088 log->PutCString ("ProcessGDBRemote::DoDestroy() - Tried resuming to destroy once already, not doing it again.");
Jim Ingham43c555d2012-07-04 00:35:43 +00002089 }
2090 else
2091 {
2092 // At present, the plans are discarded and the breakpoints disabled Process::Destroy,
2093 // but we really need it to happen here and it doesn't matter if we do it twice.
2094 m_thread_list.DiscardThreadPlans();
2095 DisableAllBreakpointSites();
2096
2097 bool stop_looks_like_crash = false;
2098 ThreadList &threads = GetThreadList();
2099
2100 {
Jim Ingham45350372012-09-11 00:08:52 +00002101 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00002102
2103 size_t num_threads = threads.GetSize();
2104 for (size_t i = 0; i < num_threads; i++)
2105 {
2106 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002107 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00002108 StopReason reason = eStopReasonInvalid;
2109 if (stop_info_sp)
2110 reason = stop_info_sp->GetStopReason();
2111 if (reason == eStopReasonBreakpoint
2112 || reason == eStopReasonException)
2113 {
2114 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00002115 log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.",
2116 thread_sp->GetProtocolID(),
Jim Ingham43c555d2012-07-04 00:35:43 +00002117 stop_info_sp->GetDescription());
2118 stop_looks_like_crash = true;
2119 break;
2120 }
2121 }
2122 }
2123
2124 if (stop_looks_like_crash)
2125 {
2126 if (log)
2127 log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill.");
2128 m_destroy_tried_resuming = true;
2129
2130 // If we are going to run again before killing, it would be good to suspend all the threads
2131 // before resuming so they won't get into more trouble. Sadly, for the threads stopped with
2132 // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do
2133 // have to run the risk of letting those threads proceed a bit.
2134
2135 {
Jim Ingham45350372012-09-11 00:08:52 +00002136 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00002137
2138 size_t num_threads = threads.GetSize();
2139 for (size_t i = 0; i < num_threads; i++)
2140 {
2141 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002142 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00002143 StopReason reason = eStopReasonInvalid;
2144 if (stop_info_sp)
2145 reason = stop_info_sp->GetStopReason();
2146 if (reason != eStopReasonBreakpoint
2147 && reason != eStopReasonException)
2148 {
2149 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00002150 log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.",
2151 thread_sp->GetProtocolID());
Jim Ingham43c555d2012-07-04 00:35:43 +00002152 thread_sp->SetResumeState(eStateSuspended);
2153 }
2154 }
2155 }
2156 Resume ();
2157 return Destroy();
2158 }
2159 }
2160 }
2161 }
2162
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163 // Interrupt if our inferior is running...
Jim Inghambabfc382012-06-06 00:32:39 +00002164 int exit_status = SIGABRT;
2165 std::string exit_string;
2166
Greg Clayton6ed95942011-01-22 07:12:45 +00002167 if (m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002168 {
Jim Inghamaab78372011-10-28 01:11:35 +00002169 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton6779606a2011-01-22 23:43:18 +00002170 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002171 StringExtractorGDBRemote response;
2172 bool send_async = true;
Tamas Berghammer912800c2015-02-24 10:23:39 +00002173 GDBRemoteCommunication::ScopedTimeout (m_gdb_comm, 3);
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00002174
Greg Clayton3dedae12013-12-06 21:45:27 +00002175 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async) == GDBRemoteCommunication::PacketResult::Success)
Greg Clayton513c26c2011-01-29 07:10:55 +00002176 {
2177 char packet_cmd = response.GetChar(0);
2178
2179 if (packet_cmd == 'W' || packet_cmd == 'X')
2180 {
Jim Inghameac0aa42014-02-21 22:36:11 +00002181#if defined(__APPLE__)
Jim Ingham9d67cc52014-02-21 22:35:29 +00002182 // For Native processes on Mac OS X, we launch through the Host Platform, then hand the process off
2183 // to debugserver, which becomes the parent process through "PT_ATTACH". Then when we go to kill
2184 // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we call waitpid which returns
2185 // with no error and the correct status. But amusingly enough that doesn't seem to actually reap
2186 // the process, but instead it is left around as a Zombie. Probably the kernel is in the process of
2187 // switching ownership back to lldb which was the original parent, and gets confused in the handoff.
2188 // Anyway, so call waitpid here to finally reap it.
2189 PlatformSP platform_sp(GetTarget().GetPlatform());
2190 if (platform_sp && platform_sp->IsHost())
2191 {
2192 int status;
2193 ::pid_t reap_pid;
2194 reap_pid = waitpid (GetID(), &status, WNOHANG);
2195 if (log)
2196 log->Printf ("Reaped pid: %d, status: %d.\n", reap_pid, status);
2197 }
2198#endif
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002199 SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00002200 ClearThreadIDList ();
Jim Inghambabfc382012-06-06 00:32:39 +00002201 exit_status = response.GetHexU8();
2202 }
2203 else
2204 {
2205 if (log)
2206 log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str());
2207 exit_string.assign("got unexpected response to k packet: ");
2208 exit_string.append(response.GetStringRef());
Greg Clayton513c26c2011-01-29 07:10:55 +00002209 }
2210 }
2211 else
2212 {
Jim Inghambabfc382012-06-06 00:32:39 +00002213 if (log)
2214 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
2215 exit_string.assign("failed to send the k packet");
Greg Clayton513c26c2011-01-29 07:10:55 +00002216 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002217 }
Jim Inghambabfc382012-06-06 00:32:39 +00002218 else
2219 {
2220 if (log)
Ed Masteb3a53332014-03-17 17:05:22 +00002221 log->Printf ("ProcessGDBRemote::DoDestroy - killed or interrupted while attaching");
Jim Inghamcfc09352012-07-27 23:57:19 +00002222 exit_string.assign ("killed or interrupted while attaching.");
Jim Inghambabfc382012-06-06 00:32:39 +00002223 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002224 }
Jim Inghambabfc382012-06-06 00:32:39 +00002225 else
2226 {
2227 // If we missed setting the exit status on the way out, do it here.
2228 // NB set exit status can be called multiple times, the first one sets the status.
2229 exit_string.assign("destroying when not connected to debugserver");
2230 }
2231
2232 SetExitStatus(exit_status, exit_string.c_str());
2233
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002234 StopAsyncThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235 KillDebugserverProcess ();
2236 return error;
2237}
2238
Greg Clayton8cda7f02013-05-21 21:55:59 +00002239void
2240ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response)
2241{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002242 Mutex::Locker locker (m_last_stop_packet_mutex);
Greg Clayton8cda7f02013-05-21 21:55:59 +00002243 const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos;
2244 if (did_exec)
2245 {
2246 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
2247 if (log)
2248 log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec");
2249
2250 m_thread_list_real.Clear();
2251 m_thread_list.Clear();
2252 BuildDynamicRegisterInfo (true);
2253 m_gdb_comm.ResetDiscoverableSettings();
2254 }
2255 m_last_stop_packet = response;
2256}
2257
2258
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002259//------------------------------------------------------------------
2260// Process Queries
2261//------------------------------------------------------------------
2262
2263bool
2264ProcessGDBRemote::IsAlive ()
2265{
Greg Clayton10177aa2010-12-08 05:08:21 +00002266 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002267}
2268
2269addr_t
2270ProcessGDBRemote::GetImageInfoAddress()
2271{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +00002272 return m_gdb_comm.GetShlibInfoAddr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002273}
2274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002275//------------------------------------------------------------------
2276// Process Memory
2277//------------------------------------------------------------------
2278size_t
2279ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2280{
Jason Molenda6076bf42014-05-06 04:34:52 +00002281 GetMaxMemorySize ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282 if (size > m_max_memory_size)
2283 {
2284 // Keep memory read sizes down to a sane limit. This function will be
2285 // called multiple times in order to complete the task by
2286 // lldb_private::Process so it is ok to do this.
2287 size = m_max_memory_size;
2288 }
2289
2290 char packet[64];
Jason Molenda6076bf42014-05-06 04:34:52 +00002291 int packet_len;
2292 bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
2293 if (binary_memory_read)
2294 {
2295 packet_len = ::snprintf (packet, sizeof(packet), "x0x%" PRIx64 ",0x%" PRIx64, (uint64_t)addr, (uint64_t)size);
2296 }
2297 else
2298 {
2299 packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size);
2300 }
Andy Gibbsa297a972013-06-19 19:04:53 +00002301 assert (packet_len + 1 < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002302 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002303 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304 {
Greg Clayton576d8832011-03-22 04:00:09 +00002305 if (response.IsNormalResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306 {
2307 error.Clear();
Jason Molenda6076bf42014-05-06 04:34:52 +00002308 if (binary_memory_read)
2309 {
2310 // The lower level GDBRemoteCommunication packet receive layer has already de-quoted any
2311 // 0x7d character escaping that was present in the packet
2312
2313 size_t data_received_size = response.GetBytesLeft();
2314 if (data_received_size > size)
2315 {
2316 // Don't write past the end of BUF if the remote debug server gave us too
2317 // much data for some reason.
2318 data_received_size = size;
2319 }
2320 memcpy (buf, response.GetStringRef().data(), data_received_size);
2321 return data_received_size;
2322 }
2323 else
2324 {
2325 return response.GetHexBytes(buf, size, '\xdd');
2326 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002327 }
Greg Clayton576d8832011-03-22 04:00:09 +00002328 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002329 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002330 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002331 error.SetErrorStringWithFormat("GDB server does not support reading memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002332 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002333 error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002334 }
2335 else
2336 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002337 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002338 }
2339 return 0;
2340}
2341
2342size_t
2343ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2344{
Jason Molenda6076bf42014-05-06 04:34:52 +00002345 GetMaxMemorySize ();
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00002346 if (size > m_max_memory_size)
2347 {
2348 // Keep memory read sizes down to a sane limit. This function will be
2349 // called multiple times in order to complete the task by
2350 // lldb_private::Process so it is ok to do this.
2351 size = m_max_memory_size;
2352 }
2353
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002354 StreamString packet;
Daniel Malead01b2952012-11-29 21:49:15 +00002355 packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size);
Greg Clayton7fb56d02011-02-01 01:31:41 +00002356 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002357 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002358 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002359 {
Greg Clayton576d8832011-03-22 04:00:09 +00002360 if (response.IsOKResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002361 {
2362 error.Clear();
2363 return size;
2364 }
Greg Clayton576d8832011-03-22 04:00:09 +00002365 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002366 error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002367 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002368 error.SetErrorStringWithFormat("GDB server does not support writing memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002369 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002370 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 +00002371 }
2372 else
2373 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002374 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002375 }
2376 return 0;
2377}
2378
2379lldb::addr_t
2380ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
2381{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002382 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton2a48f522011-05-14 01:50:35 +00002383 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
2384
Greg Clayton70b57652011-05-15 01:25:55 +00002385 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton2a48f522011-05-14 01:50:35 +00002386 switch (supported)
2387 {
2388 case eLazyBoolCalculate:
2389 case eLazyBoolYes:
2390 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
2391 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
2392 return allocated_addr;
2393
2394 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002395 // Call mmap() to create memory in the inferior..
2396 unsigned prot = 0;
2397 if (permissions & lldb::ePermissionsReadable)
2398 prot |= eMmapProtRead;
2399 if (permissions & lldb::ePermissionsWritable)
2400 prot |= eMmapProtWrite;
2401 if (permissions & lldb::ePermissionsExecutable)
2402 prot |= eMmapProtExec;
Greg Clayton2a48f522011-05-14 01:50:35 +00002403
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002404 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
2405 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
2406 m_addr_to_mmap_size[allocated_addr] = size;
2407 else
Todd Fialaaf245d12014-06-30 21:05:18 +00002408 {
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002409 allocated_addr = LLDB_INVALID_ADDRESS;
Todd Fialaaf245d12014-06-30 21:05:18 +00002410 if (log)
2411 log->Printf ("ProcessGDBRemote::%s no direct stub support for memory allocation, and InferiorCallMmap also failed - is stub missing register context save/restore capability?", __FUNCTION__);
2412 }
Greg Clayton2a48f522011-05-14 01:50:35 +00002413 break;
2414 }
2415
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002416 if (allocated_addr == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +00002417 error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002418 else
2419 error.Clear();
2420 return allocated_addr;
2421}
2422
2423Error
Greg Clayton46fb5582011-11-18 07:03:08 +00002424ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
2425 MemoryRegionInfo &region_info)
2426{
2427
2428 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
2429 return error;
2430}
2431
2432Error
Johnny Chen64637202012-05-23 21:09:52 +00002433ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num)
2434{
2435
2436 Error error (m_gdb_comm.GetWatchpointSupportInfo (num));
2437 return error;
2438}
2439
2440Error
Enrico Granataf04a2192012-07-13 23:18:48 +00002441ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2442{
2443 Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after));
2444 return error;
2445}
2446
2447Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002448ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
2449{
2450 Error error;
Greg Clayton70b57652011-05-15 01:25:55 +00002451 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
2452
2453 switch (supported)
2454 {
2455 case eLazyBoolCalculate:
2456 // We should never be deallocating memory without allocating memory
2457 // first so we should never get eLazyBoolCalculate
2458 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
2459 break;
2460
2461 case eLazyBoolYes:
2462 if (!m_gdb_comm.DeallocateMemory (addr))
Daniel Malead01b2952012-11-29 21:49:15 +00002463 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002464 break;
2465
2466 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002467 // Call munmap() to deallocate memory in the inferior..
Greg Clayton70b57652011-05-15 01:25:55 +00002468 {
2469 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002470 if (pos != m_addr_to_mmap_size.end() &&
2471 InferiorCallMunmap(this, addr, pos->second))
2472 m_addr_to_mmap_size.erase (pos);
2473 else
Daniel Malead01b2952012-11-29 21:49:15 +00002474 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002475 }
2476 break;
2477 }
2478
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002479 return error;
2480}
2481
2482
2483//------------------------------------------------------------------
2484// Process STDIO
2485//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002486size_t
2487ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
2488{
2489 if (m_stdio_communication.IsConnected())
2490 {
2491 ConnectionStatus status;
2492 m_stdio_communication.Write(src, src_len, status, NULL);
2493 }
Vince Harrondf3f00f2015-02-10 21:09:04 +00002494 else if (m_stdin_forward)
Vince Harrone0be4252015-02-06 18:32:57 +00002495 {
2496 m_gdb_comm.SendStdinNotification(src, src_len);
2497 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002498 return 0;
2499}
2500
2501Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002502ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002503{
2504 Error error;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002505 assert(bp_site != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002506
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002507 // Get logging info
2508 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002509 user_id_t site_id = bp_site->GetID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002510
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002511 // Get the breakpoint address
2512 const addr_t addr = bp_site->GetLoadAddress();
2513
2514 // Log that a breakpoint was requested
2515 if (log)
2516 log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr);
2517
2518 // Breakpoint already exists and is enabled
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002519 if (bp_site->IsEnabled())
2520 {
2521 if (log)
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002522 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 +00002523 return error;
2524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002525
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002526 // Get the software breakpoint trap opcode size
2527 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2528
2529 // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this breakpoint type
2530 // is supported by the remote stub. These are set to true by default, and later set to false
2531 // only after we receive an unimplemented response when sending a breakpoint packet. This means
2532 // initially that unless we were specifically instructed to use a hardware breakpoint, LLDB will
2533 // attempt to set a software breakpoint. HardwareRequired() also queries a boolean variable which
2534 // indicates if the user specifically asked for hardware breakpoints. If true then we will
2535 // skip over software breakpoints.
2536 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && (!bp_site->HardwareRequired()))
2537 {
2538 // Try to send off a software breakpoint packet ($Z0)
2539 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002540 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002541 // The breakpoint was placed successfully
2542 bp_site->SetEnabled(true);
2543 bp_site->SetType(BreakpointSite::eExternal);
Greg Claytoneb023e72013-10-11 19:48:25 +00002544 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002545 }
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002546
2547 // SendGDBStoppointTypePacket() will return an error if it was unable to set this
2548 // breakpoint. We need to differentiate between a error specific to placing this breakpoint
2549 // or if we have learned that this breakpoint type is unsupported. To do this, we
2550 // must test the support boolean for this breakpoint type to see if it now indicates that
2551 // this breakpoint type is unsupported. If they are still supported then we should return
2552 // with the error code. If they are now unsupported, then we would like to fall through
2553 // and try another form of breakpoint.
2554 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware))
2555 return error;
2556
2557 // We reach here when software breakpoints have been found to be unsupported. For future
2558 // calls to set a breakpoint, we will not attempt to set a breakpoint with a type that is
2559 // known not to be supported.
2560 if (log)
2561 log->Printf("Software breakpoints are unsupported");
2562
2563 // So we will fall through and try a hardware breakpoint
2564 }
2565
2566 // The process of setting a hardware breakpoint is much the same as above. We check the
2567 // supported boolean for this breakpoint type, and if it is thought to be supported then we
2568 // will try to set this breakpoint with a hardware breakpoint.
2569 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware))
2570 {
2571 // Try to send off a hardware breakpoint packet ($Z1)
2572 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002573 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002574 // The breakpoint was placed successfully
2575 bp_site->SetEnabled(true);
2576 bp_site->SetType(BreakpointSite::eHardware);
2577 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002578 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002579
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002580 // Check if the error was something other then an unsupported breakpoint type
2581 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware))
2582 {
2583 // Unable to set this hardware breakpoint
2584 error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)");
2585 return error;
2586 }
2587
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002588 // We will reach here when the stub gives an unsupported response to a hardware breakpoint
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002589 if (log)
2590 log->Printf("Hardware breakpoints are unsupported");
2591
2592 // Finally we will falling through to a #trap style breakpoint
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002593 }
2594
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002595 // Don't fall through when hardware breakpoints were specifically requested
2596 if (bp_site->HardwareRequired())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002597 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002598 error.SetErrorString("hardware breakpoints are not supported");
2599 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002600 }
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002601
2602 // As a last resort we want to place a manual breakpoint. An instruction
2603 // is placed into the process memory using memory write packets.
2604 return EnableSoftwareBreakpoint(bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002605}
2606
2607Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002608ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002609{
2610 Error error;
2611 assert (bp_site != NULL);
2612 addr_t addr = bp_site->GetLoadAddress();
2613 user_id_t site_id = bp_site->GetID();
Greg Clayton5160ce52013-03-27 23:08:40 +00002614 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002615 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002616 log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002617
2618 if (bp_site->IsEnabled())
2619 {
2620 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2621
Greg Clayton8b82f082011-04-12 05:54:46 +00002622 BreakpointSite::Type bp_type = bp_site->GetType();
2623 switch (bp_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002624 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002625 case BreakpointSite::eSoftware:
2626 error = DisableSoftwareBreakpoint (bp_site);
2627 break;
2628
2629 case BreakpointSite::eHardware:
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002630 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, addr, bp_op_size))
Greg Clayton8b82f082011-04-12 05:54:46 +00002631 error.SetErrorToGenericError();
2632 break;
2633
2634 case BreakpointSite::eExternal:
Jim Inghama04ef752014-03-07 11:18:02 +00002635 {
2636 GDBStoppointType stoppoint_type;
2637 if (bp_site->IsHardware())
2638 stoppoint_type = eBreakpointHardware;
2639 else
2640 stoppoint_type = eBreakpointSoftware;
2641
2642 if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size))
Greg Clayton8b82f082011-04-12 05:54:46 +00002643 error.SetErrorToGenericError();
Jim Inghama04ef752014-03-07 11:18:02 +00002644 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002645 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002646 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002647 if (error.Success())
2648 bp_site->SetEnabled(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002649 }
2650 else
2651 {
2652 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002653 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 +00002654 return error;
2655 }
2656
2657 if (error.Success())
2658 error.SetErrorToGenericError();
2659 return error;
2660}
2661
Johnny Chen11309a32011-09-06 22:38:36 +00002662// Pre-requisite: wp != NULL.
2663static GDBStoppointType
Johnny Chen01a67862011-10-14 00:42:25 +00002664GetGDBStoppointType (Watchpoint *wp)
Johnny Chen11309a32011-09-06 22:38:36 +00002665{
2666 assert(wp);
2667 bool watch_read = wp->WatchpointRead();
2668 bool watch_write = wp->WatchpointWrite();
2669
2670 // watch_read and watch_write cannot both be false.
2671 assert(watch_read || watch_write);
2672 if (watch_read && watch_write)
2673 return eWatchpointReadWrite;
Johnny Chen6d487a92011-09-09 20:35:15 +00002674 else if (watch_read)
Johnny Chen11309a32011-09-06 22:38:36 +00002675 return eWatchpointRead;
Johnny Chen6d487a92011-09-09 20:35:15 +00002676 else // Must be watch_write, then.
Johnny Chen11309a32011-09-06 22:38:36 +00002677 return eWatchpointWrite;
2678}
2679
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002680Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002681ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002682{
2683 Error error;
2684 if (wp)
2685 {
2686 user_id_t watchID = wp->GetID();
2687 addr_t addr = wp->GetLoadAddress();
Greg Clayton5160ce52013-03-27 23:08:40 +00002688 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002689 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002690 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002691 if (wp->IsEnabled())
2692 {
2693 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002694 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 +00002695 return error;
2696 }
Johnny Chen11309a32011-09-06 22:38:36 +00002697
2698 GDBStoppointType type = GetGDBStoppointType(wp);
2699 // Pass down an appropriate z/Z packet...
2700 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002701 {
Johnny Chen11309a32011-09-06 22:38:36 +00002702 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
2703 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002704 wp->SetEnabled(true, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002705 return error;
2706 }
2707 else
2708 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002709 }
Johnny Chen11309a32011-09-06 22:38:36 +00002710 else
2711 error.SetErrorString("watchpoints not supported");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002712 }
2713 else
2714 {
Johnny Chen01a67862011-10-14 00:42:25 +00002715 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716 }
2717 if (error.Success())
2718 error.SetErrorToGenericError();
2719 return error;
2720}
2721
2722Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002723ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002724{
2725 Error error;
2726 if (wp)
2727 {
2728 user_id_t watchID = wp->GetID();
2729
Greg Clayton5160ce52013-03-27 23:08:40 +00002730 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002731
2732 addr_t addr = wp->GetLoadAddress();
Jim Ingham1b5792e2012-12-18 02:03:49 +00002733
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002734 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002735 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002736
Johnny Chen11309a32011-09-06 22:38:36 +00002737 if (!wp->IsEnabled())
2738 {
2739 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002740 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 +00002741 // See also 'class WatchpointSentry' within StopInfo.cpp.
2742 // This disabling attempt might come from the user-supplied actions, we'll route it in order for
2743 // the watchpoint object to intelligently process this action.
Jim Ingham1b5792e2012-12-18 02:03:49 +00002744 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002745 return error;
2746 }
2747
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002748 if (wp->IsHardware())
2749 {
Johnny Chen11309a32011-09-06 22:38:36 +00002750 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002751 // Pass down an appropriate z/Z packet...
Johnny Chen11309a32011-09-06 22:38:36 +00002752 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2753 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002754 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002755 return error;
2756 }
2757 else
2758 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002759 }
2760 // TODO: clear software watchpoints if we implement them
2761 }
2762 else
2763 {
Johnny Chen01a67862011-10-14 00:42:25 +00002764 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002765 }
2766 if (error.Success())
2767 error.SetErrorToGenericError();
2768 return error;
2769}
2770
2771void
2772ProcessGDBRemote::Clear()
2773{
2774 m_flags = 0;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00002775 m_thread_list_real.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002776 m_thread_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002777}
2778
2779Error
2780ProcessGDBRemote::DoSignal (int signo)
2781{
2782 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002783 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002784 if (log)
2785 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2786
2787 if (!m_gdb_comm.SendAsyncSignal (signo))
2788 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2789 return error;
2790}
2791
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002792Error
Greg Clayton91a9b2472013-12-04 19:19:12 +00002793ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002794{
2795 Error error;
2796 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2797 {
2798 // If we locate debugserver, keep that located version around
2799 static FileSpec g_debugserver_file_spec;
2800
Han Ming Ong84647042012-02-25 01:07:38 +00002801 ProcessLaunchInfo debugserver_launch_info;
Oleksiy Vyalovf8ce61c2015-01-28 17:36:59 +00002802 // Make debugserver run in its own session so signals generated by
2803 // special terminal key sequences (^C) don't affect debugserver.
2804 debugserver_launch_info.SetLaunchInSeparateProcessGroup(true);
2805
Greg Clayton91a9b2472013-12-04 19:19:12 +00002806 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2807 debugserver_launch_info.SetUserID(process_info.GetUserID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002808
Todd Fiala013434e2014-07-09 01:29:05 +00002809#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Greg Claytonfda4fab2014-01-10 22:24:11 +00002810 // On iOS, still do a local connection using a random port
Greg Clayton16810922014-02-27 19:38:18 +00002811 const char *hostname = "127.0.0.1";
Greg Claytonfda4fab2014-01-10 22:24:11 +00002812 uint16_t port = get_random_port ();
2813#else
2814 // Set hostname being NULL to do the reverse connect where debugserver
2815 // will bind to port zero and it will communicate back to us the port
2816 // that we will connect to
2817 const char *hostname = NULL;
2818 uint16_t port = 0;
2819#endif
2820
2821 error = m_gdb_comm.StartDebugserverProcess (hostname,
2822 port,
Greg Clayton00fe87b2013-12-05 22:58:22 +00002823 debugserver_launch_info,
2824 port);
Greg Clayton91a9b2472013-12-04 19:19:12 +00002825
2826 if (error.Success ())
2827 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002828 else
Greg Clayton91a9b2472013-12-04 19:19:12 +00002829 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002830
2831 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2832 StartAsyncThread ();
Greg Clayton91a9b2472013-12-04 19:19:12 +00002833
2834 if (error.Fail())
2835 {
2836 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
2837
2838 if (log)
2839 log->Printf("failed to start debugserver process: %s", error.AsCString());
2840 return error;
2841 }
2842
Greg Clayton00fe87b2013-12-05 22:58:22 +00002843 if (m_gdb_comm.IsConnected())
2844 {
2845 // Finish the connection process by doing the handshake without connecting (send NULL URL)
2846 ConnectToDebugserver (NULL);
2847 }
2848 else
2849 {
Greg Claytonfda4fab2014-01-10 22:24:11 +00002850 StreamString connect_url;
2851 connect_url.Printf("connect://%s:%u", hostname, port);
2852 error = ConnectToDebugserver (connect_url.GetString().c_str());
Greg Clayton00fe87b2013-12-05 22:58:22 +00002853 }
Greg Clayton91a9b2472013-12-04 19:19:12 +00002854
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002855 }
2856 return error;
2857}
2858
2859bool
2860ProcessGDBRemote::MonitorDebugserverProcess
2861(
2862 void *callback_baton,
2863 lldb::pid_t debugserver_pid,
Greg Claytone4e45922011-11-16 05:37:56 +00002864 bool exited, // True if the process did exit
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002865 int signo, // Zero for no signal
2866 int exit_status // Exit value of process if signal is zero
2867)
2868{
Greg Claytone4e45922011-11-16 05:37:56 +00002869 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2870 // and might not exist anymore, so we need to carefully try to get the
2871 // target for this process first since we have a race condition when
2872 // we are done running between getting the notice that the inferior
2873 // process has died and the debugserver that was debugging this process.
2874 // In our test suite, we are also continually running process after
2875 // process, so we must be very careful to make sure:
2876 // 1 - process object hasn't been deleted already
2877 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002878
2879 // "debugserver_pid" argument passed in is the process ID for
2880 // debugserver that we are tracking...
Greg Clayton5160ce52013-03-27 23:08:40 +00002881 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002882
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002883 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton6779606a2011-01-22 23:43:18 +00002884
Greg Claytone4e45922011-11-16 05:37:56 +00002885 // Get a shared pointer to the target that has a matching process pointer.
2886 // This target could be gone, or the target could already have a new process
2887 // object inside of it
2888 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2889
Greg Clayton6779606a2011-01-22 23:43:18 +00002890 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002891 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 +00002892
Greg Claytone4e45922011-11-16 05:37:56 +00002893 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002894 {
Greg Claytone4e45922011-11-16 05:37:56 +00002895 // We found a process in a target that matches, but another thread
2896 // might be in the process of launching a new process that will
2897 // soon replace it, so get a shared pointer to the process so we
2898 // can keep it alive.
2899 ProcessSP process_sp (target_sp->GetProcessSP());
2900 // Now we have a shared pointer to the process that can't go away on us
2901 // so we now make sure it was the same as the one passed in, and also make
2902 // sure that our previous "process *" didn't get deleted and have a new
2903 // "process *" created in its place with the same pointer. To verify this
2904 // we make sure the process has our debugserver process ID. If we pass all
2905 // of these tests, then we are sure that this process is the one we were
2906 // looking for.
2907 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002908 {
Greg Claytone4e45922011-11-16 05:37:56 +00002909 // Sleep for a half a second to make sure our inferior process has
2910 // time to set its exit status before we set it incorrectly when
2911 // both the debugserver and the inferior process shut down.
2912 usleep (500000);
2913 // If our process hasn't yet exited, debugserver might have died.
2914 // If the process did exit, the we are reaping it.
2915 const StateType state = process->GetState();
2916
2917 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2918 state != eStateInvalid &&
2919 state != eStateUnloaded &&
2920 state != eStateExited &&
2921 state != eStateDetached)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002922 {
Greg Claytone4e45922011-11-16 05:37:56 +00002923 char error_str[1024];
2924 if (signo)
2925 {
2926 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2927 if (signal_cstr)
2928 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2929 else
2930 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2931 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002932 else
Greg Claytone4e45922011-11-16 05:37:56 +00002933 {
2934 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2935 }
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002936
Greg Claytone4e45922011-11-16 05:37:56 +00002937 process->SetExitStatus (-1, error_str);
2938 }
2939 // Debugserver has exited we need to let our ProcessGDBRemote
2940 // know that it no longer has a debugserver instance
2941 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002942 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002943 }
2944 return true;
2945}
2946
2947void
2948ProcessGDBRemote::KillDebugserverProcess ()
2949{
Greg Claytonfbb76342013-11-20 21:07:01 +00002950 m_gdb_comm.Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002951 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2952 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002953 Host::Kill (m_debugserver_pid, SIGINT);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002954 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2955 }
2956}
2957
2958void
2959ProcessGDBRemote::Initialize()
2960{
Davide Italianoc8d69822015-04-03 04:24:32 +00002961 static std::once_flag g_once_flag;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002962
Davide Italianoc8d69822015-04-03 04:24:32 +00002963 std::call_once(g_once_flag, []()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002965 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2966 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +00002967 CreateInstance,
2968 DebuggerInitialize);
Davide Italianoc8d69822015-04-03 04:24:32 +00002969 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002970}
2971
Greg Clayton7f982402013-07-15 22:54:20 +00002972void
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002973ProcessGDBRemote::DebuggerInitialize (Debugger &debugger)
Greg Clayton7f982402013-07-15 22:54:20 +00002974{
2975 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
2976 {
2977 const bool is_global_setting = true;
2978 PluginManager::CreateSettingForProcessPlugin (debugger,
2979 GetGlobalPluginProperties()->GetValueProperties(),
2980 ConstString ("Properties for the gdb-remote process plug-in."),
2981 is_global_setting);
2982 }
2983}
2984
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002985bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002986ProcessGDBRemote::StartAsyncThread ()
2987{
Greg Clayton5160ce52013-03-27 23:08:40 +00002988 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002989
2990 if (log)
2991 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Jim Ingham455fa5c2012-11-01 01:15:33 +00002992
2993 Mutex::Locker start_locker(m_async_thread_state_mutex);
Zachary Turneracee96a2014-09-23 18:32:09 +00002994 if (!m_async_thread.IsJoinable())
Jim Ingham455fa5c2012-11-01 01:15:33 +00002995 {
2996 // Create a thread that watches our internal state and controls which
2997 // events make it to clients (into the DCProcess event queue).
Zachary Turner39de3112014-09-09 20:54:56 +00002998
2999 m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Jim Ingham455fa5c2012-11-01 01:15:33 +00003000 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003001 else if (log)
3002 log->Printf("ProcessGDBRemote::%s () - Called when Async thread was already running.", __FUNCTION__);
Zachary Turner39de3112014-09-09 20:54:56 +00003003
Zachary Turneracee96a2014-09-23 18:32:09 +00003004 return m_async_thread.IsJoinable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003005}
3006
3007void
3008ProcessGDBRemote::StopAsyncThread ()
3009{
Greg Clayton5160ce52013-03-27 23:08:40 +00003010 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011
3012 if (log)
3013 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
3014
Jim Ingham455fa5c2012-11-01 01:15:33 +00003015 Mutex::Locker start_locker(m_async_thread_state_mutex);
Zachary Turneracee96a2014-09-23 18:32:09 +00003016 if (m_async_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003017 {
Jim Ingham455fa5c2012-11-01 01:15:33 +00003018 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
3019
3020 // This will shut down the async thread.
3021 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
3022
3023 // Stop the stdio thread
Zachary Turner39de3112014-09-09 20:54:56 +00003024 m_async_thread.Join(nullptr);
Pavel Labatha55a9532015-03-11 09:53:42 +00003025 m_async_thread.Reset();
Jim Ingham455fa5c2012-11-01 01:15:33 +00003026 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003027 else if (log)
3028 log->Printf("ProcessGDBRemote::%s () - Called when Async thread was not running.", __FUNCTION__);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003029}
3030
3031
Virgile Bellob2f1fb22013-08-23 12:44:05 +00003032thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003033ProcessGDBRemote::AsyncThread (void *arg)
3034{
3035 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
3036
Greg Clayton5160ce52013-03-27 23:08:40 +00003037 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003038 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003039 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040
3041 Listener listener ("ProcessGDBRemote::AsyncThread");
3042 EventSP event_sp;
3043 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
3044 eBroadcastBitAsyncThreadShouldExit;
3045
3046 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
3047 {
Greg Clayton71337622011-02-24 22:24:29 +00003048 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
3049
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003050 bool done = false;
3051 while (!done)
3052 {
3053 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003054 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003055 if (listener.WaitForEvent (NULL, event_sp))
3056 {
3057 const uint32_t event_type = event_sp->GetType();
Greg Clayton71337622011-02-24 22:24:29 +00003058 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003059 {
Greg Clayton71337622011-02-24 22:24:29 +00003060 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003061 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 +00003062
Greg Clayton71337622011-02-24 22:24:29 +00003063 switch (event_type)
3064 {
3065 case eBroadcastBitAsyncContinue:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 {
Greg Clayton71337622011-02-24 22:24:29 +00003067 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068
Greg Clayton71337622011-02-24 22:24:29 +00003069 if (continue_packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003070 {
Greg Clayton71337622011-02-24 22:24:29 +00003071 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
3072 const size_t continue_cstr_len = continue_packet->GetByteSize ();
3073 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003074 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075
Greg Clayton71337622011-02-24 22:24:29 +00003076 if (::strstr (continue_cstr, "vAttach") == NULL)
3077 process->SetPrivateState(eStateRunning);
3078 StringExtractorGDBRemote response;
3079 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003080
Greg Clayton0772ded2012-05-16 02:48:06 +00003081 // We need to immediately clear the thread ID list so we are sure to get a valid list of threads.
3082 // The thread ID list might be contained within the "response", or the stop reply packet that
3083 // caused the stop. So clear it now before we give the stop reply packet to the process
3084 // using the process->SetLastStopPacket()...
3085 process->ClearThreadIDList ();
3086
Greg Clayton71337622011-02-24 22:24:29 +00003087 switch (stop_state)
3088 {
3089 case eStateStopped:
3090 case eStateCrashed:
3091 case eStateSuspended:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00003092 process->SetLastStopPacket (response);
Greg Clayton71337622011-02-24 22:24:29 +00003093 process->SetPrivateState (stop_state);
3094 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095
Greg Clayton71337622011-02-24 22:24:29 +00003096 case eStateExited:
Jason Molendaa3329782014-03-29 18:54:20 +00003097 {
Greg Clayton09c3e3d2011-12-06 04:51:14 +00003098 process->SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00003099 process->ClearThreadIDList();
Greg Clayton71337622011-02-24 22:24:29 +00003100 response.SetFilePos(1);
Jason Molendaa3329782014-03-29 18:54:20 +00003101
3102 int exit_status = response.GetHexU8();
3103 const char *desc_cstr = NULL;
3104 StringExtractor extractor;
3105 std::string desc_string;
3106 if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';')
3107 {
3108 std::string desc_token;
3109 while (response.GetNameColonValue (desc_token, desc_string))
3110 {
3111 if (desc_token == "description")
3112 {
3113 extractor.GetStringRef().swap(desc_string);
3114 extractor.SetFilePos(0);
3115 extractor.GetHexByteString (desc_string);
3116 desc_cstr = desc_string.c_str();
3117 }
3118 }
3119 }
3120 process->SetExitStatus(exit_status, desc_cstr);
Greg Clayton71337622011-02-24 22:24:29 +00003121 done = true;
3122 break;
Jason Molendaa3329782014-03-29 18:54:20 +00003123 }
Greg Clayton71337622011-02-24 22:24:29 +00003124 case eStateInvalid:
3125 process->SetExitStatus(-1, "lost connection");
3126 break;
3127
3128 default:
3129 process->SetPrivateState (stop_state);
3130 break;
3131 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003132 }
3133 }
Greg Clayton71337622011-02-24 22:24:29 +00003134 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003135
Greg Clayton71337622011-02-24 22:24:29 +00003136 case eBroadcastBitAsyncThreadShouldExit:
3137 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003138 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton71337622011-02-24 22:24:29 +00003139 done = true;
3140 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003141
Greg Clayton71337622011-02-24 22:24:29 +00003142 default:
3143 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003144 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 +00003145 done = true;
3146 break;
3147 }
3148 }
3149 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
3150 {
3151 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
3152 {
3153 process->SetExitStatus (-1, "lost connection");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003154 done = true;
Greg Clayton71337622011-02-24 22:24:29 +00003155 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156 }
3157 }
3158 else
3159 {
3160 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003161 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 +00003162 done = true;
3163 }
3164 }
3165 }
3166
3167 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003168 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003169
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003170 return NULL;
3171}
3172
Greg Claytone996fd32011-03-08 22:40:15 +00003173//uint32_t
3174//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3175//{
3176// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
3177// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
3178// if (m_local_debugserver)
3179// {
3180// return Host::ListProcessesMatchingName (name, matches, pids);
3181// }
3182// else
3183// {
3184// // FIXME: Implement talking to the remote debugserver.
3185// return 0;
3186// }
3187//
3188//}
3189//
Jim Ingham1c823b42011-01-22 01:33:44 +00003190bool
3191ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003192 StoppointCallbackContext *context,
Jim Ingham1c823b42011-01-22 01:33:44 +00003193 lldb::user_id_t break_id,
3194 lldb::user_id_t break_loc_id)
3195{
3196 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
3197 // run so I can stop it if that's what I want to do.
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003198 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham1c823b42011-01-22 01:33:44 +00003199 if (log)
3200 log->Printf("Hit New Thread Notification breakpoint.");
3201 return false;
3202}
3203
3204
3205bool
3206ProcessGDBRemote::StartNoticingNewThreads()
3207{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003208 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton4116e932012-05-15 02:33:01 +00003209 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003210 {
Greg Clayton4116e932012-05-15 02:33:01 +00003211 if (log && log->GetVerbose())
3212 log->Printf("Enabled noticing new thread breakpoint.");
3213 m_thread_create_bp_sp->SetEnabled(true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003214 }
Greg Clayton4116e932012-05-15 02:33:01 +00003215 else
Jim Ingham1c823b42011-01-22 01:33:44 +00003216 {
Greg Clayton4116e932012-05-15 02:33:01 +00003217 PlatformSP platform_sp (m_target.GetPlatform());
3218 if (platform_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003219 {
Greg Clayton4116e932012-05-15 02:33:01 +00003220 m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target);
3221 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003222 {
Jim Ingham37cfeab2011-10-15 00:21:37 +00003223 if (log && log->GetVerbose())
Greg Clayton4116e932012-05-15 02:33:01 +00003224 log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID());
3225 m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003226 }
3227 else
3228 {
3229 if (log)
3230 log->Printf("Failed to create new thread notification breakpoint.");
Jim Ingham1c823b42011-01-22 01:33:44 +00003231 }
3232 }
3233 }
Greg Clayton4116e932012-05-15 02:33:01 +00003234 return m_thread_create_bp_sp.get() != NULL;
Jim Ingham1c823b42011-01-22 01:33:44 +00003235}
3236
3237bool
3238ProcessGDBRemote::StopNoticingNewThreads()
3239{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003240 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham37cfeab2011-10-15 00:21:37 +00003241 if (log && log->GetVerbose())
Jim Ingham0e97cbc2011-02-08 05:19:01 +00003242 log->Printf ("Disabling new thread notification breakpoint.");
Greg Clayton4116e932012-05-15 02:33:01 +00003243
3244 if (m_thread_create_bp_sp)
3245 m_thread_create_bp_sp->SetEnabled(false);
3246
Jim Ingham1c823b42011-01-22 01:33:44 +00003247 return true;
3248}
3249
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003250DynamicLoader *
Jason Molenda5e8534e2012-10-03 01:29:34 +00003251ProcessGDBRemote::GetDynamicLoader ()
3252{
3253 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +00003254 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Jason Molenda5e8534e2012-10-03 01:29:34 +00003255 return m_dyld_ap.get();
3256}
Jim Ingham1c823b42011-01-22 01:33:44 +00003257
Jason Molendaa3329782014-03-29 18:54:20 +00003258Error
3259ProcessGDBRemote::SendEventData(const char *data)
3260{
3261 int return_value;
3262 bool was_supported;
3263
3264 Error error;
3265
3266 return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported);
3267 if (return_value != 0)
3268 {
3269 if (!was_supported)
3270 error.SetErrorString("Sending events is not supported for this process.");
3271 else
3272 error.SetErrorStringWithFormat("Error sending event data: %d.", return_value);
3273 }
3274 return error;
3275}
3276
Steve Pucci03904ac2014-03-04 23:18:46 +00003277const DataBufferSP
3278ProcessGDBRemote::GetAuxvData()
3279{
3280 DataBufferSP buf;
3281 if (m_gdb_comm.GetQXferAuxvReadSupported())
3282 {
3283 std::string response_string;
3284 if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", response_string) == GDBRemoteCommunication::PacketResult::Success)
3285 buf.reset(new DataBufferHeap(response_string.c_str(), response_string.length()));
3286 }
3287 return buf;
3288}
3289
Jason Molenda705b1802014-06-13 02:37:02 +00003290StructuredData::ObjectSP
3291ProcessGDBRemote::GetExtendedInfoForThread (lldb::tid_t tid)
3292{
3293 StructuredData::ObjectSP object_sp;
3294
3295 if (m_gdb_comm.GetThreadExtendedInfoSupported())
3296 {
3297 StructuredData::ObjectSP args_dict(new StructuredData::Dictionary());
3298 SystemRuntime *runtime = GetSystemRuntime();
3299 if (runtime)
3300 {
3301 runtime->AddThreadExtendedInfoPacketHints (args_dict);
3302 }
3303 args_dict->GetAsDictionary()->AddIntegerItem ("thread", tid);
3304
3305 StreamString packet;
3306 packet << "jThreadExtendedInfo:";
3307 args_dict->Dump (packet);
3308
3309 // FIXME the final character of a JSON dictionary, '}', is the escape
3310 // character in gdb-remote binary mode. lldb currently doesn't escape
3311 // these characters in its packet output -- so we add the quoted version
3312 // of the } character here manually in case we talk to a debugserver which
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003313 // un-escapes the characters at packet read time.
Jason Molenda705b1802014-06-13 02:37:02 +00003314 packet << (char) (0x7d ^ 0x20);
3315
3316 StringExtractorGDBRemote response;
3317 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success)
3318 {
3319 StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType();
3320 if (response_type == StringExtractorGDBRemote::eResponse)
3321 {
3322 if (!response.Empty())
3323 {
3324 // The packet has already had the 0x7d xor quoting stripped out at the
3325 // GDBRemoteCommunication packet receive level.
3326 object_sp = StructuredData::ParseJSON (response.GetStringRef());
3327 }
3328 }
3329 }
3330 }
3331 return object_sp;
3332}
3333
Jason Molenda6076bf42014-05-06 04:34:52 +00003334// Establish the largest memory read/write payloads we should use.
3335// If the remote stub has a max packet size, stay under that size.
3336//
3337// If the remote stub's max packet size is crazy large, use a
3338// reasonable largeish default.
3339//
3340// If the remote stub doesn't advertise a max packet size, use a
3341// conservative default.
3342
3343void
3344ProcessGDBRemote::GetMaxMemorySize()
3345{
3346 const uint64_t reasonable_largeish_default = 128 * 1024;
3347 const uint64_t conservative_default = 512;
3348
3349 if (m_max_memory_size == 0)
3350 {
3351 uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize();
3352 if (stub_max_size != UINT64_MAX && stub_max_size != 0)
3353 {
3354 // Save the stub's claimed maximum packet size
3355 m_remote_stub_max_memory_size = stub_max_size;
3356
3357 // Even if the stub says it can support ginormous packets,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003358 // don't exceed our reasonable largeish default packet size.
Jason Molenda6076bf42014-05-06 04:34:52 +00003359 if (stub_max_size > reasonable_largeish_default)
3360 {
3361 stub_max_size = reasonable_largeish_default;
3362 }
3363
3364 m_max_memory_size = stub_max_size;
3365 }
3366 else
3367 {
3368 m_max_memory_size = conservative_default;
3369 }
3370 }
3371}
3372
3373void
3374ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max)
3375{
3376 if (user_specified_max != 0)
3377 {
3378 GetMaxMemorySize ();
3379
3380 if (m_remote_stub_max_memory_size != 0)
3381 {
3382 if (m_remote_stub_max_memory_size < user_specified_max)
3383 {
3384 m_max_memory_size = m_remote_stub_max_memory_size; // user specified a packet size too big, go as big
3385 // as the remote stub says we can go.
3386 }
3387 else
3388 {
3389 m_max_memory_size = user_specified_max; // user's packet size is good
3390 }
3391 }
3392 else
3393 {
3394 m_max_memory_size = user_specified_max; // user's packet size is probably fine
3395 }
3396 }
3397}
3398
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003399bool
3400ProcessGDBRemote::GetModuleSpec(const FileSpec& module_file_spec,
3401 const ArchSpec& arch,
3402 ModuleSpec &module_spec)
3403{
3404 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
3405
3406 if (!m_gdb_comm.GetModuleInfo (module_file_spec, arch, module_spec))
3407 {
3408 if (log)
3409 log->Printf ("ProcessGDBRemote::%s - failed to get module info for %s:%s",
3410 __FUNCTION__, module_file_spec.GetPath ().c_str (),
3411 arch.GetTriple ().getTriple ().c_str ());
3412 return false;
3413 }
3414
3415 if (log)
3416 {
3417 StreamString stream;
3418 module_spec.Dump (stream);
3419 log->Printf ("ProcessGDBRemote::%s - got module info for (%s:%s) : %s",
3420 __FUNCTION__, module_file_spec.GetPath ().c_str (),
3421 arch.GetTriple ().getTriple ().c_str (), stream.GetString ().c_str ());
3422 }
3423
3424 return true;
3425}
3426
Greg Clayton02686b82012-10-15 22:42:16 +00003427class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed
Greg Clayton998255b2012-10-13 02:07:45 +00003428{
3429private:
3430
3431public:
Greg Clayton02686b82012-10-15 22:42:16 +00003432 CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) :
Greg Clayton998255b2012-10-13 02:07:45 +00003433 CommandObjectParsed (interpreter,
Greg Clayton02686b82012-10-15 22:42:16 +00003434 "process plugin packet history",
3435 "Dumps the packet history buffer. ",
Greg Clayton998255b2012-10-13 02:07:45 +00003436 NULL)
3437 {
3438 }
3439
Greg Clayton02686b82012-10-15 22:42:16 +00003440 ~CommandObjectProcessGDBRemotePacketHistory ()
Greg Clayton998255b2012-10-13 02:07:45 +00003441 {
3442 }
3443
3444 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003445 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton998255b2012-10-13 02:07:45 +00003446 {
Greg Clayton02686b82012-10-15 22:42:16 +00003447 const size_t argc = command.GetArgumentCount();
3448 if (argc == 0)
3449 {
3450 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3451 if (process)
3452 {
3453 process->GetGDBRemote().DumpHistory(result.GetOutputStream());
3454 result.SetStatus (eReturnStatusSuccessFinishResult);
3455 return true;
3456 }
3457 }
3458 else
3459 {
3460 result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str());
3461 }
3462 result.SetStatus (eReturnStatusFailed);
3463 return false;
3464 }
3465};
3466
Jason Molenda6076bf42014-05-06 04:34:52 +00003467class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed
3468{
3469private:
3470
3471public:
3472 CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) :
3473 CommandObjectParsed (interpreter,
3474 "process plugin packet xfer-size",
3475 "Maximum size that lldb will try to read/write one one chunk.",
3476 NULL)
3477 {
3478 }
3479
3480 ~CommandObjectProcessGDBRemotePacketXferSize ()
3481 {
3482 }
3483
3484 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003485 DoExecute (Args& command, CommandReturnObject &result) override
Jason Molenda6076bf42014-05-06 04:34:52 +00003486 {
3487 const size_t argc = command.GetArgumentCount();
3488 if (argc == 0)
3489 {
3490 result.AppendErrorWithFormat ("'%s' takes an argument to specify the max amount to be transferred when reading/writing", m_cmd_name.c_str());
3491 result.SetStatus (eReturnStatusFailed);
3492 return false;
3493 }
3494
3495 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3496 if (process)
3497 {
3498 const char *packet_size = command.GetArgumentAtIndex(0);
3499 errno = 0;
3500 uint64_t user_specified_max = strtoul (packet_size, NULL, 10);
3501 if (errno == 0 && user_specified_max != 0)
3502 {
3503 process->SetUserSpecifiedMaxMemoryTransferSize (user_specified_max);
3504 result.SetStatus (eReturnStatusSuccessFinishResult);
3505 return true;
3506 }
3507 }
3508 result.SetStatus (eReturnStatusFailed);
3509 return false;
3510 }
3511};
3512
3513
Greg Clayton02686b82012-10-15 22:42:16 +00003514class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed
3515{
3516private:
3517
3518public:
3519 CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) :
3520 CommandObjectParsed (interpreter,
3521 "process plugin packet send",
3522 "Send a custom packet through the GDB remote protocol and print the answer. "
3523 "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.",
3524 NULL)
3525 {
3526 }
3527
3528 ~CommandObjectProcessGDBRemotePacketSend ()
3529 {
3530 }
3531
3532 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003533 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton02686b82012-10-15 22:42:16 +00003534 {
3535 const size_t argc = command.GetArgumentCount();
3536 if (argc == 0)
3537 {
3538 result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str());
3539 result.SetStatus (eReturnStatusFailed);
3540 return false;
3541 }
3542
3543 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3544 if (process)
3545 {
Han Ming Ong84145852012-11-26 20:42:03 +00003546 for (size_t i=0; i<argc; ++ i)
Greg Clayton02686b82012-10-15 22:42:16 +00003547 {
Han Ming Ong84145852012-11-26 20:42:03 +00003548 const char *packet_cstr = command.GetArgumentAtIndex(0);
3549 bool send_async = true;
3550 StringExtractorGDBRemote response;
3551 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3552 result.SetStatus (eReturnStatusSuccessFinishResult);
3553 Stream &output_strm = result.GetOutputStream();
3554 output_strm.Printf (" packet: %s\n", packet_cstr);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003555 std::string &response_str = response.GetStringRef();
3556
Han Ming Ong399289e2013-06-21 19:56:59 +00003557 if (strstr(packet_cstr, "qGetProfileData") != NULL)
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003558 {
3559 response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response);
3560 }
3561
Han Ming Ong84145852012-11-26 20:42:03 +00003562 if (response_str.empty())
3563 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3564 else
3565 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
Greg Clayton02686b82012-10-15 22:42:16 +00003566 }
Greg Clayton02686b82012-10-15 22:42:16 +00003567 }
Greg Clayton998255b2012-10-13 02:07:45 +00003568 return true;
3569 }
3570};
3571
Greg Claytonba4a0a52013-02-01 23:03:47 +00003572class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw
3573{
3574private:
3575
3576public:
3577 CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) :
3578 CommandObjectRaw (interpreter,
3579 "process plugin packet monitor",
Greg Claytoneee5e982013-02-14 18:39:30 +00003580 "Send a qRcmd packet through the GDB remote protocol and print the response."
3581 "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 +00003582 NULL)
3583 {
3584 }
3585
3586 ~CommandObjectProcessGDBRemotePacketMonitor ()
3587 {
3588 }
3589
3590 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003591 DoExecute (const char *command, CommandReturnObject &result) override
Greg Claytonba4a0a52013-02-01 23:03:47 +00003592 {
3593 if (command == NULL || command[0] == '\0')
3594 {
3595 result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str());
3596 result.SetStatus (eReturnStatusFailed);
3597 return false;
3598 }
3599
3600 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3601 if (process)
3602 {
3603 StreamString packet;
Greg Claytoneee5e982013-02-14 18:39:30 +00003604 packet.PutCString("qRcmd,");
Greg Claytonba4a0a52013-02-01 23:03:47 +00003605 packet.PutBytesAsRawHex8(command, strlen(command));
3606 const char *packet_cstr = packet.GetString().c_str();
3607
3608 bool send_async = true;
3609 StringExtractorGDBRemote response;
3610 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3611 result.SetStatus (eReturnStatusSuccessFinishResult);
3612 Stream &output_strm = result.GetOutputStream();
3613 output_strm.Printf (" packet: %s\n", packet_cstr);
3614 const std::string &response_str = response.GetStringRef();
3615
3616 if (response_str.empty())
3617 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3618 else
3619 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
3620 }
3621 return true;
3622 }
3623};
3624
Greg Clayton02686b82012-10-15 22:42:16 +00003625class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword
3626{
3627private:
3628
3629public:
3630 CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) :
3631 CommandObjectMultiword (interpreter,
3632 "process plugin packet",
3633 "Commands that deal with GDB remote packets.",
3634 NULL)
3635 {
3636 LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter)));
3637 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter)));
Greg Claytonba4a0a52013-02-01 23:03:47 +00003638 LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter)));
Jason Molenda6076bf42014-05-06 04:34:52 +00003639 LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter)));
Greg Clayton02686b82012-10-15 22:42:16 +00003640 }
3641
3642 ~CommandObjectProcessGDBRemotePacket ()
3643 {
3644 }
3645};
Greg Clayton998255b2012-10-13 02:07:45 +00003646
3647class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword
3648{
3649public:
3650 CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) :
3651 CommandObjectMultiword (interpreter,
3652 "process plugin",
3653 "A set of commands for operating on a ProcessGDBRemote process.",
3654 "process plugin <subcommand> [<subcommand-options>]")
3655 {
3656 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter)));
3657 }
3658
3659 ~CommandObjectMultiwordProcessGDBRemote ()
3660 {
3661 }
3662};
3663
Greg Clayton998255b2012-10-13 02:07:45 +00003664CommandObject *
3665ProcessGDBRemote::GetPluginCommandObject()
3666{
3667 if (!m_command_sp)
3668 m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter()));
3669 return m_command_sp.get();
3670}