blob: 8a1f120f3083770ebf68856bd6f85e13c684d328 [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
Greg Clayton451fa822012-04-09 22:46:21 +000056namespace lldb
57{
58 // Provide a function that can easily dump the packet history if we know a
59 // ProcessGDBRemote * value (which we can get from logs or from debugging).
60 // We need the function in the lldb namespace so it makes it into the final
61 // executable since the LLDB shared library only exports stuff in the lldb
62 // namespace. This allows you to attach with a debugger and call this
63 // function and get the packet history dumped to a file.
64 void
65 DumpProcessGDBRemotePacketHistory (void *p, const char *path)
66 {
67 ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (path);
68 }
69};
Chris Lattner24943d22010-06-08 16:52:24 +000070
Chris Lattner24943d22010-06-08 16:52:24 +000071
72#define DEBUGSERVER_BASENAME "debugserver"
73using namespace lldb;
74using namespace lldb_private;
75
Jim Inghamf9600482011-03-29 21:45:47 +000076static bool rand_initialized = false;
77
Chris Lattner24943d22010-06-08 16:52:24 +000078static inline uint16_t
79get_random_port ()
80{
Jim Inghamf9600482011-03-29 21:45:47 +000081 if (!rand_initialized)
82 {
Stephen Wilson60f19d52011-03-30 00:12:40 +000083 time_t seed = time(NULL);
84
Jim Inghamf9600482011-03-29 21:45:47 +000085 rand_initialized = true;
Stephen Wilson60f19d52011-03-30 00:12:40 +000086 srand(seed);
Jim Inghamf9600482011-03-29 21:45:47 +000087 }
Stephen Wilson50daf772011-03-25 18:16:28 +000088 return (rand() % (UINT16_MAX - 1000u)) + 1000u;
Chris Lattner24943d22010-06-08 16:52:24 +000089}
90
91
92const char *
93ProcessGDBRemote::GetPluginNameStatic()
94{
Greg Claytonb1888f22011-03-19 01:12:21 +000095 return "gdb-remote";
Chris Lattner24943d22010-06-08 16:52:24 +000096}
97
98const char *
99ProcessGDBRemote::GetPluginDescriptionStatic()
100{
101 return "GDB Remote protocol based debugging plug-in.";
102}
103
104void
105ProcessGDBRemote::Terminate()
106{
107 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
108}
109
110
Greg Clayton46c9a352012-02-09 06:16:32 +0000111lldb::ProcessSP
112ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner24943d22010-06-08 16:52:24 +0000113{
Greg Clayton46c9a352012-02-09 06:16:32 +0000114 lldb::ProcessSP process_sp;
115 if (crash_file_path == NULL)
116 process_sp.reset (new ProcessGDBRemote (target, listener));
117 return process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000118}
119
120bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000121ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000122{
Greg Clayton61ddf562011-10-21 21:41:45 +0000123 if (plugin_specified_by_name)
124 return true;
125
Chris Lattner24943d22010-06-08 16:52:24 +0000126 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +0000127 Module *exe_module = target.GetExecutableModulePointer();
128 if (exe_module)
Greg Clayton46c9a352012-02-09 06:16:32 +0000129 {
130 ObjectFile *exe_objfile = exe_module->GetObjectFile();
131 // We can't debug core files...
132 switch (exe_objfile->GetType())
133 {
134 case ObjectFile::eTypeInvalid:
135 case ObjectFile::eTypeCoreFile:
136 case ObjectFile::eTypeDebugInfo:
137 case ObjectFile::eTypeObjectFile:
138 case ObjectFile::eTypeSharedLibrary:
139 case ObjectFile::eTypeStubLibrary:
140 return false;
141 case ObjectFile::eTypeExecutable:
142 case ObjectFile::eTypeDynamicLinker:
143 case ObjectFile::eTypeUnknown:
144 break;
145 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000146 return exe_module->GetFileSpec().Exists();
Greg Clayton46c9a352012-02-09 06:16:32 +0000147 }
Jim Ingham7508e732010-08-09 23:31:02 +0000148 // However, if there is no executable module, we return true since we might be preparing to attach.
149 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000150}
151
152//----------------------------------------------------------------------
153// ProcessGDBRemote constructor
154//----------------------------------------------------------------------
155ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
156 Process (target, listener),
Chris Lattner24943d22010-06-08 16:52:24 +0000157 m_flags (0),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000158 m_gdb_comm(false),
Chris Lattner24943d22010-06-08 16:52:24 +0000159 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000160 m_last_stop_packet (),
Greg Clayton06709002011-12-06 04:51:14 +0000161 m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
Chris Lattner24943d22010-06-08 16:52:24 +0000162 m_register_info (),
Jim Ingham5a15e692012-02-16 06:50:00 +0000163 m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
Chris Lattner24943d22010-06-08 16:52:24 +0000164 m_async_thread (LLDB_INVALID_HOST_THREAD),
Greg Claytonc1f45872011-02-12 06:28:37 +0000165 m_continue_c_tids (),
166 m_continue_C_tids (),
167 m_continue_s_tids (),
168 m_continue_S_tids (),
Chris Lattner24943d22010-06-08 16:52:24 +0000169 m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000170 m_max_memory_size (512),
Jim Ingham7508e732010-08-09 23:31:02 +0000171 m_waiting_for_attach (false),
Jim Ingham55e01d82011-01-22 01:33:44 +0000172 m_thread_observation_bps()
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
Greg Claytonff39f742011-04-01 00:29:43 +0000174 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
175 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Chris Lattner24943d22010-06-08 16:52:24 +0000176}
177
178//----------------------------------------------------------------------
179// Destructor
180//----------------------------------------------------------------------
181ProcessGDBRemote::~ProcessGDBRemote()
182{
183 // m_mach_process.UnregisterNotificationCallbacks (this);
184 Clear();
Greg Clayton2f57db02011-10-01 00:45:15 +0000185 // We need to call finalize on the process before destroying ourselves
186 // to make sure all of the broadcaster cleanup goes as planned. If we
187 // destruct this class, then Process::~Process() might have problems
188 // trying to fully destroy the broadcaster.
189 Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000190}
191
192//----------------------------------------------------------------------
193// PluginInterface
194//----------------------------------------------------------------------
195const char *
196ProcessGDBRemote::GetPluginName()
197{
198 return "Process debugging plug-in that uses the GDB remote protocol";
199}
200
201const char *
202ProcessGDBRemote::GetShortPluginName()
203{
204 return GetPluginNameStatic();
205}
206
207uint32_t
208ProcessGDBRemote::GetPluginVersion()
209{
210 return 1;
211}
212
213void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000214ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner24943d22010-06-08 16:52:24 +0000215{
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000216 if (!force && m_register_info.GetNumRegisters() > 0)
217 return;
218
219 char packet[128];
Chris Lattner24943d22010-06-08 16:52:24 +0000220 m_register_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000221 uint32_t reg_offset = 0;
222 uint32_t reg_num = 0;
Greg Clayton61d043b2011-03-22 04:00:09 +0000223 StringExtractorGDBRemote::ResponseType response_type;
224 for (response_type = StringExtractorGDBRemote::eResponse;
225 response_type == StringExtractorGDBRemote::eResponse;
226 ++reg_num)
Chris Lattner24943d22010-06-08 16:52:24 +0000227 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000228 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
229 assert (packet_len < sizeof(packet));
Chris Lattner24943d22010-06-08 16:52:24 +0000230 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000231 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false))
Chris Lattner24943d22010-06-08 16:52:24 +0000232 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000233 response_type = response.GetResponseType();
234 if (response_type == StringExtractorGDBRemote::eResponse)
Chris Lattner24943d22010-06-08 16:52:24 +0000235 {
236 std::string name;
237 std::string value;
238 ConstString reg_name;
239 ConstString alt_name;
240 ConstString set_name;
241 RegisterInfo reg_info = { NULL, // Name
242 NULL, // Alt name
243 0, // byte size
244 reg_offset, // offset
245 eEncodingUint, // encoding
246 eFormatHex, // formate
Chris Lattner24943d22010-06-08 16:52:24 +0000247 {
248 LLDB_INVALID_REGNUM, // GCC reg num
249 LLDB_INVALID_REGNUM, // DWARF reg num
250 LLDB_INVALID_REGNUM, // generic reg num
Jason Molenda3a4ea242010-09-10 07:49:16 +0000251 reg_num, // GDB reg num
252 reg_num // native register number
Greg Claytoncd330422012-02-29 19:27:27 +0000253 },
254 NULL,
255 NULL
Chris Lattner24943d22010-06-08 16:52:24 +0000256 };
257
258 while (response.GetNameColonValue(name, value))
259 {
260 if (name.compare("name") == 0)
261 {
262 reg_name.SetCString(value.c_str());
263 }
264 else if (name.compare("alt-name") == 0)
265 {
266 alt_name.SetCString(value.c_str());
267 }
268 else if (name.compare("bitsize") == 0)
269 {
270 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
271 }
272 else if (name.compare("offset") == 0)
273 {
274 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda53d96862010-06-11 23:44:18 +0000275 if (reg_offset != offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000276 {
277 reg_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000278 }
279 }
280 else if (name.compare("encoding") == 0)
281 {
282 if (value.compare("uint") == 0)
283 reg_info.encoding = eEncodingUint;
284 else if (value.compare("sint") == 0)
285 reg_info.encoding = eEncodingSint;
286 else if (value.compare("ieee754") == 0)
287 reg_info.encoding = eEncodingIEEE754;
288 else if (value.compare("vector") == 0)
289 reg_info.encoding = eEncodingVector;
290 }
291 else if (name.compare("format") == 0)
292 {
293 if (value.compare("binary") == 0)
294 reg_info.format = eFormatBinary;
295 else if (value.compare("decimal") == 0)
296 reg_info.format = eFormatDecimal;
297 else if (value.compare("hex") == 0)
298 reg_info.format = eFormatHex;
299 else if (value.compare("float") == 0)
300 reg_info.format = eFormatFloat;
301 else if (value.compare("vector-sint8") == 0)
302 reg_info.format = eFormatVectorOfSInt8;
303 else if (value.compare("vector-uint8") == 0)
304 reg_info.format = eFormatVectorOfUInt8;
305 else if (value.compare("vector-sint16") == 0)
306 reg_info.format = eFormatVectorOfSInt16;
307 else if (value.compare("vector-uint16") == 0)
308 reg_info.format = eFormatVectorOfUInt16;
309 else if (value.compare("vector-sint32") == 0)
310 reg_info.format = eFormatVectorOfSInt32;
311 else if (value.compare("vector-uint32") == 0)
312 reg_info.format = eFormatVectorOfUInt32;
313 else if (value.compare("vector-float32") == 0)
314 reg_info.format = eFormatVectorOfFloat32;
315 else if (value.compare("vector-uint128") == 0)
316 reg_info.format = eFormatVectorOfUInt128;
317 }
318 else if (name.compare("set") == 0)
319 {
320 set_name.SetCString(value.c_str());
321 }
322 else if (name.compare("gcc") == 0)
323 {
324 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
325 }
326 else if (name.compare("dwarf") == 0)
327 {
328 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
329 }
330 else if (name.compare("generic") == 0)
331 {
332 if (value.compare("pc") == 0)
333 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
334 else if (value.compare("sp") == 0)
335 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
336 else if (value.compare("fp") == 0)
337 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
338 else if (value.compare("ra") == 0)
339 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
340 else if (value.compare("flags") == 0)
341 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
Greg Clayton5a269102011-05-22 04:32:55 +0000342 else if (value.find("arg") == 0)
343 {
344 if (value.size() == 4)
345 {
346 switch (value[3])
347 {
348 case '1': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG1; break;
349 case '2': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG2; break;
350 case '3': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG3; break;
351 case '4': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG4; break;
352 case '5': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG5; break;
353 case '6': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG6; break;
354 case '7': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG7; break;
355 case '8': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG8; break;
356 }
357 }
358 }
Chris Lattner24943d22010-06-08 16:52:24 +0000359 }
360 }
361
Jason Molenda53d96862010-06-11 23:44:18 +0000362 reg_info.byte_offset = reg_offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000363 assert (reg_info.byte_size != 0);
364 reg_offset += reg_info.byte_size;
365 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
366 }
367 }
368 else
369 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000370 response_type = StringExtractorGDBRemote::eError;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000371 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000372 }
373 }
374
375 if (reg_num == 0)
376 {
377 // We didn't get anything. See if we are debugging ARM and fill with
378 // a hard coded register set until we can get an updated debugserver
379 // down on the devices.
Jason Molenda428b5502011-10-06 01:45:46 +0000380
381 if (!GetTarget().GetArchitecture().IsValid()
382 && m_gdb_comm.GetHostArchitecture().IsValid()
383 && m_gdb_comm.GetHostArchitecture().GetMachine() == llvm::Triple::arm
384 && m_gdb_comm.GetHostArchitecture().GetTriple().getVendor() == llvm::Triple::Apple)
385 {
Chris Lattner24943d22010-06-08 16:52:24 +0000386 m_register_info.HardcodeARMRegisters();
Jason Molenda428b5502011-10-06 01:45:46 +0000387 }
388 else if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::arm)
389 {
390 m_register_info.HardcodeARMRegisters();
391 }
Chris Lattner24943d22010-06-08 16:52:24 +0000392 }
393 m_register_info.Finalize ();
394}
395
396Error
397ProcessGDBRemote::WillLaunch (Module* module)
398{
399 return WillLaunchOrAttach ();
400}
401
402Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000403ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000404{
405 return WillLaunchOrAttach ();
406}
407
408Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000409ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +0000410{
411 return WillLaunchOrAttach ();
412}
413
414Error
Greg Claytone71e2582011-02-04 01:58:07 +0000415ProcessGDBRemote::DoConnectRemote (const char *remote_url)
416{
417 Error error (WillLaunchOrAttach ());
418
419 if (error.Fail())
420 return error;
421
Greg Clayton180546b2011-04-30 01:09:13 +0000422 error = ConnectToDebugserver (remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +0000423
424 if (error.Fail())
425 return error;
426 StartAsyncThread ();
427
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000428 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytone71e2582011-02-04 01:58:07 +0000429 if (pid == LLDB_INVALID_PROCESS_ID)
430 {
431 // We don't have a valid process ID, so note that we are connected
432 // and could now request to launch or attach, or get remote process
433 // listings...
434 SetPrivateState (eStateConnected);
435 }
436 else
437 {
438 // We have a valid process
439 SetID (pid);
Greg Clayton37f962e2011-08-22 02:49:39 +0000440 GetThreadList();
Greg Clayton261a18b2011-06-02 22:22:38 +0000441 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytone71e2582011-02-04 01:58:07 +0000442 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000443 const StateType state = SetThreadStopInfo (m_last_stop_packet);
Greg Claytone71e2582011-02-04 01:58:07 +0000444 if (state == eStateStopped)
445 {
446 SetPrivateState (state);
447 }
448 else
Greg Claytond9919d32011-12-01 23:28:38 +0000449 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 +0000450 }
451 else
Greg Claytond9919d32011-12-01 23:28:38 +0000452 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 +0000453 }
454 return error;
455}
456
457Error
Chris Lattner24943d22010-06-08 16:52:24 +0000458ProcessGDBRemote::WillLaunchOrAttach ()
459{
460 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000461 m_stdio_communication.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000462 return error;
463}
464
465//----------------------------------------------------------------------
466// Process Control
467//----------------------------------------------------------------------
468Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000469ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000470{
Greg Clayton4b407112010-09-30 21:49:03 +0000471 Error error;
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000472
473 uint32_t launch_flags = launch_info.GetFlags().Get();
474 const char *stdin_path = NULL;
475 const char *stdout_path = NULL;
476 const char *stderr_path = NULL;
477 const char *working_dir = launch_info.GetWorkingDirectory();
478
479 const ProcessLaunchInfo::FileAction *file_action;
480 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
481 if (file_action)
482 {
483 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
484 stdin_path = file_action->GetPath();
485 }
486 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
487 if (file_action)
488 {
489 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
490 stdout_path = file_action->GetPath();
491 }
492 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
493 if (file_action)
494 {
495 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
496 stderr_path = file_action->GetPath();
497 }
498
Chris Lattner24943d22010-06-08 16:52:24 +0000499 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
500 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
501 // ::LogSetLogFile ("/dev/stdout");
Greg Clayton716cefb2011-08-09 05:20:29 +0000502 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000503
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000504 ObjectFile * object_file = exe_module->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000505 if (object_file)
506 {
Chris Lattner24943d22010-06-08 16:52:24 +0000507 char host_port[128];
508 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytone71e2582011-02-04 01:58:07 +0000509 char connect_url[128];
510 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000511
Greg Claytona2f74232011-02-24 22:24:29 +0000512 // Make sure we aren't already connected?
513 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000514 {
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000515 error = StartDebugserverProcess (host_port, launch_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000516 if (error.Fail())
Greg Clayton716cefb2011-08-09 05:20:29 +0000517 {
Johnny Chenc143d622011-08-09 18:56:45 +0000518 if (log)
519 log->Printf("failed to start debugserver process: %s", error.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000520 return error;
Greg Clayton716cefb2011-08-09 05:20:29 +0000521 }
Chris Lattner24943d22010-06-08 16:52:24 +0000522
Greg Claytone71e2582011-02-04 01:58:07 +0000523 error = ConnectToDebugserver (connect_url);
Greg Claytona2f74232011-02-24 22:24:29 +0000524 }
525
526 if (error.Success())
527 {
528 lldb_utility::PseudoTerminal pty;
529 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Claytonafb81862011-03-02 21:34:46 +0000530
531 // If the debugserver is local and we aren't disabling STDIO, lets use
532 // a pseudo terminal to instead of relying on the 'O' packets for stdio
533 // since 'O' packets can really slow down debugging if the inferior
534 // does a lot of output.
Greg Claytonb4747822011-06-24 22:32:10 +0000535 PlatformSP platform_sp (m_target.GetPlatform());
536 if (platform_sp && platform_sp->IsHost() && !disable_stdio)
Greg Claytona2f74232011-02-24 22:24:29 +0000537 {
538 const char *slave_name = NULL;
539 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000540 {
Greg Claytona2f74232011-02-24 22:24:29 +0000541 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
542 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000543 }
Greg Claytona2f74232011-02-24 22:24:29 +0000544 if (stdin_path == NULL)
545 stdin_path = slave_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000546
Greg Claytona2f74232011-02-24 22:24:29 +0000547 if (stdout_path == NULL)
548 stdout_path = slave_name;
549
550 if (stderr_path == NULL)
551 stderr_path = slave_name;
552 }
553
Greg Claytonafb81862011-03-02 21:34:46 +0000554 // Set STDIN to /dev/null if we want STDIO disabled or if either
555 // STDOUT or STDERR have been set to something and STDIN hasn't
556 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000557 stdin_path = "/dev/null";
558
Greg Claytonafb81862011-03-02 21:34:46 +0000559 // Set STDOUT to /dev/null if we want STDIO disabled or if either
560 // STDIN or STDERR have been set to something and STDOUT hasn't
561 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000562 stdout_path = "/dev/null";
563
Greg Claytonafb81862011-03-02 21:34:46 +0000564 // Set STDERR to /dev/null if we want STDIO disabled or if either
565 // STDIN or STDOUT have been set to something and STDERR hasn't
566 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000567 stderr_path = "/dev/null";
568
569 if (stdin_path)
570 m_gdb_comm.SetSTDIN (stdin_path);
571 if (stdout_path)
572 m_gdb_comm.SetSTDOUT (stdout_path);
573 if (stderr_path)
574 m_gdb_comm.SetSTDERR (stderr_path);
575
576 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
577
Greg Claytona4582402011-05-08 04:53:50 +0000578 m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName());
Greg Claytona2f74232011-02-24 22:24:29 +0000579
580 if (working_dir && working_dir[0])
581 {
582 m_gdb_comm.SetWorkingDir (working_dir);
583 }
584
585 // Send the environment and the program + arguments after we connect
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000586 const Args &environment = launch_info.GetEnvironmentEntries();
587 if (environment.GetArgumentCount())
Greg Claytona2f74232011-02-24 22:24:29 +0000588 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000589 size_t num_environment_entries = environment.GetArgumentCount();
590 for (size_t i=0; i<num_environment_entries; ++i)
Greg Clayton960d6a42010-08-03 00:35:52 +0000591 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000592 const char *env_entry = environment.GetArgumentAtIndex(i);
593 if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0)
Greg Claytona2f74232011-02-24 22:24:29 +0000594 break;
Greg Clayton960d6a42010-08-03 00:35:52 +0000595 }
Greg Claytona2f74232011-02-24 22:24:29 +0000596 }
Greg Clayton960d6a42010-08-03 00:35:52 +0000597
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000598 const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000599 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector());
Greg Claytona2f74232011-02-24 22:24:29 +0000600 if (arg_packet_err == 0)
601 {
602 std::string error_str;
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000603 if (m_gdb_comm.GetLaunchSuccess (error_str))
Chris Lattner24943d22010-06-08 16:52:24 +0000604 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000605 SetID (m_gdb_comm.GetCurrentProcessID ());
Chris Lattner24943d22010-06-08 16:52:24 +0000606 }
607 else
608 {
Greg Claytona2f74232011-02-24 22:24:29 +0000609 error.SetErrorString (error_str.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000610 }
Greg Claytona2f74232011-02-24 22:24:29 +0000611 }
612 else
613 {
Greg Clayton9c236732011-10-26 00:56:27 +0000614 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
Greg Claytona2f74232011-02-24 22:24:29 +0000615 }
Greg Clayton7c4fc6e2011-08-10 22:05:39 +0000616
617 m_gdb_comm.SetPacketTimeout (old_packet_timeout);
Chris Lattner24943d22010-06-08 16:52:24 +0000618
Greg Claytona2f74232011-02-24 22:24:29 +0000619 if (GetID() == LLDB_INVALID_PROCESS_ID)
620 {
Johnny Chenc143d622011-08-09 18:56:45 +0000621 if (log)
622 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000623 KillDebugserverProcess ();
624 return error;
625 }
626
Greg Clayton261a18b2011-06-02 22:22:38 +0000627 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false))
Greg Claytona2f74232011-02-24 22:24:29 +0000628 {
Greg Clayton261a18b2011-06-02 22:22:38 +0000629 SetPrivateState (SetThreadStopInfo (m_last_stop_packet));
Greg Claytona2f74232011-02-24 22:24:29 +0000630
631 if (!disable_stdio)
632 {
633 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
Greg Clayton464c6162011-11-17 22:14:31 +0000634 SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor());
Greg Claytona2f74232011-02-24 22:24:29 +0000635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636 }
637 }
Greg Clayton716cefb2011-08-09 05:20:29 +0000638 else
639 {
Johnny Chenc143d622011-08-09 18:56:45 +0000640 if (log)
641 log->Printf("failed to connect to debugserver: %s", error.AsCString());
Greg Clayton716cefb2011-08-09 05:20:29 +0000642 }
Chris Lattner24943d22010-06-08 16:52:24 +0000643 }
644 else
645 {
646 // Set our user ID to an invalid process ID.
647 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000648 error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",
649 exe_module->GetFileSpec().GetFilename().AsCString(),
650 exe_module->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000651 }
Chris Lattner24943d22010-06-08 16:52:24 +0000652 return error;
Greg Clayton4b407112010-09-30 21:49:03 +0000653
Chris Lattner24943d22010-06-08 16:52:24 +0000654}
655
656
657Error
Greg Claytone71e2582011-02-04 01:58:07 +0000658ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner24943d22010-06-08 16:52:24 +0000659{
660 Error error;
661 // Sleep and wait a bit for debugserver to start to listen...
662 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
663 if (conn_ap.get())
664 {
Chris Lattner24943d22010-06-08 16:52:24 +0000665 const uint32_t max_retry_count = 50;
666 uint32_t retry_count = 0;
667 while (!m_gdb_comm.IsConnected())
668 {
Greg Claytone71e2582011-02-04 01:58:07 +0000669 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner24943d22010-06-08 16:52:24 +0000670 {
671 m_gdb_comm.SetConnection (conn_ap.release());
672 break;
673 }
674 retry_count++;
675
676 if (retry_count >= max_retry_count)
677 break;
678
679 usleep (100000);
680 }
681 }
682
683 if (!m_gdb_comm.IsConnected())
684 {
685 if (error.Success())
686 error.SetErrorString("not connected to remote gdb server");
687 return error;
688 }
689
Greg Clayton24bc5d92011-03-30 18:16:51 +0000690 // We always seem to be able to open a connection to a local port
691 // so we need to make sure we can then send data to it. If we can't
692 // then we aren't actually connected to anything, so try and do the
693 // handshake with the remote GDB server and make sure that goes
694 // alright.
695 if (!m_gdb_comm.HandshakeWithServer (NULL))
Chris Lattner24943d22010-06-08 16:52:24 +0000696 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000697 m_gdb_comm.Disconnect();
698 if (error.Success())
699 error.SetErrorString("not connected to remote gdb server");
700 return error;
Chris Lattner24943d22010-06-08 16:52:24 +0000701 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000702 m_gdb_comm.ResetDiscoverableSettings();
703 m_gdb_comm.QueryNoAckModeSupported ();
704 m_gdb_comm.GetThreadSuffixSupported ();
705 m_gdb_comm.GetHostInfo ();
706 m_gdb_comm.GetVContSupported ('c');
Chris Lattner24943d22010-06-08 16:52:24 +0000707 return error;
708}
709
710void
711ProcessGDBRemote::DidLaunchOrAttach ()
712{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000713 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
714 if (log)
715 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton75c703d2011-02-16 04:46:07 +0000716 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000717 {
718 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
719
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000720 BuildDynamicRegisterInfo (false);
Greg Clayton20d338f2010-11-18 05:57:03 +0000721
Chris Lattner24943d22010-06-08 16:52:24 +0000722 // See if the GDB server supports the qHostInfo information
Greg Claytonfc7920f2011-02-09 03:09:55 +0000723
Greg Claytoncb8977d2011-03-23 00:09:55 +0000724 const ArchSpec &gdb_remote_arch = m_gdb_comm.GetHostArchitecture();
725 if (gdb_remote_arch.IsValid())
Greg Claytonfc7920f2011-02-09 03:09:55 +0000726 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000727 ArchSpec &target_arch = GetTarget().GetArchitecture();
728
729 if (target_arch.IsValid())
730 {
731 // If the remote host is ARM and we have apple as the vendor, then
732 // ARM executables and shared libraries can have mixed ARM architectures.
733 // You can have an armv6 executable, and if the host is armv7, then the
734 // system will load the best possible architecture for all shared libraries
735 // it has, so we really need to take the remote host architecture as our
736 // defacto architecture in this case.
737
738 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
739 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
740 {
741 target_arch = gdb_remote_arch;
742 }
743 else
744 {
745 // Fill in what is missing in the triple
746 const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple();
747 llvm::Triple &target_triple = target_arch.GetTriple();
Greg Clayton2f085c62011-05-15 01:25:55 +0000748 if (target_triple.getVendorName().size() == 0)
749 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000750 target_triple.setVendor (remote_triple.getVendor());
751
Greg Clayton2f085c62011-05-15 01:25:55 +0000752 if (target_triple.getOSName().size() == 0)
753 {
754 target_triple.setOS (remote_triple.getOS());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000755
Greg Clayton2f085c62011-05-15 01:25:55 +0000756 if (target_triple.getEnvironmentName().size() == 0)
757 target_triple.setEnvironment (remote_triple.getEnvironment());
758 }
759 }
Greg Claytoncb8977d2011-03-23 00:09:55 +0000760 }
761 }
762 else
763 {
764 // The target doesn't have a valid architecture yet, set it from
765 // the architecture we got from the remote GDB server
766 target_arch = gdb_remote_arch;
767 }
Greg Claytonfc7920f2011-02-09 03:09:55 +0000768 }
Chris Lattner24943d22010-06-08 16:52:24 +0000769 }
770}
771
772void
773ProcessGDBRemote::DidLaunch ()
774{
775 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000776}
777
778Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000779ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000780{
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000781 ProcessAttachInfo attach_info;
782 return DoAttachToProcessWithID(attach_pid, attach_info);
783}
784
785Error
786ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
787{
Chris Lattner24943d22010-06-08 16:52:24 +0000788 Error error;
789 // Clear out and clean up from any current state
790 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000791 if (attach_pid != LLDB_INVALID_PROCESS_ID)
792 {
Greg Claytona2f74232011-02-24 22:24:29 +0000793 // Make sure we aren't already connected?
794 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000795 {
Greg Claytona2f74232011-02-24 22:24:29 +0000796 char host_port[128];
797 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
798 char connect_url[128];
799 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000800
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000801 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000802
803 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000804 {
Greg Claytona2f74232011-02-24 22:24:29 +0000805 const char *error_string = error.AsCString();
806 if (error_string == NULL)
807 error_string = "unable to launch " DEBUGSERVER_BASENAME;
808
809 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000810 }
Greg Claytona2f74232011-02-24 22:24:29 +0000811 else
812 {
813 error = ConnectToDebugserver (connect_url);
814 }
815 }
816
817 if (error.Success())
818 {
819 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +0000820 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", attach_pid);
Greg Clayton489575c2011-11-19 02:11:30 +0000821 SetID (attach_pid);
Greg Claytona2f74232011-02-24 22:24:29 +0000822 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner24943d22010-06-08 16:52:24 +0000823 }
824 }
Chris Lattner24943d22010-06-08 16:52:24 +0000825 return error;
826}
827
828size_t
829ProcessGDBRemote::AttachInputReaderCallback
830(
831 void *baton,
832 InputReader *reader,
833 lldb::InputReaderAction notification,
834 const char *bytes,
835 size_t bytes_len
836)
837{
838 if (notification == eInputReaderGotToken)
839 {
840 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
841 if (gdb_process->m_waiting_for_attach)
842 gdb_process->m_waiting_for_attach = false;
843 reader->SetIsDone(true);
844 return 1;
845 }
846 return 0;
847}
848
849Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000850ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000851{
852 Error error;
853 // Clear out and clean up from any current state
854 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000855
Chris Lattner24943d22010-06-08 16:52:24 +0000856 if (process_name && process_name[0])
857 {
Greg Claytona2f74232011-02-24 22:24:29 +0000858 // Make sure we aren't already connected?
859 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000860 {
Greg Claytona2f74232011-02-24 22:24:29 +0000861 char host_port[128];
862 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
863 char connect_url[128];
864 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
865
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000866 error = StartDebugserverProcess (host_port, attach_info);
Greg Claytona2f74232011-02-24 22:24:29 +0000867 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000868 {
Greg Claytona2f74232011-02-24 22:24:29 +0000869 const char *error_string = error.AsCString();
870 if (error_string == NULL)
871 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner24943d22010-06-08 16:52:24 +0000872
Greg Claytona2f74232011-02-24 22:24:29 +0000873 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000874 }
Greg Claytona2f74232011-02-24 22:24:29 +0000875 else
876 {
877 error = ConnectToDebugserver (connect_url);
878 }
879 }
880
881 if (error.Success())
882 {
883 StreamString packet;
884
885 if (wait_for_launch)
886 packet.PutCString("vAttachWait");
887 else
888 packet.PutCString("vAttachName");
889 packet.PutChar(';');
890 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
891
892 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
893
Chris Lattner24943d22010-06-08 16:52:24 +0000894 }
895 }
Chris Lattner24943d22010-06-08 16:52:24 +0000896 return error;
897}
898
Chris Lattner24943d22010-06-08 16:52:24 +0000899
900void
901ProcessGDBRemote::DidAttach ()
902{
Greg Claytone71e2582011-02-04 01:58:07 +0000903 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000904}
905
906Error
907ProcessGDBRemote::WillResume ()
908{
Greg Claytonc1f45872011-02-12 06:28:37 +0000909 m_continue_c_tids.clear();
910 m_continue_C_tids.clear();
911 m_continue_s_tids.clear();
912 m_continue_S_tids.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000913 return Error();
914}
915
916Error
917ProcessGDBRemote::DoResume ()
918{
Jim Ingham3ae449a2010-11-17 02:32:00 +0000919 Error error;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000920 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
921 if (log)
922 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytonb749a262010-12-03 06:02:24 +0000923
924 Listener listener ("gdb-remote.resume-packet-sent");
925 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
926 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000927 StreamString continue_packet;
928 bool continue_packet_error = false;
929 if (m_gdb_comm.HasAnyVContSupport ())
930 {
931 continue_packet.PutCString ("vCont");
932
933 if (!m_continue_c_tids.empty())
934 {
935 if (m_gdb_comm.GetVContSupported ('c'))
936 {
937 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 +0000938 continue_packet.Printf(";c:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000939 }
940 else
941 continue_packet_error = true;
942 }
943
944 if (!continue_packet_error && !m_continue_C_tids.empty())
945 {
946 if (m_gdb_comm.GetVContSupported ('C'))
947 {
948 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 +0000949 continue_packet.Printf(";C%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000950 }
951 else
952 continue_packet_error = true;
953 }
Greg Claytonb749a262010-12-03 06:02:24 +0000954
Greg Claytonc1f45872011-02-12 06:28:37 +0000955 if (!continue_packet_error && !m_continue_s_tids.empty())
956 {
957 if (m_gdb_comm.GetVContSupported ('s'))
958 {
959 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 +0000960 continue_packet.Printf(";s:%4.4llx", *t_pos);
Greg Claytonc1f45872011-02-12 06:28:37 +0000961 }
962 else
963 continue_packet_error = true;
964 }
965
966 if (!continue_packet_error && !m_continue_S_tids.empty())
967 {
968 if (m_gdb_comm.GetVContSupported ('S'))
969 {
970 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 +0000971 continue_packet.Printf(";S%2.2x:%4.4llx", s_pos->second, s_pos->first);
Greg Claytonc1f45872011-02-12 06:28:37 +0000972 }
973 else
974 continue_packet_error = true;
975 }
976
977 if (continue_packet_error)
978 continue_packet.GetString().clear();
979 }
980 else
981 continue_packet_error = true;
982
983 if (continue_packet_error)
984 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000985 // Either no vCont support, or we tried to use part of the vCont
986 // packet that wasn't supported by the remote GDB server.
987 // We need to try and make a simple packet that can do our continue
988 const size_t num_threads = GetThreadList().GetSize();
989 const size_t num_continue_c_tids = m_continue_c_tids.size();
990 const size_t num_continue_C_tids = m_continue_C_tids.size();
991 const size_t num_continue_s_tids = m_continue_s_tids.size();
992 const size_t num_continue_S_tids = m_continue_S_tids.size();
993 if (num_continue_c_tids > 0)
994 {
995 if (num_continue_c_tids == num_threads)
996 {
997 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +0000998 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +0000999 continue_packet.PutChar ('c');
1000 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001001 }
1002 else if (num_continue_c_tids == 1 &&
1003 num_continue_C_tids == 0 &&
1004 num_continue_s_tids == 0 &&
1005 num_continue_S_tids == 0 )
1006 {
1007 // Only one thread is continuing
Greg Claytonb72d0f02011-04-12 05:54:46 +00001008 m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +00001009 continue_packet.PutChar ('c');
Greg Claytonde1dd812011-06-24 03:21:43 +00001010 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001011 }
1012 }
1013
Greg Claytonde1dd812011-06-24 03:21:43 +00001014 if (continue_packet_error && num_continue_C_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +00001015 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001016 if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&
1017 num_continue_C_tids > 0 &&
1018 num_continue_s_tids == 0 &&
1019 num_continue_S_tids == 0 )
Greg Claytonc1f45872011-02-12 06:28:37 +00001020 {
1021 const int continue_signo = m_continue_C_tids.front().second;
Greg Claytonde1dd812011-06-24 03:21:43 +00001022 // Only one thread is continuing
Greg Claytonc1f45872011-02-12 06:28:37 +00001023 if (num_continue_C_tids > 1)
1024 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001025 // More that one thread with a signal, yet we don't have
1026 // vCont support and we are being asked to resume each
1027 // thread with a signal, we need to make sure they are
1028 // all the same signal, or we can't issue the continue
1029 // accurately with the current support...
1030 if (num_continue_C_tids > 1)
Greg Claytonc1f45872011-02-12 06:28:37 +00001031 {
Greg Claytonde1dd812011-06-24 03:21:43 +00001032 continue_packet_error = false;
1033 for (size_t i=1; i<m_continue_C_tids.size(); ++i)
1034 {
1035 if (m_continue_C_tids[i].second != continue_signo)
1036 continue_packet_error = true;
1037 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001038 }
Greg Claytonde1dd812011-06-24 03:21:43 +00001039 if (!continue_packet_error)
1040 m_gdb_comm.SetCurrentThreadForRun (-1);
1041 }
1042 else
1043 {
1044 // Set the continue thread ID
1045 continue_packet_error = false;
1046 m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001047 }
1048 if (!continue_packet_error)
1049 {
1050 // Add threads continuing with the same signo...
Greg Claytonc1f45872011-02-12 06:28:37 +00001051 continue_packet.Printf("C%2.2x", continue_signo);
1052 }
1053 }
Greg Claytonc1f45872011-02-12 06:28:37 +00001054 }
1055
Greg Claytonde1dd812011-06-24 03:21:43 +00001056 if (continue_packet_error && num_continue_s_tids > 0)
Greg Claytonc1f45872011-02-12 06:28:37 +00001057 {
1058 if (num_continue_s_tids == num_threads)
1059 {
1060 // All threads are resuming...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001061 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonde1dd812011-06-24 03:21:43 +00001062 continue_packet.PutChar ('s');
1063 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001064 }
1065 else if (num_continue_c_tids == 0 &&
1066 num_continue_C_tids == 0 &&
1067 num_continue_s_tids == 1 &&
1068 num_continue_S_tids == 0 )
1069 {
1070 // Only one thread is stepping
Greg Claytonb72d0f02011-04-12 05:54:46 +00001071 m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front());
Greg Claytonc1f45872011-02-12 06:28:37 +00001072 continue_packet.PutChar ('s');
Greg Claytonde1dd812011-06-24 03:21:43 +00001073 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001074 }
1075 }
1076
1077 if (!continue_packet_error && num_continue_S_tids > 0)
1078 {
1079 if (num_continue_S_tids == num_threads)
1080 {
1081 const int step_signo = m_continue_S_tids.front().second;
1082 // Are all threads trying to step with the same signal?
Greg Claytonde1dd812011-06-24 03:21:43 +00001083 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001084 if (num_continue_S_tids > 1)
1085 {
1086 for (size_t i=1; i<num_threads; ++i)
1087 {
1088 if (m_continue_S_tids[i].second != step_signo)
1089 continue_packet_error = true;
1090 }
1091 }
1092 if (!continue_packet_error)
1093 {
1094 // Add threads stepping with the same signo...
Greg Claytonb72d0f02011-04-12 05:54:46 +00001095 m_gdb_comm.SetCurrentThreadForRun (-1);
Greg Claytonc1f45872011-02-12 06:28:37 +00001096 continue_packet.Printf("S%2.2x", step_signo);
1097 }
1098 }
1099 else if (num_continue_c_tids == 0 &&
1100 num_continue_C_tids == 0 &&
1101 num_continue_s_tids == 0 &&
1102 num_continue_S_tids == 1 )
1103 {
1104 // Only one thread is stepping with signal
Greg Claytonb72d0f02011-04-12 05:54:46 +00001105 m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first);
Greg Claytonc1f45872011-02-12 06:28:37 +00001106 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
Greg Claytonde1dd812011-06-24 03:21:43 +00001107 continue_packet_error = false;
Greg Claytonc1f45872011-02-12 06:28:37 +00001108 }
1109 }
1110 }
1111
1112 if (continue_packet_error)
1113 {
1114 error.SetErrorString ("can't make continue packet for this resume");
1115 }
1116 else
1117 {
1118 EventSP event_sp;
1119 TimeValue timeout;
1120 timeout = TimeValue::Now();
1121 timeout.OffsetWithSeconds (5);
1122 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1123
1124 if (listener.WaitForEvent (&timeout, event_sp) == false)
1125 error.SetErrorString("Resume timed out.");
1126 }
Greg Claytonb749a262010-12-03 06:02:24 +00001127 }
1128
Jim Ingham3ae449a2010-11-17 02:32:00 +00001129 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001130}
1131
Chris Lattner24943d22010-06-08 16:52:24 +00001132uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +00001133ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Chris Lattner24943d22010-06-08 16:52:24 +00001134{
1135 // locker will keep a mutex locked until it goes out of scope
Greg Claytone005f2c2010-11-06 01:53:30 +00001136 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001137 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +00001138 log->Printf ("ProcessGDBRemote::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton37f962e2011-08-22 02:49:39 +00001139 // Update the thread list's stop id immediately so we don't recurse into this function.
Chris Lattner24943d22010-06-08 16:52:24 +00001140
Greg Clayton37f962e2011-08-22 02:49:39 +00001141 std::vector<lldb::tid_t> thread_ids;
1142 bool sequence_mutex_unavailable = false;
1143 const size_t num_thread_ids = m_gdb_comm.GetCurrentThreadIDs (thread_ids, sequence_mutex_unavailable);
1144 if (num_thread_ids > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001145 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001146 for (size_t i=0; i<num_thread_ids; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001147 {
Greg Clayton37f962e2011-08-22 02:49:39 +00001148 tid_t tid = thread_ids[i];
1149 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
1150 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +00001151 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +00001152 new_thread_list.AddThread(thread_sp);
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001153 }
Chris Lattner24943d22010-06-08 16:52:24 +00001154 }
Greg Clayton37f962e2011-08-22 02:49:39 +00001155
1156 if (sequence_mutex_unavailable == false)
1157 SetThreadStopInfo (m_last_stop_packet);
1158 return new_thread_list.GetSize(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001159}
1160
1161
1162StateType
1163ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1164{
Greg Clayton261a18b2011-06-02 22:22:38 +00001165 stop_packet.SetFilePos (0);
Chris Lattner24943d22010-06-08 16:52:24 +00001166 const char stop_type = stop_packet.GetChar();
1167 switch (stop_type)
1168 {
1169 case 'T':
1170 case 'S':
1171 {
Greg Claytonc3c46612011-02-15 00:19:15 +00001172 if (GetStopID() == 0)
1173 {
1174 // Our first stop, make sure we have a process ID, and also make
1175 // sure we know about our registers
1176 if (GetID() == LLDB_INVALID_PROCESS_ID)
1177 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001178 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonc3c46612011-02-15 00:19:15 +00001179 if (pid != LLDB_INVALID_PROCESS_ID)
1180 SetID (pid);
1181 }
1182 BuildDynamicRegisterInfo (true);
1183 }
Chris Lattner24943d22010-06-08 16:52:24 +00001184 // Stop with signal and thread info
1185 const uint8_t signo = stop_packet.GetHexU8();
1186 std::string name;
1187 std::string value;
1188 std::string thread_name;
Greg Clayton65611552011-06-04 01:26:29 +00001189 std::string reason;
1190 std::string description;
Chris Lattner24943d22010-06-08 16:52:24 +00001191 uint32_t exc_type = 0;
Greg Clayton7661a982010-07-23 16:45:51 +00001192 std::vector<addr_t> exc_data;
Chris Lattner24943d22010-06-08 16:52:24 +00001193 uint32_t tid = LLDB_INVALID_THREAD_ID;
1194 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
1195 uint32_t exc_data_count = 0;
Greg Claytona875b642011-01-09 21:07:35 +00001196 ThreadSP thread_sp;
1197
Chris Lattner24943d22010-06-08 16:52:24 +00001198 while (stop_packet.GetNameColonValue(name, value))
1199 {
1200 if (name.compare("metype") == 0)
1201 {
1202 // exception type in big endian hex
1203 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1204 }
1205 else if (name.compare("mecount") == 0)
1206 {
1207 // exception count in big endian hex
1208 exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16);
1209 }
1210 else if (name.compare("medata") == 0)
1211 {
1212 // exception data in big endian hex
1213 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1214 }
1215 else if (name.compare("thread") == 0)
1216 {
1217 // thread in big endian hex
1218 tid = Args::StringToUInt32 (value.c_str(), 0, 16);
Greg Claytonffa43a62011-11-17 04:46:02 +00001219 // m_thread_list does have its own mutex, but we need to
1220 // hold onto the mutex between the call to m_thread_list.FindThreadByID(...)
1221 // and the m_thread_list.AddThread(...) so it doesn't change on us
Greg Claytonc3c46612011-02-15 00:19:15 +00001222 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytona875b642011-01-09 21:07:35 +00001223 thread_sp = m_thread_list.FindThreadByID(tid, false);
Greg Claytonc3c46612011-02-15 00:19:15 +00001224 if (!thread_sp)
1225 {
1226 // Create the thread if we need to
Greg Claytonf4124de2012-02-21 00:09:25 +00001227 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Claytonc3c46612011-02-15 00:19:15 +00001228 m_thread_list.AddThread(thread_sp);
1229 }
Chris Lattner24943d22010-06-08 16:52:24 +00001230 }
Greg Clayton4862fa22011-01-08 03:17:57 +00001231 else if (name.compare("hexname") == 0)
1232 {
1233 StringExtractor name_extractor;
1234 // Swap "value" over into "name_extractor"
1235 name_extractor.GetStringRef().swap(value);
1236 // Now convert the HEX bytes into a string value
1237 name_extractor.GetHexByteString (value);
1238 thread_name.swap (value);
1239 }
Chris Lattner24943d22010-06-08 16:52:24 +00001240 else if (name.compare("name") == 0)
1241 {
1242 thread_name.swap (value);
1243 }
Greg Clayton0a7f75f2010-09-09 06:32:46 +00001244 else if (name.compare("qaddr") == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001245 {
1246 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1247 }
Greg Clayton65611552011-06-04 01:26:29 +00001248 else if (name.compare("reason") == 0)
1249 {
1250 reason.swap(value);
1251 }
1252 else if (name.compare("description") == 0)
1253 {
1254 StringExtractor desc_extractor;
1255 // Swap "value" over into "name_extractor"
1256 desc_extractor.GetStringRef().swap(value);
1257 // Now convert the HEX bytes into a string value
1258 desc_extractor.GetHexByteString (thread_name);
1259 }
Greg Claytona875b642011-01-09 21:07:35 +00001260 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1261 {
1262 // We have a register number that contains an expedited
1263 // register value. Lets supply this register to our thread
1264 // so it won't have to go and read it.
1265 if (thread_sp)
1266 {
1267 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1268
1269 if (reg != UINT32_MAX)
1270 {
1271 StringExtractor reg_value_extractor;
1272 // Swap "value" over into "reg_value_extractor"
1273 reg_value_extractor.GetStringRef().swap(value);
Greg Claytonc3c46612011-02-15 00:19:15 +00001274 if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor))
1275 {
1276 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1277 name.c_str(),
1278 reg,
1279 reg,
1280 reg_value_extractor.GetStringRef().c_str(),
1281 stop_packet.GetStringRef().c_str());
1282 }
Greg Claytona875b642011-01-09 21:07:35 +00001283 }
1284 }
1285 }
Chris Lattner24943d22010-06-08 16:52:24 +00001286 }
Chris Lattner24943d22010-06-08 16:52:24 +00001287
1288 if (thread_sp)
1289 {
1290 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1291
1292 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Ingham9082c8a2011-01-28 02:23:12 +00001293 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001294 if (exc_type != 0)
1295 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001296 const size_t exc_data_size = exc_data.size();
Greg Clayton643ee732010-08-04 01:40:35 +00001297
1298 gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1299 exc_type,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001300 exc_data_size,
1301 exc_data_size >= 1 ? exc_data[0] : 0,
Johnny Chen36889ad2011-09-17 01:05:03 +00001302 exc_data_size >= 2 ? exc_data[1] : 0,
1303 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner24943d22010-06-08 16:52:24 +00001304 }
Greg Clayton65611552011-06-04 01:26:29 +00001305 else
Chris Lattner24943d22010-06-08 16:52:24 +00001306 {
Greg Clayton65611552011-06-04 01:26:29 +00001307 bool handled = false;
1308 if (!reason.empty())
1309 {
1310 if (reason.compare("trace") == 0)
1311 {
1312 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1313 handled = true;
1314 }
1315 else if (reason.compare("breakpoint") == 0)
1316 {
1317 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001318 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001319 if (bp_site_sp)
1320 {
1321 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1322 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1323 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1324 if (bp_site_sp->ValidForThisThread (gdb_thread))
1325 {
1326 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1327 handled = true;
1328 }
1329 }
1330
1331 if (!handled)
1332 {
1333 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1334 }
1335 }
1336 else if (reason.compare("trap") == 0)
1337 {
1338 // Let the trap just use the standard signal stop reason below...
1339 }
1340 else if (reason.compare("watchpoint") == 0)
1341 {
1342 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1343 // TODO: locate the watchpoint somehow...
1344 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
1345 handled = true;
1346 }
1347 else if (reason.compare("exception") == 0)
1348 {
1349 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
1350 handled = true;
1351 }
1352 }
1353
1354 if (signo)
1355 {
1356 if (signo == SIGTRAP)
1357 {
1358 // Currently we are going to assume SIGTRAP means we are either
1359 // hitting a breakpoint or hardware single stepping.
1360 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001361 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001362 if (bp_site_sp)
1363 {
1364 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1365 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1366 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1367 if (bp_site_sp->ValidForThisThread (gdb_thread))
1368 {
1369 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1370 handled = true;
1371 }
1372 }
1373 if (!handled)
1374 {
1375 // TODO: check for breakpoint or trap opcode in case there is a hard
1376 // coded software trap
1377 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1378 handled = true;
1379 }
1380 }
1381 if (!handled)
Greg Clayton37f962e2011-08-22 02:49:39 +00001382 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Chris Lattner24943d22010-06-08 16:52:24 +00001383 }
1384 else
1385 {
Greg Clayton643ee732010-08-04 01:40:35 +00001386 StopInfoSP invalid_stop_info_sp;
1387 gdb_thread->SetStopInfo (invalid_stop_info_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001388 }
Greg Clayton65611552011-06-04 01:26:29 +00001389
1390 if (!description.empty())
1391 {
1392 lldb::StopInfoSP stop_info_sp (gdb_thread->GetStopInfo ());
1393 if (stop_info_sp)
1394 {
1395 stop_info_sp->SetDescription (description.c_str());
Greg Clayton153ccd72011-08-10 02:10:13 +00001396 }
Greg Clayton65611552011-06-04 01:26:29 +00001397 else
1398 {
1399 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
1400 }
1401 }
1402 }
Chris Lattner24943d22010-06-08 16:52:24 +00001403 }
1404 return eStateStopped;
1405 }
1406 break;
1407
1408 case 'W':
1409 // process exited
1410 return eStateExited;
1411
1412 default:
1413 break;
1414 }
1415 return eStateInvalid;
1416}
1417
1418void
1419ProcessGDBRemote::RefreshStateAfterStop ()
1420{
Chris Lattner24943d22010-06-08 16:52:24 +00001421 // Let all threads recover from stopping and do any clean up based
1422 // on the previous thread state (if any).
1423 m_thread_list.RefreshStateAfterStop();
Greg Clayton261a18b2011-06-02 22:22:38 +00001424 SetThreadStopInfo (m_last_stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +00001425}
1426
1427Error
Jim Ingham3ae449a2010-11-17 02:32:00 +00001428ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner24943d22010-06-08 16:52:24 +00001429{
1430 Error error;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001431
Greg Claytona4881d02011-01-22 07:12:45 +00001432 bool timed_out = false;
1433 Mutex::Locker locker;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001434
1435 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton20d338f2010-11-18 05:57:03 +00001436 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001437 // We are being asked to halt during an attach. We need to just close
1438 // our file handle and debugserver will go away, and we can be done...
1439 m_gdb_comm.Disconnect();
Greg Clayton20d338f2010-11-18 05:57:03 +00001440 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001441 else
1442 {
Greg Clayton05e4d972012-03-29 01:55:41 +00001443 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001444 {
1445 if (timed_out)
1446 error.SetErrorString("timed out sending interrupt packet");
1447 else
1448 error.SetErrorString("unknown error sending interrupt packet");
1449 }
Greg Clayton05e4d972012-03-29 01:55:41 +00001450
1451 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001452 }
Chris Lattner24943d22010-06-08 16:52:24 +00001453 return error;
1454}
1455
1456Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001457ProcessGDBRemote::InterruptIfRunning
1458(
1459 bool discard_thread_plans,
1460 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +00001461 EventSP &stop_event_sp
1462)
Chris Lattner24943d22010-06-08 16:52:24 +00001463{
1464 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001465
Greg Clayton2860ba92011-01-23 19:58:49 +00001466 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1467
Greg Clayton68ca8232011-01-25 02:58:48 +00001468 bool paused_private_state_thread = false;
Greg Clayton2860ba92011-01-23 19:58:49 +00001469 const bool is_running = m_gdb_comm.IsRunning();
1470 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00001471 log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
Greg Clayton2860ba92011-01-23 19:58:49 +00001472 discard_thread_plans,
Greg Clayton68ca8232011-01-25 02:58:48 +00001473 catch_stop_event,
Greg Clayton2860ba92011-01-23 19:58:49 +00001474 is_running);
1475
Greg Clayton2860ba92011-01-23 19:58:49 +00001476 if (discard_thread_plans)
1477 {
1478 if (log)
1479 log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans");
1480 m_thread_list.DiscardThreadPlans();
1481 }
1482 if (is_running)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001483 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001484 if (catch_stop_event)
1485 {
1486 if (log)
1487 log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread");
1488 PausePrivateStateThread();
1489 paused_private_state_thread = true;
1490 }
1491
Greg Clayton4fb400f2010-09-27 21:07:38 +00001492 bool timed_out = false;
1493 Mutex::Locker locker;
Greg Clayton72e1c782011-01-22 23:43:18 +00001494
Greg Clayton05e4d972012-03-29 01:55:41 +00001495 if (!m_gdb_comm.SendInterrupt (locker, 1, timed_out))
Greg Clayton4fb400f2010-09-27 21:07:38 +00001496 {
1497 if (timed_out)
1498 error.SetErrorString("timed out sending interrupt packet");
1499 else
1500 error.SetErrorString("unknown error sending interrupt packet");
Greg Clayton68ca8232011-01-25 02:58:48 +00001501 if (paused_private_state_thread)
Greg Clayton72e1c782011-01-22 23:43:18 +00001502 ResumePrivateStateThread();
1503 return error;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001504 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001505
Greg Clayton72e1c782011-01-22 23:43:18 +00001506 if (catch_stop_event)
1507 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001508 // LISTEN HERE
Greg Clayton72e1c782011-01-22 23:43:18 +00001509 TimeValue timeout_time;
1510 timeout_time = TimeValue::Now();
Greg Clayton68ca8232011-01-25 02:58:48 +00001511 timeout_time.OffsetWithSeconds(5);
1512 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
Greg Clayton2860ba92011-01-23 19:58:49 +00001513
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001514 timed_out = state == eStateInvalid;
Greg Clayton2860ba92011-01-23 19:58:49 +00001515 if (log)
1516 log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001517
Greg Clayton2860ba92011-01-23 19:58:49 +00001518 if (timed_out)
Greg Clayton72e1c782011-01-22 23:43:18 +00001519 error.SetErrorString("unable to verify target stopped");
1520 }
1521
Greg Clayton68ca8232011-01-25 02:58:48 +00001522 if (paused_private_state_thread)
Greg Clayton2860ba92011-01-23 19:58:49 +00001523 {
1524 if (log)
1525 log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread");
Greg Clayton72e1c782011-01-22 23:43:18 +00001526 ResumePrivateStateThread();
Greg Clayton2860ba92011-01-23 19:58:49 +00001527 }
Greg Clayton4fb400f2010-09-27 21:07:38 +00001528 }
Chris Lattner24943d22010-06-08 16:52:24 +00001529 return error;
1530}
1531
Greg Clayton4fb400f2010-09-27 21:07:38 +00001532Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001533ProcessGDBRemote::WillDetach ()
1534{
Greg Clayton2860ba92011-01-23 19:58:49 +00001535 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1536 if (log)
1537 log->Printf ("ProcessGDBRemote::WillDetach()");
1538
Greg Clayton72e1c782011-01-22 23:43:18 +00001539 bool discard_thread_plans = true;
1540 bool catch_stop_event = true;
Greg Clayton72e1c782011-01-22 23:43:18 +00001541 EventSP event_sp;
Greg Clayton68ca8232011-01-25 02:58:48 +00001542 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
Greg Clayton72e1c782011-01-22 23:43:18 +00001543}
1544
1545Error
Greg Clayton4fb400f2010-09-27 21:07:38 +00001546ProcessGDBRemote::DoDetach()
1547{
1548 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001549 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton4fb400f2010-09-27 21:07:38 +00001550 if (log)
1551 log->Printf ("ProcessGDBRemote::DoDetach()");
1552
1553 DisableAllBreakpointSites ();
1554
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001555 m_thread_list.DiscardThreadPlans();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001556
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001557 size_t response_size = m_gdb_comm.SendPacket ("D", 1);
1558 if (log)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001559 {
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001560 if (response_size)
1561 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1562 else
1563 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed");
Greg Clayton4fb400f2010-09-27 21:07:38 +00001564 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001565 // Sleep for one second to let the process get all detached...
Greg Clayton4fb400f2010-09-27 21:07:38 +00001566 StopAsyncThread ();
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001567
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001568 SetPrivateState (eStateDetached);
1569 ResumePrivateStateThread();
1570
1571 //KillDebugserverProcess ();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001572 return error;
1573}
Chris Lattner24943d22010-06-08 16:52:24 +00001574
1575Error
1576ProcessGDBRemote::DoDestroy ()
1577{
1578 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001579 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001580 if (log)
1581 log->Printf ("ProcessGDBRemote::DoDestroy()");
1582
1583 // Interrupt if our inferior is running...
Greg Claytona4881d02011-01-22 07:12:45 +00001584 if (m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +00001585 {
Jim Ingham8226e942011-10-28 01:11:35 +00001586 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton72e1c782011-01-22 23:43:18 +00001587 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001588
1589 StringExtractorGDBRemote response;
1590 bool send_async = true;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001591 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001592 {
1593 char packet_cmd = response.GetChar(0);
1594
1595 if (packet_cmd == 'W' || packet_cmd == 'X')
1596 {
Greg Clayton06709002011-12-06 04:51:14 +00001597 SetLastStopPacket (response);
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001598 SetExitStatus(response.GetHexU8(), NULL);
1599 }
1600 }
1601 else
1602 {
1603 SetExitStatus(SIGABRT, NULL);
1604 //error.SetErrorString("kill packet failed");
1605 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001606 }
1607 }
Chris Lattner24943d22010-06-08 16:52:24 +00001608 StopAsyncThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001609 KillDebugserverProcess ();
1610 return error;
1611}
1612
Chris Lattner24943d22010-06-08 16:52:24 +00001613//------------------------------------------------------------------
1614// Process Queries
1615//------------------------------------------------------------------
1616
1617bool
1618ProcessGDBRemote::IsAlive ()
1619{
Greg Clayton58e844b2010-12-08 05:08:21 +00001620 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner24943d22010-06-08 16:52:24 +00001621}
1622
1623addr_t
1624ProcessGDBRemote::GetImageInfoAddress()
1625{
1626 if (!m_gdb_comm.IsRunning())
1627 {
1628 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001629 if (m_gdb_comm.SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
Chris Lattner24943d22010-06-08 16:52:24 +00001630 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001631 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001632 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1633 }
1634 }
1635 return LLDB_INVALID_ADDRESS;
1636}
1637
Chris Lattner24943d22010-06-08 16:52:24 +00001638//------------------------------------------------------------------
1639// Process Memory
1640//------------------------------------------------------------------
1641size_t
1642ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1643{
1644 if (size > m_max_memory_size)
1645 {
1646 // Keep memory read sizes down to a sane limit. This function will be
1647 // called multiple times in order to complete the task by
1648 // lldb_private::Process so it is ok to do this.
1649 size = m_max_memory_size;
1650 }
1651
1652 char packet[64];
1653 const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size);
1654 assert (packet_len + 1 < sizeof(packet));
1655 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001656 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001657 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001658 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001659 {
1660 error.Clear();
1661 return response.GetHexBytes(buf, size, '\xdd');
1662 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001663 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001664 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001665 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001666 error.SetErrorStringWithFormat("'%s' packet unsupported", packet);
1667 else
1668 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str());
1669 }
1670 else
1671 {
1672 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet);
1673 }
1674 return 0;
1675}
1676
1677size_t
1678ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1679{
Greg Claytonc8bc1c32011-05-16 02:35:02 +00001680 if (size > m_max_memory_size)
1681 {
1682 // Keep memory read sizes down to a sane limit. This function will be
1683 // called multiple times in order to complete the task by
1684 // lldb_private::Process so it is ok to do this.
1685 size = m_max_memory_size;
1686 }
1687
Chris Lattner24943d22010-06-08 16:52:24 +00001688 StreamString packet;
1689 packet.Printf("M%llx,%zx:", addr, size);
Greg Claytoncd548032011-02-01 01:31:41 +00001690 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00001691 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001692 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001693 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001694 if (response.IsOKResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001695 {
1696 error.Clear();
1697 return size;
1698 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001699 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001700 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001701 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001702 error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str());
1703 else
1704 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
1705 }
1706 else
1707 {
1708 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str());
1709 }
1710 return 0;
1711}
1712
1713lldb::addr_t
1714ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
1715{
Greg Clayton989816b2011-05-14 01:50:35 +00001716 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
1717
Greg Clayton2f085c62011-05-15 01:25:55 +00001718 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton989816b2011-05-14 01:50:35 +00001719 switch (supported)
1720 {
1721 case eLazyBoolCalculate:
1722 case eLazyBoolYes:
1723 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
1724 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
1725 return allocated_addr;
1726
1727 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001728 // Call mmap() to create memory in the inferior..
1729 unsigned prot = 0;
1730 if (permissions & lldb::ePermissionsReadable)
1731 prot |= eMmapProtRead;
1732 if (permissions & lldb::ePermissionsWritable)
1733 prot |= eMmapProtWrite;
1734 if (permissions & lldb::ePermissionsExecutable)
1735 prot |= eMmapProtExec;
Greg Clayton989816b2011-05-14 01:50:35 +00001736
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001737 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
1738 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
1739 m_addr_to_mmap_size[allocated_addr] = size;
1740 else
1741 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton989816b2011-05-14 01:50:35 +00001742 break;
1743 }
1744
Chris Lattner24943d22010-06-08 16:52:24 +00001745 if (allocated_addr == LLDB_INVALID_ADDRESS)
Greg Clayton613b8732011-05-17 03:37:42 +00001746 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
Chris Lattner24943d22010-06-08 16:52:24 +00001747 else
1748 error.Clear();
1749 return allocated_addr;
1750}
1751
1752Error
Greg Claytona9385532011-11-18 07:03:08 +00001753ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
1754 MemoryRegionInfo &region_info)
1755{
1756
1757 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
1758 return error;
1759}
1760
1761Error
Chris Lattner24943d22010-06-08 16:52:24 +00001762ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
1763{
1764 Error error;
Greg Clayton2f085c62011-05-15 01:25:55 +00001765 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
1766
1767 switch (supported)
1768 {
1769 case eLazyBoolCalculate:
1770 // We should never be deallocating memory without allocating memory
1771 // first so we should never get eLazyBoolCalculate
1772 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
1773 break;
1774
1775 case eLazyBoolYes:
1776 if (!m_gdb_comm.DeallocateMemory (addr))
1777 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
1778 break;
1779
1780 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001781 // Call munmap() to deallocate memory in the inferior..
Greg Clayton2f085c62011-05-15 01:25:55 +00001782 {
1783 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001784 if (pos != m_addr_to_mmap_size.end() &&
1785 InferiorCallMunmap(this, addr, pos->second))
1786 m_addr_to_mmap_size.erase (pos);
1787 else
1788 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
Greg Clayton2f085c62011-05-15 01:25:55 +00001789 }
1790 break;
1791 }
1792
Chris Lattner24943d22010-06-08 16:52:24 +00001793 return error;
1794}
1795
1796
1797//------------------------------------------------------------------
1798// Process STDIO
1799//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001800size_t
1801ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
1802{
1803 if (m_stdio_communication.IsConnected())
1804 {
1805 ConnectionStatus status;
1806 m_stdio_communication.Write(src, src_len, status, NULL);
1807 }
1808 return 0;
1809}
1810
1811Error
1812ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site)
1813{
1814 Error error;
1815 assert (bp_site != NULL);
1816
Greg Claytone005f2c2010-11-06 01:53:30 +00001817 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001818 user_id_t site_id = bp_site->GetID();
1819 const addr_t addr = bp_site->GetLoadAddress();
1820 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001821 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001822
1823 if (bp_site->IsEnabled())
1824 {
1825 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001826 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 +00001827 return error;
1828 }
1829 else
1830 {
1831 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1832
1833 if (bp_site->HardwarePreferred())
1834 {
1835 // Try and set hardware breakpoint, and if that fails, fall through
1836 // and set a software breakpoint?
Greg Claytonb72d0f02011-04-12 05:54:46 +00001837 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner24943d22010-06-08 16:52:24 +00001838 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001839 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001840 {
1841 bp_site->SetEnabled(true);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001842 bp_site->SetType (BreakpointSite::eHardware);
Chris Lattner24943d22010-06-08 16:52:24 +00001843 return error;
1844 }
Chris Lattner24943d22010-06-08 16:52:24 +00001845 }
1846 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001847
1848 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner24943d22010-06-08 16:52:24 +00001849 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001850 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
1851 {
1852 bp_site->SetEnabled(true);
1853 bp_site->SetType (BreakpointSite::eExternal);
1854 return error;
1855 }
Chris Lattner24943d22010-06-08 16:52:24 +00001856 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001857
1858 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +00001859 }
1860
1861 if (log)
1862 {
1863 const char *err_string = error.AsCString();
1864 log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s",
1865 bp_site->GetLoadAddress(),
1866 err_string ? err_string : "NULL");
1867 }
1868 // We shouldn't reach here on a successful breakpoint enable...
1869 if (error.Success())
1870 error.SetErrorToGenericError();
1871 return error;
1872}
1873
1874Error
1875ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site)
1876{
1877 Error error;
1878 assert (bp_site != NULL);
1879 addr_t addr = bp_site->GetLoadAddress();
1880 user_id_t site_id = bp_site->GetID();
Greg Claytone005f2c2010-11-06 01:53:30 +00001881 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001882 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001883 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001884
1885 if (bp_site->IsEnabled())
1886 {
1887 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1888
Greg Claytonb72d0f02011-04-12 05:54:46 +00001889 BreakpointSite::Type bp_type = bp_site->GetType();
1890 switch (bp_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001891 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001892 case BreakpointSite::eSoftware:
1893 error = DisableSoftwareBreakpoint (bp_site);
1894 break;
1895
1896 case BreakpointSite::eHardware:
1897 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1898 error.SetErrorToGenericError();
1899 break;
1900
1901 case BreakpointSite::eExternal:
1902 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1903 error.SetErrorToGenericError();
1904 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001905 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001906 if (error.Success())
1907 bp_site->SetEnabled(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001908 }
1909 else
1910 {
1911 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001912 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 +00001913 return error;
1914 }
1915
1916 if (error.Success())
1917 error.SetErrorToGenericError();
1918 return error;
1919}
1920
Johnny Chen21900fb2011-09-06 22:38:36 +00001921// Pre-requisite: wp != NULL.
1922static GDBStoppointType
Johnny Chenecd4feb2011-10-14 00:42:25 +00001923GetGDBStoppointType (Watchpoint *wp)
Johnny Chen21900fb2011-09-06 22:38:36 +00001924{
1925 assert(wp);
1926 bool watch_read = wp->WatchpointRead();
1927 bool watch_write = wp->WatchpointWrite();
1928
1929 // watch_read and watch_write cannot both be false.
1930 assert(watch_read || watch_write);
1931 if (watch_read && watch_write)
1932 return eWatchpointReadWrite;
Johnny Chen48a5e852011-09-09 20:35:15 +00001933 else if (watch_read)
Johnny Chen21900fb2011-09-06 22:38:36 +00001934 return eWatchpointRead;
Johnny Chen48a5e852011-09-09 20:35:15 +00001935 else // Must be watch_write, then.
Johnny Chen21900fb2011-09-06 22:38:36 +00001936 return eWatchpointWrite;
1937}
1938
Chris Lattner24943d22010-06-08 16:52:24 +00001939Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001940ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001941{
1942 Error error;
1943 if (wp)
1944 {
1945 user_id_t watchID = wp->GetID();
1946 addr_t addr = wp->GetLoadAddress();
Greg Claytone005f2c2010-11-06 01:53:30 +00001947 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001948 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001949 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %llu)", watchID);
Chris Lattner24943d22010-06-08 16:52:24 +00001950 if (wp->IsEnabled())
1951 {
1952 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001953 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %llu) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001954 return error;
1955 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001956
1957 GDBStoppointType type = GetGDBStoppointType(wp);
1958 // Pass down an appropriate z/Z packet...
1959 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner24943d22010-06-08 16:52:24 +00001960 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001961 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
1962 {
1963 wp->SetEnabled(true);
1964 return error;
1965 }
1966 else
1967 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001968 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001969 else
1970 error.SetErrorString("watchpoints not supported");
Chris Lattner24943d22010-06-08 16:52:24 +00001971 }
1972 else
1973 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001974 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00001975 }
1976 if (error.Success())
1977 error.SetErrorToGenericError();
1978 return error;
1979}
1980
1981Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001982ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001983{
1984 Error error;
1985 if (wp)
1986 {
1987 user_id_t watchID = wp->GetID();
1988
Greg Claytone005f2c2010-11-06 01:53:30 +00001989 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001990
1991 addr_t addr = wp->GetLoadAddress();
1992 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001993 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001994
Johnny Chen21900fb2011-09-06 22:38:36 +00001995 if (!wp->IsEnabled())
1996 {
1997 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001998 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", watchID, (uint64_t)addr);
Johnny Chen21900fb2011-09-06 22:38:36 +00001999 return error;
2000 }
2001
Chris Lattner24943d22010-06-08 16:52:24 +00002002 if (wp->IsHardware())
2003 {
Johnny Chen21900fb2011-09-06 22:38:36 +00002004 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner24943d22010-06-08 16:52:24 +00002005 // Pass down an appropriate z/Z packet...
Johnny Chen21900fb2011-09-06 22:38:36 +00002006 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2007 {
2008 wp->SetEnabled(false);
2009 return error;
2010 }
2011 else
2012 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00002013 }
2014 // TODO: clear software watchpoints if we implement them
2015 }
2016 else
2017 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00002018 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00002019 }
2020 if (error.Success())
2021 error.SetErrorToGenericError();
2022 return error;
2023}
2024
2025void
2026ProcessGDBRemote::Clear()
2027{
2028 m_flags = 0;
2029 m_thread_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002030}
2031
2032Error
2033ProcessGDBRemote::DoSignal (int signo)
2034{
2035 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00002036 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002037 if (log)
2038 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2039
2040 if (!m_gdb_comm.SendAsyncSignal (signo))
2041 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2042 return error;
2043}
2044
Chris Lattner24943d22010-06-08 16:52:24 +00002045Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002046ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2047{
2048 ProcessLaunchInfo launch_info;
2049 return StartDebugserverProcess(debugserver_url, launch_info);
2050}
2051
2052Error
2053ProcessGDBRemote::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 +00002054{
2055 Error error;
2056 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2057 {
2058 // If we locate debugserver, keep that located version around
2059 static FileSpec g_debugserver_file_spec;
2060
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002061 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner24943d22010-06-08 16:52:24 +00002062 char debugserver_path[PATH_MAX];
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002063 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner24943d22010-06-08 16:52:24 +00002064
2065 // Always check to see if we have an environment override for the path
2066 // to the debugserver to use and use it if we do.
2067 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2068 if (env_debugserver_path)
Greg Clayton537a7a82010-10-20 20:54:39 +00002069 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +00002070 else
2071 debugserver_file_spec = g_debugserver_file_spec;
2072 bool debugserver_exists = debugserver_file_spec.Exists();
2073 if (!debugserver_exists)
2074 {
2075 // The debugserver binary is in the LLDB.framework/Resources
2076 // directory.
Greg Clayton24b48ff2010-10-17 22:03:32 +00002077 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner24943d22010-06-08 16:52:24 +00002078 {
Greg Clayton24b48ff2010-10-17 22:03:32 +00002079 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002080 debugserver_exists = debugserver_file_spec.Exists();
Greg Clayton24b48ff2010-10-17 22:03:32 +00002081 if (debugserver_exists)
2082 {
2083 g_debugserver_file_spec = debugserver_file_spec;
2084 }
2085 else
2086 {
2087 g_debugserver_file_spec.Clear();
2088 debugserver_file_spec.Clear();
2089 }
Chris Lattner24943d22010-06-08 16:52:24 +00002090 }
2091 }
2092
2093 if (debugserver_exists)
2094 {
2095 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2096
2097 m_stdio_communication.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002098
Greg Claytone005f2c2010-11-06 01:53:30 +00002099 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002100
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002101 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner24943d22010-06-08 16:52:24 +00002102 char arg_cstr[PATH_MAX];
Chris Lattner24943d22010-06-08 16:52:24 +00002103
Chris Lattner24943d22010-06-08 16:52:24 +00002104 // Start args with "debugserver /file/path -r --"
2105 debugserver_args.AppendArgument(debugserver_path);
2106 debugserver_args.AppendArgument(debugserver_url);
Greg Clayton24b48ff2010-10-17 22:03:32 +00002107 // use native registers, not the GDB registers
2108 debugserver_args.AppendArgument("--native-regs");
2109 // make debugserver run in its own session so signals generated by
2110 // special terminal key sequences (^C) don't affect debugserver
2111 debugserver_args.AppendArgument("--setsid");
Chris Lattner24943d22010-06-08 16:52:24 +00002112
Chris Lattner24943d22010-06-08 16:52:24 +00002113 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2114 if (env_debugserver_log_file)
2115 {
2116 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2117 debugserver_args.AppendArgument(arg_cstr);
2118 }
2119
2120 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2121 if (env_debugserver_log_flags)
2122 {
2123 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2124 debugserver_args.AppendArgument(arg_cstr);
2125 }
Greg Claytoncc3e6402011-01-25 06:55:13 +00002126// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002127// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner24943d22010-06-08 16:52:24 +00002128
Greg Claytonb72d0f02011-04-12 05:54:46 +00002129 // We currently send down all arguments, attach pids, or attach
2130 // process names in dedicated GDB server packets, so we don't need
2131 // to pass them as arguments. This is currently because of all the
2132 // things we need to setup prior to launching: the environment,
2133 // current working dir, file actions, etc.
2134#if 0
Chris Lattner24943d22010-06-08 16:52:24 +00002135 // Now append the program arguments
Greg Claytona2f74232011-02-24 22:24:29 +00002136 if (inferior_argv)
Chris Lattner24943d22010-06-08 16:52:24 +00002137 {
Greg Claytona2f74232011-02-24 22:24:29 +00002138 // Terminate the debugserver args so we can now append the inferior args
2139 debugserver_args.AppendArgument("--");
Chris Lattner24943d22010-06-08 16:52:24 +00002140
Greg Claytona2f74232011-02-24 22:24:29 +00002141 for (int i = 0; inferior_argv[i] != NULL; ++i)
2142 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner24943d22010-06-08 16:52:24 +00002143 }
2144 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2145 {
2146 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2147 debugserver_args.AppendArgument (arg_cstr);
2148 }
2149 else if (attach_name && attach_name[0])
2150 {
2151 if (wait_for_launch)
2152 debugserver_args.AppendArgument ("--waitfor");
2153 else
2154 debugserver_args.AppendArgument ("--attach");
2155 debugserver_args.AppendArgument (attach_name);
2156 }
Chris Lattner24943d22010-06-08 16:52:24 +00002157#endif
Greg Claytonb72d0f02011-04-12 05:54:46 +00002158
2159 ProcessLaunchInfo::FileAction file_action;
2160
2161 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2162 // to "/dev/null" if we run into any problems.
2163 file_action.Close (STDIN_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002164 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002165 file_action.Close (STDOUT_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002166 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002167 file_action.Close (STDERR_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002168 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner24943d22010-06-08 16:52:24 +00002169
2170 if (log)
2171 {
2172 StreamString strm;
2173 debugserver_args.Dump (&strm);
2174 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2175 }
2176
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002177 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2178 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Clayton1c4642c2011-11-16 05:37:56 +00002179
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002180 error = Host::LaunchProcess(debugserver_launch_info);
Greg Claytone9d0df42010-07-02 01:29:13 +00002181
Greg Claytonb72d0f02011-04-12 05:54:46 +00002182 if (error.Success ())
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002183 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Claytonb72d0f02011-04-12 05:54:46 +00002184 else
Chris Lattner24943d22010-06-08 16:52:24 +00002185 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2186
2187 if (error.Fail() || log)
Greg Claytond9919d32011-12-01 23:28:38 +00002188 error.PutToLog(log.get(), "Host::LaunchProcess (launch_info) => pid=%llu, path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner24943d22010-06-08 16:52:24 +00002189 }
2190 else
2191 {
Greg Clayton9c236732011-10-26 00:56:27 +00002192 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002193 }
2194
2195 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2196 StartAsyncThread ();
2197 }
2198 return error;
2199}
2200
2201bool
2202ProcessGDBRemote::MonitorDebugserverProcess
2203(
2204 void *callback_baton,
2205 lldb::pid_t debugserver_pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +00002206 bool exited, // True if the process did exit
Chris Lattner24943d22010-06-08 16:52:24 +00002207 int signo, // Zero for no signal
2208 int exit_status // Exit value of process if signal is zero
2209)
2210{
Greg Clayton1c4642c2011-11-16 05:37:56 +00002211 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2212 // and might not exist anymore, so we need to carefully try to get the
2213 // target for this process first since we have a race condition when
2214 // we are done running between getting the notice that the inferior
2215 // process has died and the debugserver that was debugging this process.
2216 // In our test suite, we are also continually running process after
2217 // process, so we must be very careful to make sure:
2218 // 1 - process object hasn't been deleted already
2219 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner24943d22010-06-08 16:52:24 +00002220
2221 // "debugserver_pid" argument passed in is the process ID for
2222 // debugserver that we are tracking...
Greg Clayton1c4642c2011-11-16 05:37:56 +00002223 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002224
Greg Clayton75ccf502010-08-21 02:22:51 +00002225 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton72e1c782011-01-22 23:43:18 +00002226
Greg Clayton1c4642c2011-11-16 05:37:56 +00002227 // Get a shared pointer to the target that has a matching process pointer.
2228 // This target could be gone, or the target could already have a new process
2229 // object inside of it
2230 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2231
Greg Clayton72e1c782011-01-22 23:43:18 +00002232 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00002233 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 +00002234
Greg Clayton1c4642c2011-11-16 05:37:56 +00002235 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002236 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002237 // We found a process in a target that matches, but another thread
2238 // might be in the process of launching a new process that will
2239 // soon replace it, so get a shared pointer to the process so we
2240 // can keep it alive.
2241 ProcessSP process_sp (target_sp->GetProcessSP());
2242 // Now we have a shared pointer to the process that can't go away on us
2243 // so we now make sure it was the same as the one passed in, and also make
2244 // sure that our previous "process *" didn't get deleted and have a new
2245 // "process *" created in its place with the same pointer. To verify this
2246 // we make sure the process has our debugserver process ID. If we pass all
2247 // of these tests, then we are sure that this process is the one we were
2248 // looking for.
2249 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner24943d22010-06-08 16:52:24 +00002250 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002251 // Sleep for a half a second to make sure our inferior process has
2252 // time to set its exit status before we set it incorrectly when
2253 // both the debugserver and the inferior process shut down.
2254 usleep (500000);
2255 // If our process hasn't yet exited, debugserver might have died.
2256 // If the process did exit, the we are reaping it.
2257 const StateType state = process->GetState();
2258
2259 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2260 state != eStateInvalid &&
2261 state != eStateUnloaded &&
2262 state != eStateExited &&
2263 state != eStateDetached)
Chris Lattner24943d22010-06-08 16:52:24 +00002264 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002265 char error_str[1024];
2266 if (signo)
2267 {
2268 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2269 if (signal_cstr)
2270 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2271 else
2272 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2273 }
Chris Lattner24943d22010-06-08 16:52:24 +00002274 else
Greg Clayton1c4642c2011-11-16 05:37:56 +00002275 {
2276 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2277 }
Greg Clayton75ccf502010-08-21 02:22:51 +00002278
Greg Clayton1c4642c2011-11-16 05:37:56 +00002279 process->SetExitStatus (-1, error_str);
2280 }
2281 // Debugserver has exited we need to let our ProcessGDBRemote
2282 // know that it no longer has a debugserver instance
2283 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton75ccf502010-08-21 02:22:51 +00002284 }
Chris Lattner24943d22010-06-08 16:52:24 +00002285 }
2286 return true;
2287}
2288
2289void
2290ProcessGDBRemote::KillDebugserverProcess ()
2291{
2292 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2293 {
2294 ::kill (m_debugserver_pid, SIGINT);
2295 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2296 }
2297}
2298
2299void
2300ProcessGDBRemote::Initialize()
2301{
2302 static bool g_initialized = false;
2303
2304 if (g_initialized == false)
2305 {
2306 g_initialized = true;
2307 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2308 GetPluginDescriptionStatic(),
2309 CreateInstance);
2310
2311 Log::Callbacks log_callbacks = {
2312 ProcessGDBRemoteLog::DisableLog,
2313 ProcessGDBRemoteLog::EnableLog,
2314 ProcessGDBRemoteLog::ListLogCategories
2315 };
2316
2317 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2318 }
2319}
2320
2321bool
Chris Lattner24943d22010-06-08 16:52:24 +00002322ProcessGDBRemote::StartAsyncThread ()
2323{
Greg Claytone005f2c2010-11-06 01:53:30 +00002324 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002325
2326 if (log)
2327 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2328
2329 // Create a thread that watches our internal state and controls which
2330 // events make it to clients (into the DCProcess event queue).
2331 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002332 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002333}
2334
2335void
2336ProcessGDBRemote::StopAsyncThread ()
2337{
Greg Claytone005f2c2010-11-06 01:53:30 +00002338 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002339
2340 if (log)
2341 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2342
2343 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
Jim Ingham8226e942011-10-28 01:11:35 +00002344
2345 // This will shut down the async thread.
2346 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Chris Lattner24943d22010-06-08 16:52:24 +00002347
2348 // Stop the stdio thread
Greg Clayton09c81ef2011-02-08 01:34:25 +00002349 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002350 {
2351 Host::ThreadJoin (m_async_thread, NULL, NULL);
2352 }
2353}
2354
2355
2356void *
2357ProcessGDBRemote::AsyncThread (void *arg)
2358{
2359 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2360
Greg Claytone005f2c2010-11-06 01:53:30 +00002361 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002362 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002363 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002364
2365 Listener listener ("ProcessGDBRemote::AsyncThread");
2366 EventSP event_sp;
2367 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2368 eBroadcastBitAsyncThreadShouldExit;
2369
2370 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2371 {
Greg Claytona2f74232011-02-24 22:24:29 +00002372 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2373
Chris Lattner24943d22010-06-08 16:52:24 +00002374 bool done = false;
2375 while (!done)
2376 {
2377 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002378 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002379 if (listener.WaitForEvent (NULL, event_sp))
2380 {
2381 const uint32_t event_type = event_sp->GetType();
Greg Claytona2f74232011-02-24 22:24:29 +00002382 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner24943d22010-06-08 16:52:24 +00002383 {
Greg Claytona2f74232011-02-24 22:24:29 +00002384 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002385 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 +00002386
Greg Claytona2f74232011-02-24 22:24:29 +00002387 switch (event_type)
2388 {
2389 case eBroadcastBitAsyncContinue:
Chris Lattner24943d22010-06-08 16:52:24 +00002390 {
Greg Claytona2f74232011-02-24 22:24:29 +00002391 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002392
Greg Claytona2f74232011-02-24 22:24:29 +00002393 if (continue_packet)
Chris Lattner24943d22010-06-08 16:52:24 +00002394 {
Greg Claytona2f74232011-02-24 22:24:29 +00002395 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2396 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2397 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002398 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002399
Greg Claytona2f74232011-02-24 22:24:29 +00002400 if (::strstr (continue_cstr, "vAttach") == NULL)
2401 process->SetPrivateState(eStateRunning);
2402 StringExtractorGDBRemote response;
2403 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner24943d22010-06-08 16:52:24 +00002404
Greg Claytona2f74232011-02-24 22:24:29 +00002405 switch (stop_state)
2406 {
2407 case eStateStopped:
2408 case eStateCrashed:
2409 case eStateSuspended:
Greg Clayton06709002011-12-06 04:51:14 +00002410 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002411 process->SetPrivateState (stop_state);
2412 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002413
Greg Claytona2f74232011-02-24 22:24:29 +00002414 case eStateExited:
Greg Clayton06709002011-12-06 04:51:14 +00002415 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002416 response.SetFilePos(1);
2417 process->SetExitStatus(response.GetHexU8(), NULL);
2418 done = true;
2419 break;
2420
2421 case eStateInvalid:
2422 process->SetExitStatus(-1, "lost connection");
2423 break;
2424
2425 default:
2426 process->SetPrivateState (stop_state);
2427 break;
2428 }
Chris Lattner24943d22010-06-08 16:52:24 +00002429 }
2430 }
Greg Claytona2f74232011-02-24 22:24:29 +00002431 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002432
Greg Claytona2f74232011-02-24 22:24:29 +00002433 case eBroadcastBitAsyncThreadShouldExit:
2434 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002435 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Claytona2f74232011-02-24 22:24:29 +00002436 done = true;
2437 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002438
Greg Claytona2f74232011-02-24 22:24:29 +00002439 default:
2440 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002441 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 +00002442 done = true;
2443 break;
2444 }
2445 }
2446 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2447 {
2448 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2449 {
2450 process->SetExitStatus (-1, "lost connection");
Chris Lattner24943d22010-06-08 16:52:24 +00002451 done = true;
Greg Claytona2f74232011-02-24 22:24:29 +00002452 }
Chris Lattner24943d22010-06-08 16:52:24 +00002453 }
2454 }
2455 else
2456 {
2457 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002458 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 +00002459 done = true;
2460 }
2461 }
2462 }
2463
2464 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002465 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002466
2467 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2468 return NULL;
2469}
2470
Chris Lattner24943d22010-06-08 16:52:24 +00002471const char *
2472ProcessGDBRemote::GetDispatchQueueNameForThread
2473(
2474 addr_t thread_dispatch_qaddr,
2475 std::string &dispatch_queue_name
2476)
2477{
2478 dispatch_queue_name.clear();
2479 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2480 {
2481 // Cache the dispatch_queue_offsets_addr value so we don't always have
2482 // to look it up
2483 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2484 {
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002485 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
2486 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Clayton444fe992012-02-26 05:51:37 +00002487 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
2488 ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002489 if (module_sp)
2490 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2491
2492 if (dispatch_queue_offsets_symbol == NULL)
2493 {
Greg Clayton444fe992012-02-26 05:51:37 +00002494 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
2495 module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002496 if (module_sp)
2497 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2498 }
Chris Lattner24943d22010-06-08 16:52:24 +00002499 if (dispatch_queue_offsets_symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +00002500 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00002501
2502 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2503 return NULL;
2504 }
2505
2506 uint8_t memory_buffer[8];
Greg Clayton395fc332011-02-15 21:59:32 +00002507 DataExtractor data (memory_buffer,
2508 sizeof(memory_buffer),
2509 m_target.GetArchitecture().GetByteOrder(),
2510 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00002511
2512 // Excerpt from src/queue_private.h
2513 struct dispatch_queue_offsets_s
2514 {
2515 uint16_t dqo_version;
2516 uint16_t dqo_label;
2517 uint16_t dqo_label_size;
2518 } dispatch_queue_offsets;
2519
2520
2521 Error error;
2522 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
2523 {
2524 uint32_t data_offset = 0;
2525 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
2526 {
2527 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
2528 {
2529 data_offset = 0;
2530 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
2531 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
2532 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
2533 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
2534 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
2535 dispatch_queue_name.erase (bytes_read);
2536 }
2537 }
2538 }
2539 }
2540 if (dispatch_queue_name.empty())
2541 return NULL;
2542 return dispatch_queue_name.c_str();
2543}
2544
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002545//uint32_t
2546//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2547//{
2548// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
2549// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
2550// if (m_local_debugserver)
2551// {
2552// return Host::ListProcessesMatchingName (name, matches, pids);
2553// }
2554// else
2555// {
2556// // FIXME: Implement talking to the remote debugserver.
2557// return 0;
2558// }
2559//
2560//}
2561//
Jim Ingham55e01d82011-01-22 01:33:44 +00002562bool
2563ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
2564 lldb_private::StoppointCallbackContext *context,
2565 lldb::user_id_t break_id,
2566 lldb::user_id_t break_loc_id)
2567{
2568 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
2569 // run so I can stop it if that's what I want to do.
2570 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2571 if (log)
2572 log->Printf("Hit New Thread Notification breakpoint.");
2573 return false;
2574}
2575
2576
2577bool
2578ProcessGDBRemote::StartNoticingNewThreads()
2579{
2580 static const char *bp_names[] =
2581 {
2582 "start_wqthread",
Jim Inghamff276fe2011-02-08 05:19:01 +00002583 "_pthread_wqthread",
Jim Ingham55e01d82011-01-22 01:33:44 +00002584 "_pthread_start",
2585 NULL
2586 };
2587
2588 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2589 size_t num_bps = m_thread_observation_bps.size();
2590 if (num_bps != 0)
2591 {
2592 for (int i = 0; i < num_bps; i++)
2593 {
2594 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2595 if (break_sp)
2596 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002597 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002598 log->Printf("Enabled noticing new thread breakpoint.");
2599 break_sp->SetEnabled(true);
2600 }
2601 }
2602 }
2603 else
2604 {
2605 for (int i = 0; bp_names[i] != NULL; i++)
2606 {
Jim Inghamd6d47972011-09-23 00:54:11 +00002607 Breakpoint *breakpoint = m_target.CreateBreakpoint (NULL, NULL, bp_names[i], eFunctionNameTypeFull, true).get();
Jim Ingham55e01d82011-01-22 01:33:44 +00002608 if (breakpoint)
2609 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002610 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002611 log->Printf("Successfully created new thread notification breakpoint at \"%s\".", bp_names[i]);
2612 m_thread_observation_bps.push_back(breakpoint->GetID());
2613 breakpoint->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
2614 }
2615 else
2616 {
2617 if (log)
2618 log->Printf("Failed to create new thread notification breakpoint.");
2619 return false;
2620 }
2621 }
2622 }
2623
2624 return true;
2625}
2626
2627bool
2628ProcessGDBRemote::StopNoticingNewThreads()
2629{
Jim Inghamff276fe2011-02-08 05:19:01 +00002630 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham6bb73372011-10-15 00:21:37 +00002631 if (log && log->GetVerbose())
Jim Inghamff276fe2011-02-08 05:19:01 +00002632 log->Printf ("Disabling new thread notification breakpoint.");
Jim Ingham55e01d82011-01-22 01:33:44 +00002633 size_t num_bps = m_thread_observation_bps.size();
2634 if (num_bps != 0)
2635 {
2636 for (int i = 0; i < num_bps; i++)
2637 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002638
2639 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2640 if (break_sp)
2641 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002642 break_sp->SetEnabled(false);
2643 }
2644 }
2645 }
2646 return true;
2647}
2648
2649