blob: 7d768ecbc295b3fedebbb5dd3639f18fc2d613b5 [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
Greg Claytoncd330422012-02-29 19:27:27 +0000239 },
240 NULL,
241 NULL
Chris Lattner24943d22010-06-08 16:52:24 +0000242 };
243
244 while (response.GetNameColonValue(name, value))
245 {
246 if (name.compare("name") == 0)
247 {
248 reg_name.SetCString(value.c_str());
249 }
250 else if (name.compare("alt-name") == 0)
251 {
252 alt_name.SetCString(value.c_str());
253 }
254 else if (name.compare("bitsize") == 0)
255 {
256 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
257 }
258 else if (name.compare("offset") == 0)
259 {
260 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda53d96862010-06-11 23:44:18 +0000261 if (reg_offset != offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
263 reg_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000264 }
265 }
266 else if (name.compare("encoding") == 0)
267 {
268 if (value.compare("uint") == 0)
269 reg_info.encoding = eEncodingUint;
270 else if (value.compare("sint") == 0)
271 reg_info.encoding = eEncodingSint;
272 else if (value.compare("ieee754") == 0)
273 reg_info.encoding = eEncodingIEEE754;
274 else if (value.compare("vector") == 0)
275 reg_info.encoding = eEncodingVector;
276 }
277 else if (name.compare("format") == 0)
278 {
279 if (value.compare("binary") == 0)
280 reg_info.format = eFormatBinary;
281 else if (value.compare("decimal") == 0)
282 reg_info.format = eFormatDecimal;
283 else if (value.compare("hex") == 0)
284 reg_info.format = eFormatHex;
285 else if (value.compare("float") == 0)
286 reg_info.format = eFormatFloat;
287 else if (value.compare("vector-sint8") == 0)
288 reg_info.format = eFormatVectorOfSInt8;
289 else if (value.compare("vector-uint8") == 0)
290 reg_info.format = eFormatVectorOfUInt8;
291 else if (value.compare("vector-sint16") == 0)
292 reg_info.format = eFormatVectorOfSInt16;
293 else if (value.compare("vector-uint16") == 0)
294 reg_info.format = eFormatVectorOfUInt16;
295 else if (value.compare("vector-sint32") == 0)
296 reg_info.format = eFormatVectorOfSInt32;
297 else if (value.compare("vector-uint32") == 0)
298 reg_info.format = eFormatVectorOfUInt32;
299 else if (value.compare("vector-float32") == 0)
300 reg_info.format = eFormatVectorOfFloat32;
301 else if (value.compare("vector-uint128") == 0)
302 reg_info.format = eFormatVectorOfUInt128;
303 }
304 else if (name.compare("set") == 0)
305 {
306 set_name.SetCString(value.c_str());
307 }
308 else if (name.compare("gcc") == 0)
309 {
310 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
311 }
312 else if (name.compare("dwarf") == 0)
313 {
314 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
315 }
316 else if (name.compare("generic") == 0)
317 {
318 if (value.compare("pc") == 0)
319 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
320 else if (value.compare("sp") == 0)
321 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
322 else if (value.compare("fp") == 0)
323 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
324 else if (value.compare("ra") == 0)
325 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
326 else if (value.compare("flags") == 0)
327 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
Greg Clayton5a269102011-05-22 04:32:55 +0000328 else if (value.find("arg") == 0)
329 {
330 if (value.size() == 4)
331 {
332 switch (value[3])
333 {
334 case '1': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG1; break;
335 case '2': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG2; break;
336 case '3': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG3; break;
337 case '4': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG4; break;
338 case '5': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG5; break;
339 case '6': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG6; break;
340 case '7': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG7; break;
341 case '8': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG8; break;
342 }
343 }
344 }
Chris Lattner24943d22010-06-08 16:52:24 +0000345 }
346 }
347
Jason Molenda53d96862010-06-11 23:44:18 +0000348 reg_info.byte_offset = reg_offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000349 assert (reg_info.byte_size != 0);
350 reg_offset += reg_info.byte_size;
351 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
352 }
353 }
354 else
355 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000356 response_type = StringExtractorGDBRemote::eError;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000357 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000358 }
359 }
360
361 if (reg_num == 0)
362 {
363 // We didn't get anything. See if we are debugging ARM and fill with
364 // a hard coded register set until we can get an updated debugserver
365 // down on the devices.
Jason Molenda428b5502011-10-06 01:45:46 +0000366
367 if (!GetTarget().GetArchitecture().IsValid()
368 && m_gdb_comm.GetHostArchitecture().IsValid()
369 && m_gdb_comm.GetHostArchitecture().GetMachine() == llvm::Triple::arm
370 && m_gdb_comm.GetHostArchitecture().GetTriple().getVendor() == llvm::Triple::Apple)
371 {
Chris Lattner24943d22010-06-08 16:52:24 +0000372 m_register_info.HardcodeARMRegisters();
Jason Molenda428b5502011-10-06 01:45:46 +0000373 }
374 else if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::arm)
375 {
376 m_register_info.HardcodeARMRegisters();
377 }
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379 m_register_info.Finalize ();
380}
381
382Error
383ProcessGDBRemote::WillLaunch (Module* module)
384{
385 return WillLaunchOrAttach ();
386}
387
388Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000389ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000390{
391 return WillLaunchOrAttach ();
392}
393
394Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000395ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +0000396{
397 return WillLaunchOrAttach ();
398}
399
400Error
Greg Claytone71e2582011-02-04 01:58:07 +0000401ProcessGDBRemote::DoConnectRemote (const char *remote_url)
402{
403 Error error (WillLaunchOrAttach ());
404
405 if (error.Fail())
406 return error;
407
Greg Clayton180546b2011-04-30 01:09:13 +0000408 error = ConnectToDebugserver (remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +0000409
410 if (error.Fail())
411 return error;
412 StartAsyncThread ();
413
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000414 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone71e2582011-02-04 01:58:07 +0000415 if (pid == LLDB_INVALID_PROCESS_ID)
416 {
417 // We don't have a valid process ID, so note that we are connected
418 // and could now request to launch or attach, or get remote process
419 // listings...
420 SetPrivateState (eStateConnected);
421 }
422 else
423 {
424 // We have a valid process
425 SetID (pid);
Greg Clayton37f962e2011-08-22 02:49:39 +0000426 GetThreadList();
Greg Clayton261a18b2011-06-02 22:22:38 +0000427 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytone71e2582011-02-04 01:58:07 +0000428 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000429 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytone71e2582011-02-04 01:58:07 +0000430 if (state == eStateStopped)
431 {
432 SetPrivateState (state);
433 }
434 else
Greg Claytond9919d32011-12-01 23:28:38 +0000435 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 +0000436 }
437 else
Greg Claytond9919d32011-12-01 23:28:38 +0000438 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 +0000439 }
440 return error;
441}
442
443Error
Chris Lattner24943d22010-06-08 16:52:24 +0000444ProcessGDBRemote::WillLaunchOrAttach ()
445{
446 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000447 m_stdio_communication.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000448 return error;
449}
450
451//----------------------------------------------------------------------
452// Process Control
453//----------------------------------------------------------------------
454Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000455ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000456{
Greg Clayton4b407112010-09-30 21:49:03 +0000457 Error error;
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000458
459 uint32_t launch_flags = launch_info.GetFlags().Get();
460 const char *stdin_path = NULL;
461 const char *stdout_path = NULL;
462 const char *stderr_path = NULL;
463 const char *working_dir = launch_info.GetWorkingDirectory();
464
465 const ProcessLaunchInfo::FileAction *file_action;
466 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
467 if (file_action)
468 {
469 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
470 stdin_path = file_action->GetPath();
471 }
472 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
473 if (file_action)
474 {
475 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
476 stdout_path = file_action->GetPath();
477 }
478 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
479 if (file_action)
480 {
481 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
482 stderr_path = file_action->GetPath();
483 }
484
Chris Lattner24943d22010-06-08 16:52:24 +0000485 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
486 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
487 // ::LogSetLogFile ("/dev/stdout");
Greg Clayton716cefb2011-08-09 05:20:29 +0000488 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000489
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000490 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000491 if (object_file)
492 {
Chris Lattner24943d22010-06-08 16:52:24 +0000493 char host_port[128];
494 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytone71e2582011-02-04 01:58:07 +0000495 char connect_url[128];
496 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000497
Greg Claytona2f74232011-02-24 22:24:29 +0000498 // Make sure we aren't already connected?
499 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000500 {
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000501 error = StartDebugserverProcess (host_port, launch_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000502 if (error.Fail())
Greg Clayton716cefb2011-08-09 05:20:29 +0000503 {
Johnny Chenc143d622011-08-09 18:56:45 +0000504 if (log)
505 log->Printf("failed to start debugserver process: %s", error.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000506 return error;
Greg Clayton716cefb2011-08-09 05:20:29 +0000507 }
Chris Lattner24943d22010-06-08 16:52:24 +0000508
Greg Claytone71e2582011-02-04 01:58:07 +0000509 error = ConnectToDebugserver (connect_url);
Greg Claytona2f74232011-02-24 22:24:29 +0000510 }
511
512 if (error.Success())
513 {
514 lldb_utility::PseudoTerminal pty;
515 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Claytonafb81862011-03-02 21:34:46 +0000516
517 // If the debugserver is local and we aren't disabling STDIO, lets use
518 // a pseudo terminal to instead of relying on the 'O' packets for stdio
519 // since 'O' packets can really slow down debugging if the inferior
520 // does a lot of output.
Greg Claytonb4747822011-06-24 22:32:10 +0000521 PlatformSP platform_sp (m_target.GetPlatform());
522 if (platform_sp && platform_sp->IsHost() && !disable_stdio)
Greg Claytona2f74232011-02-24 22:24:29 +0000523 {
524 const char *slave_name = NULL;
525 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000526 {
Greg Claytona2f74232011-02-24 22:24:29 +0000527 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
528 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000529 }
Greg Claytona2f74232011-02-24 22:24:29 +0000530 if (stdin_path == NULL)
531 stdin_path = slave_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000532
Greg Claytona2f74232011-02-24 22:24:29 +0000533 if (stdout_path == NULL)
534 stdout_path = slave_name;
535
536 if (stderr_path == NULL)
537 stderr_path = slave_name;
538 }
539
Greg Claytonafb81862011-03-02 21:34:46 +0000540 // Set STDIN to /dev/null if we want STDIO disabled or if either
541 // STDOUT or STDERR have been set to something and STDIN hasn't
542 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000543 stdin_path = "/dev/null";
544
Greg Claytonafb81862011-03-02 21:34:46 +0000545 // Set STDOUT to /dev/null if we want STDIO disabled or if either
546 // STDIN or STDERR have been set to something and STDOUT hasn't
547 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000548 stdout_path = "/dev/null";
549
Greg Claytonafb81862011-03-02 21:34:46 +0000550 // Set STDERR to /dev/null if we want STDIO disabled or if either
551 // STDIN or STDOUT have been set to something and STDERR hasn't
552 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000553 stderr_path = "/dev/null";
554
555 if (stdin_path)
556 m_gdb_comm.SetSTDIN (stdin_path);
557 if (stdout_path)
558 m_gdb_comm.SetSTDOUT (stdout_path);
559 if (stderr_path)
560 m_gdb_comm.SetSTDERR (stderr_path);
561
562 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
563
Greg Claytona4582402011-05-08 04:53:50 +0000564 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Claytona2f74232011-02-24 22:24:29 +0000565
566 if (working_dir && working_dir[0])
567 {
568 m_gdb_comm.SetWorkingDir (working_dir);
569 }
570
571 // Send the environment and the program + arguments after we connect
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000572 const Args &environment = launch_info.GetEnvironmentEntries();
573 if (environment.GetArgumentCount())
Greg Claytona2f74232011-02-24 22:24:29 +0000574 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000575 size_t num_environment_entries = environment.GetArgumentCount();
576 for (size_t i=0; i<num_environment_entries; ++i)
Greg Clayton960d6a42010-08-03 00:35:52 +0000577 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000578 const char *env_entry = environment.GetArgumentAtIndex(i);
579 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Claytona2f74232011-02-24 22:24:29 +0000580 break;
Greg Clayton960d6a42010-08-03 00:35:52 +0000581 }
Greg Claytona2f74232011-02-24 22:24:29 +0000582 }
Greg Clayton960d6a42010-08-03 00:35:52 +0000583
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000584 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000585 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector());
Greg Claytona2f74232011-02-24 22:24:29 +0000586 if (arg_packet_err == 0)
587 {
588 std::string error_str;
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000589 if (m_gdb_comm.GetLaunchSuccess (error_str))
Chris Lattner24943d22010-06-08 16:52:24 +0000590 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000591 SetID (m_gdb_comm.GetCurrentProcessID ());
Chris Lattner24943d22010-06-08 16:52:24 +0000592 }
593 else
594 {
Greg Claytona2f74232011-02-24 22:24:29 +0000595 error.SetErrorString (error_str.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000596 }
Greg Claytona2f74232011-02-24 22:24:29 +0000597 }
598 else
599 {
Greg Clayton9c236732011-10-26 00:56:27 +0000600 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Greg Claytona2f74232011-02-24 22:24:29 +0000601 }
Greg Clayton7c4fc6e2011-08-10 22:05:39 +0000602
603 m_gdb_comm.SetPacketTimeout (old_packet_timeout);
Chris Lattner24943d22010-06-08 16:52:24 +0000604
Greg Claytona2f74232011-02-24 22:24:29 +0000605 if (GetID() == LLDB_INVALID_PROCESS_ID)
606 {
Johnny Chenc143d622011-08-09 18:56:45 +0000607 if (log)
608 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000609 KillDebugserverProcess ();
610 return error;
611 }
612
Greg Clayton261a18b2011-06-02 22:22:38 +0000613 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytona2f74232011-02-24 22:24:29 +0000614 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000615 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Claytona2f74232011-02-24 22:24:29 +0000616
617 if (!disable_stdio)
618 {
619 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Clayton464c6162011-11-17 22:14:31 +0000620 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Claytona2f74232011-02-24 22:24:29 +0000621 }
Chris Lattner24943d22010-06-08 16:52:24 +0000622 }
623 }
Greg Clayton716cefb2011-08-09 05:20:29 +0000624 else
625 {
Johnny Chenc143d622011-08-09 18:56:45 +0000626 if (log)
627 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton716cefb2011-08-09 05:20:29 +0000628 }
Chris Lattner24943d22010-06-08 16:52:24 +0000629 }
630 else
631 {
632 // Set our user ID to an invalid process ID.
633 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000634 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
635 exe_module->GetFileSpec().GetFilename().AsCString(),
636 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000637 }
Chris Lattner24943d22010-06-08 16:52:24 +0000638 return error;
Greg Clayton4b407112010-09-30 21:49:03 +0000639
Chris Lattner24943d22010-06-08 16:52:24 +0000640}
641
642
643Error
Greg Claytone71e2582011-02-04 01:58:07 +0000644ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner24943d22010-06-08 16:52:24 +0000645{
646 Error error;
647 // Sleep and wait a bit for debugserver to start to listen...
648 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
649 if (conn_ap.get())
650 {
Chris Lattner24943d22010-06-08 16:52:24 +0000651 const uint32_t max_retry_count = 50;
652 uint32_t retry_count = 0;
653 while (!m_gdb_comm.IsConnected())
654 {
Greg Claytone71e2582011-02-04 01:58:07 +0000655 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner24943d22010-06-08 16:52:24 +0000656 {
657 m_gdb_comm.SetConnection (conn_ap.release());
658 break;
659 }
660 retry_count++;
661
662 if (retry_count >= max_retry_count)
663 break;
664
665 usleep (100000);
666 }
667 }
668
669 if (!m_gdb_comm.IsConnected())
670 {
671 if (error.Success())
672 error.SetErrorString("not connected to remote gdb server");
673 return error;
674 }
675
Greg Clayton24bc5d92011-03-30 18:16:51 +0000676 // We always seem to be able to open a connection to a local port
677 // so we need to make sure we can then send data to it. If we can't
678 // then we aren't actually connected to anything, so try and do the
679 // handshake with the remote GDB server and make sure that goes
680 // alright.
681 if (!m_gdb_comm.HandshakeWithServer (NULL))
Chris Lattner24943d22010-06-08 16:52:24 +0000682 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000683 m_gdb_comm.Disconnect();
684 if (error.Success())
685 error.SetErrorString("not connected to remote gdb server");
686 return error;
Chris Lattner24943d22010-06-08 16:52:24 +0000687 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000688 m_gdb_comm.ResetDiscoverableSettings();
689 m_gdb_comm.QueryNoAckModeSupported ();
690 m_gdb_comm.GetThreadSuffixSupported ();
691 m_gdb_comm.GetHostInfo ();
692 m_gdb_comm.GetVContSupported ('c');
Chris Lattner24943d22010-06-08 16:52:24 +0000693 return error;
694}
695
696void
697ProcessGDBRemote::DidLaunchOrAttach ()
698{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000699 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
700 if (log)
701 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton75c703d2011-02-16 04:46:07 +0000702 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000703 {
704 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
705
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000706 BuildDynamicRegisterInfo (false);
Greg Clayton20d338f2010-11-18 05:57:03 +0000707
Chris Lattner24943d22010-06-08 16:52:24 +0000708 // See if the GDB server supports the qHostInfo information
Greg Claytonfc7920f2011-02-09 03:09:55 +0000709
Greg Claytoncb8977d2011-03-23 00:09:55 +0000710 const ArchSpec &gdb_remote_arch = m_gdb_comm.GetHostArchitecture();
711 if (gdb_remote_arch.IsValid())
Greg Claytonfc7920f2011-02-09 03:09:55 +0000712 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000713 ArchSpec &target_arch = GetTarget().GetArchitecture();
714
715 if (target_arch.IsValid())
716 {
717 // If the remote host is ARM and we have apple as the vendor, then
718 // ARM executables and shared libraries can have mixed ARM architectures.
719 // You can have an armv6 executable, and if the host is armv7, then the
720 // system will load the best possible architecture for all shared libraries
721 // it has, so we really need to take the remote host architecture as our
722 // defacto architecture in this case.
723
724 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
725 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
726 {
727 target_arch = gdb_remote_arch;
728 }
729 else
730 {
731 // Fill in what is missing in the triple
732 const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple();
733 llvm::Triple &target_triple = target_arch.GetTriple();
Greg Clayton2f085c62011-05-15 01:25:55 +0000734 if (target_triple.getVendorName().size() == 0)
735 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000736 target_triple.setVendor (remote_triple.getVendor());
737
Greg Clayton2f085c62011-05-15 01:25:55 +0000738 if (target_triple.getOSName().size() == 0)
739 {
740 target_triple.setOS (remote_triple.getOS());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000741
Greg Clayton2f085c62011-05-15 01:25:55 +0000742 if (target_triple.getEnvironmentName().size() == 0)
743 target_triple.setEnvironment (remote_triple.getEnvironment());
744 }
745 }
Greg Claytoncb8977d2011-03-23 00:09:55 +0000746 }
747 }
748 else
749 {
750 // The target doesn't have a valid architecture yet, set it from
751 // the architecture we got from the remote GDB server
752 target_arch = gdb_remote_arch;
753 }
Greg Claytonfc7920f2011-02-09 03:09:55 +0000754 }
Chris Lattner24943d22010-06-08 16:52:24 +0000755 }
756}
757
758void
759ProcessGDBRemote::DidLaunch ()
760{
761 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000762}
763
764Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000765ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000766{
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000767 ProcessAttachInfo attach_info;
768 return DoAttachToProcessWithID(attach_pid, attach_info);
769}
770
771Error
772ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
773{
Chris Lattner24943d22010-06-08 16:52:24 +0000774 Error error;
775 // Clear out and clean up from any current state
776 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000777 if (attach_pid != LLDB_INVALID_PROCESS_ID)
778 {
Greg Claytona2f74232011-02-24 22:24:29 +0000779 // Make sure we aren't already connected?
780 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000781 {
Greg Claytona2f74232011-02-24 22:24:29 +0000782 char host_port[128];
783 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
784 char connect_url[128];
785 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000786
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000787 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000788
789 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000790 {
Greg Claytona2f74232011-02-24 22:24:29 +0000791 const char *error_string = error.AsCString();
792 if (error_string == NULL)
793 error_string = "unable to launch " DEBUGSERVER_BASENAME;
794
795 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000796 }
Greg Claytona2f74232011-02-24 22:24:29 +0000797 else
798 {
799 error = ConnectToDebugserver (connect_url);
800 }
801 }
802
803 if (error.Success())
804 {
805 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +0000806 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", attach_pid);
Greg Clayton489575c2011-11-19 02:11:30 +0000807 SetID (attach_pid);
Greg Claytona2f74232011-02-24 22:24:29 +0000808 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner24943d22010-06-08 16:52:24 +0000809 }
810 }
Chris Lattner24943d22010-06-08 16:52:24 +0000811 return error;
812}
813
814size_t
815ProcessGDBRemote::AttachInputReaderCallback
816(
817 void *baton,
818 InputReader *reader,
819 lldb::InputReaderAction notification,
820 const char *bytes,
821 size_t bytes_len
822)
823{
824 if (notification == eInputReaderGotToken)
825 {
826 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
827 if (gdb_process->m_waiting_for_attach)
828 gdb_process->m_waiting_for_attach = false;
829 reader->SetIsDone(true);
830 return 1;
831 }
832 return 0;
833}
834
835Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000836ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000837{
838 Error error;
839 // Clear out and clean up from any current state
840 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000841
Chris Lattner24943d22010-06-08 16:52:24 +0000842 if (process_name && process_name[0])
843 {
Greg Claytona2f74232011-02-24 22:24:29 +0000844 // Make sure we aren't already connected?
845 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000846 {
Greg Claytona2f74232011-02-24 22:24:29 +0000847 char host_port[128];
848 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
849 char connect_url[128];
850 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
851
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000852 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000853 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000854 {
Greg Claytona2f74232011-02-24 22:24:29 +0000855 const char *error_string = error.AsCString();
856 if (error_string == NULL)
857 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner24943d22010-06-08 16:52:24 +0000858
Greg Claytona2f74232011-02-24 22:24:29 +0000859 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000860 }
Greg Claytona2f74232011-02-24 22:24:29 +0000861 else
862 {
863 error = ConnectToDebugserver (connect_url);
864 }
865 }
866
867 if (error.Success())
868 {
869 StreamString packet;
870
871 if (wait_for_launch)
872 packet.PutCString("vAttachWait");
873 else
874 packet.PutCString("vAttachName");
875 packet.PutChar(';');
876 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
877
878 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
879
Chris Lattner24943d22010-06-08 16:52:24 +0000880 }
881 }
Chris Lattner24943d22010-06-08 16:52:24 +0000882 return error;
883}
884
Chris Lattner24943d22010-06-08 16:52:24 +0000885
886void
887ProcessGDBRemote::DidAttach ()
888{
Greg Claytone71e2582011-02-04 01:58:07 +0000889 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000890}
891
892Error
893ProcessGDBRemote::WillResume ()
894{
Greg Claytonc1f45872011-02-12 06:28:37 +0000895 m_continue_c_tids.clear();
896 m_continue_C_tids.clear();
897 m_continue_s_tids.clear();
898 m_continue_S_tids.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000899 return Error();
900}
901
902Error
903ProcessGDBRemote::DoResume ()
904{
Jim Ingham3ae449a2010-11-17 02:32:00 +0000905 Error error;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000906 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
907 if (log)
908 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytonb749a262010-12-03 06:02:24 +0000909
910 Listener listener ("gdb-remote.resume-packet-sent");
911 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
912 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000913 StreamString continue_packet;
914 bool continue_packet_error = false;
915 if (m_gdb_comm.HasAnyVContSupport ())
916 {
917 continue_packet.PutCString ("vCont");
918
919 if (!m_continue_c_tids.empty())
920 {
921 if (m_gdb_comm.GetVContSupported ('c'))
922 {
923 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 +0000924 continue_packet.Printf(";c:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000925 }
926 else
927 continue_packet_error = true;
928 }
929
930 if (!continue_packet_error && !m_continue_C_tids.empty())
931 {
932 if (m_gdb_comm.GetVContSupported ('C'))
933 {
934 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 +0000935 continue_packet.Printf(";C%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000936 }
937 else
938 continue_packet_error = true;
939 }
Greg Claytonb749a262010-12-03 06:02:24 +0000940
Greg Claytonc1f45872011-02-12 06:28:37 +0000941 if (!continue_packet_error && !m_continue_s_tids.empty())
942 {
943 if (m_gdb_comm.GetVContSupported ('s'))
944 {
945 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 +0000946 continue_packet.Printf(";s:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000947 }
948 else
949 continue_packet_error = true;
950 }
951
952 if (!continue_packet_error && !m_continue_S_tids.empty())
953 {
954 if (m_gdb_comm.GetVContSupported ('S'))
955 {
956 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 +0000957 continue_packet.Printf(";S%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000958 }
959 else
960 continue_packet_error = true;
961 }
962
963 if (continue_packet_error)
964 continue_packet.GetString().clear();
965 }
966 else
967 continue_packet_error = true;
968
969 if (continue_packet_error)
970 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000971 // Either no vCont support, or we tried to use part of the vCont
972 // packet that wasn't supported by the remote GDB server.
973 // We need to try and make a simple packet that can do our continue
974 const size_t num_threads = GetThreadList().GetSize();
975 const size_t num_continue_c_tids = m_continue_c_tids.size();
976 const size_t num_continue_C_tids = m_continue_C_tids.size();
977 const size_t num_continue_s_tids = m_continue_s_tids.size();
978 const size_t num_continue_S_tids = m_continue_S_tids.size();
979 if (num_continue_c_tids > 0)
980 {
981 if (num_continue_c_tids == num_threads)
982 {
983 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +0000984 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +0000985 continue_packet.PutChar ('c');
986 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +0000987 }
988 else if (num_continue_c_tids == 1 &&
989 num_continue_C_tids == 0 &&
990 num_continue_s_tids == 0 &&
991 num_continue_S_tids == 0 )
992 {
993 // Only one thread is continuing
Greg Claytonb72d0f02011-04-12 05:54:46 +0000994 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +0000995 continue_packet.PutChar ('c');
Greg Claytonde1dd812011-06-24 03:21:43 +0000996 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +0000997 }
998 }
999
Greg Claytonde1dd812011-06-24 03:21:43 +00001000 if (continue_packet_error && num_continue_C_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +00001001 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001002 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1003 num_continue_C_tids > 0 &&
1004 num_continue_s_tids == 0 &&
1005 num_continue_S_tids == 0 )
Greg Claytonc1f45872011-02-12 06:28:37 +00001006 {
1007 const int continue_signo = m_continue_C_tids.front().second;
Greg Claytonde1dd812011-06-24 03:21:43 +00001008 // Only one thread is continuing
Greg Claytonc1f45872011-02-12 06:28:37 +00001009 if (num_continue_C_tids > 1)
1010 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001011 // More that one thread with a signal, yet we don't have
1012 // vCont support and we are being asked to resume each
1013 // thread with a signal, we need to make sure they are
1014 // all the same signal, or we can't issue the continue
1015 // accurately with the current support...
1016 if (num_continue_C_tids > 1)
Greg Claytonc1f45872011-02-12 06:28:37 +00001017 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001018 continue_packet_error = false;
1019 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1020 {
1021 if (m_continue_C_tids[i].second != continue_signo)
1022 continue_packet_error = true;
1023 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001024 }
Greg Claytonde1dd812011-06-24 03:21:43 +00001025 if (!continue_packet_error)
1026 m_gdb_comm.SetCurrentThreadForRun (-1);
1027 }
1028 else
1029 {
1030 // Set the continue thread ID
1031 continue_packet_error = false;
1032 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001033 }
1034 if (!continue_packet_error)
1035 {
1036 // Add threads continuing with the same signo...
Greg Claytonc1f45872011-02-12 06:28:37 +00001037 continue_packet.Printf("C%2.2x", continue_signo);
1038 }
1039 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001040 }
1041
Greg Claytonde1dd812011-06-24 03:21:43 +00001042 if (continue_packet_error && num_continue_s_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +00001043 {
1044 if (num_continue_s_tids == num_threads)
1045 {
1046 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001047 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +00001048 continue_packet.PutChar ('s');
1049 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001050 }
1051 else if (num_continue_c_tids == 0 &&
1052 num_continue_C_tids == 0 &&
1053 num_continue_s_tids == 1 &&
1054 num_continue_S_tids == 0 )
1055 {
1056 // Only one thread is stepping
Greg Claytonb72d0f02011-04-12 05:54:46 +00001057 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +00001058 continue_packet.PutChar ('s');
Greg Claytonde1dd812011-06-24 03:21:43 +00001059 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001060 }
1061 }
1062
1063 if (!continue_packet_error && num_continue_S_tids > 0)
1064 {
1065 if (num_continue_S_tids == num_threads)
1066 {
1067 const int step_signo = m_continue_S_tids.front().second;
1068 // Are all threads trying to step with the same signal?
Greg Claytonde1dd812011-06-24 03:21:43 +00001069 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001070 if (num_continue_S_tids > 1)
1071 {
1072 for (size_t i=1; i<num_threads; ++i)
1073 {
1074 if (m_continue_S_tids[i].second != step_signo)
1075 continue_packet_error = true;
1076 }
1077 }
1078 if (!continue_packet_error)
1079 {
1080 // Add threads stepping with the same signo...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001081 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonc1f45872011-02-12 06:28:37 +00001082 continue_packet.Printf("S%2.2x", step_signo);
1083 }
1084 }
1085 else if (num_continue_c_tids == 0 &&
1086 num_continue_C_tids == 0 &&
1087 num_continue_s_tids == 0 &&
1088 num_continue_S_tids == 1 )
1089 {
1090 // Only one thread is stepping with signal
Greg Claytonb72d0f02011-04-12 05:54:46 +00001091 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001092 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Claytonde1dd812011-06-24 03:21:43 +00001093 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001094 }
1095 }
1096 }
1097
1098 if (continue_packet_error)
1099 {
1100 error.SetErrorString ("can't make continue packet for this resume");
1101 }
1102 else
1103 {
1104 EventSP event_sp;
1105 TimeValue timeout;
1106 timeout = TimeValue::Now();
1107 timeout.OffsetWithSeconds (5);
1108 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1109
1110 if (listener.WaitForEvent (&timeout, event_sp) == false)
1111 error.SetErrorString("Resume timed out.");
1112 }
Greg Claytonb749a262010-12-03 06:02:24 +00001113 }
1114
Jim Ingham3ae449a2010-11-17 02:32:00 +00001115 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001116}
1117
Chris Lattner24943d22010-06-08 16:52:24 +00001118uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +00001119ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner24943d22010-06-08 16:52:24 +00001120{
1121 // locker will keep a mutex locked until it goes out of scope
Greg Claytone005f2c2010-11-06 01:53:30 +00001122 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001123 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +00001124 log->Printf ("ProcessGDBRemote::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton37f962e2011-08-22 02:49:39 +00001125 // Update the thread list's stop id immediately so we don't recurse into this function.
Chris Lattner24943d22010-06-08 16:52:24 +00001126
Greg Clayton37f962e2011-08-22 02:49:39 +00001127 std::vector<lldb::tid_t> thread_ids;
1128 bool sequence_mutex_unavailable = false;
1129 const size_t num_thread_ids = m_gdb_comm.GetCurrentThreadIDs (thread_ids, sequence_mutex_unavailable);
1130 if (num_thread_ids > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001131 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001132 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001133 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001134 tid_t tid = thread_ids[i];
1135 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
1136 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +00001137 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +00001138 new_thread_list.AddThread(thread_sp);
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001139 }
Chris Lattner24943d22010-06-08 16:52:24 +00001140 }
Greg Clayton37f962e2011-08-22 02:49:39 +00001141
1142 if (sequence_mutex_unavailable == false)
1143 SetThreadStopInfo (m_last_stop_packet);
1144 return new_thread_list.GetSize(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001145}
1146
1147
1148StateType
1149ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1150{
Greg Clayton261a18b2011-06-02 22:22:38 +00001151 stop_packet.SetFilePos (0);
Chris Lattner24943d22010-06-08 16:52:24 +00001152 const char stop_type = stop_packet.GetChar();
1153 switch (stop_type)
1154 {
1155 case 'T':
1156 case 'S':
1157 {
Greg Claytonc3c46612011-02-15 00:19:15 +00001158 if (GetStopID() == 0)
1159 {
1160 // Our first stop, make sure we have a process ID, and also make
1161 // sure we know about our registers
1162 if (GetID() == LLDB_INVALID_PROCESS_ID)
1163 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001164 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonc3c46612011-02-15 00:19:15 +00001165 if (pid != LLDB_INVALID_PROCESS_ID)
1166 SetID (pid);
1167 }
1168 BuildDynamicRegisterInfo (true);
1169 }
Chris Lattner24943d22010-06-08 16:52:24 +00001170 // Stop with signal and thread info
1171 const uint8_t signo = stop_packet.GetHexU8();
1172 std::string name;
1173 std::string value;
1174 std::string thread_name;
Greg Clayton65611552011-06-04 01:26:29 +00001175 std::string reason;
1176 std::string description;
Chris Lattner24943d22010-06-08 16:52:24 +00001177 uint32_t exc_type = 0;
Greg Clayton7661a982010-07-23 16:45:51 +00001178 std::vector<addr_t> exc_data;
Chris Lattner24943d22010-06-08 16:52:24 +00001179 uint32_t tid = LLDB_INVALID_THREAD_ID;
1180 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
1181 uint32_t exc_data_count = 0;
Greg Claytona875b642011-01-09 21:07:35 +00001182 ThreadSP thread_sp;
1183
Chris Lattner24943d22010-06-08 16:52:24 +00001184 while (stop_packet.GetNameColonValue(name, value))
1185 {
1186 if (name.compare("metype") == 0)
1187 {
1188 // exception type in big endian hex
1189 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1190 }
1191 else if (name.compare("mecount") == 0)
1192 {
1193 // exception count in big endian hex
1194 exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16);
1195 }
1196 else if (name.compare("medata") == 0)
1197 {
1198 // exception data in big endian hex
1199 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1200 }
1201 else if (name.compare("thread") == 0)
1202 {
1203 // thread in big endian hex
1204 tid = Args::StringToUInt32 (value.c_str(), 0, 16);
Greg Claytonffa43a62011-11-17 04:46:02 +00001205 // m_thread_list does have its own mutex, but we need to
1206 // hold onto the mutex between the call to m_thread_list.FindThreadByID(...)
1207 // and the m_thread_list.AddThread(...) so it doesn't change on us
Greg Claytonc3c46612011-02-15 00:19:15 +00001208 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytona875b642011-01-09 21:07:35 +00001209 thread_sp = m_thread_list.FindThreadByID(tid, false);
Greg Claytonc3c46612011-02-15 00:19:15 +00001210 if (!thread_sp)
1211 {
1212 // Create the thread if we need to
Greg Claytonf4124de2012-02-21 00:09:25 +00001213 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Claytonc3c46612011-02-15 00:19:15 +00001214 m_thread_list.AddThread(thread_sp);
1215 }
Chris Lattner24943d22010-06-08 16:52:24 +00001216 }
Greg Clayton4862fa22011-01-08 03:17:57 +00001217 else if (name.compare("hexname") == 0)
1218 {
1219 StringExtractor name_extractor;
1220 // Swap "value" over into "name_extractor"
1221 name_extractor.GetStringRef().swap(value);
1222 // Now convert the HEX bytes into a string value
1223 name_extractor.GetHexByteString (value);
1224 thread_name.swap (value);
1225 }
Chris Lattner24943d22010-06-08 16:52:24 +00001226 else if (name.compare("name") == 0)
1227 {
1228 thread_name.swap (value);
1229 }
Greg Clayton0a7f75f2010-09-09 06:32:46 +00001230 else if (name.compare("qaddr") == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001231 {
1232 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1233 }
Greg Clayton65611552011-06-04 01:26:29 +00001234 else if (name.compare("reason") == 0)
1235 {
1236 reason.swap(value);
1237 }
1238 else if (name.compare("description") == 0)
1239 {
1240 StringExtractor desc_extractor;
1241 // Swap "value" over into "name_extractor"
1242 desc_extractor.GetStringRef().swap(value);
1243 // Now convert the HEX bytes into a string value
1244 desc_extractor.GetHexByteString (thread_name);
1245 }
Greg Claytona875b642011-01-09 21:07:35 +00001246 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1247 {
1248 // We have a register number that contains an expedited
1249 // register value. Lets supply this register to our thread
1250 // so it won't have to go and read it.
1251 if (thread_sp)
1252 {
1253 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1254
1255 if (reg != UINT32_MAX)
1256 {
1257 StringExtractor reg_value_extractor;
1258 // Swap "value" over into "reg_value_extractor"
1259 reg_value_extractor.GetStringRef().swap(value);
Greg Claytonc3c46612011-02-15 00:19:15 +00001260 if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor))
1261 {
1262 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1263 name.c_str(),
1264 reg,
1265 reg,
1266 reg_value_extractor.GetStringRef().c_str(),
1267 stop_packet.GetStringRef().c_str());
1268 }
Greg Claytona875b642011-01-09 21:07:35 +00001269 }
1270 }
1271 }
Chris Lattner24943d22010-06-08 16:52:24 +00001272 }
Chris Lattner24943d22010-06-08 16:52:24 +00001273
1274 if (thread_sp)
1275 {
1276 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1277
1278 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Ingham9082c8a2011-01-28 02:23:12 +00001279 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001280 if (exc_type != 0)
1281 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001282 const size_t exc_data_size = exc_data.size();
Greg Clayton643ee732010-08-04 01:40:35 +00001283
1284 gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1285 exc_type,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001286 exc_data_size,
1287 exc_data_size >= 1 ? exc_data[0] : 0,
Johnny Chen36889ad2011-09-17 01:05:03 +00001288 exc_data_size >= 2 ? exc_data[1] : 0,
1289 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner24943d22010-06-08 16:52:24 +00001290 }
Greg Clayton65611552011-06-04 01:26:29 +00001291 else
Chris Lattner24943d22010-06-08 16:52:24 +00001292 {
Greg Clayton65611552011-06-04 01:26:29 +00001293 bool handled = false;
1294 if (!reason.empty())
1295 {
1296 if (reason.compare("trace") == 0)
1297 {
1298 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1299 handled = true;
1300 }
1301 else if (reason.compare("breakpoint") == 0)
1302 {
1303 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001304 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001305 if (bp_site_sp)
1306 {
1307 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1308 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1309 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1310 if (bp_site_sp->ValidForThisThread (gdb_thread))
1311 {
1312 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1313 handled = true;
1314 }
1315 }
1316
1317 if (!handled)
1318 {
1319 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1320 }
1321 }
1322 else if (reason.compare("trap") == 0)
1323 {
1324 // Let the trap just use the standard signal stop reason below...
1325 }
1326 else if (reason.compare("watchpoint") == 0)
1327 {
1328 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1329 // TODO: locate the watchpoint somehow...
1330 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
1331 handled = true;
1332 }
1333 else if (reason.compare("exception") == 0)
1334 {
1335 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
1336 handled = true;
1337 }
1338 }
1339
1340 if (signo)
1341 {
1342 if (signo == SIGTRAP)
1343 {
1344 // Currently we are going to assume SIGTRAP means we are either
1345 // hitting a breakpoint or hardware single stepping.
1346 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001347 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001348 if (bp_site_sp)
1349 {
1350 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1351 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1352 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1353 if (bp_site_sp->ValidForThisThread (gdb_thread))
1354 {
1355 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1356 handled = true;
1357 }
1358 }
1359 if (!handled)
1360 {
1361 // TODO: check for breakpoint or trap opcode in case there is a hard
1362 // coded software trap
1363 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1364 handled = true;
1365 }
1366 }
1367 if (!handled)
Greg Clayton37f962e2011-08-22 02:49:39 +00001368 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Chris Lattner24943d22010-06-08 16:52:24 +00001369 }
1370 else
1371 {
Greg Clayton643ee732010-08-04 01:40:35 +00001372 StopInfoSP invalid_stop_info_sp;
1373 gdb_thread->SetStopInfo (invalid_stop_info_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001374 }
Greg Clayton65611552011-06-04 01:26:29 +00001375
1376 if (!description.empty())
1377 {
1378 lldb::StopInfoSP stop_info_sp (gdb_thread->GetStopInfo ());
1379 if (stop_info_sp)
1380 {
1381 stop_info_sp->SetDescription (description.c_str());
Greg Clayton153ccd72011-08-10 02:10:13 +00001382 }
Greg Clayton65611552011-06-04 01:26:29 +00001383 else
1384 {
1385 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
1386 }
1387 }
1388 }
Chris Lattner24943d22010-06-08 16:52:24 +00001389 }
1390 return eStateStopped;
1391 }
1392 break;
1393
1394 case 'W':
1395 // process exited
1396 return eStateExited;
1397
1398 default:
1399 break;
1400 }
1401 return eStateInvalid;
1402}
1403
1404void
1405ProcessGDBRemote::RefreshStateAfterStop ()
1406{
Chris Lattner24943d22010-06-08 16:52:24 +00001407 // Let all threads recover from stopping and do any clean up based
1408 // on the previous thread state (if any).
1409 m_thread_list.RefreshStateAfterStop();
Greg Clayton261a18b2011-06-02 22:22:38 +00001410 SetThreadStopInfo (m_last_stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +00001411}
1412
1413Error
Jim Ingham3ae449a2010-11-17 02:32:00 +00001414ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner24943d22010-06-08 16:52:24 +00001415{
1416 Error error;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001417
Greg Claytona4881d02011-01-22 07:12:45 +00001418 bool timed_out = false;
1419 Mutex::Locker locker;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001420
1421 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton20d338f2010-11-18 05:57:03 +00001422 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001423 // We are being asked to halt during an attach. We need to just close
1424 // our file handle and debugserver will go away, and we can be done...
1425 m_gdb_comm.Disconnect();
Greg Clayton20d338f2010-11-18 05:57:03 +00001426 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001427 else
1428 {
1429 if (!m_gdb_comm.SendInterrupt (locker, 2, caused_stop, timed_out))
1430 {
1431 if (timed_out)
1432 error.SetErrorString("timed out sending interrupt packet");
1433 else
1434 error.SetErrorString("unknown error sending interrupt packet");
1435 }
1436 }
Chris Lattner24943d22010-06-08 16:52:24 +00001437 return error;
1438}
1439
1440Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001441ProcessGDBRemote::InterruptIfRunning
1442(
1443 bool discard_thread_plans,
1444 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +00001445 EventSP &stop_event_sp
1446)
Chris Lattner24943d22010-06-08 16:52:24 +00001447{
1448 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001449
Greg Clayton2860ba92011-01-23 19:58:49 +00001450 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1451
Greg Clayton68ca8232011-01-25 02:58:48 +00001452 bool paused_private_state_thread = false;
Greg Clayton2860ba92011-01-23 19:58:49 +00001453 const bool is_running = m_gdb_comm.IsRunning();
1454 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00001455 log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
Greg Clayton2860ba92011-01-23 19:58:49 +00001456 discard_thread_plans,
Greg Clayton68ca8232011-01-25 02:58:48 +00001457 catch_stop_event,
Greg Clayton2860ba92011-01-23 19:58:49 +00001458 is_running);
1459
Greg Clayton2860ba92011-01-23 19:58:49 +00001460 if (discard_thread_plans)
1461 {
1462 if (log)
1463 log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans");
1464 m_thread_list.DiscardThreadPlans();
1465 }
1466 if (is_running)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001467 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001468 if (catch_stop_event)
1469 {
1470 if (log)
1471 log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread");
1472 PausePrivateStateThread();
1473 paused_private_state_thread = true;
1474 }
1475
Greg Clayton4fb400f2010-09-27 21:07:38 +00001476 bool timed_out = false;
Greg Claytona4881d02011-01-22 07:12:45 +00001477 bool sent_interrupt = false;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001478 Mutex::Locker locker;
Greg Clayton72e1c782011-01-22 23:43:18 +00001479
Greg Clayton72e1c782011-01-22 23:43:18 +00001480 if (!m_gdb_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
Greg Clayton4fb400f2010-09-27 21:07:38 +00001481 {
1482 if (timed_out)
1483 error.SetErrorString("timed out sending interrupt packet");
1484 else
1485 error.SetErrorString("unknown error sending interrupt packet");
Greg Clayton68ca8232011-01-25 02:58:48 +00001486 if (paused_private_state_thread)
Greg Clayton72e1c782011-01-22 23:43:18 +00001487 ResumePrivateStateThread();
1488 return error;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001489 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001490
Greg Clayton72e1c782011-01-22 23:43:18 +00001491 if (catch_stop_event)
1492 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001493 // LISTEN HERE
Greg Clayton72e1c782011-01-22 23:43:18 +00001494 TimeValue timeout_time;
1495 timeout_time = TimeValue::Now();
Greg Clayton68ca8232011-01-25 02:58:48 +00001496 timeout_time.OffsetWithSeconds(5);
1497 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
Greg Clayton2860ba92011-01-23 19:58:49 +00001498
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001499 timed_out = state == eStateInvalid;
Greg Clayton2860ba92011-01-23 19:58:49 +00001500 if (log)
1501 log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001502
Greg Clayton2860ba92011-01-23 19:58:49 +00001503 if (timed_out)
Greg Clayton72e1c782011-01-22 23:43:18 +00001504 error.SetErrorString("unable to verify target stopped");
1505 }
1506
Greg Clayton68ca8232011-01-25 02:58:48 +00001507 if (paused_private_state_thread)
Greg Clayton2860ba92011-01-23 19:58:49 +00001508 {
1509 if (log)
1510 log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread");
Greg Clayton72e1c782011-01-22 23:43:18 +00001511 ResumePrivateStateThread();
Greg Clayton2860ba92011-01-23 19:58:49 +00001512 }
Greg Clayton4fb400f2010-09-27 21:07:38 +00001513 }
Chris Lattner24943d22010-06-08 16:52:24 +00001514 return error;
1515}
1516
Greg Clayton4fb400f2010-09-27 21:07:38 +00001517Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001518ProcessGDBRemote::WillDetach ()
1519{
Greg Clayton2860ba92011-01-23 19:58:49 +00001520 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1521 if (log)
1522 log->Printf ("ProcessGDBRemote::WillDetach()");
1523
Greg Clayton72e1c782011-01-22 23:43:18 +00001524 bool discard_thread_plans = true;
1525 bool catch_stop_event = true;
Greg Clayton72e1c782011-01-22 23:43:18 +00001526 EventSP event_sp;
Greg Clayton68ca8232011-01-25 02:58:48 +00001527 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
Greg Clayton72e1c782011-01-22 23:43:18 +00001528}
1529
1530Error
Greg Clayton4fb400f2010-09-27 21:07:38 +00001531ProcessGDBRemote::DoDetach()
1532{
1533 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001534 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton4fb400f2010-09-27 21:07:38 +00001535 if (log)
1536 log->Printf ("ProcessGDBRemote::DoDetach()");
1537
1538 DisableAllBreakpointSites ();
1539
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001540 m_thread_list.DiscardThreadPlans();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001541
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001542 size_t response_size = m_gdb_comm.SendPacket ("D", 1);
1543 if (log)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001544 {
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001545 if (response_size)
1546 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1547 else
1548 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed");
Greg Clayton4fb400f2010-09-27 21:07:38 +00001549 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001550 // Sleep for one second to let the process get all detached...
Greg Clayton4fb400f2010-09-27 21:07:38 +00001551 StopAsyncThread ();
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001552
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001553 SetPrivateState (eStateDetached);
1554 ResumePrivateStateThread();
1555
1556 //KillDebugserverProcess ();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001557 return error;
1558}
Chris Lattner24943d22010-06-08 16:52:24 +00001559
1560Error
1561ProcessGDBRemote::DoDestroy ()
1562{
1563 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001564 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001565 if (log)
1566 log->Printf ("ProcessGDBRemote::DoDestroy()");
1567
1568 // Interrupt if our inferior is running...
Greg Claytona4881d02011-01-22 07:12:45 +00001569 if (m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +00001570 {
Jim Ingham8226e942011-10-28 01:11:35 +00001571 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton72e1c782011-01-22 23:43:18 +00001572 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001573
1574 StringExtractorGDBRemote response;
1575 bool send_async = true;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001576 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001577 {
1578 char packet_cmd = response.GetChar(0);
1579
1580 if (packet_cmd == 'W' || packet_cmd == 'X')
1581 {
Greg Clayton06709002011-12-06 04:51:14 +00001582 SetLastStopPacket (response);
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001583 SetExitStatus(response.GetHexU8(), NULL);
1584 }
1585 }
1586 else
1587 {
1588 SetExitStatus(SIGABRT, NULL);
1589 //error.SetErrorString("kill packet failed");
1590 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001591 }
1592 }
Chris Lattner24943d22010-06-08 16:52:24 +00001593 StopAsyncThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001594 KillDebugserverProcess ();
1595 return error;
1596}
1597
Chris Lattner24943d22010-06-08 16:52:24 +00001598//------------------------------------------------------------------
1599// Process Queries
1600//------------------------------------------------------------------
1601
1602bool
1603ProcessGDBRemote::IsAlive ()
1604{
Greg Clayton58e844b2010-12-08 05:08:21 +00001605 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner24943d22010-06-08 16:52:24 +00001606}
1607
1608addr_t
1609ProcessGDBRemote::GetImageInfoAddress()
1610{
1611 if (!m_gdb_comm.IsRunning())
1612 {
1613 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001614 if (m_gdb_comm.SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
Chris Lattner24943d22010-06-08 16:52:24 +00001615 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001616 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001617 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1618 }
1619 }
1620 return LLDB_INVALID_ADDRESS;
1621}
1622
Chris Lattner24943d22010-06-08 16:52:24 +00001623//------------------------------------------------------------------
1624// Process Memory
1625//------------------------------------------------------------------
1626size_t
1627ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1628{
1629 if (size > m_max_memory_size)
1630 {
1631 // Keep memory read sizes down to a sane limit. This function will be
1632 // called multiple times in order to complete the task by
1633 // lldb_private::Process so it is ok to do this.
1634 size = m_max_memory_size;
1635 }
1636
1637 char packet[64];
1638 const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size);
1639 assert (packet_len + 1 < sizeof(packet));
1640 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001641 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001642 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001643 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001644 {
1645 error.Clear();
1646 return response.GetHexBytes(buf, size, '\xdd');
1647 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001648 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001649 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001650 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001651 error.SetErrorStringWithFormat("'%s' packet unsupported", packet);
1652 else
1653 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str());
1654 }
1655 else
1656 {
1657 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet);
1658 }
1659 return 0;
1660}
1661
1662size_t
1663ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1664{
Greg Claytonc8bc1c32011-05-16 02:35:02 +00001665 if (size > m_max_memory_size)
1666 {
1667 // Keep memory read sizes down to a sane limit. This function will be
1668 // called multiple times in order to complete the task by
1669 // lldb_private::Process so it is ok to do this.
1670 size = m_max_memory_size;
1671 }
1672
Chris Lattner24943d22010-06-08 16:52:24 +00001673 StreamString packet;
1674 packet.Printf("M%llx,%zx:", addr, size);
Greg Claytoncd548032011-02-01 01:31:41 +00001675 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00001676 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001677 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001678 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001679 if (response.IsOKResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001680 {
1681 error.Clear();
1682 return size;
1683 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001684 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001685 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001686 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001687 error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str());
1688 else
1689 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
1690 }
1691 else
1692 {
1693 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str());
1694 }
1695 return 0;
1696}
1697
1698lldb::addr_t
1699ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
1700{
Greg Clayton989816b2011-05-14 01:50:35 +00001701 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
1702
Greg Clayton2f085c62011-05-15 01:25:55 +00001703 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton989816b2011-05-14 01:50:35 +00001704 switch (supported)
1705 {
1706 case eLazyBoolCalculate:
1707 case eLazyBoolYes:
1708 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
1709 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
1710 return allocated_addr;
1711
1712 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001713 // Call mmap() to create memory in the inferior..
1714 unsigned prot = 0;
1715 if (permissions & lldb::ePermissionsReadable)
1716 prot |= eMmapProtRead;
1717 if (permissions & lldb::ePermissionsWritable)
1718 prot |= eMmapProtWrite;
1719 if (permissions & lldb::ePermissionsExecutable)
1720 prot |= eMmapProtExec;
Greg Clayton989816b2011-05-14 01:50:35 +00001721
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001722 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
1723 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
1724 m_addr_to_mmap_size[allocated_addr] = size;
1725 else
1726 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton989816b2011-05-14 01:50:35 +00001727 break;
1728 }
1729
Chris Lattner24943d22010-06-08 16:52:24 +00001730 if (allocated_addr == LLDB_INVALID_ADDRESS)
Greg Clayton613b8732011-05-17 03:37:42 +00001731 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
Chris Lattner24943d22010-06-08 16:52:24 +00001732 else
1733 error.Clear();
1734 return allocated_addr;
1735}
1736
1737Error
Greg Claytona9385532011-11-18 07:03:08 +00001738ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
1739 MemoryRegionInfo &region_info)
1740{
1741
1742 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
1743 return error;
1744}
1745
1746Error
Chris Lattner24943d22010-06-08 16:52:24 +00001747ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
1748{
1749 Error error;
Greg Clayton2f085c62011-05-15 01:25:55 +00001750 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
1751
1752 switch (supported)
1753 {
1754 case eLazyBoolCalculate:
1755 // We should never be deallocating memory without allocating memory
1756 // first so we should never get eLazyBoolCalculate
1757 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
1758 break;
1759
1760 case eLazyBoolYes:
1761 if (!m_gdb_comm.DeallocateMemory (addr))
1762 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
1763 break;
1764
1765 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001766 // Call munmap() to deallocate memory in the inferior..
Greg Clayton2f085c62011-05-15 01:25:55 +00001767 {
1768 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001769 if (pos != m_addr_to_mmap_size.end() &&
1770 InferiorCallMunmap(this, addr, pos->second))
1771 m_addr_to_mmap_size.erase (pos);
1772 else
1773 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
Greg Clayton2f085c62011-05-15 01:25:55 +00001774 }
1775 break;
1776 }
1777
Chris Lattner24943d22010-06-08 16:52:24 +00001778 return error;
1779}
1780
1781
1782//------------------------------------------------------------------
1783// Process STDIO
1784//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001785size_t
1786ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
1787{
1788 if (m_stdio_communication.IsConnected())
1789 {
1790 ConnectionStatus status;
1791 m_stdio_communication.Write(src, src_len, status, NULL);
1792 }
1793 return 0;
1794}
1795
1796Error
1797ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site)
1798{
1799 Error error;
1800 assert (bp_site != NULL);
1801
Greg Claytone005f2c2010-11-06 01:53:30 +00001802 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001803 user_id_t site_id = bp_site->GetID();
1804 const addr_t addr = bp_site->GetLoadAddress();
1805 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001806 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001807
1808 if (bp_site->IsEnabled())
1809 {
1810 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001811 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 +00001812 return error;
1813 }
1814 else
1815 {
1816 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1817
1818 if (bp_site->HardwarePreferred())
1819 {
1820 // Try and set hardware breakpoint, and if that fails, fall through
1821 // and set a software breakpoint?
Greg Claytonb72d0f02011-04-12 05:54:46 +00001822 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner24943d22010-06-08 16:52:24 +00001823 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001824 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001825 {
1826 bp_site->SetEnabled(true);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001827 bp_site->SetType (BreakpointSite::eHardware);
Chris Lattner24943d22010-06-08 16:52:24 +00001828 return error;
1829 }
Chris Lattner24943d22010-06-08 16:52:24 +00001830 }
1831 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001832
1833 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner24943d22010-06-08 16:52:24 +00001834 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001835 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
1836 {
1837 bp_site->SetEnabled(true);
1838 bp_site->SetType (BreakpointSite::eExternal);
1839 return error;
1840 }
Chris Lattner24943d22010-06-08 16:52:24 +00001841 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001842
1843 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +00001844 }
1845
1846 if (log)
1847 {
1848 const char *err_string = error.AsCString();
1849 log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s",
1850 bp_site->GetLoadAddress(),
1851 err_string ? err_string : "NULL");
1852 }
1853 // We shouldn't reach here on a successful breakpoint enable...
1854 if (error.Success())
1855 error.SetErrorToGenericError();
1856 return error;
1857}
1858
1859Error
1860ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site)
1861{
1862 Error error;
1863 assert (bp_site != NULL);
1864 addr_t addr = bp_site->GetLoadAddress();
1865 user_id_t site_id = bp_site->GetID();
Greg Claytone005f2c2010-11-06 01:53:30 +00001866 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001867 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001868 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001869
1870 if (bp_site->IsEnabled())
1871 {
1872 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1873
Greg Claytonb72d0f02011-04-12 05:54:46 +00001874 BreakpointSite::Type bp_type = bp_site->GetType();
1875 switch (bp_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001876 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001877 case BreakpointSite::eSoftware:
1878 error = DisableSoftwareBreakpoint (bp_site);
1879 break;
1880
1881 case BreakpointSite::eHardware:
1882 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1883 error.SetErrorToGenericError();
1884 break;
1885
1886 case BreakpointSite::eExternal:
1887 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1888 error.SetErrorToGenericError();
1889 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001890 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001891 if (error.Success())
1892 bp_site->SetEnabled(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001893 }
1894 else
1895 {
1896 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001897 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 +00001898 return error;
1899 }
1900
1901 if (error.Success())
1902 error.SetErrorToGenericError();
1903 return error;
1904}
1905
Johnny Chen21900fb2011-09-06 22:38:36 +00001906// Pre-requisite: wp != NULL.
1907static GDBStoppointType
Johnny Chenecd4feb2011-10-14 00:42:25 +00001908GetGDBStoppointType (Watchpoint *wp)
Johnny Chen21900fb2011-09-06 22:38:36 +00001909{
1910 assert(wp);
1911 bool watch_read = wp->WatchpointRead();
1912 bool watch_write = wp->WatchpointWrite();
1913
1914 // watch_read and watch_write cannot both be false.
1915 assert(watch_read || watch_write);
1916 if (watch_read && watch_write)
1917 return eWatchpointReadWrite;
Johnny Chen48a5e852011-09-09 20:35:15 +00001918 else if (watch_read)
Johnny Chen21900fb2011-09-06 22:38:36 +00001919 return eWatchpointRead;
Johnny Chen48a5e852011-09-09 20:35:15 +00001920 else // Must be watch_write, then.
Johnny Chen21900fb2011-09-06 22:38:36 +00001921 return eWatchpointWrite;
1922}
1923
Chris Lattner24943d22010-06-08 16:52:24 +00001924Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001925ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001926{
1927 Error error;
1928 if (wp)
1929 {
1930 user_id_t watchID = wp->GetID();
1931 addr_t addr = wp->GetLoadAddress();
Greg Claytone005f2c2010-11-06 01:53:30 +00001932 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001933 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001934 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %llu)", watchID);
Chris Lattner24943d22010-06-08 16:52:24 +00001935 if (wp->IsEnabled())
1936 {
1937 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001938 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %llu) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001939 return error;
1940 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001941
1942 GDBStoppointType type = GetGDBStoppointType(wp);
1943 // Pass down an appropriate z/Z packet...
1944 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner24943d22010-06-08 16:52:24 +00001945 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001946 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
1947 {
1948 wp->SetEnabled(true);
1949 return error;
1950 }
1951 else
1952 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001953 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001954 else
1955 error.SetErrorString("watchpoints not supported");
Chris Lattner24943d22010-06-08 16:52:24 +00001956 }
1957 else
1958 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001959 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00001960 }
1961 if (error.Success())
1962 error.SetErrorToGenericError();
1963 return error;
1964}
1965
1966Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001967ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001968{
1969 Error error;
1970 if (wp)
1971 {
1972 user_id_t watchID = wp->GetID();
1973
Greg Claytone005f2c2010-11-06 01:53:30 +00001974 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001975
1976 addr_t addr = wp->GetLoadAddress();
1977 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001978 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001979
Johnny Chen21900fb2011-09-06 22:38:36 +00001980 if (!wp->IsEnabled())
1981 {
1982 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001983 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", watchID, (uint64_t)addr);
Johnny Chen21900fb2011-09-06 22:38:36 +00001984 return error;
1985 }
1986
Chris Lattner24943d22010-06-08 16:52:24 +00001987 if (wp->IsHardware())
1988 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001989 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner24943d22010-06-08 16:52:24 +00001990 // Pass down an appropriate z/Z packet...
Johnny Chen21900fb2011-09-06 22:38:36 +00001991 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
1992 {
1993 wp->SetEnabled(false);
1994 return error;
1995 }
1996 else
1997 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001998 }
1999 // TODO: clear software watchpoints if we implement them
2000 }
2001 else
2002 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00002003 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00002004 }
2005 if (error.Success())
2006 error.SetErrorToGenericError();
2007 return error;
2008}
2009
2010void
2011ProcessGDBRemote::Clear()
2012{
2013 m_flags = 0;
2014 m_thread_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002015}
2016
2017Error
2018ProcessGDBRemote::DoSignal (int signo)
2019{
2020 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00002021 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002022 if (log)
2023 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2024
2025 if (!m_gdb_comm.SendAsyncSignal (signo))
2026 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2027 return error;
2028}
2029
Chris Lattner24943d22010-06-08 16:52:24 +00002030Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002031ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2032{
2033 ProcessLaunchInfo launch_info;
2034 return StartDebugserverProcess(debugserver_url, launch_info);
2035}
2036
2037Error
2038ProcessGDBRemote::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 +00002039{
2040 Error error;
2041 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2042 {
2043 // If we locate debugserver, keep that located version around
2044 static FileSpec g_debugserver_file_spec;
2045
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002046 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner24943d22010-06-08 16:52:24 +00002047 char debugserver_path[PATH_MAX];
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002048 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner24943d22010-06-08 16:52:24 +00002049
2050 // Always check to see if we have an environment override for the path
2051 // to the debugserver to use and use it if we do.
2052 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2053 if (env_debugserver_path)
Greg Clayton537a7a82010-10-20 20:54:39 +00002054 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +00002055 else
2056 debugserver_file_spec = g_debugserver_file_spec;
2057 bool debugserver_exists = debugserver_file_spec.Exists();
2058 if (!debugserver_exists)
2059 {
2060 // The debugserver binary is in the LLDB.framework/Resources
2061 // directory.
Greg Clayton24b48ff2010-10-17 22:03:32 +00002062 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner24943d22010-06-08 16:52:24 +00002063 {
Greg Clayton24b48ff2010-10-17 22:03:32 +00002064 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002065 debugserver_exists = debugserver_file_spec.Exists();
Greg Clayton24b48ff2010-10-17 22:03:32 +00002066 if (debugserver_exists)
2067 {
2068 g_debugserver_file_spec = debugserver_file_spec;
2069 }
2070 else
2071 {
2072 g_debugserver_file_spec.Clear();
2073 debugserver_file_spec.Clear();
2074 }
Chris Lattner24943d22010-06-08 16:52:24 +00002075 }
2076 }
2077
2078 if (debugserver_exists)
2079 {
2080 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2081
2082 m_stdio_communication.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002083
Greg Claytone005f2c2010-11-06 01:53:30 +00002084 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002085
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002086 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner24943d22010-06-08 16:52:24 +00002087 char arg_cstr[PATH_MAX];
Chris Lattner24943d22010-06-08 16:52:24 +00002088
Chris Lattner24943d22010-06-08 16:52:24 +00002089 // Start args with "debugserver /file/path -r --"
2090 debugserver_args.AppendArgument(debugserver_path);
2091 debugserver_args.AppendArgument(debugserver_url);
Greg Clayton24b48ff2010-10-17 22:03:32 +00002092 // use native registers, not the GDB registers
2093 debugserver_args.AppendArgument("--native-regs");
2094 // make debugserver run in its own session so signals generated by
2095 // special terminal key sequences (^C) don't affect debugserver
2096 debugserver_args.AppendArgument("--setsid");
Chris Lattner24943d22010-06-08 16:52:24 +00002097
Chris Lattner24943d22010-06-08 16:52:24 +00002098 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2099 if (env_debugserver_log_file)
2100 {
2101 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2102 debugserver_args.AppendArgument(arg_cstr);
2103 }
2104
2105 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2106 if (env_debugserver_log_flags)
2107 {
2108 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2109 debugserver_args.AppendArgument(arg_cstr);
2110 }
Greg Claytoncc3e6402011-01-25 06:55:13 +00002111// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002112// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner24943d22010-06-08 16:52:24 +00002113
Greg Claytonb72d0f02011-04-12 05:54:46 +00002114 // We currently send down all arguments, attach pids, or attach
2115 // process names in dedicated GDB server packets, so we don't need
2116 // to pass them as arguments. This is currently because of all the
2117 // things we need to setup prior to launching: the environment,
2118 // current working dir, file actions, etc.
2119#if 0
Chris Lattner24943d22010-06-08 16:52:24 +00002120 // Now append the program arguments
Greg Claytona2f74232011-02-24 22:24:29 +00002121 if (inferior_argv)
Chris Lattner24943d22010-06-08 16:52:24 +00002122 {
Greg Claytona2f74232011-02-24 22:24:29 +00002123 // Terminate the debugserver args so we can now append the inferior args
2124 debugserver_args.AppendArgument("--");
Chris Lattner24943d22010-06-08 16:52:24 +00002125
Greg Claytona2f74232011-02-24 22:24:29 +00002126 for (int i = 0; inferior_argv[i] != NULL; ++i)
2127 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner24943d22010-06-08 16:52:24 +00002128 }
2129 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2130 {
2131 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2132 debugserver_args.AppendArgument (arg_cstr);
2133 }
2134 else if (attach_name && attach_name[0])
2135 {
2136 if (wait_for_launch)
2137 debugserver_args.AppendArgument ("--waitfor");
2138 else
2139 debugserver_args.AppendArgument ("--attach");
2140 debugserver_args.AppendArgument (attach_name);
2141 }
Chris Lattner24943d22010-06-08 16:52:24 +00002142#endif
Greg Claytonb72d0f02011-04-12 05:54:46 +00002143
2144 ProcessLaunchInfo::FileAction file_action;
2145
2146 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2147 // to "/dev/null" if we run into any problems.
2148 file_action.Close (STDIN_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 (STDOUT_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002151 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002152 file_action.Close (STDERR_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002153 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner24943d22010-06-08 16:52:24 +00002154
2155 if (log)
2156 {
2157 StreamString strm;
2158 debugserver_args.Dump (&strm);
2159 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2160 }
2161
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002162 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2163 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Clayton1c4642c2011-11-16 05:37:56 +00002164
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002165 error = Host::LaunchProcess(debugserver_launch_info);
Greg Claytone9d0df42010-07-02 01:29:13 +00002166
Greg Claytonb72d0f02011-04-12 05:54:46 +00002167 if (error.Success ())
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002168 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Claytonb72d0f02011-04-12 05:54:46 +00002169 else
Chris Lattner24943d22010-06-08 16:52:24 +00002170 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2171
2172 if (error.Fail() || log)
Greg Claytond9919d32011-12-01 23:28:38 +00002173 error.PutToLog(log.get(), "Host::LaunchProcess (launch_info) => pid=%llu, path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner24943d22010-06-08 16:52:24 +00002174 }
2175 else
2176 {
Greg Clayton9c236732011-10-26 00:56:27 +00002177 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002178 }
2179
2180 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2181 StartAsyncThread ();
2182 }
2183 return error;
2184}
2185
2186bool
2187ProcessGDBRemote::MonitorDebugserverProcess
2188(
2189 void *callback_baton,
2190 lldb::pid_t debugserver_pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +00002191 bool exited, // True if the process did exit
Chris Lattner24943d22010-06-08 16:52:24 +00002192 int signo, // Zero for no signal
2193 int exit_status // Exit value of process if signal is zero
2194)
2195{
Greg Clayton1c4642c2011-11-16 05:37:56 +00002196 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2197 // and might not exist anymore, so we need to carefully try to get the
2198 // target for this process first since we have a race condition when
2199 // we are done running between getting the notice that the inferior
2200 // process has died and the debugserver that was debugging this process.
2201 // In our test suite, we are also continually running process after
2202 // process, so we must be very careful to make sure:
2203 // 1 - process object hasn't been deleted already
2204 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner24943d22010-06-08 16:52:24 +00002205
2206 // "debugserver_pid" argument passed in is the process ID for
2207 // debugserver that we are tracking...
Greg Clayton1c4642c2011-11-16 05:37:56 +00002208 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002209
Greg Clayton75ccf502010-08-21 02:22:51 +00002210 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton72e1c782011-01-22 23:43:18 +00002211
Greg Clayton1c4642c2011-11-16 05:37:56 +00002212 // Get a shared pointer to the target that has a matching process pointer.
2213 // This target could be gone, or the target could already have a new process
2214 // object inside of it
2215 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2216
Greg Clayton72e1c782011-01-22 23:43:18 +00002217 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00002218 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 +00002219
Greg Clayton1c4642c2011-11-16 05:37:56 +00002220 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002221 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002222 // We found a process in a target that matches, but another thread
2223 // might be in the process of launching a new process that will
2224 // soon replace it, so get a shared pointer to the process so we
2225 // can keep it alive.
2226 ProcessSP process_sp (target_sp->GetProcessSP());
2227 // Now we have a shared pointer to the process that can't go away on us
2228 // so we now make sure it was the same as the one passed in, and also make
2229 // sure that our previous "process *" didn't get deleted and have a new
2230 // "process *" created in its place with the same pointer. To verify this
2231 // we make sure the process has our debugserver process ID. If we pass all
2232 // of these tests, then we are sure that this process is the one we were
2233 // looking for.
2234 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner24943d22010-06-08 16:52:24 +00002235 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002236 // Sleep for a half a second to make sure our inferior process has
2237 // time to set its exit status before we set it incorrectly when
2238 // both the debugserver and the inferior process shut down.
2239 usleep (500000);
2240 // If our process hasn't yet exited, debugserver might have died.
2241 // If the process did exit, the we are reaping it.
2242 const StateType state = process->GetState();
2243
2244 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2245 state != eStateInvalid &&
2246 state != eStateUnloaded &&
2247 state != eStateExited &&
2248 state != eStateDetached)
Chris Lattner24943d22010-06-08 16:52:24 +00002249 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002250 char error_str[1024];
2251 if (signo)
2252 {
2253 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2254 if (signal_cstr)
2255 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2256 else
2257 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2258 }
Chris Lattner24943d22010-06-08 16:52:24 +00002259 else
Greg Clayton1c4642c2011-11-16 05:37:56 +00002260 {
2261 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2262 }
Greg Clayton75ccf502010-08-21 02:22:51 +00002263
Greg Clayton1c4642c2011-11-16 05:37:56 +00002264 process->SetExitStatus (-1, error_str);
2265 }
2266 // Debugserver has exited we need to let our ProcessGDBRemote
2267 // know that it no longer has a debugserver instance
2268 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton75ccf502010-08-21 02:22:51 +00002269 }
Chris Lattner24943d22010-06-08 16:52:24 +00002270 }
2271 return true;
2272}
2273
2274void
2275ProcessGDBRemote::KillDebugserverProcess ()
2276{
2277 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2278 {
2279 ::kill (m_debugserver_pid, SIGINT);
2280 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2281 }
2282}
2283
2284void
2285ProcessGDBRemote::Initialize()
2286{
2287 static bool g_initialized = false;
2288
2289 if (g_initialized == false)
2290 {
2291 g_initialized = true;
2292 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2293 GetPluginDescriptionStatic(),
2294 CreateInstance);
2295
2296 Log::Callbacks log_callbacks = {
2297 ProcessGDBRemoteLog::DisableLog,
2298 ProcessGDBRemoteLog::EnableLog,
2299 ProcessGDBRemoteLog::ListLogCategories
2300 };
2301
2302 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2303 }
2304}
2305
2306bool
Chris Lattner24943d22010-06-08 16:52:24 +00002307ProcessGDBRemote::StartAsyncThread ()
2308{
Greg Claytone005f2c2010-11-06 01:53:30 +00002309 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002310
2311 if (log)
2312 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2313
2314 // Create a thread that watches our internal state and controls which
2315 // events make it to clients (into the DCProcess event queue).
2316 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002317 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002318}
2319
2320void
2321ProcessGDBRemote::StopAsyncThread ()
2322{
Greg Claytone005f2c2010-11-06 01:53:30 +00002323 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002324
2325 if (log)
2326 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2327
2328 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
Jim Ingham8226e942011-10-28 01:11:35 +00002329
2330 // This will shut down the async thread.
2331 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Chris Lattner24943d22010-06-08 16:52:24 +00002332
2333 // Stop the stdio thread
Greg Clayton09c81ef2011-02-08 01:34:25 +00002334 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002335 {
2336 Host::ThreadJoin (m_async_thread, NULL, NULL);
2337 }
2338}
2339
2340
2341void *
2342ProcessGDBRemote::AsyncThread (void *arg)
2343{
2344 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2345
Greg Claytone005f2c2010-11-06 01:53:30 +00002346 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002347 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002348 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002349
2350 Listener listener ("ProcessGDBRemote::AsyncThread");
2351 EventSP event_sp;
2352 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2353 eBroadcastBitAsyncThreadShouldExit;
2354
2355 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2356 {
Greg Claytona2f74232011-02-24 22:24:29 +00002357 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2358
Chris Lattner24943d22010-06-08 16:52:24 +00002359 bool done = false;
2360 while (!done)
2361 {
2362 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002363 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002364 if (listener.WaitForEvent (NULL, event_sp))
2365 {
2366 const uint32_t event_type = event_sp->GetType();
Greg Claytona2f74232011-02-24 22:24:29 +00002367 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner24943d22010-06-08 16:52:24 +00002368 {
Greg Claytona2f74232011-02-24 22:24:29 +00002369 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002370 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 +00002371
Greg Claytona2f74232011-02-24 22:24:29 +00002372 switch (event_type)
2373 {
2374 case eBroadcastBitAsyncContinue:
Chris Lattner24943d22010-06-08 16:52:24 +00002375 {
Greg Claytona2f74232011-02-24 22:24:29 +00002376 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002377
Greg Claytona2f74232011-02-24 22:24:29 +00002378 if (continue_packet)
Chris Lattner24943d22010-06-08 16:52:24 +00002379 {
Greg Claytona2f74232011-02-24 22:24:29 +00002380 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2381 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2382 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002383 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002384
Greg Claytona2f74232011-02-24 22:24:29 +00002385 if (::strstr (continue_cstr, "vAttach") == NULL)
2386 process->SetPrivateState(eStateRunning);
2387 StringExtractorGDBRemote response;
2388 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner24943d22010-06-08 16:52:24 +00002389
Greg Claytona2f74232011-02-24 22:24:29 +00002390 switch (stop_state)
2391 {
2392 case eStateStopped:
2393 case eStateCrashed:
2394 case eStateSuspended:
Greg Clayton06709002011-12-06 04:51:14 +00002395 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002396 process->SetPrivateState (stop_state);
2397 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002398
Greg Claytona2f74232011-02-24 22:24:29 +00002399 case eStateExited:
Greg Clayton06709002011-12-06 04:51:14 +00002400 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002401 response.SetFilePos(1);
2402 process->SetExitStatus(response.GetHexU8(), NULL);
2403 done = true;
2404 break;
2405
2406 case eStateInvalid:
2407 process->SetExitStatus(-1, "lost connection");
2408 break;
2409
2410 default:
2411 process->SetPrivateState (stop_state);
2412 break;
2413 }
Chris Lattner24943d22010-06-08 16:52:24 +00002414 }
2415 }
Greg Claytona2f74232011-02-24 22:24:29 +00002416 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002417
Greg Claytona2f74232011-02-24 22:24:29 +00002418 case eBroadcastBitAsyncThreadShouldExit:
2419 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002420 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Claytona2f74232011-02-24 22:24:29 +00002421 done = true;
2422 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002423
Greg Claytona2f74232011-02-24 22:24:29 +00002424 default:
2425 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002426 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 +00002427 done = true;
2428 break;
2429 }
2430 }
2431 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2432 {
2433 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2434 {
2435 process->SetExitStatus (-1, "lost connection");
Chris Lattner24943d22010-06-08 16:52:24 +00002436 done = true;
Greg Claytona2f74232011-02-24 22:24:29 +00002437 }
Chris Lattner24943d22010-06-08 16:52:24 +00002438 }
2439 }
2440 else
2441 {
2442 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002443 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 +00002444 done = true;
2445 }
2446 }
2447 }
2448
2449 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002450 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002451
2452 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2453 return NULL;
2454}
2455
Chris Lattner24943d22010-06-08 16:52:24 +00002456const char *
2457ProcessGDBRemote::GetDispatchQueueNameForThread
2458(
2459 addr_t thread_dispatch_qaddr,
2460 std::string &dispatch_queue_name
2461)
2462{
2463 dispatch_queue_name.clear();
2464 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2465 {
2466 // Cache the dispatch_queue_offsets_addr value so we don't always have
2467 // to look it up
2468 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2469 {
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002470 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
2471 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Clayton444fe992012-02-26 05:51:37 +00002472 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
2473 ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002474 if (module_sp)
2475 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2476
2477 if (dispatch_queue_offsets_symbol == NULL)
2478 {
Greg Clayton444fe992012-02-26 05:51:37 +00002479 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
2480 module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002481 if (module_sp)
2482 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2483 }
Chris Lattner24943d22010-06-08 16:52:24 +00002484 if (dispatch_queue_offsets_symbol)
Greg Claytoneea26402010-09-14 23:36:40 +00002485 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetValue().GetLoadAddress(&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00002486
2487 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2488 return NULL;
2489 }
2490
2491 uint8_t memory_buffer[8];
Greg Clayton395fc332011-02-15 21:59:32 +00002492 DataExtractor data (memory_buffer,
2493 sizeof(memory_buffer),
2494 m_target.GetArchitecture().GetByteOrder(),
2495 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00002496
2497 // Excerpt from src/queue_private.h
2498 struct dispatch_queue_offsets_s
2499 {
2500 uint16_t dqo_version;
2501 uint16_t dqo_label;
2502 uint16_t dqo_label_size;
2503 } dispatch_queue_offsets;
2504
2505
2506 Error error;
2507 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
2508 {
2509 uint32_t data_offset = 0;
2510 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
2511 {
2512 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
2513 {
2514 data_offset = 0;
2515 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
2516 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
2517 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
2518 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
2519 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
2520 dispatch_queue_name.erase (bytes_read);
2521 }
2522 }
2523 }
2524 }
2525 if (dispatch_queue_name.empty())
2526 return NULL;
2527 return dispatch_queue_name.c_str();
2528}
2529
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002530//uint32_t
2531//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2532//{
2533// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
2534// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
2535// if (m_local_debugserver)
2536// {
2537// return Host::ListProcessesMatchingName (name, matches, pids);
2538// }
2539// else
2540// {
2541// // FIXME: Implement talking to the remote debugserver.
2542// return 0;
2543// }
2544//
2545//}
2546//
Jim Ingham55e01d82011-01-22 01:33:44 +00002547bool
2548ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
2549 lldb_private::StoppointCallbackContext *context,
2550 lldb::user_id_t break_id,
2551 lldb::user_id_t break_loc_id)
2552{
2553 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
2554 // run so I can stop it if that's what I want to do.
2555 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2556 if (log)
2557 log->Printf("Hit New Thread Notification breakpoint.");
2558 return false;
2559}
2560
2561
2562bool
2563ProcessGDBRemote::StartNoticingNewThreads()
2564{
2565 static const char *bp_names[] =
2566 {
2567 "start_wqthread",
Jim Inghamff276fe2011-02-08 05:19:01 +00002568 "_pthread_wqthread",
Jim Ingham55e01d82011-01-22 01:33:44 +00002569 "_pthread_start",
2570 NULL
2571 };
2572
2573 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2574 size_t num_bps = m_thread_observation_bps.size();
2575 if (num_bps != 0)
2576 {
2577 for (int i = 0; i < num_bps; i++)
2578 {
2579 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2580 if (break_sp)
2581 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002582 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002583 log->Printf("Enabled noticing new thread breakpoint.");
2584 break_sp->SetEnabled(true);
2585 }
2586 }
2587 }
2588 else
2589 {
2590 for (int i = 0; bp_names[i] != NULL; i++)
2591 {
Jim Inghamd6d47972011-09-23 00:54:11 +00002592 Breakpoint *breakpoint = m_target.CreateBreakpoint (NULL, NULL, bp_names[i], eFunctionNameTypeFull, true).get();
Jim Ingham55e01d82011-01-22 01:33:44 +00002593 if (breakpoint)
2594 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002595 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002596 log->Printf("Successfully created new thread notification breakpoint at \"%s\".", bp_names[i]);
2597 m_thread_observation_bps.push_back(breakpoint->GetID());
2598 breakpoint->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
2599 }
2600 else
2601 {
2602 if (log)
2603 log->Printf("Failed to create new thread notification breakpoint.");
2604 return false;
2605 }
2606 }
2607 }
2608
2609 return true;
2610}
2611
2612bool
2613ProcessGDBRemote::StopNoticingNewThreads()
2614{
Jim Inghamff276fe2011-02-08 05:19:01 +00002615 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham6bb73372011-10-15 00:21:37 +00002616 if (log && log->GetVerbose())
Jim Inghamff276fe2011-02-08 05:19:01 +00002617 log->Printf ("Disabling new thread notification breakpoint.");
Jim Ingham55e01d82011-01-22 01:33:44 +00002618 size_t num_bps = m_thread_observation_bps.size();
2619 if (num_bps != 0)
2620 {
2621 for (int i = 0; i < num_bps; i++)
2622 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002623
2624 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2625 if (break_sp)
2626 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002627 break_sp->SetEnabled(false);
2628 }
2629 }
2630 }
2631 return true;
2632}
2633
2634