blob: aa934e1f22c755d65e259909d434e843413cebc0 [file] [log] [blame]
Chris Lattner24943d22010-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
10// C Includes
11#include <errno.h>
Chris Lattner24943d22010-06-08 16:52:24 +000012#include <spawn.h>
Stephen Wilson50daf772011-03-25 18:16:28 +000013#include <stdlib.h>
Greg Clayton989816b2011-05-14 01:50:35 +000014#include <sys/mman.h> // for mmap
Chris Lattner24943d22010-06-08 16:52:24 +000015#include <sys/stat.h>
Greg Clayton989816b2011-05-14 01:50:35 +000016#include <sys/types.h>
Stephen Wilson60f19d52011-03-30 00:12:40 +000017#include <time.h>
Chris Lattner24943d22010-06-08 16:52:24 +000018
19// C++ Includes
20#include <algorithm>
21#include <map>
22
23// Other libraries and framework includes
24
Johnny Chenecd4feb2011-10-14 00:42:25 +000025#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000026#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/ArchSpec.h"
28#include "lldb/Core/Debugger.h"
29#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000030#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Core/InputReader.h"
32#include "lldb/Core/Module.h"
33#include "lldb/Core/PluginManager.h"
34#include "lldb/Core/State.h"
35#include "lldb/Core/StreamString.h"
36#include "lldb/Core/Timer.h"
Greg Clayton2f085c62011-05-15 01:25:55 +000037#include "lldb/Core/Value.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038#include "lldb/Host/TimeValue.h"
39#include "lldb/Symbol/ObjectFile.h"
40#include "lldb/Target/DynamicLoader.h"
41#include "lldb/Target/Target.h"
42#include "lldb/Target/TargetList.h"
Greg Clayton989816b2011-05-14 01:50:35 +000043#include "lldb/Target/ThreadPlanCallFunction.h"
Jason Molendadea5ea72010-06-09 21:28:42 +000044#include "lldb/Utility/PseudoTerminal.h"
Chris Lattner24943d22010-06-08 16:52:24 +000045
46// Project includes
47#include "lldb/Host/Host.h"
Peter Collingbourne4d623e82011-06-03 20:40:38 +000048#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000049#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000050#include "GDBRemoteRegisterContext.h"
51#include "ProcessGDBRemote.h"
52#include "ProcessGDBRemoteLog.h"
53#include "ThreadGDBRemote.h"
Greg Clayton643ee732010-08-04 01:40:35 +000054#include "StopInfoMachException.h"
55
Chris Lattner24943d22010-06-08 16:52:24 +000056
Chris Lattner24943d22010-06-08 16:52:24 +000057
58#define DEBUGSERVER_BASENAME "debugserver"
59using namespace lldb;
60using namespace lldb_private;
61
Jim Inghamf9600482011-03-29 21:45:47 +000062static bool rand_initialized = false;
63
Chris Lattner24943d22010-06-08 16:52:24 +000064static inline uint16_t
65get_random_port ()
66{
Jim Inghamf9600482011-03-29 21:45:47 +000067 if (!rand_initialized)
68 {
Stephen Wilson60f19d52011-03-30 00:12:40 +000069 time_t seed = time(NULL);
70
Jim Inghamf9600482011-03-29 21:45:47 +000071 rand_initialized = true;
Stephen Wilson60f19d52011-03-30 00:12:40 +000072 srand(seed);
Jim Inghamf9600482011-03-29 21:45:47 +000073 }
Stephen Wilson50daf772011-03-25 18:16:28 +000074 return (rand() % (UINT16_MAX - 1000u)) + 1000u;
Chris Lattner24943d22010-06-08 16:52:24 +000075}
76
77
78const char *
79ProcessGDBRemote::GetPluginNameStatic()
80{
Greg Claytonb1888f22011-03-19 01:12:21 +000081 return "gdb-remote";
Chris Lattner24943d22010-06-08 16:52:24 +000082}
83
84const char *
85ProcessGDBRemote::GetPluginDescriptionStatic()
86{
87 return "GDB Remote protocol based debugging plug-in.";
88}
89
90void
91ProcessGDBRemote::Terminate()
92{
93 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
94}
95
96
Greg Clayton46c9a352012-02-09 06:16:32 +000097lldb::ProcessSP
98ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner24943d22010-06-08 16:52:24 +000099{
Greg Clayton46c9a352012-02-09 06:16:32 +0000100 lldb::ProcessSP process_sp;
101 if (crash_file_path == NULL)
102 process_sp.reset (new ProcessGDBRemote (target, listener));
103 return process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000104}
105
106bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000107ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000108{
Greg Clayton61ddf562011-10-21 21:41:45 +0000109 if (plugin_specified_by_name)
110 return true;
111
Chris Lattner24943d22010-06-08 16:52:24 +0000112 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +0000113 Module *exe_module = target.GetExecutableModulePointer();
114 if (exe_module)
Greg Clayton46c9a352012-02-09 06:16:32 +0000115 {
116 ObjectFile *exe_objfile = exe_module->GetObjectFile();
117 // We can't debug core files...
118 switch (exe_objfile->GetType())
119 {
120 case ObjectFile::eTypeInvalid:
121 case ObjectFile::eTypeCoreFile:
122 case ObjectFile::eTypeDebugInfo:
123 case ObjectFile::eTypeObjectFile:
124 case ObjectFile::eTypeSharedLibrary:
125 case ObjectFile::eTypeStubLibrary:
126 return false;
127 case ObjectFile::eTypeExecutable:
128 case ObjectFile::eTypeDynamicLinker:
129 case ObjectFile::eTypeUnknown:
130 break;
131 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000132 return exe_module->GetFileSpec().Exists();
Greg Clayton46c9a352012-02-09 06:16:32 +0000133 }
Jim Ingham7508e732010-08-09 23:31:02 +0000134 // However, if there is no executable module, we return true since we might be preparing to attach.
135 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138//----------------------------------------------------------------------
139// ProcessGDBRemote constructor
140//----------------------------------------------------------------------
141ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
142 Process (target, listener),
Chris Lattner24943d22010-06-08 16:52:24 +0000143 m_flags (0),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000144 m_gdb_comm(false),
Chris Lattner24943d22010-06-08 16:52:24 +0000145 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000146 m_last_stop_packet (),
Greg Clayton06709002011-12-06 04:51:14 +0000147 m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
Chris Lattner24943d22010-06-08 16:52:24 +0000148 m_register_info (),
Jim Ingham5a15e692012-02-16 06:50:00 +0000149 m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_async_thread (LLDB_INVALID_HOST_THREAD),
Greg Claytonc1f45872011-02-12 06:28:37 +0000151 m_continue_c_tids (),
152 m_continue_C_tids (),
153 m_continue_s_tids (),
154 m_continue_S_tids (),
Chris Lattner24943d22010-06-08 16:52:24 +0000155 m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000156 m_max_memory_size (512),
Jim Ingham7508e732010-08-09 23:31:02 +0000157 m_waiting_for_attach (false),
Jim Ingham55e01d82011-01-22 01:33:44 +0000158 m_thread_observation_bps()
Chris Lattner24943d22010-06-08 16:52:24 +0000159{
Greg Claytonff39f742011-04-01 00:29:43 +0000160 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
161 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Chris Lattner24943d22010-06-08 16:52:24 +0000162}
163
164//----------------------------------------------------------------------
165// Destructor
166//----------------------------------------------------------------------
167ProcessGDBRemote::~ProcessGDBRemote()
168{
169 // m_mach_process.UnregisterNotificationCallbacks (this);
170 Clear();
Greg Clayton2f57db02011-10-01 00:45:15 +0000171 // We need to call finalize on the process before destroying ourselves
172 // to make sure all of the broadcaster cleanup goes as planned. If we
173 // destruct this class, then Process::~Process() might have problems
174 // trying to fully destroy the broadcaster.
175 Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000176}
177
178//----------------------------------------------------------------------
179// PluginInterface
180//----------------------------------------------------------------------
181const char *
182ProcessGDBRemote::GetPluginName()
183{
184 return "Process debugging plug-in that uses the GDB remote protocol";
185}
186
187const char *
188ProcessGDBRemote::GetShortPluginName()
189{
190 return GetPluginNameStatic();
191}
192
193uint32_t
194ProcessGDBRemote::GetPluginVersion()
195{
196 return 1;
197}
198
199void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000200ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner24943d22010-06-08 16:52:24 +0000201{
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000202 if (!force && m_register_info.GetNumRegisters() > 0)
203 return;
204
205 char packet[128];
Chris Lattner24943d22010-06-08 16:52:24 +0000206 m_register_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000207 uint32_t reg_offset = 0;
208 uint32_t reg_num = 0;
Greg Clayton61d043b2011-03-22 04:00:09 +0000209 StringExtractorGDBRemote::ResponseType response_type;
210 for (response_type = StringExtractorGDBRemote::eResponse;
211 response_type == StringExtractorGDBRemote::eResponse;
212 ++reg_num)
Chris Lattner24943d22010-06-08 16:52:24 +0000213 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000214 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
215 assert (packet_len < sizeof(packet));
Chris Lattner24943d22010-06-08 16:52:24 +0000216 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000217 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false))
Chris Lattner24943d22010-06-08 16:52:24 +0000218 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000219 response_type = response.GetResponseType();
220 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner24943d22010-06-08 16:52:24 +0000221 {
222 std::string name;
223 std::string value;
224 ConstString reg_name;
225 ConstString alt_name;
226 ConstString set_name;
227 RegisterInfo reg_info = { NULL, // Name
228 NULL, // Alt name
229 0, // byte size
230 reg_offset, // offset
231 eEncodingUint, // encoding
232 eFormatHex, // formate
Chris Lattner24943d22010-06-08 16:52:24 +0000233 {
234 LLDB_INVALID_REGNUM, // GCC reg num
235 LLDB_INVALID_REGNUM, // DWARF reg num
236 LLDB_INVALID_REGNUM, // generic reg num
Jason Molenda3a4ea242010-09-10 07:49:16 +0000237 reg_num, // GDB reg num
238 reg_num // native register number
Chris Lattner24943d22010-06-08 16:52:24 +0000239 }
240 };
241
242 while (response.GetNameColonValue(name, value))
243 {
244 if (name.compare("name") == 0)
245 {
246 reg_name.SetCString(value.c_str());
247 }
248 else if (name.compare("alt-name") == 0)
249 {
250 alt_name.SetCString(value.c_str());
251 }
252 else if (name.compare("bitsize") == 0)
253 {
254 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
255 }
256 else if (name.compare("offset") == 0)
257 {
258 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda53d96862010-06-11 23:44:18 +0000259 if (reg_offset != offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000260 {
261 reg_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000262 }
263 }
264 else if (name.compare("encoding") == 0)
265 {
266 if (value.compare("uint") == 0)
267 reg_info.encoding = eEncodingUint;
268 else if (value.compare("sint") == 0)
269 reg_info.encoding = eEncodingSint;
270 else if (value.compare("ieee754") == 0)
271 reg_info.encoding = eEncodingIEEE754;
272 else if (value.compare("vector") == 0)
273 reg_info.encoding = eEncodingVector;
274 }
275 else if (name.compare("format") == 0)
276 {
277 if (value.compare("binary") == 0)
278 reg_info.format = eFormatBinary;
279 else if (value.compare("decimal") == 0)
280 reg_info.format = eFormatDecimal;
281 else if (value.compare("hex") == 0)
282 reg_info.format = eFormatHex;
283 else if (value.compare("float") == 0)
284 reg_info.format = eFormatFloat;
285 else if (value.compare("vector-sint8") == 0)
286 reg_info.format = eFormatVectorOfSInt8;
287 else if (value.compare("vector-uint8") == 0)
288 reg_info.format = eFormatVectorOfUInt8;
289 else if (value.compare("vector-sint16") == 0)
290 reg_info.format = eFormatVectorOfSInt16;
291 else if (value.compare("vector-uint16") == 0)
292 reg_info.format = eFormatVectorOfUInt16;
293 else if (value.compare("vector-sint32") == 0)
294 reg_info.format = eFormatVectorOfSInt32;
295 else if (value.compare("vector-uint32") == 0)
296 reg_info.format = eFormatVectorOfUInt32;
297 else if (value.compare("vector-float32") == 0)
298 reg_info.format = eFormatVectorOfFloat32;
299 else if (value.compare("vector-uint128") == 0)
300 reg_info.format = eFormatVectorOfUInt128;
301 }
302 else if (name.compare("set") == 0)
303 {
304 set_name.SetCString(value.c_str());
305 }
306 else if (name.compare("gcc") == 0)
307 {
308 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
309 }
310 else if (name.compare("dwarf") == 0)
311 {
312 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
313 }
314 else if (name.compare("generic") == 0)
315 {
316 if (value.compare("pc") == 0)
317 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
318 else if (value.compare("sp") == 0)
319 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
320 else if (value.compare("fp") == 0)
321 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
322 else if (value.compare("ra") == 0)
323 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
324 else if (value.compare("flags") == 0)
325 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
Greg Clayton5a269102011-05-22 04:32:55 +0000326 else if (value.find("arg") == 0)
327 {
328 if (value.size() == 4)
329 {
330 switch (value[3])
331 {
332 case '1': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG1; break;
333 case '2': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG2; break;
334 case '3': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG3; break;
335 case '4': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG4; break;
336 case '5': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG5; break;
337 case '6': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG6; break;
338 case '7': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG7; break;
339 case '8': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG8; break;
340 }
341 }
342 }
Chris Lattner24943d22010-06-08 16:52:24 +0000343 }
344 }
345
Jason Molenda53d96862010-06-11 23:44:18 +0000346 reg_info.byte_offset = reg_offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000347 assert (reg_info.byte_size != 0);
348 reg_offset += reg_info.byte_size;
349 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
350 }
351 }
352 else
353 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000354 response_type = StringExtractorGDBRemote::eError;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000355 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000356 }
357 }
358
359 if (reg_num == 0)
360 {
361 // We didn't get anything. See if we are debugging ARM and fill with
362 // a hard coded register set until we can get an updated debugserver
363 // down on the devices.
Jason Molenda428b5502011-10-06 01:45:46 +0000364
365 if (!GetTarget().GetArchitecture().IsValid()
366 && m_gdb_comm.GetHostArchitecture().IsValid()
367 && m_gdb_comm.GetHostArchitecture().GetMachine() == llvm::Triple::arm
368 && m_gdb_comm.GetHostArchitecture().GetTriple().getVendor() == llvm::Triple::Apple)
369 {
Chris Lattner24943d22010-06-08 16:52:24 +0000370 m_register_info.HardcodeARMRegisters();
Jason Molenda428b5502011-10-06 01:45:46 +0000371 }
372 else if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::arm)
373 {
374 m_register_info.HardcodeARMRegisters();
375 }
Chris Lattner24943d22010-06-08 16:52:24 +0000376 }
377 m_register_info.Finalize ();
378}
379
380Error
381ProcessGDBRemote::WillLaunch (Module* module)
382{
383 return WillLaunchOrAttach ();
384}
385
386Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000387ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000388{
389 return WillLaunchOrAttach ();
390}
391
392Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000393ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +0000394{
395 return WillLaunchOrAttach ();
396}
397
398Error
Greg Claytone71e2582011-02-04 01:58:07 +0000399ProcessGDBRemote::DoConnectRemote (const char *remote_url)
400{
401 Error error (WillLaunchOrAttach ());
402
403 if (error.Fail())
404 return error;
405
Greg Clayton180546b2011-04-30 01:09:13 +0000406 error = ConnectToDebugserver (remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +0000407
408 if (error.Fail())
409 return error;
410 StartAsyncThread ();
411
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000412 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone71e2582011-02-04 01:58:07 +0000413 if (pid == LLDB_INVALID_PROCESS_ID)
414 {
415 // We don't have a valid process ID, so note that we are connected
416 // and could now request to launch or attach, or get remote process
417 // listings...
418 SetPrivateState (eStateConnected);
419 }
420 else
421 {
422 // We have a valid process
423 SetID (pid);
Greg Clayton37f962e2011-08-22 02:49:39 +0000424 GetThreadList();
Greg Clayton261a18b2011-06-02 22:22:38 +0000425 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytone71e2582011-02-04 01:58:07 +0000426 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000427 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytone71e2582011-02-04 01:58:07 +0000428 if (state == eStateStopped)
429 {
430 SetPrivateState (state);
431 }
432 else
Greg Claytond9919d32011-12-01 23:28:38 +0000433 error.SetErrorStringWithFormat ("Process %llu was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state));
Greg Claytone71e2582011-02-04 01:58:07 +0000434 }
435 else
Greg Claytond9919d32011-12-01 23:28:38 +0000436 error.SetErrorStringWithFormat ("Process %llu was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +0000437 }
438 return error;
439}
440
441Error
Chris Lattner24943d22010-06-08 16:52:24 +0000442ProcessGDBRemote::WillLaunchOrAttach ()
443{
444 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000445 m_stdio_communication.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000446 return error;
447}
448
449//----------------------------------------------------------------------
450// Process Control
451//----------------------------------------------------------------------
452Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000453ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000454{
Greg Clayton4b407112010-09-30 21:49:03 +0000455 Error error;
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000456
457 uint32_t launch_flags = launch_info.GetFlags().Get();
458 const char *stdin_path = NULL;
459 const char *stdout_path = NULL;
460 const char *stderr_path = NULL;
461 const char *working_dir = launch_info.GetWorkingDirectory();
462
463 const ProcessLaunchInfo::FileAction *file_action;
464 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
465 if (file_action)
466 {
467 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
468 stdin_path = file_action->GetPath();
469 }
470 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
471 if (file_action)
472 {
473 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
474 stdout_path = file_action->GetPath();
475 }
476 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
477 if (file_action)
478 {
479 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
480 stderr_path = file_action->GetPath();
481 }
482
Chris Lattner24943d22010-06-08 16:52:24 +0000483 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
484 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
485 // ::LogSetLogFile ("/dev/stdout");
Greg Clayton716cefb2011-08-09 05:20:29 +0000486 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000487
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000488 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000489 if (object_file)
490 {
Chris Lattner24943d22010-06-08 16:52:24 +0000491 char host_port[128];
492 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytone71e2582011-02-04 01:58:07 +0000493 char connect_url[128];
494 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000495
Greg Claytona2f74232011-02-24 22:24:29 +0000496 // Make sure we aren't already connected?
497 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000498 {
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000499 error = StartDebugserverProcess (host_port, launch_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000500 if (error.Fail())
Greg Clayton716cefb2011-08-09 05:20:29 +0000501 {
Johnny Chenc143d622011-08-09 18:56:45 +0000502 if (log)
503 log->Printf("failed to start debugserver process: %s", error.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000504 return error;
Greg Clayton716cefb2011-08-09 05:20:29 +0000505 }
Chris Lattner24943d22010-06-08 16:52:24 +0000506
Greg Claytone71e2582011-02-04 01:58:07 +0000507 error = ConnectToDebugserver (connect_url);
Greg Claytona2f74232011-02-24 22:24:29 +0000508 }
509
510 if (error.Success())
511 {
512 lldb_utility::PseudoTerminal pty;
513 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Claytonafb81862011-03-02 21:34:46 +0000514
515 // If the debugserver is local and we aren't disabling STDIO, lets use
516 // a pseudo terminal to instead of relying on the 'O' packets for stdio
517 // since 'O' packets can really slow down debugging if the inferior
518 // does a lot of output.
Greg Claytonb4747822011-06-24 22:32:10 +0000519 PlatformSP platform_sp (m_target.GetPlatform());
520 if (platform_sp && platform_sp->IsHost() && !disable_stdio)
Greg Claytona2f74232011-02-24 22:24:29 +0000521 {
522 const char *slave_name = NULL;
523 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000524 {
Greg Claytona2f74232011-02-24 22:24:29 +0000525 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
526 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000527 }
Greg Claytona2f74232011-02-24 22:24:29 +0000528 if (stdin_path == NULL)
529 stdin_path = slave_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000530
Greg Claytona2f74232011-02-24 22:24:29 +0000531 if (stdout_path == NULL)
532 stdout_path = slave_name;
533
534 if (stderr_path == NULL)
535 stderr_path = slave_name;
536 }
537
Greg Claytonafb81862011-03-02 21:34:46 +0000538 // Set STDIN to /dev/null if we want STDIO disabled or if either
539 // STDOUT or STDERR have been set to something and STDIN hasn't
540 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000541 stdin_path = "/dev/null";
542
Greg Claytonafb81862011-03-02 21:34:46 +0000543 // Set STDOUT to /dev/null if we want STDIO disabled or if either
544 // STDIN or STDERR have been set to something and STDOUT hasn't
545 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000546 stdout_path = "/dev/null";
547
Greg Claytonafb81862011-03-02 21:34:46 +0000548 // Set STDERR to /dev/null if we want STDIO disabled or if either
549 // STDIN or STDOUT have been set to something and STDERR hasn't
550 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000551 stderr_path = "/dev/null";
552
553 if (stdin_path)
554 m_gdb_comm.SetSTDIN (stdin_path);
555 if (stdout_path)
556 m_gdb_comm.SetSTDOUT (stdout_path);
557 if (stderr_path)
558 m_gdb_comm.SetSTDERR (stderr_path);
559
560 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
561
Greg Claytona4582402011-05-08 04:53:50 +0000562 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Claytona2f74232011-02-24 22:24:29 +0000563
564 if (working_dir && working_dir[0])
565 {
566 m_gdb_comm.SetWorkingDir (working_dir);
567 }
568
569 // Send the environment and the program + arguments after we connect
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000570 const Args &environment = launch_info.GetEnvironmentEntries();
571 if (environment.GetArgumentCount())
Greg Claytona2f74232011-02-24 22:24:29 +0000572 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000573 size_t num_environment_entries = environment.GetArgumentCount();
574 for (size_t i=0; i<num_environment_entries; ++i)
Greg Clayton960d6a42010-08-03 00:35:52 +0000575 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000576 const char *env_entry = environment.GetArgumentAtIndex(i);
577 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Claytona2f74232011-02-24 22:24:29 +0000578 break;
Greg Clayton960d6a42010-08-03 00:35:52 +0000579 }
Greg Claytona2f74232011-02-24 22:24:29 +0000580 }
Greg Clayton960d6a42010-08-03 00:35:52 +0000581
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000582 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000583 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector());
Greg Claytona2f74232011-02-24 22:24:29 +0000584 if (arg_packet_err == 0)
585 {
586 std::string error_str;
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000587 if (m_gdb_comm.GetLaunchSuccess (error_str))
Chris Lattner24943d22010-06-08 16:52:24 +0000588 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000589 SetID (m_gdb_comm.GetCurrentProcessID ());
Chris Lattner24943d22010-06-08 16:52:24 +0000590 }
591 else
592 {
Greg Claytona2f74232011-02-24 22:24:29 +0000593 error.SetErrorString (error_str.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000594 }
Greg Claytona2f74232011-02-24 22:24:29 +0000595 }
596 else
597 {
Greg Clayton9c236732011-10-26 00:56:27 +0000598 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Greg Claytona2f74232011-02-24 22:24:29 +0000599 }
Greg Clayton7c4fc6e2011-08-10 22:05:39 +0000600
601 m_gdb_comm.SetPacketTimeout (old_packet_timeout);
Chris Lattner24943d22010-06-08 16:52:24 +0000602
Greg Claytona2f74232011-02-24 22:24:29 +0000603 if (GetID() == LLDB_INVALID_PROCESS_ID)
604 {
Johnny Chenc143d622011-08-09 18:56:45 +0000605 if (log)
606 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000607 KillDebugserverProcess ();
608 return error;
609 }
610
Greg Clayton261a18b2011-06-02 22:22:38 +0000611 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytona2f74232011-02-24 22:24:29 +0000612 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000613 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Claytona2f74232011-02-24 22:24:29 +0000614
615 if (!disable_stdio)
616 {
617 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Clayton464c6162011-11-17 22:14:31 +0000618 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Claytona2f74232011-02-24 22:24:29 +0000619 }
Chris Lattner24943d22010-06-08 16:52:24 +0000620 }
621 }
Greg Clayton716cefb2011-08-09 05:20:29 +0000622 else
623 {
Johnny Chenc143d622011-08-09 18:56:45 +0000624 if (log)
625 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton716cefb2011-08-09 05:20:29 +0000626 }
Chris Lattner24943d22010-06-08 16:52:24 +0000627 }
628 else
629 {
630 // Set our user ID to an invalid process ID.
631 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000632 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
633 exe_module->GetFileSpec().GetFilename().AsCString(),
634 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636 return error;
Greg Clayton4b407112010-09-30 21:49:03 +0000637
Chris Lattner24943d22010-06-08 16:52:24 +0000638}
639
640
641Error
Greg Claytone71e2582011-02-04 01:58:07 +0000642ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner24943d22010-06-08 16:52:24 +0000643{
644 Error error;
645 // Sleep and wait a bit for debugserver to start to listen...
646 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
647 if (conn_ap.get())
648 {
Chris Lattner24943d22010-06-08 16:52:24 +0000649 const uint32_t max_retry_count = 50;
650 uint32_t retry_count = 0;
651 while (!m_gdb_comm.IsConnected())
652 {
Greg Claytone71e2582011-02-04 01:58:07 +0000653 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner24943d22010-06-08 16:52:24 +0000654 {
655 m_gdb_comm.SetConnection (conn_ap.release());
656 break;
657 }
658 retry_count++;
659
660 if (retry_count >= max_retry_count)
661 break;
662
663 usleep (100000);
664 }
665 }
666
667 if (!m_gdb_comm.IsConnected())
668 {
669 if (error.Success())
670 error.SetErrorString("not connected to remote gdb server");
671 return error;
672 }
673
Greg Clayton24bc5d92011-03-30 18:16:51 +0000674 // We always seem to be able to open a connection to a local port
675 // so we need to make sure we can then send data to it. If we can't
676 // then we aren't actually connected to anything, so try and do the
677 // handshake with the remote GDB server and make sure that goes
678 // alright.
679 if (!m_gdb_comm.HandshakeWithServer (NULL))
Chris Lattner24943d22010-06-08 16:52:24 +0000680 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000681 m_gdb_comm.Disconnect();
682 if (error.Success())
683 error.SetErrorString("not connected to remote gdb server");
684 return error;
Chris Lattner24943d22010-06-08 16:52:24 +0000685 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000686 m_gdb_comm.ResetDiscoverableSettings();
687 m_gdb_comm.QueryNoAckModeSupported ();
688 m_gdb_comm.GetThreadSuffixSupported ();
689 m_gdb_comm.GetHostInfo ();
690 m_gdb_comm.GetVContSupported ('c');
Chris Lattner24943d22010-06-08 16:52:24 +0000691 return error;
692}
693
694void
695ProcessGDBRemote::DidLaunchOrAttach ()
696{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000697 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
698 if (log)
699 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton75c703d2011-02-16 04:46:07 +0000700 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000701 {
702 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
703
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000704 BuildDynamicRegisterInfo (false);
Greg Clayton20d338f2010-11-18 05:57:03 +0000705
Chris Lattner24943d22010-06-08 16:52:24 +0000706 // See if the GDB server supports the qHostInfo information
Greg Claytonfc7920f2011-02-09 03:09:55 +0000707
Greg Claytoncb8977d2011-03-23 00:09:55 +0000708 const ArchSpec &gdb_remote_arch = m_gdb_comm.GetHostArchitecture();
709 if (gdb_remote_arch.IsValid())
Greg Claytonfc7920f2011-02-09 03:09:55 +0000710 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000711 ArchSpec &target_arch = GetTarget().GetArchitecture();
712
713 if (target_arch.IsValid())
714 {
715 // If the remote host is ARM and we have apple as the vendor, then
716 // ARM executables and shared libraries can have mixed ARM architectures.
717 // You can have an armv6 executable, and if the host is armv7, then the
718 // system will load the best possible architecture for all shared libraries
719 // it has, so we really need to take the remote host architecture as our
720 // defacto architecture in this case.
721
722 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
723 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
724 {
725 target_arch = gdb_remote_arch;
726 }
727 else
728 {
729 // Fill in what is missing in the triple
730 const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple();
731 llvm::Triple &target_triple = target_arch.GetTriple();
Greg Clayton2f085c62011-05-15 01:25:55 +0000732 if (target_triple.getVendorName().size() == 0)
733 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000734 target_triple.setVendor (remote_triple.getVendor());
735
Greg Clayton2f085c62011-05-15 01:25:55 +0000736 if (target_triple.getOSName().size() == 0)
737 {
738 target_triple.setOS (remote_triple.getOS());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000739
Greg Clayton2f085c62011-05-15 01:25:55 +0000740 if (target_triple.getEnvironmentName().size() == 0)
741 target_triple.setEnvironment (remote_triple.getEnvironment());
742 }
743 }
Greg Claytoncb8977d2011-03-23 00:09:55 +0000744 }
745 }
746 else
747 {
748 // The target doesn't have a valid architecture yet, set it from
749 // the architecture we got from the remote GDB server
750 target_arch = gdb_remote_arch;
751 }
Greg Claytonfc7920f2011-02-09 03:09:55 +0000752 }
Chris Lattner24943d22010-06-08 16:52:24 +0000753 }
754}
755
756void
757ProcessGDBRemote::DidLaunch ()
758{
759 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000760}
761
762Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000763ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000764{
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000765 ProcessAttachInfo attach_info;
766 return DoAttachToProcessWithID(attach_pid, attach_info);
767}
768
769Error
770ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
771{
Chris Lattner24943d22010-06-08 16:52:24 +0000772 Error error;
773 // Clear out and clean up from any current state
774 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000775 if (attach_pid != LLDB_INVALID_PROCESS_ID)
776 {
Greg Claytona2f74232011-02-24 22:24:29 +0000777 // Make sure we aren't already connected?
778 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000779 {
Greg Claytona2f74232011-02-24 22:24:29 +0000780 char host_port[128];
781 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
782 char connect_url[128];
783 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000784
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000785 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000786
787 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000788 {
Greg Claytona2f74232011-02-24 22:24:29 +0000789 const char *error_string = error.AsCString();
790 if (error_string == NULL)
791 error_string = "unable to launch " DEBUGSERVER_BASENAME;
792
793 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000794 }
Greg Claytona2f74232011-02-24 22:24:29 +0000795 else
796 {
797 error = ConnectToDebugserver (connect_url);
798 }
799 }
800
801 if (error.Success())
802 {
803 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +0000804 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", attach_pid);
Greg Clayton489575c2011-11-19 02:11:30 +0000805 SetID (attach_pid);
Greg Claytona2f74232011-02-24 22:24:29 +0000806 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner24943d22010-06-08 16:52:24 +0000807 }
808 }
Chris Lattner24943d22010-06-08 16:52:24 +0000809 return error;
810}
811
812size_t
813ProcessGDBRemote::AttachInputReaderCallback
814(
815 void *baton,
816 InputReader *reader,
817 lldb::InputReaderAction notification,
818 const char *bytes,
819 size_t bytes_len
820)
821{
822 if (notification == eInputReaderGotToken)
823 {
824 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
825 if (gdb_process->m_waiting_for_attach)
826 gdb_process->m_waiting_for_attach = false;
827 reader->SetIsDone(true);
828 return 1;
829 }
830 return 0;
831}
832
833Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000834ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000835{
836 Error error;
837 // Clear out and clean up from any current state
838 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000839
Chris Lattner24943d22010-06-08 16:52:24 +0000840 if (process_name && process_name[0])
841 {
Greg Claytona2f74232011-02-24 22:24:29 +0000842 // Make sure we aren't already connected?
843 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000844 {
Greg Claytona2f74232011-02-24 22:24:29 +0000845 char host_port[128];
846 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
847 char connect_url[128];
848 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
849
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000850 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000851 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000852 {
Greg Claytona2f74232011-02-24 22:24:29 +0000853 const char *error_string = error.AsCString();
854 if (error_string == NULL)
855 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner24943d22010-06-08 16:52:24 +0000856
Greg Claytona2f74232011-02-24 22:24:29 +0000857 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000858 }
Greg Claytona2f74232011-02-24 22:24:29 +0000859 else
860 {
861 error = ConnectToDebugserver (connect_url);
862 }
863 }
864
865 if (error.Success())
866 {
867 StreamString packet;
868
869 if (wait_for_launch)
870 packet.PutCString("vAttachWait");
871 else
872 packet.PutCString("vAttachName");
873 packet.PutChar(';');
874 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
875
876 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
877
Chris Lattner24943d22010-06-08 16:52:24 +0000878 }
879 }
Chris Lattner24943d22010-06-08 16:52:24 +0000880 return error;
881}
882
Chris Lattner24943d22010-06-08 16:52:24 +0000883
884void
885ProcessGDBRemote::DidAttach ()
886{
Greg Claytone71e2582011-02-04 01:58:07 +0000887 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000888}
889
890Error
891ProcessGDBRemote::WillResume ()
892{
Greg Claytonc1f45872011-02-12 06:28:37 +0000893 m_continue_c_tids.clear();
894 m_continue_C_tids.clear();
895 m_continue_s_tids.clear();
896 m_continue_S_tids.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000897 return Error();
898}
899
900Error
901ProcessGDBRemote::DoResume ()
902{
Jim Ingham3ae449a2010-11-17 02:32:00 +0000903 Error error;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000904 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
905 if (log)
906 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytonb749a262010-12-03 06:02:24 +0000907
908 Listener listener ("gdb-remote.resume-packet-sent");
909 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
910 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000911 StreamString continue_packet;
912 bool continue_packet_error = false;
913 if (m_gdb_comm.HasAnyVContSupport ())
914 {
915 continue_packet.PutCString ("vCont");
916
917 if (!m_continue_c_tids.empty())
918 {
919 if (m_gdb_comm.GetVContSupported ('c'))
920 {
921 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)
Greg Claytond9919d32011-12-01 23:28:38 +0000922 continue_packet.Printf(";c:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000923 }
924 else
925 continue_packet_error = true;
926 }
927
928 if (!continue_packet_error && !m_continue_C_tids.empty())
929 {
930 if (m_gdb_comm.GetVContSupported ('C'))
931 {
932 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)
Greg Claytond9919d32011-12-01 23:28:38 +0000933 continue_packet.Printf(";C%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000934 }
935 else
936 continue_packet_error = true;
937 }
Greg Claytonb749a262010-12-03 06:02:24 +0000938
Greg Claytonc1f45872011-02-12 06:28:37 +0000939 if (!continue_packet_error && !m_continue_s_tids.empty())
940 {
941 if (m_gdb_comm.GetVContSupported ('s'))
942 {
943 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)
Greg Claytond9919d32011-12-01 23:28:38 +0000944 continue_packet.Printf(";s:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000945 }
946 else
947 continue_packet_error = true;
948 }
949
950 if (!continue_packet_error && !m_continue_S_tids.empty())
951 {
952 if (m_gdb_comm.GetVContSupported ('S'))
953 {
954 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)
Greg Claytond9919d32011-12-01 23:28:38 +0000955 continue_packet.Printf(";S%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000956 }
957 else
958 continue_packet_error = true;
959 }
960
961 if (continue_packet_error)
962 continue_packet.GetString().clear();
963 }
964 else
965 continue_packet_error = true;
966
967 if (continue_packet_error)
968 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000969 // Either no vCont support, or we tried to use part of the vCont
970 // packet that wasn't supported by the remote GDB server.
971 // We need to try and make a simple packet that can do our continue
972 const size_t num_threads = GetThreadList().GetSize();
973 const size_t num_continue_c_tids = m_continue_c_tids.size();
974 const size_t num_continue_C_tids = m_continue_C_tids.size();
975 const size_t num_continue_s_tids = m_continue_s_tids.size();
976 const size_t num_continue_S_tids = m_continue_S_tids.size();
977 if (num_continue_c_tids > 0)
978 {
979 if (num_continue_c_tids == num_threads)
980 {
981 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +0000982 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +0000983 continue_packet.PutChar ('c');
984 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +0000985 }
986 else if (num_continue_c_tids == 1 &&
987 num_continue_C_tids == 0 &&
988 num_continue_s_tids == 0 &&
989 num_continue_S_tids == 0 )
990 {
991 // Only one thread is continuing
Greg Claytonb72d0f02011-04-12 05:54:46 +0000992 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +0000993 continue_packet.PutChar ('c');
Greg Claytonde1dd812011-06-24 03:21:43 +0000994 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +0000995 }
996 }
997
Greg Claytonde1dd812011-06-24 03:21:43 +0000998 if (continue_packet_error && num_continue_C_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +0000999 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001000 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1001 num_continue_C_tids > 0 &&
1002 num_continue_s_tids == 0 &&
1003 num_continue_S_tids == 0 )
Greg Claytonc1f45872011-02-12 06:28:37 +00001004 {
1005 const int continue_signo = m_continue_C_tids.front().second;
Greg Claytonde1dd812011-06-24 03:21:43 +00001006 // Only one thread is continuing
Greg Claytonc1f45872011-02-12 06:28:37 +00001007 if (num_continue_C_tids > 1)
1008 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001009 // More that one thread with a signal, yet we don't have
1010 // vCont support and we are being asked to resume each
1011 // thread with a signal, we need to make sure they are
1012 // all the same signal, or we can't issue the continue
1013 // accurately with the current support...
1014 if (num_continue_C_tids > 1)
Greg Claytonc1f45872011-02-12 06:28:37 +00001015 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001016 continue_packet_error = false;
1017 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1018 {
1019 if (m_continue_C_tids[i].second != continue_signo)
1020 continue_packet_error = true;
1021 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001022 }
Greg Claytonde1dd812011-06-24 03:21:43 +00001023 if (!continue_packet_error)
1024 m_gdb_comm.SetCurrentThreadForRun (-1);
1025 }
1026 else
1027 {
1028 // Set the continue thread ID
1029 continue_packet_error = false;
1030 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001031 }
1032 if (!continue_packet_error)
1033 {
1034 // Add threads continuing with the same signo...
Greg Claytonc1f45872011-02-12 06:28:37 +00001035 continue_packet.Printf("C%2.2x", continue_signo);
1036 }
1037 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001038 }
1039
Greg Claytonde1dd812011-06-24 03:21:43 +00001040 if (continue_packet_error && num_continue_s_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +00001041 {
1042 if (num_continue_s_tids == num_threads)
1043 {
1044 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001045 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +00001046 continue_packet.PutChar ('s');
1047 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001048 }
1049 else if (num_continue_c_tids == 0 &&
1050 num_continue_C_tids == 0 &&
1051 num_continue_s_tids == 1 &&
1052 num_continue_S_tids == 0 )
1053 {
1054 // Only one thread is stepping
Greg Claytonb72d0f02011-04-12 05:54:46 +00001055 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +00001056 continue_packet.PutChar ('s');
Greg Claytonde1dd812011-06-24 03:21:43 +00001057 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001058 }
1059 }
1060
1061 if (!continue_packet_error && num_continue_S_tids > 0)
1062 {
1063 if (num_continue_S_tids == num_threads)
1064 {
1065 const int step_signo = m_continue_S_tids.front().second;
1066 // Are all threads trying to step with the same signal?
Greg Claytonde1dd812011-06-24 03:21:43 +00001067 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001068 if (num_continue_S_tids > 1)
1069 {
1070 for (size_t i=1; i<num_threads; ++i)
1071 {
1072 if (m_continue_S_tids[i].second != step_signo)
1073 continue_packet_error = true;
1074 }
1075 }
1076 if (!continue_packet_error)
1077 {
1078 // Add threads stepping with the same signo...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001079 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonc1f45872011-02-12 06:28:37 +00001080 continue_packet.Printf("S%2.2x", step_signo);
1081 }
1082 }
1083 else if (num_continue_c_tids == 0 &&
1084 num_continue_C_tids == 0 &&
1085 num_continue_s_tids == 0 &&
1086 num_continue_S_tids == 1 )
1087 {
1088 // Only one thread is stepping with signal
Greg Claytonb72d0f02011-04-12 05:54:46 +00001089 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001090 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Claytonde1dd812011-06-24 03:21:43 +00001091 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001092 }
1093 }
1094 }
1095
1096 if (continue_packet_error)
1097 {
1098 error.SetErrorString ("can't make continue packet for this resume");
1099 }
1100 else
1101 {
1102 EventSP event_sp;
1103 TimeValue timeout;
1104 timeout = TimeValue::Now();
1105 timeout.OffsetWithSeconds (5);
1106 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1107
1108 if (listener.WaitForEvent (&timeout, event_sp) == false)
1109 error.SetErrorString("Resume timed out.");
1110 }
Greg Claytonb749a262010-12-03 06:02:24 +00001111 }
1112
Jim Ingham3ae449a2010-11-17 02:32:00 +00001113 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001114}
1115
Chris Lattner24943d22010-06-08 16:52:24 +00001116uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +00001117ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner24943d22010-06-08 16:52:24 +00001118{
1119 // locker will keep a mutex locked until it goes out of scope
Greg Claytone005f2c2010-11-06 01:53:30 +00001120 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001121 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +00001122 log->Printf ("ProcessGDBRemote::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton37f962e2011-08-22 02:49:39 +00001123 // Update the thread list's stop id immediately so we don't recurse into this function.
Chris Lattner24943d22010-06-08 16:52:24 +00001124
Greg Clayton37f962e2011-08-22 02:49:39 +00001125 std::vector<lldb::tid_t> thread_ids;
1126 bool sequence_mutex_unavailable = false;
1127 const size_t num_thread_ids = m_gdb_comm.GetCurrentThreadIDs (thread_ids, sequence_mutex_unavailable);
1128 if (num_thread_ids > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001129 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001130 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001131 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001132 tid_t tid = thread_ids[i];
1133 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
1134 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +00001135 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +00001136 new_thread_list.AddThread(thread_sp);
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001137 }
Chris Lattner24943d22010-06-08 16:52:24 +00001138 }
Greg Clayton37f962e2011-08-22 02:49:39 +00001139
1140 if (sequence_mutex_unavailable == false)
1141 SetThreadStopInfo (m_last_stop_packet);
1142 return new_thread_list.GetSize(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001143}
1144
1145
1146StateType
1147ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1148{
Greg Clayton261a18b2011-06-02 22:22:38 +00001149 stop_packet.SetFilePos (0);
Chris Lattner24943d22010-06-08 16:52:24 +00001150 const char stop_type = stop_packet.GetChar();
1151 switch (stop_type)
1152 {
1153 case 'T':
1154 case 'S':
1155 {
Greg Claytonc3c46612011-02-15 00:19:15 +00001156 if (GetStopID() == 0)
1157 {
1158 // Our first stop, make sure we have a process ID, and also make
1159 // sure we know about our registers
1160 if (GetID() == LLDB_INVALID_PROCESS_ID)
1161 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001162 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonc3c46612011-02-15 00:19:15 +00001163 if (pid != LLDB_INVALID_PROCESS_ID)
1164 SetID (pid);
1165 }
1166 BuildDynamicRegisterInfo (true);
1167 }
Chris Lattner24943d22010-06-08 16:52:24 +00001168 // Stop with signal and thread info
1169 const uint8_t signo = stop_packet.GetHexU8();
1170 std::string name;
1171 std::string value;
1172 std::string thread_name;
Greg Clayton65611552011-06-04 01:26:29 +00001173 std::string reason;
1174 std::string description;
Chris Lattner24943d22010-06-08 16:52:24 +00001175 uint32_t exc_type = 0;
Greg Clayton7661a982010-07-23 16:45:51 +00001176 std::vector<addr_t> exc_data;
Chris Lattner24943d22010-06-08 16:52:24 +00001177 uint32_t tid = LLDB_INVALID_THREAD_ID;
1178 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
1179 uint32_t exc_data_count = 0;
Greg Claytona875b642011-01-09 21:07:35 +00001180 ThreadSP thread_sp;
1181
Chris Lattner24943d22010-06-08 16:52:24 +00001182 while (stop_packet.GetNameColonValue(name, value))
1183 {
1184 if (name.compare("metype") == 0)
1185 {
1186 // exception type in big endian hex
1187 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1188 }
1189 else if (name.compare("mecount") == 0)
1190 {
1191 // exception count in big endian hex
1192 exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16);
1193 }
1194 else if (name.compare("medata") == 0)
1195 {
1196 // exception data in big endian hex
1197 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1198 }
1199 else if (name.compare("thread") == 0)
1200 {
1201 // thread in big endian hex
1202 tid = Args::StringToUInt32 (value.c_str(), 0, 16);
Greg Claytonffa43a62011-11-17 04:46:02 +00001203 // m_thread_list does have its own mutex, but we need to
1204 // hold onto the mutex between the call to m_thread_list.FindThreadByID(...)
1205 // and the m_thread_list.AddThread(...) so it doesn't change on us
Greg Claytonc3c46612011-02-15 00:19:15 +00001206 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytona875b642011-01-09 21:07:35 +00001207 thread_sp = m_thread_list.FindThreadByID(tid, false);
Greg Claytonc3c46612011-02-15 00:19:15 +00001208 if (!thread_sp)
1209 {
1210 // Create the thread if we need to
Greg Claytonf4124de2012-02-21 00:09:25 +00001211 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Claytonc3c46612011-02-15 00:19:15 +00001212 m_thread_list.AddThread(thread_sp);
1213 }
Chris Lattner24943d22010-06-08 16:52:24 +00001214 }
Greg Clayton4862fa22011-01-08 03:17:57 +00001215 else if (name.compare("hexname") == 0)
1216 {
1217 StringExtractor name_extractor;
1218 // Swap "value" over into "name_extractor"
1219 name_extractor.GetStringRef().swap(value);
1220 // Now convert the HEX bytes into a string value
1221 name_extractor.GetHexByteString (value);
1222 thread_name.swap (value);
1223 }
Chris Lattner24943d22010-06-08 16:52:24 +00001224 else if (name.compare("name") == 0)
1225 {
1226 thread_name.swap (value);
1227 }
Greg Clayton0a7f75f2010-09-09 06:32:46 +00001228 else if (name.compare("qaddr") == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001229 {
1230 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1231 }
Greg Clayton65611552011-06-04 01:26:29 +00001232 else if (name.compare("reason") == 0)
1233 {
1234 reason.swap(value);
1235 }
1236 else if (name.compare("description") == 0)
1237 {
1238 StringExtractor desc_extractor;
1239 // Swap "value" over into "name_extractor"
1240 desc_extractor.GetStringRef().swap(value);
1241 // Now convert the HEX bytes into a string value
1242 desc_extractor.GetHexByteString (thread_name);
1243 }
Greg Claytona875b642011-01-09 21:07:35 +00001244 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1245 {
1246 // We have a register number that contains an expedited
1247 // register value. Lets supply this register to our thread
1248 // so it won't have to go and read it.
1249 if (thread_sp)
1250 {
1251 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1252
1253 if (reg != UINT32_MAX)
1254 {
1255 StringExtractor reg_value_extractor;
1256 // Swap "value" over into "reg_value_extractor"
1257 reg_value_extractor.GetStringRef().swap(value);
Greg Claytonc3c46612011-02-15 00:19:15 +00001258 if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor))
1259 {
1260 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1261 name.c_str(),
1262 reg,
1263 reg,
1264 reg_value_extractor.GetStringRef().c_str(),
1265 stop_packet.GetStringRef().c_str());
1266 }
Greg Claytona875b642011-01-09 21:07:35 +00001267 }
1268 }
1269 }
Chris Lattner24943d22010-06-08 16:52:24 +00001270 }
Chris Lattner24943d22010-06-08 16:52:24 +00001271
1272 if (thread_sp)
1273 {
1274 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1275
1276 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Ingham9082c8a2011-01-28 02:23:12 +00001277 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001278 if (exc_type != 0)
1279 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001280 const size_t exc_data_size = exc_data.size();
Greg Clayton643ee732010-08-04 01:40:35 +00001281
1282 gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1283 exc_type,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001284 exc_data_size,
1285 exc_data_size >= 1 ? exc_data[0] : 0,
Johnny Chen36889ad2011-09-17 01:05:03 +00001286 exc_data_size >= 2 ? exc_data[1] : 0,
1287 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner24943d22010-06-08 16:52:24 +00001288 }
Greg Clayton65611552011-06-04 01:26:29 +00001289 else
Chris Lattner24943d22010-06-08 16:52:24 +00001290 {
Greg Clayton65611552011-06-04 01:26:29 +00001291 bool handled = false;
1292 if (!reason.empty())
1293 {
1294 if (reason.compare("trace") == 0)
1295 {
1296 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1297 handled = true;
1298 }
1299 else if (reason.compare("breakpoint") == 0)
1300 {
1301 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001302 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001303 if (bp_site_sp)
1304 {
1305 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1306 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1307 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1308 if (bp_site_sp->ValidForThisThread (gdb_thread))
1309 {
1310 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1311 handled = true;
1312 }
1313 }
1314
1315 if (!handled)
1316 {
1317 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1318 }
1319 }
1320 else if (reason.compare("trap") == 0)
1321 {
1322 // Let the trap just use the standard signal stop reason below...
1323 }
1324 else if (reason.compare("watchpoint") == 0)
1325 {
1326 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1327 // TODO: locate the watchpoint somehow...
1328 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
1329 handled = true;
1330 }
1331 else if (reason.compare("exception") == 0)
1332 {
1333 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
1334 handled = true;
1335 }
1336 }
1337
1338 if (signo)
1339 {
1340 if (signo == SIGTRAP)
1341 {
1342 // Currently we are going to assume SIGTRAP means we are either
1343 // hitting a breakpoint or hardware single stepping.
1344 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001345 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001346 if (bp_site_sp)
1347 {
1348 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1349 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1350 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1351 if (bp_site_sp->ValidForThisThread (gdb_thread))
1352 {
1353 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1354 handled = true;
1355 }
1356 }
1357 if (!handled)
1358 {
1359 // TODO: check for breakpoint or trap opcode in case there is a hard
1360 // coded software trap
1361 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1362 handled = true;
1363 }
1364 }
1365 if (!handled)
Greg Clayton37f962e2011-08-22 02:49:39 +00001366 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Chris Lattner24943d22010-06-08 16:52:24 +00001367 }
1368 else
1369 {
Greg Clayton643ee732010-08-04 01:40:35 +00001370 StopInfoSP invalid_stop_info_sp;
1371 gdb_thread->SetStopInfo (invalid_stop_info_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001372 }
Greg Clayton65611552011-06-04 01:26:29 +00001373
1374 if (!description.empty())
1375 {
1376 lldb::StopInfoSP stop_info_sp (gdb_thread->GetStopInfo ());
1377 if (stop_info_sp)
1378 {
1379 stop_info_sp->SetDescription (description.c_str());
Greg Clayton153ccd72011-08-10 02:10:13 +00001380 }
Greg Clayton65611552011-06-04 01:26:29 +00001381 else
1382 {
1383 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
1384 }
1385 }
1386 }
Chris Lattner24943d22010-06-08 16:52:24 +00001387 }
1388 return eStateStopped;
1389 }
1390 break;
1391
1392 case 'W':
1393 // process exited
1394 return eStateExited;
1395
1396 default:
1397 break;
1398 }
1399 return eStateInvalid;
1400}
1401
1402void
1403ProcessGDBRemote::RefreshStateAfterStop ()
1404{
Chris Lattner24943d22010-06-08 16:52:24 +00001405 // Let all threads recover from stopping and do any clean up based
1406 // on the previous thread state (if any).
1407 m_thread_list.RefreshStateAfterStop();
Greg Clayton261a18b2011-06-02 22:22:38 +00001408 SetThreadStopInfo (m_last_stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +00001409}
1410
1411Error
Jim Ingham3ae449a2010-11-17 02:32:00 +00001412ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner24943d22010-06-08 16:52:24 +00001413{
1414 Error error;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001415
Greg Claytona4881d02011-01-22 07:12:45 +00001416 bool timed_out = false;
1417 Mutex::Locker locker;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001418
1419 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton20d338f2010-11-18 05:57:03 +00001420 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001421 // We are being asked to halt during an attach. We need to just close
1422 // our file handle and debugserver will go away, and we can be done...
1423 m_gdb_comm.Disconnect();
Greg Clayton20d338f2010-11-18 05:57:03 +00001424 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001425 else
1426 {
1427 if (!m_gdb_comm.SendInterrupt (locker, 2, caused_stop, timed_out))
1428 {
1429 if (timed_out)
1430 error.SetErrorString("timed out sending interrupt packet");
1431 else
1432 error.SetErrorString("unknown error sending interrupt packet");
1433 }
1434 }
Chris Lattner24943d22010-06-08 16:52:24 +00001435 return error;
1436}
1437
1438Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001439ProcessGDBRemote::InterruptIfRunning
1440(
1441 bool discard_thread_plans,
1442 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +00001443 EventSP &stop_event_sp
1444)
Chris Lattner24943d22010-06-08 16:52:24 +00001445{
1446 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001447
Greg Clayton2860ba92011-01-23 19:58:49 +00001448 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1449
Greg Clayton68ca8232011-01-25 02:58:48 +00001450 bool paused_private_state_thread = false;
Greg Clayton2860ba92011-01-23 19:58:49 +00001451 const bool is_running = m_gdb_comm.IsRunning();
1452 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00001453 log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
Greg Clayton2860ba92011-01-23 19:58:49 +00001454 discard_thread_plans,
Greg Clayton68ca8232011-01-25 02:58:48 +00001455 catch_stop_event,
Greg Clayton2860ba92011-01-23 19:58:49 +00001456 is_running);
1457
Greg Clayton2860ba92011-01-23 19:58:49 +00001458 if (discard_thread_plans)
1459 {
1460 if (log)
1461 log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans");
1462 m_thread_list.DiscardThreadPlans();
1463 }
1464 if (is_running)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001465 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001466 if (catch_stop_event)
1467 {
1468 if (log)
1469 log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread");
1470 PausePrivateStateThread();
1471 paused_private_state_thread = true;
1472 }
1473
Greg Clayton4fb400f2010-09-27 21:07:38 +00001474 bool timed_out = false;
Greg Claytona4881d02011-01-22 07:12:45 +00001475 bool sent_interrupt = false;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001476 Mutex::Locker locker;
Greg Clayton72e1c782011-01-22 23:43:18 +00001477
Greg Clayton72e1c782011-01-22 23:43:18 +00001478 if (!m_gdb_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
Greg Clayton4fb400f2010-09-27 21:07:38 +00001479 {
1480 if (timed_out)
1481 error.SetErrorString("timed out sending interrupt packet");
1482 else
1483 error.SetErrorString("unknown error sending interrupt packet");
Greg Clayton68ca8232011-01-25 02:58:48 +00001484 if (paused_private_state_thread)
Greg Clayton72e1c782011-01-22 23:43:18 +00001485 ResumePrivateStateThread();
1486 return error;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001487 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001488
Greg Clayton72e1c782011-01-22 23:43:18 +00001489 if (catch_stop_event)
1490 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001491 // LISTEN HERE
Greg Clayton72e1c782011-01-22 23:43:18 +00001492 TimeValue timeout_time;
1493 timeout_time = TimeValue::Now();
Greg Clayton68ca8232011-01-25 02:58:48 +00001494 timeout_time.OffsetWithSeconds(5);
1495 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
Greg Clayton2860ba92011-01-23 19:58:49 +00001496
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001497 timed_out = state == eStateInvalid;
Greg Clayton2860ba92011-01-23 19:58:49 +00001498 if (log)
1499 log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001500
Greg Clayton2860ba92011-01-23 19:58:49 +00001501 if (timed_out)
Greg Clayton72e1c782011-01-22 23:43:18 +00001502 error.SetErrorString("unable to verify target stopped");
1503 }
1504
Greg Clayton68ca8232011-01-25 02:58:48 +00001505 if (paused_private_state_thread)
Greg Clayton2860ba92011-01-23 19:58:49 +00001506 {
1507 if (log)
1508 log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread");
Greg Clayton72e1c782011-01-22 23:43:18 +00001509 ResumePrivateStateThread();
Greg Clayton2860ba92011-01-23 19:58:49 +00001510 }
Greg Clayton4fb400f2010-09-27 21:07:38 +00001511 }
Chris Lattner24943d22010-06-08 16:52:24 +00001512 return error;
1513}
1514
Greg Clayton4fb400f2010-09-27 21:07:38 +00001515Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001516ProcessGDBRemote::WillDetach ()
1517{
Greg Clayton2860ba92011-01-23 19:58:49 +00001518 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1519 if (log)
1520 log->Printf ("ProcessGDBRemote::WillDetach()");
1521
Greg Clayton72e1c782011-01-22 23:43:18 +00001522 bool discard_thread_plans = true;
1523 bool catch_stop_event = true;
Greg Clayton72e1c782011-01-22 23:43:18 +00001524 EventSP event_sp;
Greg Clayton68ca8232011-01-25 02:58:48 +00001525 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
Greg Clayton72e1c782011-01-22 23:43:18 +00001526}
1527
1528Error
Greg Clayton4fb400f2010-09-27 21:07:38 +00001529ProcessGDBRemote::DoDetach()
1530{
1531 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001532 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton4fb400f2010-09-27 21:07:38 +00001533 if (log)
1534 log->Printf ("ProcessGDBRemote::DoDetach()");
1535
1536 DisableAllBreakpointSites ();
1537
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001538 m_thread_list.DiscardThreadPlans();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001539
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001540 size_t response_size = m_gdb_comm.SendPacket ("D", 1);
1541 if (log)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001542 {
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001543 if (response_size)
1544 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1545 else
1546 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed");
Greg Clayton4fb400f2010-09-27 21:07:38 +00001547 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001548 // Sleep for one second to let the process get all detached...
Greg Clayton4fb400f2010-09-27 21:07:38 +00001549 StopAsyncThread ();
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001550
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001551 SetPrivateState (eStateDetached);
1552 ResumePrivateStateThread();
1553
1554 //KillDebugserverProcess ();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001555 return error;
1556}
Chris Lattner24943d22010-06-08 16:52:24 +00001557
1558Error
1559ProcessGDBRemote::DoDestroy ()
1560{
1561 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001562 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001563 if (log)
1564 log->Printf ("ProcessGDBRemote::DoDestroy()");
1565
1566 // Interrupt if our inferior is running...
Greg Claytona4881d02011-01-22 07:12:45 +00001567 if (m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +00001568 {
Jim Ingham8226e942011-10-28 01:11:35 +00001569 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton72e1c782011-01-22 23:43:18 +00001570 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001571
1572 StringExtractorGDBRemote response;
1573 bool send_async = true;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001574 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001575 {
1576 char packet_cmd = response.GetChar(0);
1577
1578 if (packet_cmd == 'W' || packet_cmd == 'X')
1579 {
Greg Clayton06709002011-12-06 04:51:14 +00001580 SetLastStopPacket (response);
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001581 SetExitStatus(response.GetHexU8(), NULL);
1582 }
1583 }
1584 else
1585 {
1586 SetExitStatus(SIGABRT, NULL);
1587 //error.SetErrorString("kill packet failed");
1588 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001589 }
1590 }
Chris Lattner24943d22010-06-08 16:52:24 +00001591 StopAsyncThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001592 KillDebugserverProcess ();
1593 return error;
1594}
1595
Chris Lattner24943d22010-06-08 16:52:24 +00001596//------------------------------------------------------------------
1597// Process Queries
1598//------------------------------------------------------------------
1599
1600bool
1601ProcessGDBRemote::IsAlive ()
1602{
Greg Clayton58e844b2010-12-08 05:08:21 +00001603 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner24943d22010-06-08 16:52:24 +00001604}
1605
1606addr_t
1607ProcessGDBRemote::GetImageInfoAddress()
1608{
1609 if (!m_gdb_comm.IsRunning())
1610 {
1611 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001612 if (m_gdb_comm.SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
Chris Lattner24943d22010-06-08 16:52:24 +00001613 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001614 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001615 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1616 }
1617 }
1618 return LLDB_INVALID_ADDRESS;
1619}
1620
Chris Lattner24943d22010-06-08 16:52:24 +00001621//------------------------------------------------------------------
1622// Process Memory
1623//------------------------------------------------------------------
1624size_t
1625ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1626{
1627 if (size > m_max_memory_size)
1628 {
1629 // Keep memory read sizes down to a sane limit. This function will be
1630 // called multiple times in order to complete the task by
1631 // lldb_private::Process so it is ok to do this.
1632 size = m_max_memory_size;
1633 }
1634
1635 char packet[64];
1636 const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size);
1637 assert (packet_len + 1 < sizeof(packet));
1638 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001639 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001640 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001641 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001642 {
1643 error.Clear();
1644 return response.GetHexBytes(buf, size, '\xdd');
1645 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001646 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001647 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001648 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001649 error.SetErrorStringWithFormat("'%s' packet unsupported", packet);
1650 else
1651 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str());
1652 }
1653 else
1654 {
1655 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet);
1656 }
1657 return 0;
1658}
1659
1660size_t
1661ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1662{
Greg Claytonc8bc1c32011-05-16 02:35:02 +00001663 if (size > m_max_memory_size)
1664 {
1665 // Keep memory read sizes down to a sane limit. This function will be
1666 // called multiple times in order to complete the task by
1667 // lldb_private::Process so it is ok to do this.
1668 size = m_max_memory_size;
1669 }
1670
Chris Lattner24943d22010-06-08 16:52:24 +00001671 StreamString packet;
1672 packet.Printf("M%llx,%zx:", addr, size);
Greg Claytoncd548032011-02-01 01:31:41 +00001673 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00001674 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001675 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001676 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001677 if (response.IsOKResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001678 {
1679 error.Clear();
1680 return size;
1681 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001682 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001683 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001684 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001685 error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str());
1686 else
1687 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
1688 }
1689 else
1690 {
1691 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str());
1692 }
1693 return 0;
1694}
1695
1696lldb::addr_t
1697ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
1698{
Greg Clayton989816b2011-05-14 01:50:35 +00001699 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
1700
Greg Clayton2f085c62011-05-15 01:25:55 +00001701 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton989816b2011-05-14 01:50:35 +00001702 switch (supported)
1703 {
1704 case eLazyBoolCalculate:
1705 case eLazyBoolYes:
1706 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
1707 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
1708 return allocated_addr;
1709
1710 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001711 // Call mmap() to create memory in the inferior..
1712 unsigned prot = 0;
1713 if (permissions & lldb::ePermissionsReadable)
1714 prot |= eMmapProtRead;
1715 if (permissions & lldb::ePermissionsWritable)
1716 prot |= eMmapProtWrite;
1717 if (permissions & lldb::ePermissionsExecutable)
1718 prot |= eMmapProtExec;
Greg Clayton989816b2011-05-14 01:50:35 +00001719
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001720 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
1721 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
1722 m_addr_to_mmap_size[allocated_addr] = size;
1723 else
1724 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton989816b2011-05-14 01:50:35 +00001725 break;
1726 }
1727
Chris Lattner24943d22010-06-08 16:52:24 +00001728 if (allocated_addr == LLDB_INVALID_ADDRESS)
Greg Clayton613b8732011-05-17 03:37:42 +00001729 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
Chris Lattner24943d22010-06-08 16:52:24 +00001730 else
1731 error.Clear();
1732 return allocated_addr;
1733}
1734
1735Error
Greg Claytona9385532011-11-18 07:03:08 +00001736ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
1737 MemoryRegionInfo &region_info)
1738{
1739
1740 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
1741 return error;
1742}
1743
1744Error
Chris Lattner24943d22010-06-08 16:52:24 +00001745ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
1746{
1747 Error error;
Greg Clayton2f085c62011-05-15 01:25:55 +00001748 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
1749
1750 switch (supported)
1751 {
1752 case eLazyBoolCalculate:
1753 // We should never be deallocating memory without allocating memory
1754 // first so we should never get eLazyBoolCalculate
1755 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
1756 break;
1757
1758 case eLazyBoolYes:
1759 if (!m_gdb_comm.DeallocateMemory (addr))
1760 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
1761 break;
1762
1763 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001764 // Call munmap() to deallocate memory in the inferior..
Greg Clayton2f085c62011-05-15 01:25:55 +00001765 {
1766 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001767 if (pos != m_addr_to_mmap_size.end() &&
1768 InferiorCallMunmap(this, addr, pos->second))
1769 m_addr_to_mmap_size.erase (pos);
1770 else
1771 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
Greg Clayton2f085c62011-05-15 01:25:55 +00001772 }
1773 break;
1774 }
1775
Chris Lattner24943d22010-06-08 16:52:24 +00001776 return error;
1777}
1778
1779
1780//------------------------------------------------------------------
1781// Process STDIO
1782//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001783size_t
1784ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
1785{
1786 if (m_stdio_communication.IsConnected())
1787 {
1788 ConnectionStatus status;
1789 m_stdio_communication.Write(src, src_len, status, NULL);
1790 }
1791 return 0;
1792}
1793
1794Error
1795ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site)
1796{
1797 Error error;
1798 assert (bp_site != NULL);
1799
Greg Claytone005f2c2010-11-06 01:53:30 +00001800 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001801 user_id_t site_id = bp_site->GetID();
1802 const addr_t addr = bp_site->GetLoadAddress();
1803 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001804 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001805
1806 if (bp_site->IsEnabled())
1807 {
1808 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001809 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx -- SUCCESS (already enabled)", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001810 return error;
1811 }
1812 else
1813 {
1814 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1815
1816 if (bp_site->HardwarePreferred())
1817 {
1818 // Try and set hardware breakpoint, and if that fails, fall through
1819 // and set a software breakpoint?
Greg Claytonb72d0f02011-04-12 05:54:46 +00001820 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner24943d22010-06-08 16:52:24 +00001821 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001822 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001823 {
1824 bp_site->SetEnabled(true);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001825 bp_site->SetType (BreakpointSite::eHardware);
Chris Lattner24943d22010-06-08 16:52:24 +00001826 return error;
1827 }
Chris Lattner24943d22010-06-08 16:52:24 +00001828 }
1829 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001830
1831 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner24943d22010-06-08 16:52:24 +00001832 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001833 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
1834 {
1835 bp_site->SetEnabled(true);
1836 bp_site->SetType (BreakpointSite::eExternal);
1837 return error;
1838 }
Chris Lattner24943d22010-06-08 16:52:24 +00001839 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001840
1841 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +00001842 }
1843
1844 if (log)
1845 {
1846 const char *err_string = error.AsCString();
1847 log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s",
1848 bp_site->GetLoadAddress(),
1849 err_string ? err_string : "NULL");
1850 }
1851 // We shouldn't reach here on a successful breakpoint enable...
1852 if (error.Success())
1853 error.SetErrorToGenericError();
1854 return error;
1855}
1856
1857Error
1858ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site)
1859{
1860 Error error;
1861 assert (bp_site != NULL);
1862 addr_t addr = bp_site->GetLoadAddress();
1863 user_id_t site_id = bp_site->GetID();
Greg Claytone005f2c2010-11-06 01:53:30 +00001864 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001865 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001866 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001867
1868 if (bp_site->IsEnabled())
1869 {
1870 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1871
Greg Claytonb72d0f02011-04-12 05:54:46 +00001872 BreakpointSite::Type bp_type = bp_site->GetType();
1873 switch (bp_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001874 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001875 case BreakpointSite::eSoftware:
1876 error = DisableSoftwareBreakpoint (bp_site);
1877 break;
1878
1879 case BreakpointSite::eHardware:
1880 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1881 error.SetErrorToGenericError();
1882 break;
1883
1884 case BreakpointSite::eExternal:
1885 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1886 error.SetErrorToGenericError();
1887 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001888 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001889 if (error.Success())
1890 bp_site->SetEnabled(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001891 }
1892 else
1893 {
1894 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001895 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001896 return error;
1897 }
1898
1899 if (error.Success())
1900 error.SetErrorToGenericError();
1901 return error;
1902}
1903
Johnny Chen21900fb2011-09-06 22:38:36 +00001904// Pre-requisite: wp != NULL.
1905static GDBStoppointType
Johnny Chenecd4feb2011-10-14 00:42:25 +00001906GetGDBStoppointType (Watchpoint *wp)
Johnny Chen21900fb2011-09-06 22:38:36 +00001907{
1908 assert(wp);
1909 bool watch_read = wp->WatchpointRead();
1910 bool watch_write = wp->WatchpointWrite();
1911
1912 // watch_read and watch_write cannot both be false.
1913 assert(watch_read || watch_write);
1914 if (watch_read && watch_write)
1915 return eWatchpointReadWrite;
Johnny Chen48a5e852011-09-09 20:35:15 +00001916 else if (watch_read)
Johnny Chen21900fb2011-09-06 22:38:36 +00001917 return eWatchpointRead;
Johnny Chen48a5e852011-09-09 20:35:15 +00001918 else // Must be watch_write, then.
Johnny Chen21900fb2011-09-06 22:38:36 +00001919 return eWatchpointWrite;
1920}
1921
Chris Lattner24943d22010-06-08 16:52:24 +00001922Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001923ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001924{
1925 Error error;
1926 if (wp)
1927 {
1928 user_id_t watchID = wp->GetID();
1929 addr_t addr = wp->GetLoadAddress();
Greg Claytone005f2c2010-11-06 01:53:30 +00001930 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001931 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001932 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %llu)", watchID);
Chris Lattner24943d22010-06-08 16:52:24 +00001933 if (wp->IsEnabled())
1934 {
1935 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001936 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %llu) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001937 return error;
1938 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001939
1940 GDBStoppointType type = GetGDBStoppointType(wp);
1941 // Pass down an appropriate z/Z packet...
1942 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner24943d22010-06-08 16:52:24 +00001943 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001944 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
1945 {
1946 wp->SetEnabled(true);
1947 return error;
1948 }
1949 else
1950 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001951 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001952 else
1953 error.SetErrorString("watchpoints not supported");
Chris Lattner24943d22010-06-08 16:52:24 +00001954 }
1955 else
1956 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001957 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00001958 }
1959 if (error.Success())
1960 error.SetErrorToGenericError();
1961 return error;
1962}
1963
1964Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001965ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001966{
1967 Error error;
1968 if (wp)
1969 {
1970 user_id_t watchID = wp->GetID();
1971
Greg Claytone005f2c2010-11-06 01:53:30 +00001972 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001973
1974 addr_t addr = wp->GetLoadAddress();
1975 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001976 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001977
Johnny Chen21900fb2011-09-06 22:38:36 +00001978 if (!wp->IsEnabled())
1979 {
1980 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001981 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", watchID, (uint64_t)addr);
Johnny Chen21900fb2011-09-06 22:38:36 +00001982 return error;
1983 }
1984
Chris Lattner24943d22010-06-08 16:52:24 +00001985 if (wp->IsHardware())
1986 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001987 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner24943d22010-06-08 16:52:24 +00001988 // Pass down an appropriate z/Z packet...
Johnny Chen21900fb2011-09-06 22:38:36 +00001989 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
1990 {
1991 wp->SetEnabled(false);
1992 return error;
1993 }
1994 else
1995 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001996 }
1997 // TODO: clear software watchpoints if we implement them
1998 }
1999 else
2000 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00002001 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00002002 }
2003 if (error.Success())
2004 error.SetErrorToGenericError();
2005 return error;
2006}
2007
2008void
2009ProcessGDBRemote::Clear()
2010{
2011 m_flags = 0;
2012 m_thread_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002013}
2014
2015Error
2016ProcessGDBRemote::DoSignal (int signo)
2017{
2018 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00002019 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002020 if (log)
2021 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2022
2023 if (!m_gdb_comm.SendAsyncSignal (signo))
2024 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2025 return error;
2026}
2027
Chris Lattner24943d22010-06-08 16:52:24 +00002028Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002029ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2030{
2031 ProcessLaunchInfo launch_info;
2032 return StartDebugserverProcess(debugserver_url, launch_info);
2033}
2034
2035Error
2036ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url, const ProcessInfo &process_info) // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
Chris Lattner24943d22010-06-08 16:52:24 +00002037{
2038 Error error;
2039 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2040 {
2041 // If we locate debugserver, keep that located version around
2042 static FileSpec g_debugserver_file_spec;
2043
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002044 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner24943d22010-06-08 16:52:24 +00002045 char debugserver_path[PATH_MAX];
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002046 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner24943d22010-06-08 16:52:24 +00002047
2048 // Always check to see if we have an environment override for the path
2049 // to the debugserver to use and use it if we do.
2050 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2051 if (env_debugserver_path)
Greg Clayton537a7a82010-10-20 20:54:39 +00002052 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +00002053 else
2054 debugserver_file_spec = g_debugserver_file_spec;
2055 bool debugserver_exists = debugserver_file_spec.Exists();
2056 if (!debugserver_exists)
2057 {
2058 // The debugserver binary is in the LLDB.framework/Resources
2059 // directory.
Greg Clayton24b48ff2010-10-17 22:03:32 +00002060 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner24943d22010-06-08 16:52:24 +00002061 {
Greg Clayton24b48ff2010-10-17 22:03:32 +00002062 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002063 debugserver_exists = debugserver_file_spec.Exists();
Greg Clayton24b48ff2010-10-17 22:03:32 +00002064 if (debugserver_exists)
2065 {
2066 g_debugserver_file_spec = debugserver_file_spec;
2067 }
2068 else
2069 {
2070 g_debugserver_file_spec.Clear();
2071 debugserver_file_spec.Clear();
2072 }
Chris Lattner24943d22010-06-08 16:52:24 +00002073 }
2074 }
2075
2076 if (debugserver_exists)
2077 {
2078 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2079
2080 m_stdio_communication.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002081
Greg Claytone005f2c2010-11-06 01:53:30 +00002082 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002083
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002084 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner24943d22010-06-08 16:52:24 +00002085 char arg_cstr[PATH_MAX];
Chris Lattner24943d22010-06-08 16:52:24 +00002086
Chris Lattner24943d22010-06-08 16:52:24 +00002087 // Start args with "debugserver /file/path -r --"
2088 debugserver_args.AppendArgument(debugserver_path);
2089 debugserver_args.AppendArgument(debugserver_url);
Greg Clayton24b48ff2010-10-17 22:03:32 +00002090 // use native registers, not the GDB registers
2091 debugserver_args.AppendArgument("--native-regs");
2092 // make debugserver run in its own session so signals generated by
2093 // special terminal key sequences (^C) don't affect debugserver
2094 debugserver_args.AppendArgument("--setsid");
Chris Lattner24943d22010-06-08 16:52:24 +00002095
Chris Lattner24943d22010-06-08 16:52:24 +00002096 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2097 if (env_debugserver_log_file)
2098 {
2099 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2100 debugserver_args.AppendArgument(arg_cstr);
2101 }
2102
2103 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2104 if (env_debugserver_log_flags)
2105 {
2106 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2107 debugserver_args.AppendArgument(arg_cstr);
2108 }
Greg Claytoncc3e6402011-01-25 06:55:13 +00002109// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002110// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner24943d22010-06-08 16:52:24 +00002111
Greg Claytonb72d0f02011-04-12 05:54:46 +00002112 // We currently send down all arguments, attach pids, or attach
2113 // process names in dedicated GDB server packets, so we don't need
2114 // to pass them as arguments. This is currently because of all the
2115 // things we need to setup prior to launching: the environment,
2116 // current working dir, file actions, etc.
2117#if 0
Chris Lattner24943d22010-06-08 16:52:24 +00002118 // Now append the program arguments
Greg Claytona2f74232011-02-24 22:24:29 +00002119 if (inferior_argv)
Chris Lattner24943d22010-06-08 16:52:24 +00002120 {
Greg Claytona2f74232011-02-24 22:24:29 +00002121 // Terminate the debugserver args so we can now append the inferior args
2122 debugserver_args.AppendArgument("--");
Chris Lattner24943d22010-06-08 16:52:24 +00002123
Greg Claytona2f74232011-02-24 22:24:29 +00002124 for (int i = 0; inferior_argv[i] != NULL; ++i)
2125 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner24943d22010-06-08 16:52:24 +00002126 }
2127 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2128 {
2129 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2130 debugserver_args.AppendArgument (arg_cstr);
2131 }
2132 else if (attach_name && attach_name[0])
2133 {
2134 if (wait_for_launch)
2135 debugserver_args.AppendArgument ("--waitfor");
2136 else
2137 debugserver_args.AppendArgument ("--attach");
2138 debugserver_args.AppendArgument (attach_name);
2139 }
Chris Lattner24943d22010-06-08 16:52:24 +00002140#endif
Greg Claytonb72d0f02011-04-12 05:54:46 +00002141
2142 ProcessLaunchInfo::FileAction file_action;
2143
2144 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2145 // to "/dev/null" if we run into any problems.
2146 file_action.Close (STDIN_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002147 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002148 file_action.Close (STDOUT_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002149 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002150 file_action.Close (STDERR_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002151 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner24943d22010-06-08 16:52:24 +00002152
2153 if (log)
2154 {
2155 StreamString strm;
2156 debugserver_args.Dump (&strm);
2157 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2158 }
2159
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002160 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2161 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Clayton1c4642c2011-11-16 05:37:56 +00002162
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002163 error = Host::LaunchProcess(debugserver_launch_info);
Greg Claytone9d0df42010-07-02 01:29:13 +00002164
Greg Claytonb72d0f02011-04-12 05:54:46 +00002165 if (error.Success ())
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002166 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Claytonb72d0f02011-04-12 05:54:46 +00002167 else
Chris Lattner24943d22010-06-08 16:52:24 +00002168 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2169
2170 if (error.Fail() || log)
Greg Claytond9919d32011-12-01 23:28:38 +00002171 error.PutToLog(log.get(), "Host::LaunchProcess (launch_info) => pid=%llu, path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner24943d22010-06-08 16:52:24 +00002172 }
2173 else
2174 {
Greg Clayton9c236732011-10-26 00:56:27 +00002175 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002176 }
2177
2178 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2179 StartAsyncThread ();
2180 }
2181 return error;
2182}
2183
2184bool
2185ProcessGDBRemote::MonitorDebugserverProcess
2186(
2187 void *callback_baton,
2188 lldb::pid_t debugserver_pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +00002189 bool exited, // True if the process did exit
Chris Lattner24943d22010-06-08 16:52:24 +00002190 int signo, // Zero for no signal
2191 int exit_status // Exit value of process if signal is zero
2192)
2193{
Greg Clayton1c4642c2011-11-16 05:37:56 +00002194 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2195 // and might not exist anymore, so we need to carefully try to get the
2196 // target for this process first since we have a race condition when
2197 // we are done running between getting the notice that the inferior
2198 // process has died and the debugserver that was debugging this process.
2199 // In our test suite, we are also continually running process after
2200 // process, so we must be very careful to make sure:
2201 // 1 - process object hasn't been deleted already
2202 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner24943d22010-06-08 16:52:24 +00002203
2204 // "debugserver_pid" argument passed in is the process ID for
2205 // debugserver that we are tracking...
Greg Clayton1c4642c2011-11-16 05:37:56 +00002206 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002207
Greg Clayton75ccf502010-08-21 02:22:51 +00002208 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton72e1c782011-01-22 23:43:18 +00002209
Greg Clayton1c4642c2011-11-16 05:37:56 +00002210 // Get a shared pointer to the target that has a matching process pointer.
2211 // This target could be gone, or the target could already have a new process
2212 // object inside of it
2213 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2214
Greg Clayton72e1c782011-01-22 23:43:18 +00002215 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00002216 log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%llu, signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status);
Greg Clayton72e1c782011-01-22 23:43:18 +00002217
Greg Clayton1c4642c2011-11-16 05:37:56 +00002218 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002219 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002220 // We found a process in a target that matches, but another thread
2221 // might be in the process of launching a new process that will
2222 // soon replace it, so get a shared pointer to the process so we
2223 // can keep it alive.
2224 ProcessSP process_sp (target_sp->GetProcessSP());
2225 // Now we have a shared pointer to the process that can't go away on us
2226 // so we now make sure it was the same as the one passed in, and also make
2227 // sure that our previous "process *" didn't get deleted and have a new
2228 // "process *" created in its place with the same pointer. To verify this
2229 // we make sure the process has our debugserver process ID. If we pass all
2230 // of these tests, then we are sure that this process is the one we were
2231 // looking for.
2232 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner24943d22010-06-08 16:52:24 +00002233 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002234 // Sleep for a half a second to make sure our inferior process has
2235 // time to set its exit status before we set it incorrectly when
2236 // both the debugserver and the inferior process shut down.
2237 usleep (500000);
2238 // If our process hasn't yet exited, debugserver might have died.
2239 // If the process did exit, the we are reaping it.
2240 const StateType state = process->GetState();
2241
2242 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2243 state != eStateInvalid &&
2244 state != eStateUnloaded &&
2245 state != eStateExited &&
2246 state != eStateDetached)
Chris Lattner24943d22010-06-08 16:52:24 +00002247 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002248 char error_str[1024];
2249 if (signo)
2250 {
2251 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2252 if (signal_cstr)
2253 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2254 else
2255 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2256 }
Chris Lattner24943d22010-06-08 16:52:24 +00002257 else
Greg Clayton1c4642c2011-11-16 05:37:56 +00002258 {
2259 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2260 }
Greg Clayton75ccf502010-08-21 02:22:51 +00002261
Greg Clayton1c4642c2011-11-16 05:37:56 +00002262 process->SetExitStatus (-1, error_str);
2263 }
2264 // Debugserver has exited we need to let our ProcessGDBRemote
2265 // know that it no longer has a debugserver instance
2266 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton75ccf502010-08-21 02:22:51 +00002267 }
Chris Lattner24943d22010-06-08 16:52:24 +00002268 }
2269 return true;
2270}
2271
2272void
2273ProcessGDBRemote::KillDebugserverProcess ()
2274{
2275 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2276 {
2277 ::kill (m_debugserver_pid, SIGINT);
2278 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2279 }
2280}
2281
2282void
2283ProcessGDBRemote::Initialize()
2284{
2285 static bool g_initialized = false;
2286
2287 if (g_initialized == false)
2288 {
2289 g_initialized = true;
2290 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2291 GetPluginDescriptionStatic(),
2292 CreateInstance);
2293
2294 Log::Callbacks log_callbacks = {
2295 ProcessGDBRemoteLog::DisableLog,
2296 ProcessGDBRemoteLog::EnableLog,
2297 ProcessGDBRemoteLog::ListLogCategories
2298 };
2299
2300 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2301 }
2302}
2303
2304bool
Chris Lattner24943d22010-06-08 16:52:24 +00002305ProcessGDBRemote::StartAsyncThread ()
2306{
Greg Claytone005f2c2010-11-06 01:53:30 +00002307 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002308
2309 if (log)
2310 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2311
2312 // Create a thread that watches our internal state and controls which
2313 // events make it to clients (into the DCProcess event queue).
2314 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002315 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002316}
2317
2318void
2319ProcessGDBRemote::StopAsyncThread ()
2320{
Greg Claytone005f2c2010-11-06 01:53:30 +00002321 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002322
2323 if (log)
2324 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2325
2326 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
Jim Ingham8226e942011-10-28 01:11:35 +00002327
2328 // This will shut down the async thread.
2329 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Chris Lattner24943d22010-06-08 16:52:24 +00002330
2331 // Stop the stdio thread
Greg Clayton09c81ef2011-02-08 01:34:25 +00002332 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002333 {
2334 Host::ThreadJoin (m_async_thread, NULL, NULL);
2335 }
2336}
2337
2338
2339void *
2340ProcessGDBRemote::AsyncThread (void *arg)
2341{
2342 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2343
Greg Claytone005f2c2010-11-06 01:53:30 +00002344 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002345 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002346 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002347
2348 Listener listener ("ProcessGDBRemote::AsyncThread");
2349 EventSP event_sp;
2350 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2351 eBroadcastBitAsyncThreadShouldExit;
2352
2353 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2354 {
Greg Claytona2f74232011-02-24 22:24:29 +00002355 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2356
Chris Lattner24943d22010-06-08 16:52:24 +00002357 bool done = false;
2358 while (!done)
2359 {
2360 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002361 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002362 if (listener.WaitForEvent (NULL, event_sp))
2363 {
2364 const uint32_t event_type = event_sp->GetType();
Greg Claytona2f74232011-02-24 22:24:29 +00002365 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner24943d22010-06-08 16:52:24 +00002366 {
Greg Claytona2f74232011-02-24 22:24:29 +00002367 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002368 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002369
Greg Claytona2f74232011-02-24 22:24:29 +00002370 switch (event_type)
2371 {
2372 case eBroadcastBitAsyncContinue:
Chris Lattner24943d22010-06-08 16:52:24 +00002373 {
Greg Claytona2f74232011-02-24 22:24:29 +00002374 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002375
Greg Claytona2f74232011-02-24 22:24:29 +00002376 if (continue_packet)
Chris Lattner24943d22010-06-08 16:52:24 +00002377 {
Greg Claytona2f74232011-02-24 22:24:29 +00002378 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2379 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2380 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002381 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002382
Greg Claytona2f74232011-02-24 22:24:29 +00002383 if (::strstr (continue_cstr, "vAttach") == NULL)
2384 process->SetPrivateState(eStateRunning);
2385 StringExtractorGDBRemote response;
2386 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner24943d22010-06-08 16:52:24 +00002387
Greg Claytona2f74232011-02-24 22:24:29 +00002388 switch (stop_state)
2389 {
2390 case eStateStopped:
2391 case eStateCrashed:
2392 case eStateSuspended:
Greg Clayton06709002011-12-06 04:51:14 +00002393 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002394 process->SetPrivateState (stop_state);
2395 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002396
Greg Claytona2f74232011-02-24 22:24:29 +00002397 case eStateExited:
Greg Clayton06709002011-12-06 04:51:14 +00002398 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002399 response.SetFilePos(1);
2400 process->SetExitStatus(response.GetHexU8(), NULL);
2401 done = true;
2402 break;
2403
2404 case eStateInvalid:
2405 process->SetExitStatus(-1, "lost connection");
2406 break;
2407
2408 default:
2409 process->SetPrivateState (stop_state);
2410 break;
2411 }
Chris Lattner24943d22010-06-08 16:52:24 +00002412 }
2413 }
Greg Claytona2f74232011-02-24 22:24:29 +00002414 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002415
Greg Claytona2f74232011-02-24 22:24:29 +00002416 case eBroadcastBitAsyncThreadShouldExit:
2417 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002418 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Claytona2f74232011-02-24 22:24:29 +00002419 done = true;
2420 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002421
Greg Claytona2f74232011-02-24 22:24:29 +00002422 default:
2423 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002424 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type);
Greg Claytona2f74232011-02-24 22:24:29 +00002425 done = true;
2426 break;
2427 }
2428 }
2429 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2430 {
2431 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2432 {
2433 process->SetExitStatus (-1, "lost connection");
Chris Lattner24943d22010-06-08 16:52:24 +00002434 done = true;
Greg Claytona2f74232011-02-24 22:24:29 +00002435 }
Chris Lattner24943d22010-06-08 16:52:24 +00002436 }
2437 }
2438 else
2439 {
2440 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002441 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002442 done = true;
2443 }
2444 }
2445 }
2446
2447 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002448 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002449
2450 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2451 return NULL;
2452}
2453
Chris Lattner24943d22010-06-08 16:52:24 +00002454const char *
2455ProcessGDBRemote::GetDispatchQueueNameForThread
2456(
2457 addr_t thread_dispatch_qaddr,
2458 std::string &dispatch_queue_name
2459)
2460{
2461 dispatch_queue_name.clear();
2462 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2463 {
2464 // Cache the dispatch_queue_offsets_addr value so we don't always have
2465 // to look it up
2466 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2467 {
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002468 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
2469 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Clayton24bc5d92011-03-30 18:16:51 +00002470 ModuleSP module_sp(GetTarget().GetImages().FindFirstModuleForFileSpec (FileSpec("libSystem.B.dylib", false), NULL, NULL));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002471 if (module_sp)
2472 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2473
2474 if (dispatch_queue_offsets_symbol == NULL)
2475 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00002476 module_sp = GetTarget().GetImages().FindFirstModuleForFileSpec (FileSpec("libdispatch.dylib", false), NULL, NULL);
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002477 if (module_sp)
2478 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2479 }
Chris Lattner24943d22010-06-08 16:52:24 +00002480 if (dispatch_queue_offsets_symbol)
Greg Claytoneea26402010-09-14 23:36:40 +00002481 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetValue().GetLoadAddress(&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00002482
2483 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2484 return NULL;
2485 }
2486
2487 uint8_t memory_buffer[8];
Greg Clayton395fc332011-02-15 21:59:32 +00002488 DataExtractor data (memory_buffer,
2489 sizeof(memory_buffer),
2490 m_target.GetArchitecture().GetByteOrder(),
2491 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00002492
2493 // Excerpt from src/queue_private.h
2494 struct dispatch_queue_offsets_s
2495 {
2496 uint16_t dqo_version;
2497 uint16_t dqo_label;
2498 uint16_t dqo_label_size;
2499 } dispatch_queue_offsets;
2500
2501
2502 Error error;
2503 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
2504 {
2505 uint32_t data_offset = 0;
2506 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
2507 {
2508 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
2509 {
2510 data_offset = 0;
2511 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
2512 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
2513 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
2514 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
2515 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
2516 dispatch_queue_name.erase (bytes_read);
2517 }
2518 }
2519 }
2520 }
2521 if (dispatch_queue_name.empty())
2522 return NULL;
2523 return dispatch_queue_name.c_str();
2524}
2525
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002526//uint32_t
2527//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2528//{
2529// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
2530// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
2531// if (m_local_debugserver)
2532// {
2533// return Host::ListProcessesMatchingName (name, matches, pids);
2534// }
2535// else
2536// {
2537// // FIXME: Implement talking to the remote debugserver.
2538// return 0;
2539// }
2540//
2541//}
2542//
Jim Ingham55e01d82011-01-22 01:33:44 +00002543bool
2544ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
2545 lldb_private::StoppointCallbackContext *context,
2546 lldb::user_id_t break_id,
2547 lldb::user_id_t break_loc_id)
2548{
2549 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
2550 // run so I can stop it if that's what I want to do.
2551 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2552 if (log)
2553 log->Printf("Hit New Thread Notification breakpoint.");
2554 return false;
2555}
2556
2557
2558bool
2559ProcessGDBRemote::StartNoticingNewThreads()
2560{
2561 static const char *bp_names[] =
2562 {
2563 "start_wqthread",
Jim Inghamff276fe2011-02-08 05:19:01 +00002564 "_pthread_wqthread",
Jim Ingham55e01d82011-01-22 01:33:44 +00002565 "_pthread_start",
2566 NULL
2567 };
2568
2569 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2570 size_t num_bps = m_thread_observation_bps.size();
2571 if (num_bps != 0)
2572 {
2573 for (int i = 0; i < num_bps; i++)
2574 {
2575 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2576 if (break_sp)
2577 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002578 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002579 log->Printf("Enabled noticing new thread breakpoint.");
2580 break_sp->SetEnabled(true);
2581 }
2582 }
2583 }
2584 else
2585 {
2586 for (int i = 0; bp_names[i] != NULL; i++)
2587 {
Jim Inghamd6d47972011-09-23 00:54:11 +00002588 Breakpoint *breakpoint = m_target.CreateBreakpoint (NULL, NULL, bp_names[i], eFunctionNameTypeFull, true).get();
Jim Ingham55e01d82011-01-22 01:33:44 +00002589 if (breakpoint)
2590 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002591 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002592 log->Printf("Successfully created new thread notification breakpoint at \"%s\".", bp_names[i]);
2593 m_thread_observation_bps.push_back(breakpoint->GetID());
2594 breakpoint->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
2595 }
2596 else
2597 {
2598 if (log)
2599 log->Printf("Failed to create new thread notification breakpoint.");
2600 return false;
2601 }
2602 }
2603 }
2604
2605 return true;
2606}
2607
2608bool
2609ProcessGDBRemote::StopNoticingNewThreads()
2610{
Jim Inghamff276fe2011-02-08 05:19:01 +00002611 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham6bb73372011-10-15 00:21:37 +00002612 if (log && log->GetVerbose())
Jim Inghamff276fe2011-02-08 05:19:01 +00002613 log->Printf ("Disabling new thread notification breakpoint.");
Jim Ingham55e01d82011-01-22 01:33:44 +00002614 size_t num_bps = m_thread_observation_bps.size();
2615 if (num_bps != 0)
2616 {
2617 for (int i = 0; i < num_bps; i++)
2618 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002619
2620 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2621 if (break_sp)
2622 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002623 break_sp->SetEnabled(false);
2624 }
2625 }
2626 }
2627 return true;
2628}
2629
2630