blob: 2ff5e1357504defb54c4a7cc511c7a0c1e40f79e [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>
Benjamin Kramer3f69fa62015-04-03 10:55:00 +000027#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29// Other libraries and framework includes
30
Johnny Chen01a67862011-10-14 00:42:25 +000031#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000032#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/ArchSpec.h"
34#include "lldb/Core/Debugger.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000035#include "lldb/Host/ConnectionFileDescriptor.h"
Greg Clayton53239f02011-02-08 05:05:52 +000036#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/PluginManager.h"
40#include "lldb/Core/State.h"
Greg Claytond451c1a2012-04-13 21:24:18 +000041#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Core/StreamString.h"
43#include "lldb/Core/Timer.h"
Greg Clayton70b57652011-05-15 01:25:55 +000044#include "lldb/Core/Value.h"
Zachary Turner39de3112014-09-09 20:54:56 +000045#include "lldb/Host/HostThread.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000046#include "lldb/Host/StringConvert.h"
Jason Molendad1fae142012-09-29 08:03:33 +000047#include "lldb/Host/Symbols.h"
Zachary Turner39de3112014-09-09 20:54:56 +000048#include "lldb/Host/ThreadLauncher.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Host/TimeValue.h"
Greg Clayton02686b82012-10-15 22:42:16 +000050#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000051#include "lldb/Interpreter/CommandObject.h"
52#include "lldb/Interpreter/CommandObjectMultiword.h"
Greg Clayton02686b82012-10-15 22:42:16 +000053#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000054#include "lldb/Interpreter/OptionValueProperties.h"
55#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Symbol/ObjectFile.h"
57#include "lldb/Target/DynamicLoader.h"
58#include "lldb/Target/Target.h"
59#include "lldb/Target/TargetList.h"
Greg Clayton2a48f522011-05-14 01:50:35 +000060#include "lldb/Target/ThreadPlanCallFunction.h"
Jason Molenda705b1802014-06-13 02:37:02 +000061#include "lldb/Target/SystemRuntime.h"
Jason Molendaa34a0c62010-06-09 21:28:42 +000062#include "lldb/Utility/PseudoTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64// Project includes
65#include "lldb/Host/Host.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000066#include "Plugins/Process/Utility/FreeBSDSignals.h"
Peter Collingbourne99f9aa02011-06-03 20:40:38 +000067#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000068#include "Plugins/Process/Utility/LinuxSignals.h"
Jason Molendac42d2432012-07-25 03:40:06 +000069#include "Plugins/Process/Utility/StopInfoMachException.h"
Jim Ingham43c555d2012-07-04 00:35:43 +000070#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
Greg Claytonc982c762010-07-09 20:39:50 +000071#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072#include "GDBRemoteRegisterContext.h"
73#include "ProcessGDBRemote.h"
74#include "ProcessGDBRemoteLog.h"
75#include "ThreadGDBRemote.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000076
Tamas Berghammerdb264a62015-03-31 09:52:22 +000077#define DEBUGSERVER_BASENAME "debugserver"
78using namespace lldb;
79using namespace lldb_private;
80using namespace lldb_private::process_gdb_remote;
Jason Molenda5e8534e2012-10-03 01:29:34 +000081
Greg Claytonc1422c12012-04-09 22:46:21 +000082namespace lldb
83{
84 // Provide a function that can easily dump the packet history if we know a
85 // ProcessGDBRemote * value (which we can get from logs or from debugging).
86 // We need the function in the lldb namespace so it makes it into the final
87 // executable since the LLDB shared library only exports stuff in the lldb
88 // namespace. This allows you to attach with a debugger and call this
89 // function and get the packet history dumped to a file.
90 void
91 DumpProcessGDBRemotePacketHistory (void *p, const char *path)
92 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +000093 StreamFile strm;
94 Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate));
Greg Claytond451c1a2012-04-13 21:24:18 +000095 if (error.Success())
96 ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +000097 }
Filipe Cabecinhasc34f7762012-05-23 16:27:09 +000098}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Greg Clayton7f982402013-07-15 22:54:20 +0000100namespace {
Steve Pucci5ec012d2014-01-16 22:18:14 +0000101
Greg Clayton7f982402013-07-15 22:54:20 +0000102 static PropertyDefinition
103 g_properties[] =
104 {
105 { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." },
Greg Claytonef8180a2013-10-15 00:14:28 +0000106 { "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 +0000107 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
108 };
109
110 enum
111 {
Greg Claytonef8180a2013-10-15 00:14:28 +0000112 ePropertyPacketTimeout,
113 ePropertyTargetDefinitionFile
Greg Clayton7f982402013-07-15 22:54:20 +0000114 };
115
116 class PluginProperties : public Properties
117 {
118 public:
119
120 static ConstString
121 GetSettingName ()
122 {
123 return ProcessGDBRemote::GetPluginNameStatic();
124 }
125
126 PluginProperties() :
127 Properties ()
128 {
129 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
130 m_collection_sp->Initialize(g_properties);
131 }
132
133 virtual
134 ~PluginProperties()
135 {
136 }
137
138 uint64_t
139 GetPacketTimeout()
140 {
141 const uint32_t idx = ePropertyPacketTimeout;
142 return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
143 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000144
145 bool
146 SetPacketTimeout(uint64_t timeout)
147 {
148 const uint32_t idx = ePropertyPacketTimeout;
149 return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout);
150 }
151
Greg Claytonef8180a2013-10-15 00:14:28 +0000152 FileSpec
153 GetTargetDefinitionFile () const
154 {
155 const uint32_t idx = ePropertyTargetDefinitionFile;
156 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
157 }
Greg Clayton7f982402013-07-15 22:54:20 +0000158 };
159
160 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
161
162 static const ProcessKDPPropertiesSP &
163 GetGlobalPluginProperties()
164 {
165 static ProcessKDPPropertiesSP g_settings_sp;
166 if (!g_settings_sp)
167 g_settings_sp.reset (new PluginProperties ());
168 return g_settings_sp;
169 }
170
171} // anonymous namespace end
172
Greg Claytonfda4fab2014-01-10 22:24:11 +0000173// TODO Randomly assigning a port is unsafe. We should get an unused
174// ephemeral port from the kernel and make sure we reserve it before passing
175// it to debugserver.
176
177#if defined (__APPLE__)
178#define LOW_PORT (IPPORT_RESERVED)
179#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
180#else
181#define LOW_PORT (1024u)
182#define HIGH_PORT (49151u)
183#endif
184
Todd Fiala013434e2014-07-09 01:29:05 +0000185#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000186static bool rand_initialized = false;
187
Greg Claytonfda4fab2014-01-10 22:24:11 +0000188static inline uint16_t
189get_random_port ()
190{
191 if (!rand_initialized)
192 {
193 time_t seed = time(NULL);
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000194
Greg Claytonfda4fab2014-01-10 22:24:11 +0000195 rand_initialized = true;
196 srand(seed);
197 }
198 return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT;
199}
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +0000200#endif
Greg Claytonfda4fab2014-01-10 22:24:11 +0000201
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000202ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203ProcessGDBRemote::GetPluginNameStatic()
204{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000205 static ConstString g_name("gdb-remote");
206 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
209const char *
210ProcessGDBRemote::GetPluginDescriptionStatic()
211{
212 return "GDB Remote protocol based debugging plug-in.";
213}
214
215void
216ProcessGDBRemote::Terminate()
217{
218 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
219}
220
221
Greg Claytonc3776bf2012-02-09 06:16:32 +0000222lldb::ProcessSP
223ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224{
Greg Claytonc3776bf2012-02-09 06:16:32 +0000225 lldb::ProcessSP process_sp;
226 if (crash_file_path == NULL)
227 process_sp.reset (new ProcessGDBRemote (target, listener));
228 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229}
230
231bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000232ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233{
Greg Clayton596ed242011-10-21 21:41:45 +0000234 if (plugin_specified_by_name)
235 return true;
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +0000238 Module *exe_module = target.GetExecutableModulePointer();
239 if (exe_module)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000240 {
241 ObjectFile *exe_objfile = exe_module->GetObjectFile();
242 // We can't debug core files...
243 switch (exe_objfile->GetType())
244 {
245 case ObjectFile::eTypeInvalid:
246 case ObjectFile::eTypeCoreFile:
247 case ObjectFile::eTypeDebugInfo:
248 case ObjectFile::eTypeObjectFile:
249 case ObjectFile::eTypeSharedLibrary:
250 case ObjectFile::eTypeStubLibrary:
Greg Clayton23f8c952014-03-24 23:10:19 +0000251 case ObjectFile::eTypeJIT:
Greg Claytonc3776bf2012-02-09 06:16:32 +0000252 return false;
253 case ObjectFile::eTypeExecutable:
254 case ObjectFile::eTypeDynamicLinker:
255 case ObjectFile::eTypeUnknown:
256 break;
257 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000258 return exe_module->GetFileSpec().Exists();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000259 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000260 // However, if there is no executable module, we return true since we might be preparing to attach.
261 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
264//----------------------------------------------------------------------
265// ProcessGDBRemote constructor
266//----------------------------------------------------------------------
267ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
268 Process (target, listener),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_flags (0),
Tamas Berghammere13c2732015-02-11 10:29:30 +0000270 m_gdb_comm (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Claytonc982c762010-07-09 20:39:50 +0000272 m_last_stop_packet (),
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000273 m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 m_register_info (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000275 m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
Jim Ingham455fa5c2012-11-01 01:15:33 +0000276 m_async_thread_state_mutex(Mutex::eMutexTypeRecursive),
Greg Clayton9e920902012-04-10 02:25:43 +0000277 m_thread_ids (),
Greg Clayton71fc2a32011-02-12 06:28:37 +0000278 m_continue_c_tids (),
279 m_continue_C_tids (),
280 m_continue_s_tids (),
281 m_continue_S_tids (),
Jason Molenda6076bf42014-05-06 04:34:52 +0000282 m_max_memory_size (0),
283 m_remote_stub_max_memory_size (0),
Greg Clayton4116e932012-05-15 02:33:01 +0000284 m_addr_to_mmap_size (),
285 m_thread_create_bp_sp (),
Jim Ingham43c555d2012-07-04 00:35:43 +0000286 m_waiting_for_attach (false),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000287 m_destroy_tried_resuming (false),
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000288 m_command_sp (),
Todd Fiala4ceced32014-08-29 17:35:57 +0000289 m_breakpoint_pc_offset (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290{
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000291 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
292 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Jim Inghamb1e2e842012-04-12 18:49:31 +0000293 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit");
Greg Clayton7f982402013-07-15 22:54:20 +0000294 const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
295 if (timeout_seconds > 0)
296 m_gdb_comm.SetPacketTimeout(timeout_seconds);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
298
299//----------------------------------------------------------------------
300// Destructor
301//----------------------------------------------------------------------
302ProcessGDBRemote::~ProcessGDBRemote()
303{
304 // m_mach_process.UnregisterNotificationCallbacks (this);
305 Clear();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000306 // We need to call finalize on the process before destroying ourselves
307 // to make sure all of the broadcaster cleanup goes as planned. If we
308 // destruct this class, then Process::~Process() might have problems
309 // trying to fully destroy the broadcaster.
310 Finalize();
Jim Ingham455fa5c2012-11-01 01:15:33 +0000311
312 // The general Finalize is going to try to destroy the process and that SHOULD
313 // shut down the async thread. However, if we don't kill it it will get stranded and
314 // its connection will go away so when it wakes up it will crash. So kill it for sure here.
315 StopAsyncThread();
316 KillDebugserverProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317}
318
319//----------------------------------------------------------------------
320// PluginInterface
321//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000322ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323ProcessGDBRemote::GetPluginName()
324{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 return GetPluginNameStatic();
326}
327
328uint32_t
329ProcessGDBRemote::GetPluginVersion()
330{
331 return 1;
332}
333
Greg Claytonef8180a2013-10-15 00:14:28 +0000334bool
335ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec)
336{
337 ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
338 Error error;
Zachary Turner0641ca12015-03-17 20:04:04 +0000339 StructuredData::ObjectSP module_object_sp(interpreter->LoadPluginModule(target_definition_fspec, error));
Greg Claytonef8180a2013-10-15 00:14:28 +0000340 if (module_object_sp)
341 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000342 StructuredData::DictionarySP target_definition_sp(
343 interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), "gdb-server-target-definition", error));
Greg Claytonef8180a2013-10-15 00:14:28 +0000344
Zachary Turner0641ca12015-03-17 20:04:04 +0000345 if (target_definition_sp)
Greg Claytonef8180a2013-10-15 00:14:28 +0000346 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000347 StructuredData::ObjectSP target_object(target_definition_sp->GetValueForKey("host-info"));
348 if (target_object)
Greg Clayton312bcbe2013-10-17 01:10:23 +0000349 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000350 if (auto host_info_dict = target_object->GetAsDictionary())
Greg Clayton312bcbe2013-10-17 01:10:23 +0000351 {
Zachary Turner0641ca12015-03-17 20:04:04 +0000352 StructuredData::ObjectSP triple_value = host_info_dict->GetValueForKey("triple");
353 if (auto triple_string_value = triple_value->GetAsString())
354 {
355 std::string triple_string = triple_string_value->GetValue();
356 ArchSpec host_arch(triple_string.c_str());
357 if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture()))
358 {
359 GetTarget().SetArchitecture(host_arch);
360 }
361 }
Greg Clayton312bcbe2013-10-17 01:10:23 +0000362 }
Greg Clayton312bcbe2013-10-17 01:10:23 +0000363 }
Zachary Turner0641ca12015-03-17 20:04:04 +0000364 m_breakpoint_pc_offset = 0;
365 StructuredData::ObjectSP breakpoint_pc_offset_value = target_definition_sp->GetValueForKey("breakpoint-pc-offset");
366 if (breakpoint_pc_offset_value)
367 {
368 if (auto breakpoint_pc_int_value = breakpoint_pc_offset_value->GetAsInteger())
369 m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue();
370 }
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000371
Zachary Turner0641ca12015-03-17 20:04:04 +0000372 if (m_register_info.SetRegisterInfo(*target_definition_sp, GetTarget().GetArchitecture().GetByteOrder()) > 0)
Greg Claytonef8180a2013-10-15 00:14:28 +0000373 {
374 return true;
375 }
376 }
377 }
378 return false;
379}
380
381
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382void
Greg Clayton513c26c2011-01-29 07:10:55 +0000383ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384{
Greg Clayton513c26c2011-01-29 07:10:55 +0000385 if (!force && m_register_info.GetNumRegisters() > 0)
386 return;
387
388 char packet[128];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 m_register_info.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 uint32_t reg_offset = 0;
391 uint32_t reg_num = 0;
Greg Clayton23f59502012-07-17 03:23:13 +0000392 for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse;
Greg Clayton576d8832011-03-22 04:00:09 +0000393 response_type == StringExtractorGDBRemote::eResponse;
394 ++reg_num)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 {
Greg Clayton513c26c2011-01-29 07:10:55 +0000396 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
Andy Gibbsa297a972013-06-19 19:04:53 +0000397 assert (packet_len < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000399 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 {
Greg Clayton576d8832011-03-22 04:00:09 +0000401 response_type = response.GetResponseType();
402 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
404 std::string name;
405 std::string value;
406 ConstString reg_name;
407 ConstString alt_name;
408 ConstString set_name;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000409 std::vector<uint32_t> value_regs;
410 std::vector<uint32_t> invalidate_regs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 RegisterInfo reg_info = { NULL, // Name
412 NULL, // Alt name
413 0, // byte size
414 reg_offset, // offset
415 eEncodingUint, // encoding
416 eFormatHex, // formate
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 {
418 LLDB_INVALID_REGNUM, // GCC reg num
419 LLDB_INVALID_REGNUM, // DWARF reg num
420 LLDB_INVALID_REGNUM, // generic reg num
Jason Molendafbcb7f22010-09-10 07:49:16 +0000421 reg_num, // GDB reg num
422 reg_num // native register number
Greg Clayton435d85a2012-02-29 19:27:27 +0000423 },
424 NULL,
425 NULL
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 };
427
428 while (response.GetNameColonValue(name, value))
429 {
430 if (name.compare("name") == 0)
431 {
432 reg_name.SetCString(value.c_str());
433 }
434 else if (name.compare("alt-name") == 0)
435 {
436 alt_name.SetCString(value.c_str());
437 }
438 else if (name.compare("bitsize") == 0)
439 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000440 reg_info.byte_size = StringConvert::ToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 }
442 else if (name.compare("offset") == 0)
443 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000444 uint32_t offset = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda743e86a2010-06-11 23:44:18 +0000445 if (reg_offset != offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 {
447 reg_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 }
449 }
450 else if (name.compare("encoding") == 0)
451 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000452 const Encoding encoding = Args::StringToEncoding (value.c_str());
453 if (encoding != eEncodingInvalid)
454 reg_info.encoding = encoding;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 }
456 else if (name.compare("format") == 0)
457 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000458 Format format = eFormatInvalid;
459 if (Args::StringToFormat (value.c_str(), format, NULL).Success())
460 reg_info.format = format;
461 else if (value.compare("binary") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 reg_info.format = eFormatBinary;
463 else if (value.compare("decimal") == 0)
464 reg_info.format = eFormatDecimal;
465 else if (value.compare("hex") == 0)
466 reg_info.format = eFormatHex;
467 else if (value.compare("float") == 0)
468 reg_info.format = eFormatFloat;
469 else if (value.compare("vector-sint8") == 0)
470 reg_info.format = eFormatVectorOfSInt8;
471 else if (value.compare("vector-uint8") == 0)
472 reg_info.format = eFormatVectorOfUInt8;
473 else if (value.compare("vector-sint16") == 0)
474 reg_info.format = eFormatVectorOfSInt16;
475 else if (value.compare("vector-uint16") == 0)
476 reg_info.format = eFormatVectorOfUInt16;
477 else if (value.compare("vector-sint32") == 0)
478 reg_info.format = eFormatVectorOfSInt32;
479 else if (value.compare("vector-uint32") == 0)
480 reg_info.format = eFormatVectorOfUInt32;
481 else if (value.compare("vector-float32") == 0)
482 reg_info.format = eFormatVectorOfFloat32;
483 else if (value.compare("vector-uint128") == 0)
484 reg_info.format = eFormatVectorOfUInt128;
485 }
486 else if (name.compare("set") == 0)
487 {
488 set_name.SetCString(value.c_str());
489 }
490 else if (name.compare("gcc") == 0)
491 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000492 reg_info.kinds[eRegisterKindGCC] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 }
494 else if (name.compare("dwarf") == 0)
495 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000496 reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 }
498 else if (name.compare("generic") == 0)
499 {
Greg Clayton2443cbd2012-08-24 01:42:50 +0000500 reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 }
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000502 else if (name.compare("container-regs") == 0)
503 {
504 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
505 value_pair.second = value;
506 do
507 {
508 value_pair = value_pair.second.split(',');
509 if (!value_pair.first.empty())
510 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000511 uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
Greg Clayton0ba20242013-01-21 23:32:42 +0000512 if (reg != LLDB_INVALID_REGNUM)
513 value_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000514 }
515 } while (!value_pair.second.empty());
516 }
517 else if (name.compare("invalidate-regs") == 0)
518 {
519 std::pair<llvm::StringRef, llvm::StringRef> value_pair;
520 value_pair.second = value;
521 do
522 {
523 value_pair = value_pair.second.split(',');
524 if (!value_pair.first.empty())
525 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000526 uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16);
Greg Clayton0ba20242013-01-21 23:32:42 +0000527 if (reg != LLDB_INVALID_REGNUM)
528 invalidate_regs.push_back (reg);
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000529 }
530 } while (!value_pair.second.empty());
531 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 }
533
Jason Molenda743e86a2010-06-11 23:44:18 +0000534 reg_info.byte_offset = reg_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 assert (reg_info.byte_size != 0);
536 reg_offset += reg_info.byte_size;
Greg Claytonce1ffcf2013-01-21 22:17:50 +0000537 if (!value_regs.empty())
538 {
539 value_regs.push_back(LLDB_INVALID_REGNUM);
540 reg_info.value_regs = value_regs.data();
541 }
542 if (!invalidate_regs.empty())
543 {
544 invalidate_regs.push_back(LLDB_INVALID_REGNUM);
545 reg_info.invalidate_regs = invalidate_regs.data();
546 }
547
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
549 }
Todd Fiala1a634402014-01-08 07:52:40 +0000550 else
551 {
552 break; // ensure exit before reg_num is incremented
553 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 }
555 else
556 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000557 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 }
559 }
560
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000561 // Check if qHostInfo specified a specific packet timeout for this connection.
562 // If so then lets update our setting so the user knows what the timeout is
563 // and can see it.
564 const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout();
565 if (host_packet_timeout)
566 {
567 GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout);
568 }
569
570
Greg Claytonef8180a2013-10-15 00:14:28 +0000571 if (reg_num == 0)
572 {
573 FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile ();
Greg Clayton312bcbe2013-10-17 01:10:23 +0000574
575 if (target_definition_fspec)
Greg Claytonef8180a2013-10-15 00:14:28 +0000576 {
Greg Clayton312bcbe2013-10-17 01:10:23 +0000577 // See if we can get register definitions from a python file
578 if (ParsePythonTargetDefinition (target_definition_fspec))
579 return;
Greg Claytonef8180a2013-10-15 00:14:28 +0000580 }
581 }
582
Johnny Chen2fa9de12012-05-14 18:44:23 +0000583 // We didn't get anything if the accumulated reg_num is zero. See if we are
584 // debugging ARM and fill with a hard coded register set until we can get an
585 // updated debugserver down on the devices.
586 // On the other hand, if the accumulated reg_num is positive, see if we can
587 // add composite registers to the existing primordial ones.
588 bool from_scratch = (reg_num == 0);
589
590 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000591 const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture();
592 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
593
594 // Use the process' architecture instead of the host arch, if available
595 ArchSpec remote_arch;
596 if (remote_process_arch.IsValid ())
597 remote_arch = remote_process_arch;
598 else
599 remote_arch = remote_host_arch;
600
Johnny Chen2fa9de12012-05-14 18:44:23 +0000601 if (!target_arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 {
Johnny Chen2fa9de12012-05-14 18:44:23 +0000603 if (remote_arch.IsValid()
604 && remote_arch.GetMachine() == llvm::Triple::arm
605 && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
606 m_register_info.HardcodeARMRegisters(from_scratch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Johnny Chen2fa9de12012-05-14 18:44:23 +0000608 else if (target_arch.GetMachine() == llvm::Triple::arm)
609 {
610 m_register_info.HardcodeARMRegisters(from_scratch);
611 }
612
613 // At this point, we can finalize our register info.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 m_register_info.Finalize ();
615}
616
617Error
618ProcessGDBRemote::WillLaunch (Module* module)
619{
620 return WillLaunchOrAttach ();
621}
622
623Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000624ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625{
626 return WillLaunchOrAttach ();
627}
628
629Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000630ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631{
632 return WillLaunchOrAttach ();
633}
634
635Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000636ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +0000637{
Todd Fialaaf245d12014-06-30 21:05:18 +0000638 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonb766a732011-02-04 01:58:07 +0000639 Error error (WillLaunchOrAttach ());
640
641 if (error.Fail())
642 return error;
643
Greg Clayton2289fa42011-04-30 01:09:13 +0000644 error = ConnectToDebugserver (remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000645
646 if (error.Fail())
647 return error;
648 StartAsyncThread ();
649
Greg Claytonc574ede2011-03-10 02:26:48 +0000650 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonb766a732011-02-04 01:58:07 +0000651 if (pid == LLDB_INVALID_PROCESS_ID)
652 {
653 // We don't have a valid process ID, so note that we are connected
654 // and could now request to launch or attach, or get remote process
655 // listings...
656 SetPrivateState (eStateConnected);
657 }
658 else
659 {
660 // We have a valid process
661 SetID (pid);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000662 GetThreadList();
Greg Clayton3dedae12013-12-06 21:45:27 +0000663 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success)
Greg Claytonb766a732011-02-04 01:58:07 +0000664 {
Jason Molendac62bd7b2013-12-21 05:20:36 +0000665 if (!m_target.GetArchitecture().IsValid())
666 {
667 if (m_gdb_comm.GetProcessArchitecture().IsValid())
668 {
669 m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture());
670 }
671 else
672 {
673 m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture());
674 }
Carlo Kok74389122013-10-14 07:09:13 +0000675 }
676
Greg Claytondd0e5a52011-06-02 22:22:38 +0000677 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytonb766a732011-02-04 01:58:07 +0000678 if (state == eStateStopped)
679 {
680 SetPrivateState (state);
681 }
682 else
Daniel Malead01b2952012-11-29 21:49:15 +0000683 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 +0000684 }
685 else
Daniel Malead01b2952012-11-29 21:49:15 +0000686 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 +0000687 }
Jason Molenda16d127c2012-05-03 22:37:30 +0000688
Todd Fialaaf245d12014-06-30 21:05:18 +0000689 if (log)
690 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");
691
692
693 if (error.Success()
Jason Molenda16d127c2012-05-03 22:37:30 +0000694 && !GetTarget().GetArchitecture().IsValid()
695 && m_gdb_comm.GetHostArchitecture().IsValid())
696 {
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000697 // Prefer the *process'* architecture over that of the *host*, if available.
698 if (m_gdb_comm.GetProcessArchitecture().IsValid())
699 GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture());
700 else
701 GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture());
Jason Molenda16d127c2012-05-03 22:37:30 +0000702 }
703
Todd Fialaaf245d12014-06-30 21:05:18 +0000704 if (log)
705 log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalized target architecture triple: %s", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str ());
706
707 // Set the Unix signals properly for the target.
708 // FIXME Add a gdb-remote packet to discover dynamically.
709 if (error.Success ())
710 {
Tamas Berghammerac839822015-03-04 11:34:10 +0000711 const ArchSpec arch_spec = m_gdb_comm.GetHostArchitecture();
Todd Fialaaf245d12014-06-30 21:05:18 +0000712 if (arch_spec.IsValid ())
713 {
714 if (log)
715 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 ());
716
717 switch (arch_spec.GetTriple ().getOS ())
718 {
719 case llvm::Triple::Linux:
Todd Fiala4ceced32014-08-29 17:35:57 +0000720 SetUnixSignals (UnixSignalsSP (new process_linux::LinuxSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000721 if (log)
722 log->Printf ("ProcessGDBRemote::%s using Linux unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
723 break;
724 case llvm::Triple::OpenBSD:
725 case llvm::Triple::FreeBSD:
726 case llvm::Triple::NetBSD:
Todd Fiala4ceced32014-08-29 17:35:57 +0000727 SetUnixSignals (UnixSignalsSP (new FreeBSDSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000728 if (log)
729 log->Printf ("ProcessGDBRemote::%s using *BSD unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
730 break;
731 default:
Todd Fiala4ceced32014-08-29 17:35:57 +0000732 SetUnixSignals (UnixSignalsSP (new UnixSignals ()));
Todd Fialaaf245d12014-06-30 21:05:18 +0000733 if (log)
734 log->Printf ("ProcessGDBRemote::%s using generic unix signals type for pid %" PRIu64, __FUNCTION__, GetID ());
735 break;
736 }
737 }
738 }
739
Greg Claytonb766a732011-02-04 01:58:07 +0000740 return error;
741}
742
743Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744ProcessGDBRemote::WillLaunchOrAttach ()
745{
746 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 m_stdio_communication.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 return error;
749}
750
751//----------------------------------------------------------------------
752// Process Control
753//----------------------------------------------------------------------
754Error
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000755ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756{
Todd Fiala75f47c32014-10-11 21:42:09 +0000757 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton4957bf62010-09-30 21:49:03 +0000758 Error error;
Greg Clayton982c9762011-11-03 21:22:33 +0000759
Todd Fiala75f47c32014-10-11 21:42:09 +0000760 if (log)
761 log->Printf ("ProcessGDBRemote::%s() entered", __FUNCTION__);
762
Greg Clayton982c9762011-11-03 21:22:33 +0000763 uint32_t launch_flags = launch_info.GetFlags().Get();
764 const char *stdin_path = NULL;
765 const char *stdout_path = NULL;
766 const char *stderr_path = NULL;
767 const char *working_dir = launch_info.GetWorkingDirectory();
768
Zachary Turner696b5282014-08-14 16:01:25 +0000769 const FileAction *file_action;
Greg Clayton982c9762011-11-03 21:22:33 +0000770 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
771 if (file_action)
772 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000773 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000774 stdin_path = file_action->GetPath();
775 }
776 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
777 if (file_action)
778 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000779 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000780 stdout_path = file_action->GetPath();
781 }
782 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
783 if (file_action)
784 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000785 if (file_action->GetAction() == FileAction::eFileActionOpen)
Greg Clayton982c9762011-11-03 21:22:33 +0000786 stderr_path = file_action->GetPath();
787 }
788
Todd Fiala75f47c32014-10-11 21:42:09 +0000789 if (log)
790 {
791 if (stdin_path || stdout_path || stderr_path)
Vince Harron4a8abd32015-02-13 19:15:24 +0000792 log->Printf ("ProcessGDBRemote::%s provided with STDIO paths via launch_info: stdin=%s, stdout=%s, stderr=%s",
Todd Fiala75f47c32014-10-11 21:42:09 +0000793 __FUNCTION__,
794 stdin_path ? stdin_path : "<null>",
795 stdout_path ? stdout_path : "<null>",
796 stderr_path ? stderr_path : "<null>");
797 else
798 log->Printf ("ProcessGDBRemote::%s no STDIO paths given via launch_info", __FUNCTION__);
799 }
800
Vince Harrondf3f00f2015-02-10 21:09:04 +0000801 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
802 if (stdin_path || disable_stdio)
803 {
804 // the inferior will be reading stdin from the specified file
805 // or stdio is completely disabled
806 m_stdin_forward = false;
807 }
808 else
809 {
810 m_stdin_forward = true;
811 }
812
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
814 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
815 // ::LogSetLogFile ("/dev/stdout");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816
Greg Clayton982c9762011-11-03 21:22:33 +0000817 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 if (object_file)
819 {
Greg Clayton71337622011-02-24 22:24:29 +0000820 // Make sure we aren't already connected?
821 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822 {
Greg Clayton91a9b2472013-12-04 19:19:12 +0000823 error = LaunchAndConnectToDebugserver (launch_info);
Greg Clayton71337622011-02-24 22:24:29 +0000824 }
825
826 if (error.Success())
827 {
828 lldb_utility::PseudoTerminal pty;
829 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Clayton5f2a4f92011-03-02 21:34:46 +0000830
Greg Claytonf58c2692011-06-24 22:32:10 +0000831 PlatformSP platform_sp (m_target.GetPlatform());
Vince Harrondf3f00f2015-02-10 21:09:04 +0000832 if (disable_stdio)
Greg Clayton71337622011-02-24 22:24:29 +0000833 {
Vince Harrondf3f00f2015-02-10 21:09:04 +0000834 // set to /dev/null unless redirected to a file above
835 if (!stdin_path)
836 stdin_path = "/dev/null";
837 if (!stdout_path)
838 stdout_path = "/dev/null";
839 if (!stderr_path)
840 stderr_path = "/dev/null";
841 }
842 else if (platform_sp && platform_sp->IsHost())
843 {
844 // If the debugserver is local and we aren't disabling STDIO, lets use
845 // a pseudo terminal to instead of relying on the 'O' packets for stdio
846 // since 'O' packets can really slow down debugging if the inferior
847 // does a lot of output.
Greg Clayton71337622011-02-24 22:24:29 +0000848 const char *slave_name = NULL;
849 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 {
Greg Clayton71337622011-02-24 22:24:29 +0000851 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
852 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 }
Greg Clayton71337622011-02-24 22:24:29 +0000854 if (stdin_path == NULL)
855 stdin_path = slave_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856
Greg Clayton71337622011-02-24 22:24:29 +0000857 if (stdout_path == NULL)
858 stdout_path = slave_name;
859
860 if (stderr_path == NULL)
861 stderr_path = slave_name;
Todd Fiala75f47c32014-10-11 21:42:09 +0000862
863 if (log)
Vince Harron4a8abd32015-02-13 19:15:24 +0000864 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 +0000865 __FUNCTION__,
866 stdin_path ? stdin_path : "<null>",
867 stdout_path ? stdout_path : "<null>",
868 stderr_path ? stderr_path : "<null>");
Greg Clayton71337622011-02-24 22:24:29 +0000869 }
870
Todd Fiala75f47c32014-10-11 21:42:09 +0000871 if (log)
Vince Harron4a8abd32015-02-13 19:15:24 +0000872 log->Printf ("ProcessGDBRemote::%s final STDIO paths after all adjustments: stdin=%s, stdout=%s, stderr=%s",
Todd Fiala75f47c32014-10-11 21:42:09 +0000873 __FUNCTION__,
874 stdin_path ? stdin_path : "<null>",
875 stdout_path ? stdout_path : "<null>",
876 stderr_path ? stderr_path : "<null>");
877
878 if (stdin_path)
Greg Clayton71337622011-02-24 22:24:29 +0000879 m_gdb_comm.SetSTDIN (stdin_path);
880 if (stdout_path)
881 m_gdb_comm.SetSTDOUT (stdout_path);
882 if (stderr_path)
883 m_gdb_comm.SetSTDERR (stderr_path);
884
885 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
Jim Ingham106d0282014-06-25 02:32:56 +0000886 m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError);
Greg Clayton71337622011-02-24 22:24:29 +0000887
Greg Claytonc4103b32011-05-08 04:53:50 +0000888 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Clayton71337622011-02-24 22:24:29 +0000889
Jason Molendaa3329782014-03-29 18:54:20 +0000890 const char * launch_event_data = launch_info.GetLaunchEventData();
891 if (launch_event_data != NULL && *launch_event_data != '\0')
892 m_gdb_comm.SendLaunchEventDataPacket (launch_event_data);
893
Greg Clayton71337622011-02-24 22:24:29 +0000894 if (working_dir && working_dir[0])
895 {
896 m_gdb_comm.SetWorkingDir (working_dir);
897 }
898
899 // Send the environment and the program + arguments after we connect
Greg Clayton982c9762011-11-03 21:22:33 +0000900 const Args &environment = launch_info.GetEnvironmentEntries();
901 if (environment.GetArgumentCount())
Greg Clayton71337622011-02-24 22:24:29 +0000902 {
Greg Clayton982c9762011-11-03 21:22:33 +0000903 size_t num_environment_entries = environment.GetArgumentCount();
904 for (size_t i=0; i<num_environment_entries; ++i)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000905 {
Greg Clayton982c9762011-11-03 21:22:33 +0000906 const char *env_entry = environment.GetArgumentAtIndex(i);
907 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Clayton71337622011-02-24 22:24:29 +0000908 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000909 }
Greg Clayton71337622011-02-24 22:24:29 +0000910 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000911
Greg Clayton71337622011-02-24 22:24:29 +0000912 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000913 // Scope for the scoped timeout object
914 GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10);
915
916 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info);
917 if (arg_packet_err == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000919 std::string error_str;
920 if (m_gdb_comm.GetLaunchSuccess (error_str))
921 {
922 SetID (m_gdb_comm.GetCurrentProcessID ());
923 }
924 else
925 {
926 error.SetErrorString (error_str.c_str());
927 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 }
929 else
930 {
Tamas Berghammer912800c2015-02-24 10:23:39 +0000931 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932 }
Greg Clayton71337622011-02-24 22:24:29 +0000933 }
Tamas Berghammer912800c2015-02-24 10:23:39 +0000934
Greg Clayton71337622011-02-24 22:24:29 +0000935 if (GetID() == LLDB_INVALID_PROCESS_ID)
936 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000937 if (log)
938 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton71337622011-02-24 22:24:29 +0000939 KillDebugserverProcess ();
940 return error;
941 }
942
Greg Clayton3dedae12013-12-06 21:45:27 +0000943 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success)
Greg Clayton71337622011-02-24 22:24:29 +0000944 {
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +0000945 const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture();
946
947 if (process_arch.IsValid())
Jason Molendac62bd7b2013-12-21 05:20:36 +0000948 {
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +0000949 m_target.MergeArchitecture(process_arch);
950 }
951 else
952 {
953 const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture();
954 if (host_arch.IsValid())
955 m_target.MergeArchitecture(host_arch);
Carlo Kok74389122013-10-14 07:09:13 +0000956 }
957
Greg Claytondd0e5a52011-06-02 22:22:38 +0000958 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Clayton71337622011-02-24 22:24:29 +0000959
960 if (!disable_stdio)
961 {
962 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Claytonee95ed52011-11-17 22:14:31 +0000963 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Clayton71337622011-02-24 22:24:29 +0000964 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 }
966 }
Greg Claytonc235ac72011-08-09 05:20:29 +0000967 else
968 {
Johnny Chen4c1e9202011-08-09 18:56:45 +0000969 if (log)
970 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytonc235ac72011-08-09 05:20:29 +0000971 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 }
973 else
974 {
975 // Set our user ID to an invalid process ID.
976 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton982c9762011-11-03 21:22:33 +0000977 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
978 exe_module->GetFileSpec().GetFilename().AsCString(),
979 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 return error;
Greg Clayton4957bf62010-09-30 21:49:03 +0000982
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983}
984
985
986Error
Greg Claytonb766a732011-02-04 01:58:07 +0000987ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988{
989 Error error;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000990 // Only connect if we have a valid connect URL
Vince Harron1b5a74e2015-01-21 22:42:49 +0000991 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton00fe87b2013-12-05 22:58:22 +0000992
993 if (connect_url && connect_url[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994 {
Vince Harron1b5a74e2015-01-21 22:42:49 +0000995 if (log)
996 log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, connect_url);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000997 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
998 if (conn_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999 {
Greg Clayton00fe87b2013-12-05 22:58:22 +00001000 const uint32_t max_retry_count = 50;
1001 uint32_t retry_count = 0;
1002 while (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 {
Greg Clayton00fe87b2013-12-05 22:58:22 +00001004 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
1005 {
1006 m_gdb_comm.SetConnection (conn_ap.release());
1007 break;
1008 }
1009 else if (error.WasInterrupted())
1010 {
1011 // If we were interrupted, don't keep retrying.
1012 break;
1013 }
1014
1015 retry_count++;
1016
1017 if (retry_count >= max_retry_count)
1018 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019
Greg Clayton00fe87b2013-12-05 22:58:22 +00001020 usleep (100000);
1021 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 }
1023 }
1024
1025 if (!m_gdb_comm.IsConnected())
1026 {
1027 if (error.Success())
1028 error.SetErrorString("not connected to remote gdb server");
1029 return error;
1030 }
1031
Greg Clayton32e0a752011-03-30 18:16:51 +00001032 // We always seem to be able to open a connection to a local port
1033 // so we need to make sure we can then send data to it. If we can't
1034 // then we aren't actually connected to anything, so try and do the
1035 // handshake with the remote GDB server and make sure that goes
1036 // alright.
Greg Claytonfb909312013-11-23 01:58:15 +00001037 if (!m_gdb_comm.HandshakeWithServer (&error))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001039 m_gdb_comm.Disconnect();
1040 if (error.Success())
1041 error.SetErrorString("not connected to remote gdb server");
1042 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001044 m_gdb_comm.GetThreadSuffixSupported ();
Greg Clayton44633992012-04-10 03:22:03 +00001045 m_gdb_comm.GetListThreadsInStopReplySupported ();
Greg Clayton32e0a752011-03-30 18:16:51 +00001046 m_gdb_comm.GetHostInfo ();
1047 m_gdb_comm.GetVContSupported ('c');
Jim Inghamcd16df92012-07-20 21:37:13 +00001048 m_gdb_comm.GetVAttachOrWaitSupported();
Jim Ingham03afad82012-07-02 05:40:07 +00001049
1050 size_t num_cmds = GetExtraStartupCommands().GetArgumentCount();
1051 for (size_t idx = 0; idx < num_cmds; idx++)
1052 {
1053 StringExtractorGDBRemote response;
Jim Ingham03afad82012-07-02 05:40:07 +00001054 m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false);
1055 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 return error;
1057}
1058
1059void
Jim Inghambb006ce2014-08-02 00:33:35 +00001060ProcessGDBRemote::DidLaunchOrAttach (ArchSpec& process_arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061{
Greg Clayton5160ce52013-03-27 23:08:40 +00001062 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001063 if (log)
1064 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton93d3c8332011-02-16 04:46:07 +00001065 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 {
Greg Clayton513c26c2011-01-29 07:10:55 +00001067 BuildDynamicRegisterInfo (false);
Greg Clayton3af9ea52010-11-18 05:57:03 +00001068
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069 // See if the GDB server supports the qHostInfo information
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001070
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001071
1072 // See if the GDB server supports the qProcessInfo packet, if so
1073 // prefer that over the Host information as it will be more specific
1074 // to our process.
1075
Todd Fiala75f47c32014-10-11 21:42:09 +00001076 const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture();
1077 if (remote_process_arch.IsValid())
1078 {
1079 process_arch = remote_process_arch;
1080 if (log)
1081 log->Printf ("ProcessGDBRemote::%s gdb-remote had process architecture, using %s %s",
1082 __FUNCTION__,
1083 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1084 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
1085 }
Jim Inghambb006ce2014-08-02 00:33:35 +00001086 else
Todd Fiala75f47c32014-10-11 21:42:09 +00001087 {
Jim Inghambb006ce2014-08-02 00:33:35 +00001088 process_arch = m_gdb_comm.GetHostArchitecture();
Todd Fiala75f47c32014-10-11 21:42:09 +00001089 if (log)
1090 log->Printf ("ProcessGDBRemote::%s gdb-remote did not have process architecture, using gdb-remote host architecture %s %s",
1091 __FUNCTION__,
1092 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1093 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
1094 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001095
Jim Inghambb006ce2014-08-02 00:33:35 +00001096 if (process_arch.IsValid())
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001097 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001098 const ArchSpec &target_arch = GetTarget().GetArchitecture();
Greg Claytond314e812011-03-23 00:09:55 +00001099 if (target_arch.IsValid())
1100 {
Todd Fiala75f47c32014-10-11 21:42:09 +00001101 if (log)
1102 log->Printf ("ProcessGDBRemote::%s analyzing target arch, currently %s %s",
1103 __FUNCTION__,
1104 target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>",
1105 target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>");
1106
1107 // If the remote host is ARM and we have apple as the vendor, then
Greg Claytond314e812011-03-23 00:09:55 +00001108 // ARM executables and shared libraries can have mixed ARM architectures.
1109 // You can have an armv6 executable, and if the host is armv7, then the
1110 // system will load the best possible architecture for all shared libraries
1111 // it has, so we really need to take the remote host architecture as our
1112 // defacto architecture in this case.
1113
Jim Inghambb006ce2014-08-02 00:33:35 +00001114 if (process_arch.GetMachine() == llvm::Triple::arm &&
1115 process_arch.GetTriple().getVendor() == llvm::Triple::Apple)
Greg Claytond314e812011-03-23 00:09:55 +00001116 {
Jason Molenda921c01b2014-08-03 21:42:52 +00001117 GetTarget().SetArchitecture (process_arch);
Todd Fiala75f47c32014-10-11 21:42:09 +00001118 if (log)
1119 log->Printf ("ProcessGDBRemote::%s remote process is ARM/Apple, setting target arch to %s %s",
1120 __FUNCTION__,
1121 process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>",
1122 process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>");
Greg Claytond314e812011-03-23 00:09:55 +00001123 }
1124 else
1125 {
1126 // Fill in what is missing in the triple
Jim Inghambb006ce2014-08-02 00:33:35 +00001127 const llvm::Triple &remote_triple = process_arch.GetTriple();
Tamas Berghammere724af12015-03-13 10:32:37 +00001128 llvm::Triple new_target_triple = target_arch.GetTriple();
1129 if (new_target_triple.getVendorName().size() == 0)
Greg Clayton70b57652011-05-15 01:25:55 +00001130 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001131 new_target_triple.setVendor (remote_triple.getVendor());
Greg Claytond314e812011-03-23 00:09:55 +00001132
Tamas Berghammere724af12015-03-13 10:32:37 +00001133 if (new_target_triple.getOSName().size() == 0)
Greg Clayton70b57652011-05-15 01:25:55 +00001134 {
Tamas Berghammere724af12015-03-13 10:32:37 +00001135 new_target_triple.setOS (remote_triple.getOS());
Greg Claytond314e812011-03-23 00:09:55 +00001136
Tamas Berghammere724af12015-03-13 10:32:37 +00001137 if (new_target_triple.getEnvironmentName().size() == 0)
1138 new_target_triple.setEnvironment (remote_triple.getEnvironment());
Greg Clayton70b57652011-05-15 01:25:55 +00001139 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001140
Tamas Berghammere724af12015-03-13 10:32:37 +00001141 ArchSpec new_target_arch = target_arch;
1142 new_target_arch.SetTriple(new_target_triple);
1143 GetTarget().SetArchitecture(new_target_arch);
1144 }
Greg Claytond314e812011-03-23 00:09:55 +00001145 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001146
1147 if (log)
1148 log->Printf ("ProcessGDBRemote::%s final target arch after adjustments for remote architecture: %s %s",
1149 __FUNCTION__,
1150 target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>",
1151 target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>");
Greg Claytond314e812011-03-23 00:09:55 +00001152 }
1153 else
1154 {
1155 // The target doesn't have a valid architecture yet, set it from
1156 // the architecture we got from the remote GDB server
Jason Molenda921c01b2014-08-03 21:42:52 +00001157 GetTarget().SetArchitecture (process_arch);
Greg Claytond314e812011-03-23 00:09:55 +00001158 }
Greg Clayton0d0c12a2011-02-09 03:09:55 +00001159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 }
1161}
1162
1163void
1164ProcessGDBRemote::DidLaunch ()
1165{
Jim Inghambb006ce2014-08-02 00:33:35 +00001166 ArchSpec process_arch;
1167 DidLaunchOrAttach (process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168}
1169
1170Error
Greg Claytonc982c762010-07-09 20:39:50 +00001171ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172{
Han Ming Ong84647042012-02-25 01:07:38 +00001173 ProcessAttachInfo attach_info;
1174 return DoAttachToProcessWithID(attach_pid, attach_info);
1175}
1176
1177Error
1178ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
1179{
Todd Fiala75f47c32014-10-11 21:42:09 +00001180 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 Error error;
Todd Fiala75f47c32014-10-11 21:42:09 +00001182
1183 if (log)
1184 log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__);
1185
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 // Clear out and clean up from any current state
1187 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 if (attach_pid != LLDB_INVALID_PROCESS_ID)
1189 {
Greg Clayton71337622011-02-24 22:24:29 +00001190 // Make sure we aren't already connected?
1191 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 {
Greg Clayton91a9b2472013-12-04 19:19:12 +00001193 error = LaunchAndConnectToDebugserver (attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001194
1195 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 {
Greg Clayton71337622011-02-24 22:24:29 +00001197 const char *error_string = error.AsCString();
1198 if (error_string == NULL)
1199 error_string = "unable to launch " DEBUGSERVER_BASENAME;
1200
1201 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 }
Greg Clayton71337622011-02-24 22:24:29 +00001203 }
1204
1205 if (error.Success())
1206 {
Jim Ingham106d0282014-06-25 02:32:56 +00001207 m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError());
Todd Fiala75f47c32014-10-11 21:42:09 +00001208
Greg Clayton71337622011-02-24 22:24:29 +00001209 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001210 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid);
Greg Clayton3b608422011-11-19 02:11:30 +00001211 SetID (attach_pid);
Greg Clayton71337622011-02-24 22:24:29 +00001212 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213 }
1214 }
Todd Fiala75f47c32014-10-11 21:42:09 +00001215
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 return error;
1217}
1218
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219Error
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00001220ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221{
1222 Error error;
1223 // Clear out and clean up from any current state
1224 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 if (process_name && process_name[0])
1227 {
Greg Clayton71337622011-02-24 22:24:29 +00001228 // Make sure we aren't already connected?
1229 if (!m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 {
Greg Clayton91a9b2472013-12-04 19:19:12 +00001231 error = LaunchAndConnectToDebugserver (attach_info);
Greg Clayton71337622011-02-24 22:24:29 +00001232
Greg Clayton71337622011-02-24 22:24:29 +00001233 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 {
Greg Clayton71337622011-02-24 22:24:29 +00001235 const char *error_string = error.AsCString();
1236 if (error_string == NULL)
1237 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238
Greg Clayton71337622011-02-24 22:24:29 +00001239 SetExitStatus (-1, error_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240 }
Greg Clayton71337622011-02-24 22:24:29 +00001241 }
1242
1243 if (error.Success())
1244 {
1245 StreamString packet;
1246
Jim Ingham106d0282014-06-25 02:32:56 +00001247 m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError());
1248
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00001249 if (attach_info.GetWaitForLaunch())
Jim Inghamcd16df92012-07-20 21:37:13 +00001250 {
1251 if (!m_gdb_comm.GetVAttachOrWaitSupported())
1252 {
1253 packet.PutCString ("vAttachWait");
1254 }
1255 else
1256 {
1257 if (attach_info.GetIgnoreExisting())
1258 packet.PutCString("vAttachWait");
1259 else
1260 packet.PutCString ("vAttachOrWait");
1261 }
1262 }
Greg Clayton71337622011-02-24 22:24:29 +00001263 else
1264 packet.PutCString("vAttachName");
1265 packet.PutChar(';');
1266 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
1267
1268 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
1269
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 }
1271 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272 return error;
1273}
1274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275
Greg Claytonfbb76342013-11-20 21:07:01 +00001276bool
1277ProcessGDBRemote::SetExitStatus (int exit_status, const char *cstr)
1278{
1279 m_gdb_comm.Disconnect();
1280 return Process::SetExitStatus (exit_status, cstr);
1281}
1282
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283void
Jim Inghambb006ce2014-08-02 00:33:35 +00001284ProcessGDBRemote::DidAttach (ArchSpec &process_arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285{
Jim Inghambb006ce2014-08-02 00:33:35 +00001286 // If you can figure out what the architecture is, fill it in here.
1287 process_arch.Clear();
1288 DidLaunchOrAttach (process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289}
1290
Greg Clayton90ba8112012-12-05 00:16:59 +00001291
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292Error
1293ProcessGDBRemote::WillResume ()
1294{
Greg Clayton71fc2a32011-02-12 06:28:37 +00001295 m_continue_c_tids.clear();
1296 m_continue_C_tids.clear();
1297 m_continue_s_tids.clear();
1298 m_continue_S_tids.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299 return Error();
1300}
1301
1302Error
1303ProcessGDBRemote::DoResume ()
1304{
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001305 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00001306 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton6d093452011-02-05 02:25:06 +00001307 if (log)
1308 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytone5219662010-12-03 06:02:24 +00001309
1310 Listener listener ("gdb-remote.resume-packet-sent");
1311 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
1312 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00001313 listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit);
1314
Greg Claytond1d06e42013-04-20 00:27:58 +00001315 const size_t num_threads = GetThreadList().GetSize();
1316
Greg Clayton71fc2a32011-02-12 06:28:37 +00001317 StreamString continue_packet;
1318 bool continue_packet_error = false;
1319 if (m_gdb_comm.HasAnyVContSupport ())
1320 {
Greg Claytone98008c2014-02-13 23:34:38 +00001321 if (m_continue_c_tids.size() == num_threads ||
1322 (m_continue_c_tids.empty() &&
1323 m_continue_C_tids.empty() &&
1324 m_continue_s_tids.empty() &&
1325 m_continue_S_tids.empty()))
Greg Clayton71fc2a32011-02-12 06:28:37 +00001326 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001327 // All threads are continuing, just send a "c" packet
1328 continue_packet.PutCString ("c");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001329 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001330 else
1331 {
1332 continue_packet.PutCString ("vCont");
Greg Clayton71fc2a32011-02-12 06:28:37 +00001333
Greg Claytond1d06e42013-04-20 00:27:58 +00001334 if (!m_continue_c_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001335 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001336 if (m_gdb_comm.GetVContSupported ('c'))
1337 {
1338 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)
1339 continue_packet.Printf(";c:%4.4" PRIx64, *t_pos);
1340 }
1341 else
1342 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001343 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001344
1345 if (!continue_packet_error && !m_continue_C_tids.empty())
1346 {
1347 if (m_gdb_comm.GetVContSupported ('C'))
1348 {
1349 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)
1350 continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1351 }
1352 else
1353 continue_packet_error = true;
1354 }
Greg Claytone5219662010-12-03 06:02:24 +00001355
Greg Claytond1d06e42013-04-20 00:27:58 +00001356 if (!continue_packet_error && !m_continue_s_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001357 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001358 if (m_gdb_comm.GetVContSupported ('s'))
1359 {
1360 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)
1361 continue_packet.Printf(";s:%4.4" PRIx64, *t_pos);
1362 }
1363 else
1364 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001365 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001366
1367 if (!continue_packet_error && !m_continue_S_tids.empty())
Greg Clayton71fc2a32011-02-12 06:28:37 +00001368 {
Greg Claytond1d06e42013-04-20 00:27:58 +00001369 if (m_gdb_comm.GetVContSupported ('S'))
1370 {
1371 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)
1372 continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);
1373 }
1374 else
1375 continue_packet_error = true;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001376 }
Greg Claytond1d06e42013-04-20 00:27:58 +00001377
1378 if (continue_packet_error)
1379 continue_packet.GetString().clear();
Greg Clayton71fc2a32011-02-12 06:28:37 +00001380 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001381 }
1382 else
1383 continue_packet_error = true;
1384
1385 if (continue_packet_error)
1386 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001387 // Either no vCont support, or we tried to use part of the vCont
1388 // packet that wasn't supported by the remote GDB server.
1389 // We need to try and make a simple packet that can do our continue
Greg Clayton71fc2a32011-02-12 06:28:37 +00001390 const size_t num_continue_c_tids = m_continue_c_tids.size();
1391 const size_t num_continue_C_tids = m_continue_C_tids.size();
1392 const size_t num_continue_s_tids = m_continue_s_tids.size();
1393 const size_t num_continue_S_tids = m_continue_S_tids.size();
1394 if (num_continue_c_tids > 0)
1395 {
1396 if (num_continue_c_tids == num_threads)
1397 {
1398 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001399 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001400 continue_packet.PutChar ('c');
1401 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001402 }
1403 else if (num_continue_c_tids == 1 &&
1404 num_continue_C_tids == 0 &&
1405 num_continue_s_tids == 0 &&
1406 num_continue_S_tids == 0 )
1407 {
1408 // Only one thread is continuing
Greg Clayton8b82f082011-04-12 05:54:46 +00001409 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001410 continue_packet.PutChar ('c');
Greg Clayton0c74e782011-06-24 03:21:43 +00001411 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001412 }
1413 }
1414
Greg Clayton0c74e782011-06-24 03:21:43 +00001415 if (continue_packet_error && num_continue_C_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001416 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001417 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1418 num_continue_C_tids > 0 &&
1419 num_continue_s_tids == 0 &&
1420 num_continue_S_tids == 0 )
Greg Clayton71fc2a32011-02-12 06:28:37 +00001421 {
1422 const int continue_signo = m_continue_C_tids.front().second;
Greg Clayton0c74e782011-06-24 03:21:43 +00001423 // Only one thread is continuing
Greg Clayton71fc2a32011-02-12 06:28:37 +00001424 if (num_continue_C_tids > 1)
1425 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001426 // More that one thread with a signal, yet we don't have
1427 // vCont support and we are being asked to resume each
1428 // thread with a signal, we need to make sure they are
1429 // all the same signal, or we can't issue the continue
1430 // accurately with the current support...
1431 if (num_continue_C_tids > 1)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001432 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001433 continue_packet_error = false;
1434 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1435 {
1436 if (m_continue_C_tids[i].second != continue_signo)
1437 continue_packet_error = true;
1438 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001439 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001440 if (!continue_packet_error)
1441 m_gdb_comm.SetCurrentThreadForRun (-1);
1442 }
1443 else
1444 {
1445 // Set the continue thread ID
1446 continue_packet_error = false;
1447 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001448 }
1449 if (!continue_packet_error)
1450 {
1451 // Add threads continuing with the same signo...
Greg Clayton71fc2a32011-02-12 06:28:37 +00001452 continue_packet.Printf("C%2.2x", continue_signo);
1453 }
1454 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001455 }
1456
Greg Clayton0c74e782011-06-24 03:21:43 +00001457 if (continue_packet_error && num_continue_s_tids > 0)
Greg Clayton71fc2a32011-02-12 06:28:37 +00001458 {
1459 if (num_continue_s_tids == num_threads)
1460 {
1461 // All threads are resuming...
Greg Clayton8b82f082011-04-12 05:54:46 +00001462 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton0c74e782011-06-24 03:21:43 +00001463 continue_packet.PutChar ('s');
1464 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001465 }
1466 else if (num_continue_c_tids == 0 &&
1467 num_continue_C_tids == 0 &&
1468 num_continue_s_tids == 1 &&
1469 num_continue_S_tids == 0 )
1470 {
1471 // Only one thread is stepping
Greg Clayton8b82f082011-04-12 05:54:46 +00001472 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Clayton71fc2a32011-02-12 06:28:37 +00001473 continue_packet.PutChar ('s');
Greg Clayton0c74e782011-06-24 03:21:43 +00001474 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001475 }
1476 }
1477
1478 if (!continue_packet_error && num_continue_S_tids > 0)
1479 {
1480 if (num_continue_S_tids == num_threads)
1481 {
1482 const int step_signo = m_continue_S_tids.front().second;
1483 // Are all threads trying to step with the same signal?
Greg Clayton0c74e782011-06-24 03:21:43 +00001484 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001485 if (num_continue_S_tids > 1)
1486 {
1487 for (size_t i=1; i<num_threads; ++i)
1488 {
1489 if (m_continue_S_tids[i].second != step_signo)
1490 continue_packet_error = true;
1491 }
1492 }
1493 if (!continue_packet_error)
1494 {
1495 // Add threads stepping with the same signo...
Greg Clayton8b82f082011-04-12 05:54:46 +00001496 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001497 continue_packet.Printf("S%2.2x", step_signo);
1498 }
1499 }
1500 else if (num_continue_c_tids == 0 &&
1501 num_continue_C_tids == 0 &&
1502 num_continue_s_tids == 0 &&
1503 num_continue_S_tids == 1 )
1504 {
1505 // Only one thread is stepping with signal
Greg Clayton8b82f082011-04-12 05:54:46 +00001506 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Clayton71fc2a32011-02-12 06:28:37 +00001507 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Clayton0c74e782011-06-24 03:21:43 +00001508 continue_packet_error = false;
Greg Clayton71fc2a32011-02-12 06:28:37 +00001509 }
1510 }
1511 }
1512
1513 if (continue_packet_error)
1514 {
1515 error.SetErrorString ("can't make continue packet for this resume");
1516 }
1517 else
1518 {
1519 EventSP event_sp;
1520 TimeValue timeout;
1521 timeout = TimeValue::Now();
1522 timeout.OffsetWithSeconds (5);
Zachary Turneracee96a2014-09-23 18:32:09 +00001523 if (!m_async_thread.IsJoinable())
Jim Inghamb1e2e842012-04-12 18:49:31 +00001524 {
1525 error.SetErrorString ("Trying to resume but the async thread is dead.");
1526 if (log)
1527 log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead.");
1528 return error;
1529 }
1530
Greg Clayton71fc2a32011-02-12 06:28:37 +00001531 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1532
1533 if (listener.WaitForEvent (&timeout, event_sp) == false)
Jim Inghamb1e2e842012-04-12 18:49:31 +00001534 {
Greg Clayton71fc2a32011-02-12 06:28:37 +00001535 error.SetErrorString("Resume timed out.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00001536 if (log)
1537 log->Printf ("ProcessGDBRemote::DoResume: Resume timed out.");
1538 }
1539 else if (event_sp->BroadcasterIs (&m_async_broadcaster))
1540 {
1541 error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back.");
1542 if (log)
1543 log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back.");
1544 return error;
1545 }
Greg Clayton71fc2a32011-02-12 06:28:37 +00001546 }
Greg Claytone5219662010-12-03 06:02:24 +00001547 }
1548
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001549 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550}
1551
Greg Clayton9e920902012-04-10 02:25:43 +00001552void
1553ProcessGDBRemote::ClearThreadIDList ()
1554{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001555 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001556 m_thread_ids.clear();
1557}
1558
1559bool
1560ProcessGDBRemote::UpdateThreadIDList ()
1561{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001562 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001563 bool sequence_mutex_unavailable = false;
1564 m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable);
1565 if (sequence_mutex_unavailable)
1566 {
Greg Clayton9e920902012-04-10 02:25:43 +00001567 return false; // We just didn't get the list
1568 }
1569 return true;
1570}
1571
Greg Clayton9fc13552012-04-10 00:18:59 +00001572bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001573ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574{
1575 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +00001576 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Clayton73b472d2010-10-27 03:32:59 +00001577 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +00001578 log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Clayton9e920902012-04-10 02:25:43 +00001579
1580 size_t num_thread_ids = m_thread_ids.size();
1581 // The "m_thread_ids" thread ID list should always be updated after each stop
1582 // reply packet, but in case it isn't, update it here.
1583 if (num_thread_ids == 0)
1584 {
1585 if (!UpdateThreadIDList ())
1586 return false;
1587 num_thread_ids = m_thread_ids.size();
1588 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001590 ThreadList old_thread_list_copy(old_thread_list);
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001591 if (num_thread_ids > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001593 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594 {
Greg Clayton9e920902012-04-10 02:25:43 +00001595 tid_t tid = m_thread_ids[i];
Greg Clayton160c9d82013-05-01 21:54:04 +00001596 ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001597 if (!thread_sp)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001598 {
Jim Ingham4f465cf2012-10-10 18:32:14 +00001599 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001600 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1601 log->Printf(
1602 "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001603 __FUNCTION__, static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001604 thread_sp->GetID());
1605 }
1606 else
1607 {
1608 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1609 log->Printf(
1610 "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001611 __FUNCTION__, static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001612 thread_sp->GetID());
1613 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001614 new_thread_list.AddThread(thread_sp);
Greg Claytonadc00cb2011-05-20 23:38:13 +00001615 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616 }
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001617
1618 // Whatever that is left in old_thread_list_copy are not
1619 // present in new_thread_list. Remove non-existent threads from internal id table.
1620 size_t old_num_thread_ids = old_thread_list_copy.GetSize(false);
1621 for (size_t i=0; i<old_num_thread_ids; i++)
1622 {
1623 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false));
1624 if (old_thread_sp)
1625 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001626 lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID();
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001627 m_thread_id_to_index_id_map.erase(old_thread_id);
1628 }
1629 }
1630
Greg Clayton9fc13552012-04-10 00:18:59 +00001631 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632}
1633
1634
1635StateType
1636ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1637{
Greg Claytondd0e5a52011-06-02 22:22:38 +00001638 stop_packet.SetFilePos (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 const char stop_type = stop_packet.GetChar();
1640 switch (stop_type)
1641 {
1642 case 'T':
1643 case 'S':
1644 {
Greg Clayton15fc2be2013-05-21 01:00:52 +00001645 // This is a bit of a hack, but is is required. If we did exec, we
1646 // need to clear our thread lists and also know to rebuild our dynamic
1647 // register info before we lookup and threads and populate the expedited
1648 // register values so we need to know this right away so we can cleanup
1649 // and update our registers.
Greg Clayton8cda7f02013-05-21 21:55:59 +00001650 const uint32_t stop_id = GetStopID();
1651 if (stop_id == 0)
Greg Claytone576ab22011-02-15 00:19:15 +00001652 {
1653 // Our first stop, make sure we have a process ID, and also make
1654 // sure we know about our registers
1655 if (GetID() == LLDB_INVALID_PROCESS_ID)
1656 {
Greg Claytonc574ede2011-03-10 02:26:48 +00001657 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone576ab22011-02-15 00:19:15 +00001658 if (pid != LLDB_INVALID_PROCESS_ID)
1659 SetID (pid);
1660 }
1661 BuildDynamicRegisterInfo (true);
1662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663 // Stop with signal and thread info
1664 const uint8_t signo = stop_packet.GetHexU8();
1665 std::string name;
1666 std::string value;
1667 std::string thread_name;
Greg Claytona658fd22011-06-04 01:26:29 +00001668 std::string reason;
1669 std::string description;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670 uint32_t exc_type = 0;
Greg Clayton896dff62010-07-23 16:45:51 +00001671 std::vector<addr_t> exc_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001672 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001673 ThreadSP thread_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001674 ThreadGDBRemote *gdb_thread = NULL;
Greg Clayton3e06bd92011-01-09 21:07:35 +00001675
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001676 while (stop_packet.GetNameColonValue(name, value))
1677 {
1678 if (name.compare("metype") == 0)
1679 {
1680 // exception type in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001681 exc_type = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 else if (name.compare("medata") == 0)
1684 {
1685 // exception data in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001686 exc_data.push_back(StringConvert::ToUInt64 (value.c_str(), 0, 16));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 }
1688 else if (name.compare("thread") == 0)
1689 {
1690 // thread in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001691 lldb::tid_t tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001692 // m_thread_list_real does have its own mutex, but we need to
1693 // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...)
1694 // and the m_thread_list_real.AddThread(...) so it doesn't change on us
1695 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1696 thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false);
Greg Clayton160c9d82013-05-01 21:54:04 +00001697
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001698 if (!thread_sp)
Greg Claytond1d06e42013-04-20 00:27:58 +00001699 {
Greg Claytone576ab22011-02-15 00:19:15 +00001700 // Create the thread if we need to
Jim Ingham4f465cf2012-10-10 18:32:14 +00001701 thread_sp.reset (new ThreadGDBRemote (*this, tid));
Jim Inghamdee1bc92013-06-22 00:27:45 +00001702 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
1703 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
1704 log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n",
1705 __FUNCTION__,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001706 static_cast<void*>(thread_sp.get()),
Jim Inghamdee1bc92013-06-22 00:27:45 +00001707 thread_sp->GetID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001708
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001709 m_thread_list_real.AddThread(thread_sp);
Greg Claytone576ab22011-02-15 00:19:15 +00001710 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001711 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1712
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 }
Greg Clayton9e920902012-04-10 02:25:43 +00001714 else if (name.compare("threads") == 0)
1715 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001716 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001717 m_thread_ids.clear();
Greg Clayton44633992012-04-10 03:22:03 +00001718 // A comma separated list of all threads in the current
1719 // process that includes the thread for this stop reply
1720 // packet
Greg Clayton9e920902012-04-10 02:25:43 +00001721 size_t comma_pos;
1722 lldb::tid_t tid;
1723 while ((comma_pos = value.find(',')) != std::string::npos)
1724 {
1725 value[comma_pos] = '\0';
1726 // thread in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001727 tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Greg Clayton9e920902012-04-10 02:25:43 +00001728 if (tid != LLDB_INVALID_THREAD_ID)
1729 m_thread_ids.push_back (tid);
1730 value.erase(0, comma_pos + 1);
Greg Clayton9e920902012-04-10 02:25:43 +00001731 }
Vince Harron5275aaa2015-01-15 20:08:35 +00001732 tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16);
Greg Clayton9e920902012-04-10 02:25:43 +00001733 if (tid != LLDB_INVALID_THREAD_ID)
1734 m_thread_ids.push_back (tid);
1735 }
Greg Claytonde9d0492011-01-08 03:17:57 +00001736 else if (name.compare("hexname") == 0)
1737 {
1738 StringExtractor name_extractor;
1739 // Swap "value" over into "name_extractor"
1740 name_extractor.GetStringRef().swap(value);
1741 // Now convert the HEX bytes into a string value
1742 name_extractor.GetHexByteString (value);
1743 thread_name.swap (value);
1744 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 else if (name.compare("name") == 0)
1746 {
1747 thread_name.swap (value);
1748 }
Greg Clayton6f35f5c2010-09-09 06:32:46 +00001749 else if (name.compare("qaddr") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001751 thread_dispatch_qaddr = StringConvert::ToUInt64 (value.c_str(), 0, 16);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 }
Greg Claytona658fd22011-06-04 01:26:29 +00001753 else if (name.compare("reason") == 0)
1754 {
1755 reason.swap(value);
1756 }
1757 else if (name.compare("description") == 0)
1758 {
1759 StringExtractor desc_extractor;
1760 // Swap "value" over into "name_extractor"
1761 desc_extractor.GetStringRef().swap(value);
1762 // Now convert the HEX bytes into a string value
Chaoren Lin28e57422015-02-03 01:51:25 +00001763 desc_extractor.GetHexByteString (value);
1764 description.swap(value);
Greg Claytona658fd22011-06-04 01:26:29 +00001765 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001766 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1767 {
1768 // We have a register number that contains an expedited
1769 // register value. Lets supply this register to our thread
1770 // so it won't have to go and read it.
Greg Clayton160c9d82013-05-01 21:54:04 +00001771 if (gdb_thread)
Greg Clayton3e06bd92011-01-09 21:07:35 +00001772 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001773 uint32_t reg = StringConvert::ToUInt32 (name.c_str(), UINT32_MAX, 16);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001774
1775 if (reg != UINT32_MAX)
1776 {
1777 StringExtractor reg_value_extractor;
1778 // Swap "value" over into "reg_value_extractor"
1779 reg_value_extractor.GetStringRef().swap(value);
Greg Clayton160c9d82013-05-01 21:54:04 +00001780 if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor))
Greg Claytone576ab22011-02-15 00:19:15 +00001781 {
1782 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1783 name.c_str(),
1784 reg,
1785 reg,
1786 reg_value_extractor.GetStringRef().c_str(),
1787 stop_packet.GetStringRef().c_str());
1788 }
Greg Clayton3e06bd92011-01-09 21:07:35 +00001789 }
1790 }
1791 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793
Hafiz Abid Qadeer673b4a32014-02-20 10:23:20 +00001794 // If the response is old style 'S' packet which does not provide us with thread information
1795 // then update the thread list and choose the first one.
1796 if (!thread_sp)
1797 {
1798 UpdateThreadIDList ();
1799
1800 if (!m_thread_ids.empty ())
1801 {
1802 Mutex::Locker locker (m_thread_list_real.GetMutex ());
1803 thread_sp = m_thread_list_real.FindThreadByProtocolID (m_thread_ids.front (), false);
1804 if (thread_sp)
1805 gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get ());
1806 }
1807 }
1808
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809 if (thread_sp)
1810 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001811 // Clear the stop info just in case we don't set it to anything
1812 thread_sp->SetStopInfo (StopInfoSP());
1813
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Inghamdd2fe7a2011-01-28 02:23:12 +00001815 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816 if (exc_type != 0)
1817 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00001818 const size_t exc_data_size = exc_data.size();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001819
Greg Clayton160c9d82013-05-01 21:54:04 +00001820 thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1821 exc_type,
1822 exc_data_size,
1823 exc_data_size >= 1 ? exc_data[0] : 0,
1824 exc_data_size >= 2 ? exc_data[1] : 0,
1825 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 }
Greg Claytona658fd22011-06-04 01:26:29 +00001827 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828 {
Greg Claytona658fd22011-06-04 01:26:29 +00001829 bool handled = false;
Greg Clayton8cda7f02013-05-21 21:55:59 +00001830 bool did_exec = false;
Greg Claytona658fd22011-06-04 01:26:29 +00001831 if (!reason.empty())
1832 {
1833 if (reason.compare("trace") == 0)
1834 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001835 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Greg Claytona658fd22011-06-04 01:26:29 +00001836 handled = true;
1837 }
1838 else if (reason.compare("breakpoint") == 0)
1839 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001840 addr_t pc = thread_sp->GetRegisterContext()->GetPC();
1841 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Claytona658fd22011-06-04 01:26:29 +00001842 if (bp_site_sp)
1843 {
1844 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1845 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1846 // 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 +00001847 handled = true;
Greg Clayton160c9d82013-05-01 21:54:04 +00001848 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001849 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001850 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001851 }
1852 else
1853 {
1854 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001855 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001856 }
1857 }
Greg Claytona658fd22011-06-04 01:26:29 +00001858 }
1859 else if (reason.compare("trap") == 0)
1860 {
1861 // Let the trap just use the standard signal stop reason below...
1862 }
1863 else if (reason.compare("watchpoint") == 0)
1864 {
Chaoren Lin18fe6402015-02-03 01:51:47 +00001865 StringExtractor desc_extractor(description.c_str());
1866 addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS);
1867 uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32);
1868 watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
1869 if (wp_addr != LLDB_INVALID_ADDRESS)
1870 {
1871 WatchpointSP wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr);
1872 if (wp_sp)
1873 {
1874 wp_sp->SetHardwareIndex(wp_index);
1875 watch_id = wp_sp->GetID();
1876 }
1877 }
1878 if (watch_id == LLDB_INVALID_WATCH_ID)
1879 {
1880 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_WATCHPOINTS));
1881 if (log) log->Printf ("failed to find watchpoint");
1882 }
Greg Clayton160c9d82013-05-01 21:54:04 +00001883 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
Greg Claytona658fd22011-06-04 01:26:29 +00001884 handled = true;
1885 }
1886 else if (reason.compare("exception") == 0)
1887 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001888 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001889 handled = true;
1890 }
Greg Clayton15fc2be2013-05-21 01:00:52 +00001891 else if (reason.compare("exec") == 0)
1892 {
Greg Clayton8cda7f02013-05-21 21:55:59 +00001893 did_exec = true;
Greg Clayton15fc2be2013-05-21 01:00:52 +00001894 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp));
1895 handled = true;
1896 }
Greg Claytona658fd22011-06-04 01:26:29 +00001897 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001898
Jim Ingham40083a42014-02-24 19:49:46 +00001899 if (!handled && signo && did_exec == false)
Greg Claytona658fd22011-06-04 01:26:29 +00001900 {
1901 if (signo == SIGTRAP)
1902 {
1903 // Currently we are going to assume SIGTRAP means we are either
1904 // hitting a breakpoint or hardware single stepping.
Jim Ingham54cc6e42012-07-11 21:41:19 +00001905 handled = true;
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +00001906 addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset;
Greg Clayton160c9d82013-05-01 21:54:04 +00001907 lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001908
Greg Claytona658fd22011-06-04 01:26:29 +00001909 if (bp_site_sp)
1910 {
1911 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1912 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1913 // 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 +00001914 if (bp_site_sp->ValidForThisThread (thread_sp.get()))
Greg Claytona658fd22011-06-04 01:26:29 +00001915 {
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +00001916 if(m_breakpoint_pc_offset != 0)
1917 thread_sp->GetRegisterContext()->SetPC(pc);
Greg Clayton160c9d82013-05-01 21:54:04 +00001918 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
Jim Ingham54cc6e42012-07-11 21:41:19 +00001919 }
1920 else
1921 {
1922 StopInfoSP invalid_stop_info_sp;
Greg Clayton160c9d82013-05-01 21:54:04 +00001923 thread_sp->SetStopInfo (invalid_stop_info_sp);
Greg Claytona658fd22011-06-04 01:26:29 +00001924 }
1925 }
Jim Ingham54cc6e42012-07-11 21:41:19 +00001926 else
Greg Claytona658fd22011-06-04 01:26:29 +00001927 {
Jim Ingham4dc613b2012-10-27 02:52:04 +00001928 // If we were stepping then assume the stop was the result of the trace. If we were
1929 // not stepping then report the SIGTRAP.
1930 // FIXME: We are still missing the case where we single step over a trap instruction.
Greg Clayton160c9d82013-05-01 21:54:04 +00001931 if (thread_sp->GetTemporaryResumeState() == eStateStepping)
1932 thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
Jim Ingham4dc613b2012-10-27 02:52:04 +00001933 else
Greg Clayton160c9d82013-05-01 21:54:04 +00001934 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo));
Greg Claytona658fd22011-06-04 01:26:29 +00001935 }
1936 }
1937 if (!handled)
Greg Clayton160c9d82013-05-01 21:54:04 +00001938 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Jason Molenda8214b012013-04-25 01:33:46 +00001939 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001940
Greg Claytona658fd22011-06-04 01:26:29 +00001941 if (!description.empty())
1942 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001943 lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
Greg Claytona658fd22011-06-04 01:26:29 +00001944 if (stop_info_sp)
1945 {
1946 stop_info_sp->SetDescription (description.c_str());
Greg Clayton3418c852011-08-10 02:10:13 +00001947 }
Greg Claytona658fd22011-06-04 01:26:29 +00001948 else
1949 {
Greg Clayton160c9d82013-05-01 21:54:04 +00001950 thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
Greg Claytona658fd22011-06-04 01:26:29 +00001951 }
1952 }
1953 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954 }
1955 return eStateStopped;
1956 }
1957 break;
1958
1959 case 'W':
Todd Fialaff6131a2014-05-19 04:57:23 +00001960 case 'X':
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961 // process exited
1962 return eStateExited;
1963
1964 default:
1965 break;
1966 }
1967 return eStateInvalid;
1968}
1969
1970void
1971ProcessGDBRemote::RefreshStateAfterStop ()
1972{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001973 Mutex::Locker locker(m_thread_list_real.GetMutex());
Greg Clayton9e920902012-04-10 02:25:43 +00001974 m_thread_ids.clear();
1975 // Set the thread stop info. It might have a "threads" key whose value is
1976 // a list of all thread IDs in the current process, so m_thread_ids might
1977 // get set.
1978 SetThreadStopInfo (m_last_stop_packet);
1979 // Check to see if SetThreadStopInfo() filled in m_thread_ids?
1980 if (m_thread_ids.empty())
1981 {
1982 // No, we need to fetch the thread list manually
1983 UpdateThreadIDList();
1984 }
1985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986 // Let all threads recover from stopping and do any clean up based
1987 // on the previous thread state (if any).
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001988 m_thread_list_real.RefreshStateAfterStop();
Greg Clayton9e920902012-04-10 02:25:43 +00001989
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990}
1991
1992Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001993ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994{
1995 Error error;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001996
Greg Clayton6ed95942011-01-22 07:12:45 +00001997 bool timed_out = false;
1998 Mutex::Locker locker;
Greg Clayton513c26c2011-01-29 07:10:55 +00001999
2000 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton3af9ea52010-11-18 05:57:03 +00002001 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002002 // We are being asked to halt during an attach. We need to just close
2003 // our file handle and debugserver will go away, and we can be done...
2004 m_gdb_comm.Disconnect();
Greg Clayton3af9ea52010-11-18 05:57:03 +00002005 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002006 else
2007 {
Greg Clayton2687cd12012-03-29 01:55:41 +00002008 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton513c26c2011-01-29 07:10:55 +00002009 {
2010 if (timed_out)
2011 error.SetErrorString("timed out sending interrupt packet");
2012 else
2013 error.SetErrorString("unknown error sending interrupt packet");
2014 }
Greg Clayton2687cd12012-03-29 01:55:41 +00002015
2016 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton513c26c2011-01-29 07:10:55 +00002017 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002018 return error;
2019}
2020
2021Error
Jim Inghamacff8952013-05-02 00:27:30 +00002022ProcessGDBRemote::DoDetach(bool keep_stopped)
Greg Clayton594e5ed2010-09-27 21:07:38 +00002023{
2024 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002025 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton594e5ed2010-09-27 21:07:38 +00002026 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +00002027 log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped);
2028
Jim Inghamacff8952013-05-02 00:27:30 +00002029 error = m_gdb_comm.Detach (keep_stopped);
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002030 if (log)
Greg Clayton594e5ed2010-09-27 21:07:38 +00002031 {
Jim Inghamacff8952013-05-02 00:27:30 +00002032 if (error.Success())
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002033 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
2034 else
Jim Inghamacff8952013-05-02 00:27:30 +00002035 log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>");
Greg Clayton594e5ed2010-09-27 21:07:38 +00002036 }
Jim Inghamacff8952013-05-02 00:27:30 +00002037
2038 if (!error.Success())
2039 return error;
2040
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002041 // Sleep for one second to let the process get all detached...
Greg Clayton594e5ed2010-09-27 21:07:38 +00002042 StopAsyncThread ();
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002043
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002044 SetPrivateState (eStateDetached);
2045 ResumePrivateStateThread();
2046
2047 //KillDebugserverProcess ();
Greg Clayton594e5ed2010-09-27 21:07:38 +00002048 return error;
2049}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002050
Jim Ingham43c555d2012-07-04 00:35:43 +00002051
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002052Error
2053ProcessGDBRemote::DoDestroy ()
2054{
2055 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002056 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002057 if (log)
2058 log->Printf ("ProcessGDBRemote::DoDestroy()");
2059
Jim Ingham43c555d2012-07-04 00:35:43 +00002060 // There is a bug in older iOS debugservers where they don't shut down the process
2061 // they are debugging properly. If the process is sitting at a breakpoint or an exception,
2062 // this can cause problems with restarting. So we check to see if any of our threads are stopped
2063 // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN
2064 // destroy it again.
2065 //
2066 // Note, we don't have a good way to test the version of debugserver, but I happen to know that
2067 // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of
2068 // the debugservers with this bug are equal. There really should be a better way to test this!
2069 //
2070 // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and
2071 // get called here to destroy again and we're still at a breakpoint or exception, then we should
2072 // just do the straight-forward kill.
2073 //
2074 // And of course, if we weren't able to stop the process by the time we get here, it isn't
2075 // necessary (or helpful) to do any of this.
2076
2077 if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning)
2078 {
2079 PlatformSP platform_sp = GetTarget().GetPlatform();
2080
2081 // FIXME: These should be ConstStrings so we aren't doing strcmp'ing.
2082 if (platform_sp
2083 && platform_sp->GetName()
Greg Clayton57abc5d2013-05-10 21:47:16 +00002084 && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic())
Jim Ingham43c555d2012-07-04 00:35:43 +00002085 {
2086 if (m_destroy_tried_resuming)
2087 {
2088 if (log)
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002089 log->PutCString ("ProcessGDBRemote::DoDestroy() - Tried resuming to destroy once already, not doing it again.");
Jim Ingham43c555d2012-07-04 00:35:43 +00002090 }
2091 else
2092 {
2093 // At present, the plans are discarded and the breakpoints disabled Process::Destroy,
2094 // but we really need it to happen here and it doesn't matter if we do it twice.
2095 m_thread_list.DiscardThreadPlans();
2096 DisableAllBreakpointSites();
2097
2098 bool stop_looks_like_crash = false;
2099 ThreadList &threads = GetThreadList();
2100
2101 {
Jim Ingham45350372012-09-11 00:08:52 +00002102 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00002103
2104 size_t num_threads = threads.GetSize();
2105 for (size_t i = 0; i < num_threads; i++)
2106 {
2107 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002108 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00002109 StopReason reason = eStopReasonInvalid;
2110 if (stop_info_sp)
2111 reason = stop_info_sp->GetStopReason();
2112 if (reason == eStopReasonBreakpoint
2113 || reason == eStopReasonException)
2114 {
2115 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00002116 log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.",
2117 thread_sp->GetProtocolID(),
Jim Ingham43c555d2012-07-04 00:35:43 +00002118 stop_info_sp->GetDescription());
2119 stop_looks_like_crash = true;
2120 break;
2121 }
2122 }
2123 }
2124
2125 if (stop_looks_like_crash)
2126 {
2127 if (log)
2128 log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill.");
2129 m_destroy_tried_resuming = true;
2130
2131 // If we are going to run again before killing, it would be good to suspend all the threads
2132 // before resuming so they won't get into more trouble. Sadly, for the threads stopped with
2133 // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do
2134 // have to run the risk of letting those threads proceed a bit.
2135
2136 {
Jim Ingham45350372012-09-11 00:08:52 +00002137 Mutex::Locker locker(threads.GetMutex());
Jim Ingham43c555d2012-07-04 00:35:43 +00002138
2139 size_t num_threads = threads.GetSize();
2140 for (size_t i = 0; i < num_threads; i++)
2141 {
2142 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002143 StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo();
Jim Ingham43c555d2012-07-04 00:35:43 +00002144 StopReason reason = eStopReasonInvalid;
2145 if (stop_info_sp)
2146 reason = stop_info_sp->GetStopReason();
2147 if (reason != eStopReasonBreakpoint
2148 && reason != eStopReasonException)
2149 {
2150 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00002151 log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.",
2152 thread_sp->GetProtocolID());
Jim Ingham43c555d2012-07-04 00:35:43 +00002153 thread_sp->SetResumeState(eStateSuspended);
2154 }
2155 }
2156 }
2157 Resume ();
2158 return Destroy();
2159 }
2160 }
2161 }
2162 }
2163
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002164 // Interrupt if our inferior is running...
Jim Inghambabfc382012-06-06 00:32:39 +00002165 int exit_status = SIGABRT;
2166 std::string exit_string;
2167
Greg Clayton6ed95942011-01-22 07:12:45 +00002168 if (m_gdb_comm.IsConnected())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002169 {
Jim Inghamaab78372011-10-28 01:11:35 +00002170 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton6779606a2011-01-22 23:43:18 +00002171 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002172 StringExtractorGDBRemote response;
2173 bool send_async = true;
Tamas Berghammer912800c2015-02-24 10:23:39 +00002174 GDBRemoteCommunication::ScopedTimeout (m_gdb_comm, 3);
Filipe Cabecinhas9e106052012-08-22 13:25:58 +00002175
Greg Clayton3dedae12013-12-06 21:45:27 +00002176 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async) == GDBRemoteCommunication::PacketResult::Success)
Greg Clayton513c26c2011-01-29 07:10:55 +00002177 {
2178 char packet_cmd = response.GetChar(0);
2179
2180 if (packet_cmd == 'W' || packet_cmd == 'X')
2181 {
Jim Inghameac0aa42014-02-21 22:36:11 +00002182#if defined(__APPLE__)
Jim Ingham9d67cc52014-02-21 22:35:29 +00002183 // For Native processes on Mac OS X, we launch through the Host Platform, then hand the process off
2184 // to debugserver, which becomes the parent process through "PT_ATTACH". Then when we go to kill
2185 // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we call waitpid which returns
2186 // with no error and the correct status. But amusingly enough that doesn't seem to actually reap
2187 // the process, but instead it is left around as a Zombie. Probably the kernel is in the process of
2188 // switching ownership back to lldb which was the original parent, and gets confused in the handoff.
2189 // Anyway, so call waitpid here to finally reap it.
2190 PlatformSP platform_sp(GetTarget().GetPlatform());
2191 if (platform_sp && platform_sp->IsHost())
2192 {
2193 int status;
2194 ::pid_t reap_pid;
2195 reap_pid = waitpid (GetID(), &status, WNOHANG);
2196 if (log)
2197 log->Printf ("Reaped pid: %d, status: %d.\n", reap_pid, status);
2198 }
2199#endif
Greg Clayton09c3e3d2011-12-06 04:51:14 +00002200 SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00002201 ClearThreadIDList ();
Jim Inghambabfc382012-06-06 00:32:39 +00002202 exit_status = response.GetHexU8();
2203 }
2204 else
2205 {
2206 if (log)
2207 log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str());
2208 exit_string.assign("got unexpected response to k packet: ");
2209 exit_string.append(response.GetStringRef());
Greg Clayton513c26c2011-01-29 07:10:55 +00002210 }
2211 }
2212 else
2213 {
Jim Inghambabfc382012-06-06 00:32:39 +00002214 if (log)
2215 log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet");
2216 exit_string.assign("failed to send the k packet");
Greg Clayton513c26c2011-01-29 07:10:55 +00002217 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002218 }
Jim Inghambabfc382012-06-06 00:32:39 +00002219 else
2220 {
2221 if (log)
Ed Masteb3a53332014-03-17 17:05:22 +00002222 log->Printf ("ProcessGDBRemote::DoDestroy - killed or interrupted while attaching");
Jim Inghamcfc09352012-07-27 23:57:19 +00002223 exit_string.assign ("killed or interrupted while attaching.");
Jim Inghambabfc382012-06-06 00:32:39 +00002224 }
Greg Clayton6779606a2011-01-22 23:43:18 +00002225 }
Jim Inghambabfc382012-06-06 00:32:39 +00002226 else
2227 {
2228 // If we missed setting the exit status on the way out, do it here.
2229 // NB set exit status can be called multiple times, the first one sets the status.
2230 exit_string.assign("destroying when not connected to debugserver");
2231 }
2232
2233 SetExitStatus(exit_status, exit_string.c_str());
2234
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235 StopAsyncThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002236 KillDebugserverProcess ();
2237 return error;
2238}
2239
Greg Clayton8cda7f02013-05-21 21:55:59 +00002240void
2241ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response)
2242{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002243 Mutex::Locker locker (m_last_stop_packet_mutex);
Greg Clayton8cda7f02013-05-21 21:55:59 +00002244 const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos;
2245 if (did_exec)
2246 {
2247 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
2248 if (log)
2249 log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec");
2250
2251 m_thread_list_real.Clear();
2252 m_thread_list.Clear();
2253 BuildDynamicRegisterInfo (true);
2254 m_gdb_comm.ResetDiscoverableSettings();
2255 }
2256 m_last_stop_packet = response;
2257}
2258
2259
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260//------------------------------------------------------------------
2261// Process Queries
2262//------------------------------------------------------------------
2263
2264bool
2265ProcessGDBRemote::IsAlive ()
2266{
Greg Clayton10177aa2010-12-08 05:08:21 +00002267 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002268}
2269
2270addr_t
2271ProcessGDBRemote::GetImageInfoAddress()
2272{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +00002273 return m_gdb_comm.GetShlibInfoAddr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274}
2275
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002276//------------------------------------------------------------------
2277// Process Memory
2278//------------------------------------------------------------------
2279size_t
2280ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2281{
Jason Molenda6076bf42014-05-06 04:34:52 +00002282 GetMaxMemorySize ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002283 if (size > m_max_memory_size)
2284 {
2285 // Keep memory read sizes down to a sane limit. This function will be
2286 // called multiple times in order to complete the task by
2287 // lldb_private::Process so it is ok to do this.
2288 size = m_max_memory_size;
2289 }
2290
2291 char packet[64];
Jason Molenda6076bf42014-05-06 04:34:52 +00002292 int packet_len;
2293 bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
2294 if (binary_memory_read)
2295 {
2296 packet_len = ::snprintf (packet, sizeof(packet), "x0x%" PRIx64 ",0x%" PRIx64, (uint64_t)addr, (uint64_t)size);
2297 }
2298 else
2299 {
2300 packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size);
2301 }
Andy Gibbsa297a972013-06-19 19:04:53 +00002302 assert (packet_len + 1 < (int)sizeof(packet));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002303 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002304 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305 {
Greg Clayton576d8832011-03-22 04:00:09 +00002306 if (response.IsNormalResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002307 {
2308 error.Clear();
Jason Molenda6076bf42014-05-06 04:34:52 +00002309 if (binary_memory_read)
2310 {
2311 // The lower level GDBRemoteCommunication packet receive layer has already de-quoted any
2312 // 0x7d character escaping that was present in the packet
2313
2314 size_t data_received_size = response.GetBytesLeft();
2315 if (data_received_size > size)
2316 {
2317 // Don't write past the end of BUF if the remote debug server gave us too
2318 // much data for some reason.
2319 data_received_size = size;
2320 }
2321 memcpy (buf, response.GetStringRef().data(), data_received_size);
2322 return data_received_size;
2323 }
2324 else
2325 {
2326 return response.GetHexBytes(buf, size, '\xdd');
2327 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002328 }
Greg Clayton576d8832011-03-22 04:00:09 +00002329 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002330 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002331 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002332 error.SetErrorStringWithFormat("GDB server does not support reading memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002334 error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002335 }
2336 else
2337 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002338 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002339 }
2340 return 0;
2341}
2342
2343size_t
2344ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2345{
Jason Molenda6076bf42014-05-06 04:34:52 +00002346 GetMaxMemorySize ();
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00002347 if (size > m_max_memory_size)
2348 {
2349 // Keep memory read sizes down to a sane limit. This function will be
2350 // called multiple times in order to complete the task by
2351 // lldb_private::Process so it is ok to do this.
2352 size = m_max_memory_size;
2353 }
2354
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002355 StreamString packet;
Daniel Malead01b2952012-11-29 21:49:15 +00002356 packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size);
Greg Clayton7fb56d02011-02-01 01:31:41 +00002357 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002359 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true) == GDBRemoteCommunication::PacketResult::Success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002360 {
Greg Clayton576d8832011-03-22 04:00:09 +00002361 if (response.IsOKResponse())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002362 {
2363 error.Clear();
2364 return size;
2365 }
Greg Clayton576d8832011-03-22 04:00:09 +00002366 else if (response.IsErrorResponse())
Greg Claytonb9d5df52012-12-06 22:49:16 +00002367 error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr);
Greg Clayton576d8832011-03-22 04:00:09 +00002368 else if (response.IsUnsupportedResponse())
Greg Clayton9944cd72012-09-19 01:46:31 +00002369 error.SetErrorStringWithFormat("GDB server does not support writing memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002370 else
Greg Clayton9944cd72012-09-19 01:46:31 +00002371 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 +00002372 }
2373 else
2374 {
Jim Ingham35579dd2013-06-03 19:34:01 +00002375 error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002376 }
2377 return 0;
2378}
2379
2380lldb::addr_t
2381ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
2382{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002383 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton2a48f522011-05-14 01:50:35 +00002384 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
2385
Greg Clayton70b57652011-05-15 01:25:55 +00002386 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton2a48f522011-05-14 01:50:35 +00002387 switch (supported)
2388 {
2389 case eLazyBoolCalculate:
2390 case eLazyBoolYes:
2391 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
2392 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
2393 return allocated_addr;
2394
2395 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002396 // Call mmap() to create memory in the inferior..
2397 unsigned prot = 0;
2398 if (permissions & lldb::ePermissionsReadable)
2399 prot |= eMmapProtRead;
2400 if (permissions & lldb::ePermissionsWritable)
2401 prot |= eMmapProtWrite;
2402 if (permissions & lldb::ePermissionsExecutable)
2403 prot |= eMmapProtExec;
Greg Clayton2a48f522011-05-14 01:50:35 +00002404
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002405 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
2406 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
2407 m_addr_to_mmap_size[allocated_addr] = size;
2408 else
Todd Fialaaf245d12014-06-30 21:05:18 +00002409 {
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002410 allocated_addr = LLDB_INVALID_ADDRESS;
Todd Fialaaf245d12014-06-30 21:05:18 +00002411 if (log)
2412 log->Printf ("ProcessGDBRemote::%s no direct stub support for memory allocation, and InferiorCallMmap also failed - is stub missing register context save/restore capability?", __FUNCTION__);
2413 }
Greg Clayton2a48f522011-05-14 01:50:35 +00002414 break;
2415 }
2416
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 if (allocated_addr == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +00002418 error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002419 else
2420 error.Clear();
2421 return allocated_addr;
2422}
2423
2424Error
Greg Clayton46fb5582011-11-18 07:03:08 +00002425ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
2426 MemoryRegionInfo &region_info)
2427{
2428
2429 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
2430 return error;
2431}
2432
2433Error
Johnny Chen64637202012-05-23 21:09:52 +00002434ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num)
2435{
2436
2437 Error error (m_gdb_comm.GetWatchpointSupportInfo (num));
2438 return error;
2439}
2440
2441Error
Enrico Granataf04a2192012-07-13 23:18:48 +00002442ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2443{
2444 Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after));
2445 return error;
2446}
2447
2448Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002449ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
2450{
2451 Error error;
Greg Clayton70b57652011-05-15 01:25:55 +00002452 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
2453
2454 switch (supported)
2455 {
2456 case eLazyBoolCalculate:
2457 // We should never be deallocating memory without allocating memory
2458 // first so we should never get eLazyBoolCalculate
2459 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
2460 break;
2461
2462 case eLazyBoolYes:
2463 if (!m_gdb_comm.DeallocateMemory (addr))
Daniel Malead01b2952012-11-29 21:49:15 +00002464 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002465 break;
2466
2467 case eLazyBoolNo:
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002468 // Call munmap() to deallocate memory in the inferior..
Greg Clayton70b57652011-05-15 01:25:55 +00002469 {
2470 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne99f9aa02011-06-03 20:40:38 +00002471 if (pos != m_addr_to_mmap_size.end() &&
2472 InferiorCallMunmap(this, addr, pos->second))
2473 m_addr_to_mmap_size.erase (pos);
2474 else
Daniel Malead01b2952012-11-29 21:49:15 +00002475 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Greg Clayton70b57652011-05-15 01:25:55 +00002476 }
2477 break;
2478 }
2479
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002480 return error;
2481}
2482
2483
2484//------------------------------------------------------------------
2485// Process STDIO
2486//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002487size_t
2488ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
2489{
2490 if (m_stdio_communication.IsConnected())
2491 {
2492 ConnectionStatus status;
2493 m_stdio_communication.Write(src, src_len, status, NULL);
2494 }
Vince Harrondf3f00f2015-02-10 21:09:04 +00002495 else if (m_stdin_forward)
Vince Harrone0be4252015-02-06 18:32:57 +00002496 {
2497 m_gdb_comm.SendStdinNotification(src, src_len);
2498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002499 return 0;
2500}
2501
2502Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002503ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002504{
2505 Error error;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002506 assert(bp_site != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002507
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002508 // Get logging info
2509 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002510 user_id_t site_id = bp_site->GetID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002511
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002512 // Get the breakpoint address
2513 const addr_t addr = bp_site->GetLoadAddress();
2514
2515 // Log that a breakpoint was requested
2516 if (log)
2517 log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr);
2518
2519 // Breakpoint already exists and is enabled
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002520 if (bp_site->IsEnabled())
2521 {
2522 if (log)
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002523 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 +00002524 return error;
2525 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002526
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002527 // Get the software breakpoint trap opcode size
2528 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2529
2530 // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this breakpoint type
2531 // is supported by the remote stub. These are set to true by default, and later set to false
2532 // only after we receive an unimplemented response when sending a breakpoint packet. This means
2533 // initially that unless we were specifically instructed to use a hardware breakpoint, LLDB will
2534 // attempt to set a software breakpoint. HardwareRequired() also queries a boolean variable which
2535 // indicates if the user specifically asked for hardware breakpoints. If true then we will
2536 // skip over software breakpoints.
2537 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && (!bp_site->HardwareRequired()))
2538 {
2539 // Try to send off a software breakpoint packet ($Z0)
2540 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002541 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002542 // The breakpoint was placed successfully
2543 bp_site->SetEnabled(true);
2544 bp_site->SetType(BreakpointSite::eExternal);
Greg Claytoneb023e72013-10-11 19:48:25 +00002545 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002546 }
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002547
2548 // SendGDBStoppointTypePacket() will return an error if it was unable to set this
2549 // breakpoint. We need to differentiate between a error specific to placing this breakpoint
2550 // or if we have learned that this breakpoint type is unsupported. To do this, we
2551 // must test the support boolean for this breakpoint type to see if it now indicates that
2552 // this breakpoint type is unsupported. If they are still supported then we should return
2553 // with the error code. If they are now unsupported, then we would like to fall through
2554 // and try another form of breakpoint.
2555 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware))
2556 return error;
2557
2558 // We reach here when software breakpoints have been found to be unsupported. For future
2559 // calls to set a breakpoint, we will not attempt to set a breakpoint with a type that is
2560 // known not to be supported.
2561 if (log)
2562 log->Printf("Software breakpoints are unsupported");
2563
2564 // So we will fall through and try a hardware breakpoint
2565 }
2566
2567 // The process of setting a hardware breakpoint is much the same as above. We check the
2568 // supported boolean for this breakpoint type, and if it is thought to be supported then we
2569 // will try to set this breakpoint with a hardware breakpoint.
2570 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware))
2571 {
2572 // Try to send off a hardware breakpoint packet ($Z1)
2573 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002574 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002575 // The breakpoint was placed successfully
2576 bp_site->SetEnabled(true);
2577 bp_site->SetType(BreakpointSite::eHardware);
2578 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002579 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002580
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002581 // Check if the error was something other then an unsupported breakpoint type
2582 if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware))
2583 {
2584 // Unable to set this hardware breakpoint
2585 error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)");
2586 return error;
2587 }
2588
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002589 // We will reach here when the stub gives an unsupported response to a hardware breakpoint
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002590 if (log)
2591 log->Printf("Hardware breakpoints are unsupported");
2592
2593 // Finally we will falling through to a #trap style breakpoint
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002594 }
2595
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002596 // Don't fall through when hardware breakpoints were specifically requested
2597 if (bp_site->HardwareRequired())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002598 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002599 error.SetErrorString("hardware breakpoints are not supported");
2600 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002601 }
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002602
2603 // As a last resort we want to place a manual breakpoint. An instruction
2604 // is placed into the process memory using memory write packets.
2605 return EnableSoftwareBreakpoint(bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002606}
2607
2608Error
Jim Ingham299c0c12013-02-15 02:06:30 +00002609ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002610{
2611 Error error;
2612 assert (bp_site != NULL);
2613 addr_t addr = bp_site->GetLoadAddress();
2614 user_id_t site_id = bp_site->GetID();
Greg Clayton5160ce52013-03-27 23:08:40 +00002615 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002616 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002617 log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002618
2619 if (bp_site->IsEnabled())
2620 {
2621 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
2622
Greg Clayton8b82f082011-04-12 05:54:46 +00002623 BreakpointSite::Type bp_type = bp_site->GetType();
2624 switch (bp_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002625 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002626 case BreakpointSite::eSoftware:
2627 error = DisableSoftwareBreakpoint (bp_site);
2628 break;
2629
2630 case BreakpointSite::eHardware:
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002631 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, addr, bp_op_size))
Greg Clayton8b82f082011-04-12 05:54:46 +00002632 error.SetErrorToGenericError();
2633 break;
2634
2635 case BreakpointSite::eExternal:
Jim Inghama04ef752014-03-07 11:18:02 +00002636 {
2637 GDBStoppointType stoppoint_type;
2638 if (bp_site->IsHardware())
2639 stoppoint_type = eBreakpointHardware;
2640 else
2641 stoppoint_type = eBreakpointSoftware;
2642
2643 if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size))
Greg Clayton8b82f082011-04-12 05:54:46 +00002644 error.SetErrorToGenericError();
Jim Inghama04ef752014-03-07 11:18:02 +00002645 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002646 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002647 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002648 if (error.Success())
2649 bp_site->SetEnabled(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002650 }
2651 else
2652 {
2653 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002654 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 +00002655 return error;
2656 }
2657
2658 if (error.Success())
2659 error.SetErrorToGenericError();
2660 return error;
2661}
2662
Johnny Chen11309a32011-09-06 22:38:36 +00002663// Pre-requisite: wp != NULL.
2664static GDBStoppointType
Johnny Chen01a67862011-10-14 00:42:25 +00002665GetGDBStoppointType (Watchpoint *wp)
Johnny Chen11309a32011-09-06 22:38:36 +00002666{
2667 assert(wp);
2668 bool watch_read = wp->WatchpointRead();
2669 bool watch_write = wp->WatchpointWrite();
2670
2671 // watch_read and watch_write cannot both be false.
2672 assert(watch_read || watch_write);
2673 if (watch_read && watch_write)
2674 return eWatchpointReadWrite;
Johnny Chen6d487a92011-09-09 20:35:15 +00002675 else if (watch_read)
Johnny Chen11309a32011-09-06 22:38:36 +00002676 return eWatchpointRead;
Johnny Chen6d487a92011-09-09 20:35:15 +00002677 else // Must be watch_write, then.
Johnny Chen11309a32011-09-06 22:38:36 +00002678 return eWatchpointWrite;
2679}
2680
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002681Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002682ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002683{
2684 Error error;
2685 if (wp)
2686 {
2687 user_id_t watchID = wp->GetID();
2688 addr_t addr = wp->GetLoadAddress();
Greg Clayton5160ce52013-03-27 23:08:40 +00002689 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002690 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002691 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002692 if (wp->IsEnabled())
2693 {
2694 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002695 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 +00002696 return error;
2697 }
Johnny Chen11309a32011-09-06 22:38:36 +00002698
2699 GDBStoppointType type = GetGDBStoppointType(wp);
2700 // Pass down an appropriate z/Z packet...
2701 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702 {
Johnny Chen11309a32011-09-06 22:38:36 +00002703 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
2704 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002705 wp->SetEnabled(true, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002706 return error;
2707 }
2708 else
2709 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002710 }
Johnny Chen11309a32011-09-06 22:38:36 +00002711 else
2712 error.SetErrorString("watchpoints not supported");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002713 }
2714 else
2715 {
Johnny Chen01a67862011-10-14 00:42:25 +00002716 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002717 }
2718 if (error.Success())
2719 error.SetErrorToGenericError();
2720 return error;
2721}
2722
2723Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002724ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002725{
2726 Error error;
2727 if (wp)
2728 {
2729 user_id_t watchID = wp->GetID();
2730
Greg Clayton5160ce52013-03-27 23:08:40 +00002731 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002732
2733 addr_t addr = wp->GetLoadAddress();
Jim Ingham1b5792e2012-12-18 02:03:49 +00002734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002736 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737
Johnny Chen11309a32011-09-06 22:38:36 +00002738 if (!wp->IsEnabled())
2739 {
2740 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002741 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 +00002742 // See also 'class WatchpointSentry' within StopInfo.cpp.
2743 // This disabling attempt might come from the user-supplied actions, we'll route it in order for
2744 // the watchpoint object to intelligently process this action.
Jim Ingham1b5792e2012-12-18 02:03:49 +00002745 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002746 return error;
2747 }
2748
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002749 if (wp->IsHardware())
2750 {
Johnny Chen11309a32011-09-06 22:38:36 +00002751 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002752 // Pass down an appropriate z/Z packet...
Johnny Chen11309a32011-09-06 22:38:36 +00002753 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2754 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00002755 wp->SetEnabled(false, notify);
Johnny Chen11309a32011-09-06 22:38:36 +00002756 return error;
2757 }
2758 else
2759 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002760 }
2761 // TODO: clear software watchpoints if we implement them
2762 }
2763 else
2764 {
Johnny Chen01a67862011-10-14 00:42:25 +00002765 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002766 }
2767 if (error.Success())
2768 error.SetErrorToGenericError();
2769 return error;
2770}
2771
2772void
2773ProcessGDBRemote::Clear()
2774{
2775 m_flags = 0;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00002776 m_thread_list_real.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002777 m_thread_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002778}
2779
2780Error
2781ProcessGDBRemote::DoSignal (int signo)
2782{
2783 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +00002784 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002785 if (log)
2786 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2787
2788 if (!m_gdb_comm.SendAsyncSignal (signo))
2789 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2790 return error;
2791}
2792
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002793Error
Greg Clayton91a9b2472013-12-04 19:19:12 +00002794ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002795{
2796 Error error;
2797 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2798 {
2799 // If we locate debugserver, keep that located version around
2800 static FileSpec g_debugserver_file_spec;
2801
Han Ming Ong84647042012-02-25 01:07:38 +00002802 ProcessLaunchInfo debugserver_launch_info;
Oleksiy Vyalovf8ce61c2015-01-28 17:36:59 +00002803 // Make debugserver run in its own session so signals generated by
2804 // special terminal key sequences (^C) don't affect debugserver.
2805 debugserver_launch_info.SetLaunchInSeparateProcessGroup(true);
2806
Greg Clayton91a9b2472013-12-04 19:19:12 +00002807 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2808 debugserver_launch_info.SetUserID(process_info.GetUserID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002809
Todd Fiala013434e2014-07-09 01:29:05 +00002810#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Greg Claytonfda4fab2014-01-10 22:24:11 +00002811 // On iOS, still do a local connection using a random port
Greg Clayton16810922014-02-27 19:38:18 +00002812 const char *hostname = "127.0.0.1";
Greg Claytonfda4fab2014-01-10 22:24:11 +00002813 uint16_t port = get_random_port ();
2814#else
2815 // Set hostname being NULL to do the reverse connect where debugserver
2816 // will bind to port zero and it will communicate back to us the port
2817 // that we will connect to
2818 const char *hostname = NULL;
2819 uint16_t port = 0;
2820#endif
2821
2822 error = m_gdb_comm.StartDebugserverProcess (hostname,
2823 port,
Greg Clayton00fe87b2013-12-05 22:58:22 +00002824 debugserver_launch_info,
2825 port);
Greg Clayton91a9b2472013-12-04 19:19:12 +00002826
2827 if (error.Success ())
2828 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002829 else
Greg Clayton91a9b2472013-12-04 19:19:12 +00002830 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002831
2832 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2833 StartAsyncThread ();
Greg Clayton91a9b2472013-12-04 19:19:12 +00002834
2835 if (error.Fail())
2836 {
2837 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
2838
2839 if (log)
2840 log->Printf("failed to start debugserver process: %s", error.AsCString());
2841 return error;
2842 }
2843
Greg Clayton00fe87b2013-12-05 22:58:22 +00002844 if (m_gdb_comm.IsConnected())
2845 {
2846 // Finish the connection process by doing the handshake without connecting (send NULL URL)
2847 ConnectToDebugserver (NULL);
2848 }
2849 else
2850 {
Greg Claytonfda4fab2014-01-10 22:24:11 +00002851 StreamString connect_url;
2852 connect_url.Printf("connect://%s:%u", hostname, port);
2853 error = ConnectToDebugserver (connect_url.GetString().c_str());
Greg Clayton00fe87b2013-12-05 22:58:22 +00002854 }
Greg Clayton91a9b2472013-12-04 19:19:12 +00002855
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002856 }
2857 return error;
2858}
2859
2860bool
2861ProcessGDBRemote::MonitorDebugserverProcess
2862(
2863 void *callback_baton,
2864 lldb::pid_t debugserver_pid,
Greg Claytone4e45922011-11-16 05:37:56 +00002865 bool exited, // True if the process did exit
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002866 int signo, // Zero for no signal
2867 int exit_status // Exit value of process if signal is zero
2868)
2869{
Greg Claytone4e45922011-11-16 05:37:56 +00002870 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2871 // and might not exist anymore, so we need to carefully try to get the
2872 // target for this process first since we have a race condition when
2873 // we are done running between getting the notice that the inferior
2874 // process has died and the debugserver that was debugging this process.
2875 // In our test suite, we are also continually running process after
2876 // process, so we must be very careful to make sure:
2877 // 1 - process object hasn't been deleted already
2878 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002879
2880 // "debugserver_pid" argument passed in is the process ID for
2881 // debugserver that we are tracking...
Greg Clayton5160ce52013-03-27 23:08:40 +00002882 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002883
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002884 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton6779606a2011-01-22 23:43:18 +00002885
Greg Claytone4e45922011-11-16 05:37:56 +00002886 // Get a shared pointer to the target that has a matching process pointer.
2887 // This target could be gone, or the target could already have a new process
2888 // object inside of it
2889 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2890
Greg Clayton6779606a2011-01-22 23:43:18 +00002891 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002892 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 +00002893
Greg Claytone4e45922011-11-16 05:37:56 +00002894 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002895 {
Greg Claytone4e45922011-11-16 05:37:56 +00002896 // We found a process in a target that matches, but another thread
2897 // might be in the process of launching a new process that will
2898 // soon replace it, so get a shared pointer to the process so we
2899 // can keep it alive.
2900 ProcessSP process_sp (target_sp->GetProcessSP());
2901 // Now we have a shared pointer to the process that can't go away on us
2902 // so we now make sure it was the same as the one passed in, and also make
2903 // sure that our previous "process *" didn't get deleted and have a new
2904 // "process *" created in its place with the same pointer. To verify this
2905 // we make sure the process has our debugserver process ID. If we pass all
2906 // of these tests, then we are sure that this process is the one we were
2907 // looking for.
2908 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002909 {
Greg Claytone4e45922011-11-16 05:37:56 +00002910 // Sleep for a half a second to make sure our inferior process has
2911 // time to set its exit status before we set it incorrectly when
2912 // both the debugserver and the inferior process shut down.
2913 usleep (500000);
2914 // If our process hasn't yet exited, debugserver might have died.
2915 // If the process did exit, the we are reaping it.
2916 const StateType state = process->GetState();
2917
2918 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2919 state != eStateInvalid &&
2920 state != eStateUnloaded &&
2921 state != eStateExited &&
2922 state != eStateDetached)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923 {
Greg Claytone4e45922011-11-16 05:37:56 +00002924 char error_str[1024];
2925 if (signo)
2926 {
2927 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2928 if (signal_cstr)
2929 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2930 else
2931 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2932 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002933 else
Greg Claytone4e45922011-11-16 05:37:56 +00002934 {
2935 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2936 }
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002937
Greg Claytone4e45922011-11-16 05:37:56 +00002938 process->SetExitStatus (-1, error_str);
2939 }
2940 // Debugserver has exited we need to let our ProcessGDBRemote
2941 // know that it no longer has a debugserver instance
2942 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002943 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002944 }
2945 return true;
2946}
2947
2948void
2949ProcessGDBRemote::KillDebugserverProcess ()
2950{
Greg Claytonfbb76342013-11-20 21:07:01 +00002951 m_gdb_comm.Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002952 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2953 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002954 Host::Kill (m_debugserver_pid, SIGINT);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002955 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2956 }
2957}
2958
2959void
2960ProcessGDBRemote::Initialize()
2961{
Davide Italianoc8d69822015-04-03 04:24:32 +00002962 static std::once_flag g_once_flag;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002963
Davide Italianoc8d69822015-04-03 04:24:32 +00002964 std::call_once(g_once_flag, []()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002965 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002966 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2967 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +00002968 CreateInstance,
2969 DebuggerInitialize);
Davide Italianoc8d69822015-04-03 04:24:32 +00002970 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002971}
2972
Greg Clayton7f982402013-07-15 22:54:20 +00002973void
Tamas Berghammerdb264a62015-03-31 09:52:22 +00002974ProcessGDBRemote::DebuggerInitialize (Debugger &debugger)
Greg Clayton7f982402013-07-15 22:54:20 +00002975{
2976 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
2977 {
2978 const bool is_global_setting = true;
2979 PluginManager::CreateSettingForProcessPlugin (debugger,
2980 GetGlobalPluginProperties()->GetValueProperties(),
2981 ConstString ("Properties for the gdb-remote process plug-in."),
2982 is_global_setting);
2983 }
2984}
2985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002986bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002987ProcessGDBRemote::StartAsyncThread ()
2988{
Greg Clayton5160ce52013-03-27 23:08:40 +00002989 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002990
2991 if (log)
2992 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Jim Ingham455fa5c2012-11-01 01:15:33 +00002993
2994 Mutex::Locker start_locker(m_async_thread_state_mutex);
Zachary Turneracee96a2014-09-23 18:32:09 +00002995 if (!m_async_thread.IsJoinable())
Jim Ingham455fa5c2012-11-01 01:15:33 +00002996 {
2997 // Create a thread that watches our internal state and controls which
2998 // events make it to clients (into the DCProcess event queue).
Zachary Turner39de3112014-09-09 20:54:56 +00002999
3000 m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Jim Ingham455fa5c2012-11-01 01:15:33 +00003001 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003002 else if (log)
3003 log->Printf("ProcessGDBRemote::%s () - Called when Async thread was already running.", __FUNCTION__);
Zachary Turner39de3112014-09-09 20:54:56 +00003004
Zachary Turneracee96a2014-09-23 18:32:09 +00003005 return m_async_thread.IsJoinable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003006}
3007
3008void
3009ProcessGDBRemote::StopAsyncThread ()
3010{
Greg Clayton5160ce52013-03-27 23:08:40 +00003011 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003012
3013 if (log)
3014 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
3015
Jim Ingham455fa5c2012-11-01 01:15:33 +00003016 Mutex::Locker start_locker(m_async_thread_state_mutex);
Zachary Turneracee96a2014-09-23 18:32:09 +00003017 if (m_async_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 {
Jim Ingham455fa5c2012-11-01 01:15:33 +00003019 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
3020
3021 // This will shut down the async thread.
3022 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
3023
3024 // Stop the stdio thread
Zachary Turner39de3112014-09-09 20:54:56 +00003025 m_async_thread.Join(nullptr);
Pavel Labatha55a9532015-03-11 09:53:42 +00003026 m_async_thread.Reset();
Jim Ingham455fa5c2012-11-01 01:15:33 +00003027 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003028 else if (log)
3029 log->Printf("ProcessGDBRemote::%s () - Called when Async thread was not running.", __FUNCTION__);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003030}
3031
3032
Virgile Bellob2f1fb22013-08-23 12:44:05 +00003033thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003034ProcessGDBRemote::AsyncThread (void *arg)
3035{
3036 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
3037
Greg Clayton5160ce52013-03-27 23:08:40 +00003038 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003040 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003041
3042 Listener listener ("ProcessGDBRemote::AsyncThread");
3043 EventSP event_sp;
3044 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
3045 eBroadcastBitAsyncThreadShouldExit;
3046
3047 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
3048 {
Greg Clayton71337622011-02-24 22:24:29 +00003049 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
3050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 bool done = false;
3052 while (!done)
3053 {
3054 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003055 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003056 if (listener.WaitForEvent (NULL, event_sp))
3057 {
3058 const uint32_t event_type = event_sp->GetType();
Greg Clayton71337622011-02-24 22:24:29 +00003059 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060 {
Greg Clayton71337622011-02-24 22:24:29 +00003061 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003062 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 +00003063
Greg Clayton71337622011-02-24 22:24:29 +00003064 switch (event_type)
3065 {
3066 case eBroadcastBitAsyncContinue:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003067 {
Greg Clayton71337622011-02-24 22:24:29 +00003068 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003069
Greg Clayton71337622011-02-24 22:24:29 +00003070 if (continue_packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003071 {
Greg Clayton71337622011-02-24 22:24:29 +00003072 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
3073 const size_t continue_cstr_len = continue_packet->GetByteSize ();
3074 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003075 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003076
Greg Clayton71337622011-02-24 22:24:29 +00003077 if (::strstr (continue_cstr, "vAttach") == NULL)
3078 process->SetPrivateState(eStateRunning);
3079 StringExtractorGDBRemote response;
3080 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003081
Greg Clayton0772ded2012-05-16 02:48:06 +00003082 // We need to immediately clear the thread ID list so we are sure to get a valid list of threads.
3083 // The thread ID list might be contained within the "response", or the stop reply packet that
3084 // caused the stop. So clear it now before we give the stop reply packet to the process
3085 // using the process->SetLastStopPacket()...
3086 process->ClearThreadIDList ();
3087
Greg Clayton71337622011-02-24 22:24:29 +00003088 switch (stop_state)
3089 {
3090 case eStateStopped:
3091 case eStateCrashed:
3092 case eStateSuspended:
Greg Clayton09c3e3d2011-12-06 04:51:14 +00003093 process->SetLastStopPacket (response);
Greg Clayton71337622011-02-24 22:24:29 +00003094 process->SetPrivateState (stop_state);
3095 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003096
Greg Clayton71337622011-02-24 22:24:29 +00003097 case eStateExited:
Jason Molendaa3329782014-03-29 18:54:20 +00003098 {
Greg Clayton09c3e3d2011-12-06 04:51:14 +00003099 process->SetLastStopPacket (response);
Greg Clayton9e920902012-04-10 02:25:43 +00003100 process->ClearThreadIDList();
Greg Clayton71337622011-02-24 22:24:29 +00003101 response.SetFilePos(1);
Jason Molendaa3329782014-03-29 18:54:20 +00003102
3103 int exit_status = response.GetHexU8();
3104 const char *desc_cstr = NULL;
3105 StringExtractor extractor;
3106 std::string desc_string;
3107 if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';')
3108 {
3109 std::string desc_token;
3110 while (response.GetNameColonValue (desc_token, desc_string))
3111 {
3112 if (desc_token == "description")
3113 {
3114 extractor.GetStringRef().swap(desc_string);
3115 extractor.SetFilePos(0);
3116 extractor.GetHexByteString (desc_string);
3117 desc_cstr = desc_string.c_str();
3118 }
3119 }
3120 }
3121 process->SetExitStatus(exit_status, desc_cstr);
Greg Clayton71337622011-02-24 22:24:29 +00003122 done = true;
3123 break;
Jason Molendaa3329782014-03-29 18:54:20 +00003124 }
Greg Clayton71337622011-02-24 22:24:29 +00003125 case eStateInvalid:
3126 process->SetExitStatus(-1, "lost connection");
3127 break;
3128
3129 default:
3130 process->SetPrivateState (stop_state);
3131 break;
3132 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003133 }
3134 }
Greg Clayton71337622011-02-24 22:24:29 +00003135 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136
Greg Clayton71337622011-02-24 22:24:29 +00003137 case eBroadcastBitAsyncThreadShouldExit:
3138 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003139 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton71337622011-02-24 22:24:29 +00003140 done = true;
3141 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003142
Greg Clayton71337622011-02-24 22:24:29 +00003143 default:
3144 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003145 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 +00003146 done = true;
3147 break;
3148 }
3149 }
3150 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
3151 {
3152 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
3153 {
3154 process->SetExitStatus (-1, "lost connection");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003155 done = true;
Greg Clayton71337622011-02-24 22:24:29 +00003156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003157 }
3158 }
3159 else
3160 {
3161 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003162 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 +00003163 done = true;
3164 }
3165 }
3166 }
3167
3168 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003169 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003170
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003171 return NULL;
3172}
3173
Greg Claytone996fd32011-03-08 22:40:15 +00003174//uint32_t
3175//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3176//{
3177// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
3178// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
3179// if (m_local_debugserver)
3180// {
3181// return Host::ListProcessesMatchingName (name, matches, pids);
3182// }
3183// else
3184// {
3185// // FIXME: Implement talking to the remote debugserver.
3186// return 0;
3187// }
3188//
3189//}
3190//
Jim Ingham1c823b42011-01-22 01:33:44 +00003191bool
3192ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003193 StoppointCallbackContext *context,
Jim Ingham1c823b42011-01-22 01:33:44 +00003194 lldb::user_id_t break_id,
3195 lldb::user_id_t break_loc_id)
3196{
3197 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
3198 // run so I can stop it if that's what I want to do.
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003199 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham1c823b42011-01-22 01:33:44 +00003200 if (log)
3201 log->Printf("Hit New Thread Notification breakpoint.");
3202 return false;
3203}
3204
3205
3206bool
3207ProcessGDBRemote::StartNoticingNewThreads()
3208{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003209 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton4116e932012-05-15 02:33:01 +00003210 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003211 {
Greg Clayton4116e932012-05-15 02:33:01 +00003212 if (log && log->GetVerbose())
3213 log->Printf("Enabled noticing new thread breakpoint.");
3214 m_thread_create_bp_sp->SetEnabled(true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003215 }
Greg Clayton4116e932012-05-15 02:33:01 +00003216 else
Jim Ingham1c823b42011-01-22 01:33:44 +00003217 {
Greg Clayton4116e932012-05-15 02:33:01 +00003218 PlatformSP platform_sp (m_target.GetPlatform());
3219 if (platform_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003220 {
Greg Clayton4116e932012-05-15 02:33:01 +00003221 m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target);
3222 if (m_thread_create_bp_sp)
Jim Ingham1c823b42011-01-22 01:33:44 +00003223 {
Jim Ingham37cfeab2011-10-15 00:21:37 +00003224 if (log && log->GetVerbose())
Greg Clayton4116e932012-05-15 02:33:01 +00003225 log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID());
3226 m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
Jim Ingham1c823b42011-01-22 01:33:44 +00003227 }
3228 else
3229 {
3230 if (log)
3231 log->Printf("Failed to create new thread notification breakpoint.");
Jim Ingham1c823b42011-01-22 01:33:44 +00003232 }
3233 }
3234 }
Greg Clayton4116e932012-05-15 02:33:01 +00003235 return m_thread_create_bp_sp.get() != NULL;
Jim Ingham1c823b42011-01-22 01:33:44 +00003236}
3237
3238bool
3239ProcessGDBRemote::StopNoticingNewThreads()
3240{
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003241 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham37cfeab2011-10-15 00:21:37 +00003242 if (log && log->GetVerbose())
Jim Ingham0e97cbc2011-02-08 05:19:01 +00003243 log->Printf ("Disabling new thread notification breakpoint.");
Greg Clayton4116e932012-05-15 02:33:01 +00003244
3245 if (m_thread_create_bp_sp)
3246 m_thread_create_bp_sp->SetEnabled(false);
3247
Jim Ingham1c823b42011-01-22 01:33:44 +00003248 return true;
3249}
3250
Tamas Berghammerdb264a62015-03-31 09:52:22 +00003251DynamicLoader *
Jason Molenda5e8534e2012-10-03 01:29:34 +00003252ProcessGDBRemote::GetDynamicLoader ()
3253{
3254 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +00003255 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Jason Molenda5e8534e2012-10-03 01:29:34 +00003256 return m_dyld_ap.get();
3257}
Jim Ingham1c823b42011-01-22 01:33:44 +00003258
Jason Molendaa3329782014-03-29 18:54:20 +00003259Error
3260ProcessGDBRemote::SendEventData(const char *data)
3261{
3262 int return_value;
3263 bool was_supported;
3264
3265 Error error;
3266
3267 return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported);
3268 if (return_value != 0)
3269 {
3270 if (!was_supported)
3271 error.SetErrorString("Sending events is not supported for this process.");
3272 else
3273 error.SetErrorStringWithFormat("Error sending event data: %d.", return_value);
3274 }
3275 return error;
3276}
3277
Steve Pucci03904ac2014-03-04 23:18:46 +00003278const DataBufferSP
3279ProcessGDBRemote::GetAuxvData()
3280{
3281 DataBufferSP buf;
3282 if (m_gdb_comm.GetQXferAuxvReadSupported())
3283 {
3284 std::string response_string;
3285 if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", response_string) == GDBRemoteCommunication::PacketResult::Success)
3286 buf.reset(new DataBufferHeap(response_string.c_str(), response_string.length()));
3287 }
3288 return buf;
3289}
3290
Jason Molenda705b1802014-06-13 02:37:02 +00003291StructuredData::ObjectSP
3292ProcessGDBRemote::GetExtendedInfoForThread (lldb::tid_t tid)
3293{
3294 StructuredData::ObjectSP object_sp;
3295
3296 if (m_gdb_comm.GetThreadExtendedInfoSupported())
3297 {
3298 StructuredData::ObjectSP args_dict(new StructuredData::Dictionary());
3299 SystemRuntime *runtime = GetSystemRuntime();
3300 if (runtime)
3301 {
3302 runtime->AddThreadExtendedInfoPacketHints (args_dict);
3303 }
3304 args_dict->GetAsDictionary()->AddIntegerItem ("thread", tid);
3305
3306 StreamString packet;
3307 packet << "jThreadExtendedInfo:";
3308 args_dict->Dump (packet);
3309
3310 // FIXME the final character of a JSON dictionary, '}', is the escape
3311 // character in gdb-remote binary mode. lldb currently doesn't escape
3312 // these characters in its packet output -- so we add the quoted version
3313 // of the } character here manually in case we talk to a debugserver which
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003314 // un-escapes the characters at packet read time.
Jason Molenda705b1802014-06-13 02:37:02 +00003315 packet << (char) (0x7d ^ 0x20);
3316
3317 StringExtractorGDBRemote response;
3318 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success)
3319 {
3320 StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType();
3321 if (response_type == StringExtractorGDBRemote::eResponse)
3322 {
3323 if (!response.Empty())
3324 {
3325 // The packet has already had the 0x7d xor quoting stripped out at the
3326 // GDBRemoteCommunication packet receive level.
3327 object_sp = StructuredData::ParseJSON (response.GetStringRef());
3328 }
3329 }
3330 }
3331 }
3332 return object_sp;
3333}
3334
Jason Molenda6076bf42014-05-06 04:34:52 +00003335// Establish the largest memory read/write payloads we should use.
3336// If the remote stub has a max packet size, stay under that size.
3337//
3338// If the remote stub's max packet size is crazy large, use a
3339// reasonable largeish default.
3340//
3341// If the remote stub doesn't advertise a max packet size, use a
3342// conservative default.
3343
3344void
3345ProcessGDBRemote::GetMaxMemorySize()
3346{
3347 const uint64_t reasonable_largeish_default = 128 * 1024;
3348 const uint64_t conservative_default = 512;
3349
3350 if (m_max_memory_size == 0)
3351 {
3352 uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize();
3353 if (stub_max_size != UINT64_MAX && stub_max_size != 0)
3354 {
3355 // Save the stub's claimed maximum packet size
3356 m_remote_stub_max_memory_size = stub_max_size;
3357
3358 // Even if the stub says it can support ginormous packets,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003359 // don't exceed our reasonable largeish default packet size.
Jason Molenda6076bf42014-05-06 04:34:52 +00003360 if (stub_max_size > reasonable_largeish_default)
3361 {
3362 stub_max_size = reasonable_largeish_default;
3363 }
3364
3365 m_max_memory_size = stub_max_size;
3366 }
3367 else
3368 {
3369 m_max_memory_size = conservative_default;
3370 }
3371 }
3372}
3373
3374void
3375ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max)
3376{
3377 if (user_specified_max != 0)
3378 {
3379 GetMaxMemorySize ();
3380
3381 if (m_remote_stub_max_memory_size != 0)
3382 {
3383 if (m_remote_stub_max_memory_size < user_specified_max)
3384 {
3385 m_max_memory_size = m_remote_stub_max_memory_size; // user specified a packet size too big, go as big
3386 // as the remote stub says we can go.
3387 }
3388 else
3389 {
3390 m_max_memory_size = user_specified_max; // user's packet size is good
3391 }
3392 }
3393 else
3394 {
3395 m_max_memory_size = user_specified_max; // user's packet size is probably fine
3396 }
3397 }
3398}
3399
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003400bool
3401ProcessGDBRemote::GetModuleSpec(const FileSpec& module_file_spec,
3402 const ArchSpec& arch,
3403 ModuleSpec &module_spec)
3404{
3405 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
3406
3407 if (!m_gdb_comm.GetModuleInfo (module_file_spec, arch, module_spec))
3408 {
3409 if (log)
3410 log->Printf ("ProcessGDBRemote::%s - failed to get module info for %s:%s",
3411 __FUNCTION__, module_file_spec.GetPath ().c_str (),
3412 arch.GetTriple ().getTriple ().c_str ());
3413 return false;
3414 }
3415
3416 if (log)
3417 {
3418 StreamString stream;
3419 module_spec.Dump (stream);
3420 log->Printf ("ProcessGDBRemote::%s - got module info for (%s:%s) : %s",
3421 __FUNCTION__, module_file_spec.GetPath ().c_str (),
3422 arch.GetTriple ().getTriple ().c_str (), stream.GetString ().c_str ());
3423 }
3424
3425 return true;
3426}
3427
Greg Clayton02686b82012-10-15 22:42:16 +00003428class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed
Greg Clayton998255b2012-10-13 02:07:45 +00003429{
3430private:
3431
3432public:
Greg Clayton02686b82012-10-15 22:42:16 +00003433 CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) :
Greg Clayton998255b2012-10-13 02:07:45 +00003434 CommandObjectParsed (interpreter,
Greg Clayton02686b82012-10-15 22:42:16 +00003435 "process plugin packet history",
3436 "Dumps the packet history buffer. ",
Greg Clayton998255b2012-10-13 02:07:45 +00003437 NULL)
3438 {
3439 }
3440
Greg Clayton02686b82012-10-15 22:42:16 +00003441 ~CommandObjectProcessGDBRemotePacketHistory ()
Greg Clayton998255b2012-10-13 02:07:45 +00003442 {
3443 }
3444
3445 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003446 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton998255b2012-10-13 02:07:45 +00003447 {
Greg Clayton02686b82012-10-15 22:42:16 +00003448 const size_t argc = command.GetArgumentCount();
3449 if (argc == 0)
3450 {
3451 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3452 if (process)
3453 {
3454 process->GetGDBRemote().DumpHistory(result.GetOutputStream());
3455 result.SetStatus (eReturnStatusSuccessFinishResult);
3456 return true;
3457 }
3458 }
3459 else
3460 {
3461 result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str());
3462 }
3463 result.SetStatus (eReturnStatusFailed);
3464 return false;
3465 }
3466};
3467
Jason Molenda6076bf42014-05-06 04:34:52 +00003468class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed
3469{
3470private:
3471
3472public:
3473 CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) :
3474 CommandObjectParsed (interpreter,
3475 "process plugin packet xfer-size",
3476 "Maximum size that lldb will try to read/write one one chunk.",
3477 NULL)
3478 {
3479 }
3480
3481 ~CommandObjectProcessGDBRemotePacketXferSize ()
3482 {
3483 }
3484
3485 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003486 DoExecute (Args& command, CommandReturnObject &result) override
Jason Molenda6076bf42014-05-06 04:34:52 +00003487 {
3488 const size_t argc = command.GetArgumentCount();
3489 if (argc == 0)
3490 {
3491 result.AppendErrorWithFormat ("'%s' takes an argument to specify the max amount to be transferred when reading/writing", m_cmd_name.c_str());
3492 result.SetStatus (eReturnStatusFailed);
3493 return false;
3494 }
3495
3496 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3497 if (process)
3498 {
3499 const char *packet_size = command.GetArgumentAtIndex(0);
3500 errno = 0;
3501 uint64_t user_specified_max = strtoul (packet_size, NULL, 10);
3502 if (errno == 0 && user_specified_max != 0)
3503 {
3504 process->SetUserSpecifiedMaxMemoryTransferSize (user_specified_max);
3505 result.SetStatus (eReturnStatusSuccessFinishResult);
3506 return true;
3507 }
3508 }
3509 result.SetStatus (eReturnStatusFailed);
3510 return false;
3511 }
3512};
3513
3514
Greg Clayton02686b82012-10-15 22:42:16 +00003515class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed
3516{
3517private:
3518
3519public:
3520 CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) :
3521 CommandObjectParsed (interpreter,
3522 "process plugin packet send",
3523 "Send a custom packet through the GDB remote protocol and print the answer. "
3524 "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.",
3525 NULL)
3526 {
3527 }
3528
3529 ~CommandObjectProcessGDBRemotePacketSend ()
3530 {
3531 }
3532
3533 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003534 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton02686b82012-10-15 22:42:16 +00003535 {
3536 const size_t argc = command.GetArgumentCount();
3537 if (argc == 0)
3538 {
3539 result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str());
3540 result.SetStatus (eReturnStatusFailed);
3541 return false;
3542 }
3543
3544 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3545 if (process)
3546 {
Han Ming Ong84145852012-11-26 20:42:03 +00003547 for (size_t i=0; i<argc; ++ i)
Greg Clayton02686b82012-10-15 22:42:16 +00003548 {
Han Ming Ong84145852012-11-26 20:42:03 +00003549 const char *packet_cstr = command.GetArgumentAtIndex(0);
3550 bool send_async = true;
3551 StringExtractorGDBRemote response;
3552 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3553 result.SetStatus (eReturnStatusSuccessFinishResult);
3554 Stream &output_strm = result.GetOutputStream();
3555 output_strm.Printf (" packet: %s\n", packet_cstr);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003556 std::string &response_str = response.GetStringRef();
3557
Han Ming Ong399289e2013-06-21 19:56:59 +00003558 if (strstr(packet_cstr, "qGetProfileData") != NULL)
Han Ming Ong4b6459f2013-01-18 23:11:53 +00003559 {
3560 response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response);
3561 }
3562
Han Ming Ong84145852012-11-26 20:42:03 +00003563 if (response_str.empty())
3564 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3565 else
3566 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
Greg Clayton02686b82012-10-15 22:42:16 +00003567 }
Greg Clayton02686b82012-10-15 22:42:16 +00003568 }
Greg Clayton998255b2012-10-13 02:07:45 +00003569 return true;
3570 }
3571};
3572
Greg Claytonba4a0a52013-02-01 23:03:47 +00003573class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw
3574{
3575private:
3576
3577public:
3578 CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) :
3579 CommandObjectRaw (interpreter,
3580 "process plugin packet monitor",
Greg Claytoneee5e982013-02-14 18:39:30 +00003581 "Send a qRcmd packet through the GDB remote protocol and print the response."
3582 "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 +00003583 NULL)
3584 {
3585 }
3586
3587 ~CommandObjectProcessGDBRemotePacketMonitor ()
3588 {
3589 }
3590
3591 bool
Tamas Berghammer30b8cd32015-03-23 15:50:03 +00003592 DoExecute (const char *command, CommandReturnObject &result) override
Greg Claytonba4a0a52013-02-01 23:03:47 +00003593 {
3594 if (command == NULL || command[0] == '\0')
3595 {
3596 result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str());
3597 result.SetStatus (eReturnStatusFailed);
3598 return false;
3599 }
3600
3601 ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();
3602 if (process)
3603 {
3604 StreamString packet;
Greg Claytoneee5e982013-02-14 18:39:30 +00003605 packet.PutCString("qRcmd,");
Greg Claytonba4a0a52013-02-01 23:03:47 +00003606 packet.PutBytesAsRawHex8(command, strlen(command));
3607 const char *packet_cstr = packet.GetString().c_str();
3608
3609 bool send_async = true;
3610 StringExtractorGDBRemote response;
3611 process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async);
3612 result.SetStatus (eReturnStatusSuccessFinishResult);
3613 Stream &output_strm = result.GetOutputStream();
3614 output_strm.Printf (" packet: %s\n", packet_cstr);
3615 const std::string &response_str = response.GetStringRef();
3616
3617 if (response_str.empty())
3618 output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");
3619 else
3620 output_strm.Printf ("response: %s\n", response.GetStringRef().c_str());
3621 }
3622 return true;
3623 }
3624};
3625
Greg Clayton02686b82012-10-15 22:42:16 +00003626class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword
3627{
3628private:
3629
3630public:
3631 CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) :
3632 CommandObjectMultiword (interpreter,
3633 "process plugin packet",
3634 "Commands that deal with GDB remote packets.",
3635 NULL)
3636 {
3637 LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter)));
3638 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter)));
Greg Claytonba4a0a52013-02-01 23:03:47 +00003639 LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter)));
Jason Molenda6076bf42014-05-06 04:34:52 +00003640 LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter)));
Greg Clayton02686b82012-10-15 22:42:16 +00003641 }
3642
3643 ~CommandObjectProcessGDBRemotePacket ()
3644 {
3645 }
3646};
Greg Clayton998255b2012-10-13 02:07:45 +00003647
3648class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword
3649{
3650public:
3651 CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) :
3652 CommandObjectMultiword (interpreter,
3653 "process plugin",
3654 "A set of commands for operating on a ProcessGDBRemote process.",
3655 "process plugin <subcommand> [<subcommand-options>]")
3656 {
3657 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter)));
3658 }
3659
3660 ~CommandObjectMultiwordProcessGDBRemote ()
3661 {
3662 }
3663};
3664
Greg Clayton998255b2012-10-13 02:07:45 +00003665CommandObject *
3666ProcessGDBRemote::GetPluginCommandObject()
3667{
3668 if (!m_command_sp)
3669 m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter()));
3670 return m_command_sp.get();
3671}