blob: 8b667dffd8d389617513f4d8a9d7ba3c5efe2be8 [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
Greg Claytonae932352012-04-10 00:18:59 +00001132bool
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 }
Greg Claytonae932352012-04-10 00:18:59 +00001154 SetThreadStopInfo (m_last_stop_packet);
1155 }
1156 else if (sequence_mutex_unavailable)
1157 {
1158#if defined (LLDB_CONFIGURATION_DEBUG)
1159 assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
1160#endif
1161 return false; // We just didn't get the list
Chris Lattner24943d22010-06-08 16:52:24 +00001162 }
Greg Clayton37f962e2011-08-22 02:49:39 +00001163
Greg Claytonae932352012-04-10 00:18:59 +00001164 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001165}
1166
1167
1168StateType
1169ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1170{
Greg Clayton261a18b2011-06-02 22:22:38 +00001171 stop_packet.SetFilePos (0);
Chris Lattner24943d22010-06-08 16:52:24 +00001172 const char stop_type = stop_packet.GetChar();
1173 switch (stop_type)
1174 {
1175 case 'T':
1176 case 'S':
1177 {
Greg Claytonc3c46612011-02-15 00:19:15 +00001178 if (GetStopID() == 0)
1179 {
1180 // Our first stop, make sure we have a process ID, and also make
1181 // sure we know about our registers
1182 if (GetID() == LLDB_INVALID_PROCESS_ID)
1183 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001184 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Greg Claytonc3c46612011-02-15 00:19:15 +00001185 if (pid != LLDB_INVALID_PROCESS_ID)
1186 SetID (pid);
1187 }
1188 BuildDynamicRegisterInfo (true);
1189 }
Chris Lattner24943d22010-06-08 16:52:24 +00001190 // Stop with signal and thread info
1191 const uint8_t signo = stop_packet.GetHexU8();
1192 std::string name;
1193 std::string value;
1194 std::string thread_name;
Greg Clayton65611552011-06-04 01:26:29 +00001195 std::string reason;
1196 std::string description;
Chris Lattner24943d22010-06-08 16:52:24 +00001197 uint32_t exc_type = 0;
Greg Clayton7661a982010-07-23 16:45:51 +00001198 std::vector<addr_t> exc_data;
Chris Lattner24943d22010-06-08 16:52:24 +00001199 uint32_t tid = LLDB_INVALID_THREAD_ID;
1200 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
1201 uint32_t exc_data_count = 0;
Greg Claytona875b642011-01-09 21:07:35 +00001202 ThreadSP thread_sp;
1203
Chris Lattner24943d22010-06-08 16:52:24 +00001204 while (stop_packet.GetNameColonValue(name, value))
1205 {
1206 if (name.compare("metype") == 0)
1207 {
1208 // exception type in big endian hex
1209 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1210 }
1211 else if (name.compare("mecount") == 0)
1212 {
1213 // exception count in big endian hex
1214 exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16);
1215 }
1216 else if (name.compare("medata") == 0)
1217 {
1218 // exception data in big endian hex
1219 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1220 }
1221 else if (name.compare("thread") == 0)
1222 {
1223 // thread in big endian hex
1224 tid = Args::StringToUInt32 (value.c_str(), 0, 16);
Greg Claytonffa43a62011-11-17 04:46:02 +00001225 // m_thread_list does have its own mutex, but we need to
1226 // hold onto the mutex between the call to m_thread_list.FindThreadByID(...)
1227 // and the m_thread_list.AddThread(...) so it doesn't change on us
Greg Claytonc3c46612011-02-15 00:19:15 +00001228 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytona875b642011-01-09 21:07:35 +00001229 thread_sp = m_thread_list.FindThreadByID(tid, false);
Greg Claytonc3c46612011-02-15 00:19:15 +00001230 if (!thread_sp)
1231 {
1232 // Create the thread if we need to
Greg Claytonf4124de2012-02-21 00:09:25 +00001233 thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid));
Greg Claytonc3c46612011-02-15 00:19:15 +00001234 m_thread_list.AddThread(thread_sp);
1235 }
Chris Lattner24943d22010-06-08 16:52:24 +00001236 }
Greg Clayton4862fa22011-01-08 03:17:57 +00001237 else if (name.compare("hexname") == 0)
1238 {
1239 StringExtractor name_extractor;
1240 // Swap "value" over into "name_extractor"
1241 name_extractor.GetStringRef().swap(value);
1242 // Now convert the HEX bytes into a string value
1243 name_extractor.GetHexByteString (value);
1244 thread_name.swap (value);
1245 }
Chris Lattner24943d22010-06-08 16:52:24 +00001246 else if (name.compare("name") == 0)
1247 {
1248 thread_name.swap (value);
1249 }
Greg Clayton0a7f75f2010-09-09 06:32:46 +00001250 else if (name.compare("qaddr") == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001251 {
1252 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1253 }
Greg Clayton65611552011-06-04 01:26:29 +00001254 else if (name.compare("reason") == 0)
1255 {
1256 reason.swap(value);
1257 }
1258 else if (name.compare("description") == 0)
1259 {
1260 StringExtractor desc_extractor;
1261 // Swap "value" over into "name_extractor"
1262 desc_extractor.GetStringRef().swap(value);
1263 // Now convert the HEX bytes into a string value
1264 desc_extractor.GetHexByteString (thread_name);
1265 }
Greg Claytona875b642011-01-09 21:07:35 +00001266 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1267 {
1268 // We have a register number that contains an expedited
1269 // register value. Lets supply this register to our thread
1270 // so it won't have to go and read it.
1271 if (thread_sp)
1272 {
1273 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1274
1275 if (reg != UINT32_MAX)
1276 {
1277 StringExtractor reg_value_extractor;
1278 // Swap "value" over into "reg_value_extractor"
1279 reg_value_extractor.GetStringRef().swap(value);
Greg Claytonc3c46612011-02-15 00:19:15 +00001280 if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor))
1281 {
1282 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1283 name.c_str(),
1284 reg,
1285 reg,
1286 reg_value_extractor.GetStringRef().c_str(),
1287 stop_packet.GetStringRef().c_str());
1288 }
Greg Claytona875b642011-01-09 21:07:35 +00001289 }
1290 }
1291 }
Chris Lattner24943d22010-06-08 16:52:24 +00001292 }
Chris Lattner24943d22010-06-08 16:52:24 +00001293
1294 if (thread_sp)
1295 {
1296 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1297
1298 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Ingham9082c8a2011-01-28 02:23:12 +00001299 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001300 if (exc_type != 0)
1301 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001302 const size_t exc_data_size = exc_data.size();
Greg Clayton643ee732010-08-04 01:40:35 +00001303
1304 gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1305 exc_type,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001306 exc_data_size,
1307 exc_data_size >= 1 ? exc_data[0] : 0,
Johnny Chen36889ad2011-09-17 01:05:03 +00001308 exc_data_size >= 2 ? exc_data[1] : 0,
1309 exc_data_size >= 3 ? exc_data[2] : 0));
Chris Lattner24943d22010-06-08 16:52:24 +00001310 }
Greg Clayton65611552011-06-04 01:26:29 +00001311 else
Chris Lattner24943d22010-06-08 16:52:24 +00001312 {
Greg Clayton65611552011-06-04 01:26:29 +00001313 bool handled = false;
1314 if (!reason.empty())
1315 {
1316 if (reason.compare("trace") == 0)
1317 {
1318 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1319 handled = true;
1320 }
1321 else if (reason.compare("breakpoint") == 0)
1322 {
1323 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001324 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001325 if (bp_site_sp)
1326 {
1327 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1328 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1329 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1330 if (bp_site_sp->ValidForThisThread (gdb_thread))
1331 {
1332 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1333 handled = true;
1334 }
1335 }
1336
1337 if (!handled)
1338 {
1339 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1340 }
1341 }
1342 else if (reason.compare("trap") == 0)
1343 {
1344 // Let the trap just use the standard signal stop reason below...
1345 }
1346 else if (reason.compare("watchpoint") == 0)
1347 {
1348 break_id_t watch_id = LLDB_INVALID_WATCH_ID;
1349 // TODO: locate the watchpoint somehow...
1350 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id));
1351 handled = true;
1352 }
1353 else if (reason.compare("exception") == 0)
1354 {
1355 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str()));
1356 handled = true;
1357 }
1358 }
1359
1360 if (signo)
1361 {
1362 if (signo == SIGTRAP)
1363 {
1364 // Currently we are going to assume SIGTRAP means we are either
1365 // hitting a breakpoint or hardware single stepping.
1366 addr_t pc = gdb_thread->GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +00001367 lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton65611552011-06-04 01:26:29 +00001368 if (bp_site_sp)
1369 {
1370 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
1371 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
1372 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
1373 if (bp_site_sp->ValidForThisThread (gdb_thread))
1374 {
1375 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID()));
1376 handled = true;
1377 }
1378 }
1379 if (!handled)
1380 {
1381 // TODO: check for breakpoint or trap opcode in case there is a hard
1382 // coded software trap
1383 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp));
1384 handled = true;
1385 }
1386 }
1387 if (!handled)
Greg Clayton37f962e2011-08-22 02:49:39 +00001388 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Chris Lattner24943d22010-06-08 16:52:24 +00001389 }
1390 else
1391 {
Greg Clayton643ee732010-08-04 01:40:35 +00001392 StopInfoSP invalid_stop_info_sp;
1393 gdb_thread->SetStopInfo (invalid_stop_info_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001394 }
Greg Clayton65611552011-06-04 01:26:29 +00001395
1396 if (!description.empty())
1397 {
1398 lldb::StopInfoSP stop_info_sp (gdb_thread->GetStopInfo ());
1399 if (stop_info_sp)
1400 {
1401 stop_info_sp->SetDescription (description.c_str());
Greg Clayton153ccd72011-08-10 02:10:13 +00001402 }
Greg Clayton65611552011-06-04 01:26:29 +00001403 else
1404 {
1405 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str()));
1406 }
1407 }
1408 }
Chris Lattner24943d22010-06-08 16:52:24 +00001409 }
1410 return eStateStopped;
1411 }
1412 break;
1413
1414 case 'W':
1415 // process exited
1416 return eStateExited;
1417
1418 default:
1419 break;
1420 }
1421 return eStateInvalid;
1422}
1423
1424void
1425ProcessGDBRemote::RefreshStateAfterStop ()
1426{
Chris Lattner24943d22010-06-08 16:52:24 +00001427 // Let all threads recover from stopping and do any clean up based
1428 // on the previous thread state (if any).
1429 m_thread_list.RefreshStateAfterStop();
Greg Clayton261a18b2011-06-02 22:22:38 +00001430 SetThreadStopInfo (m_last_stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +00001431}
1432
1433Error
Jim Ingham3ae449a2010-11-17 02:32:00 +00001434ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner24943d22010-06-08 16:52:24 +00001435{
1436 Error error;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001437
Greg Claytona4881d02011-01-22 07:12:45 +00001438 bool timed_out = false;
1439 Mutex::Locker locker;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001440
1441 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton20d338f2010-11-18 05:57:03 +00001442 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001443 // We are being asked to halt during an attach. We need to just close
1444 // our file handle and debugserver will go away, and we can be done...
1445 m_gdb_comm.Disconnect();
Greg Clayton20d338f2010-11-18 05:57:03 +00001446 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001447 else
1448 {
Greg Clayton05e4d972012-03-29 01:55:41 +00001449 if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001450 {
1451 if (timed_out)
1452 error.SetErrorString("timed out sending interrupt packet");
1453 else
1454 error.SetErrorString("unknown error sending interrupt packet");
1455 }
Greg Clayton05e4d972012-03-29 01:55:41 +00001456
1457 caused_stop = m_gdb_comm.GetInterruptWasSent ();
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001458 }
Chris Lattner24943d22010-06-08 16:52:24 +00001459 return error;
1460}
1461
1462Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001463ProcessGDBRemote::InterruptIfRunning
1464(
1465 bool discard_thread_plans,
1466 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +00001467 EventSP &stop_event_sp
1468)
Chris Lattner24943d22010-06-08 16:52:24 +00001469{
1470 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001471
Greg Clayton2860ba92011-01-23 19:58:49 +00001472 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1473
Greg Clayton68ca8232011-01-25 02:58:48 +00001474 bool paused_private_state_thread = false;
Greg Clayton2860ba92011-01-23 19:58:49 +00001475 const bool is_running = m_gdb_comm.IsRunning();
1476 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00001477 log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
Greg Clayton2860ba92011-01-23 19:58:49 +00001478 discard_thread_plans,
Greg Clayton68ca8232011-01-25 02:58:48 +00001479 catch_stop_event,
Greg Clayton2860ba92011-01-23 19:58:49 +00001480 is_running);
1481
Greg Clayton2860ba92011-01-23 19:58:49 +00001482 if (discard_thread_plans)
1483 {
1484 if (log)
1485 log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans");
1486 m_thread_list.DiscardThreadPlans();
1487 }
1488 if (is_running)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001489 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001490 if (catch_stop_event)
1491 {
1492 if (log)
1493 log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread");
1494 PausePrivateStateThread();
1495 paused_private_state_thread = true;
1496 }
1497
Greg Clayton4fb400f2010-09-27 21:07:38 +00001498 bool timed_out = false;
1499 Mutex::Locker locker;
Greg Clayton72e1c782011-01-22 23:43:18 +00001500
Greg Clayton05e4d972012-03-29 01:55:41 +00001501 if (!m_gdb_comm.SendInterrupt (locker, 1, timed_out))
Greg Clayton4fb400f2010-09-27 21:07:38 +00001502 {
1503 if (timed_out)
1504 error.SetErrorString("timed out sending interrupt packet");
1505 else
1506 error.SetErrorString("unknown error sending interrupt packet");
Greg Clayton68ca8232011-01-25 02:58:48 +00001507 if (paused_private_state_thread)
Greg Clayton72e1c782011-01-22 23:43:18 +00001508 ResumePrivateStateThread();
1509 return error;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001510 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001511
Greg Clayton72e1c782011-01-22 23:43:18 +00001512 if (catch_stop_event)
1513 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001514 // LISTEN HERE
Greg Clayton72e1c782011-01-22 23:43:18 +00001515 TimeValue timeout_time;
1516 timeout_time = TimeValue::Now();
Greg Clayton68ca8232011-01-25 02:58:48 +00001517 timeout_time.OffsetWithSeconds(5);
1518 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
Greg Clayton2860ba92011-01-23 19:58:49 +00001519
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001520 timed_out = state == eStateInvalid;
Greg Clayton2860ba92011-01-23 19:58:49 +00001521 if (log)
1522 log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001523
Greg Clayton2860ba92011-01-23 19:58:49 +00001524 if (timed_out)
Greg Clayton72e1c782011-01-22 23:43:18 +00001525 error.SetErrorString("unable to verify target stopped");
1526 }
1527
Greg Clayton68ca8232011-01-25 02:58:48 +00001528 if (paused_private_state_thread)
Greg Clayton2860ba92011-01-23 19:58:49 +00001529 {
1530 if (log)
1531 log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread");
Greg Clayton72e1c782011-01-22 23:43:18 +00001532 ResumePrivateStateThread();
Greg Clayton2860ba92011-01-23 19:58:49 +00001533 }
Greg Clayton4fb400f2010-09-27 21:07:38 +00001534 }
Chris Lattner24943d22010-06-08 16:52:24 +00001535 return error;
1536}
1537
Greg Clayton4fb400f2010-09-27 21:07:38 +00001538Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001539ProcessGDBRemote::WillDetach ()
1540{
Greg Clayton2860ba92011-01-23 19:58:49 +00001541 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1542 if (log)
1543 log->Printf ("ProcessGDBRemote::WillDetach()");
1544
Greg Clayton72e1c782011-01-22 23:43:18 +00001545 bool discard_thread_plans = true;
1546 bool catch_stop_event = true;
Greg Clayton72e1c782011-01-22 23:43:18 +00001547 EventSP event_sp;
Greg Clayton68ca8232011-01-25 02:58:48 +00001548 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
Greg Clayton72e1c782011-01-22 23:43:18 +00001549}
1550
1551Error
Greg Clayton4fb400f2010-09-27 21:07:38 +00001552ProcessGDBRemote::DoDetach()
1553{
1554 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001555 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton4fb400f2010-09-27 21:07:38 +00001556 if (log)
1557 log->Printf ("ProcessGDBRemote::DoDetach()");
1558
1559 DisableAllBreakpointSites ();
1560
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001561 m_thread_list.DiscardThreadPlans();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001562
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001563 size_t response_size = m_gdb_comm.SendPacket ("D", 1);
1564 if (log)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001565 {
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001566 if (response_size)
1567 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1568 else
1569 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed");
Greg Clayton4fb400f2010-09-27 21:07:38 +00001570 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001571 // Sleep for one second to let the process get all detached...
Greg Clayton4fb400f2010-09-27 21:07:38 +00001572 StopAsyncThread ();
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001573
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001574 SetPrivateState (eStateDetached);
1575 ResumePrivateStateThread();
1576
1577 //KillDebugserverProcess ();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001578 return error;
1579}
Chris Lattner24943d22010-06-08 16:52:24 +00001580
1581Error
1582ProcessGDBRemote::DoDestroy ()
1583{
1584 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001585 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001586 if (log)
1587 log->Printf ("ProcessGDBRemote::DoDestroy()");
1588
1589 // Interrupt if our inferior is running...
Greg Claytona4881d02011-01-22 07:12:45 +00001590 if (m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +00001591 {
Jim Ingham8226e942011-10-28 01:11:35 +00001592 if (m_public_state.GetValue() != eStateAttaching)
Greg Clayton72e1c782011-01-22 23:43:18 +00001593 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001594
1595 StringExtractorGDBRemote response;
1596 bool send_async = true;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001597 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async))
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001598 {
1599 char packet_cmd = response.GetChar(0);
1600
1601 if (packet_cmd == 'W' || packet_cmd == 'X')
1602 {
Greg Clayton06709002011-12-06 04:51:14 +00001603 SetLastStopPacket (response);
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001604 SetExitStatus(response.GetHexU8(), NULL);
1605 }
1606 }
1607 else
1608 {
1609 SetExitStatus(SIGABRT, NULL);
1610 //error.SetErrorString("kill packet failed");
1611 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001612 }
1613 }
Chris Lattner24943d22010-06-08 16:52:24 +00001614 StopAsyncThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001615 KillDebugserverProcess ();
1616 return error;
1617}
1618
Chris Lattner24943d22010-06-08 16:52:24 +00001619//------------------------------------------------------------------
1620// Process Queries
1621//------------------------------------------------------------------
1622
1623bool
1624ProcessGDBRemote::IsAlive ()
1625{
Greg Clayton58e844b2010-12-08 05:08:21 +00001626 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner24943d22010-06-08 16:52:24 +00001627}
1628
1629addr_t
1630ProcessGDBRemote::GetImageInfoAddress()
1631{
1632 if (!m_gdb_comm.IsRunning())
1633 {
1634 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001635 if (m_gdb_comm.SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
Chris Lattner24943d22010-06-08 16:52:24 +00001636 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001637 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001638 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1639 }
1640 }
1641 return LLDB_INVALID_ADDRESS;
1642}
1643
Chris Lattner24943d22010-06-08 16:52:24 +00001644//------------------------------------------------------------------
1645// Process Memory
1646//------------------------------------------------------------------
1647size_t
1648ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1649{
1650 if (size > m_max_memory_size)
1651 {
1652 // Keep memory read sizes down to a sane limit. This function will be
1653 // called multiple times in order to complete the task by
1654 // lldb_private::Process so it is ok to do this.
1655 size = m_max_memory_size;
1656 }
1657
1658 char packet[64];
1659 const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size);
1660 assert (packet_len + 1 < sizeof(packet));
1661 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001662 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001663 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001664 if (response.IsNormalResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001665 {
1666 error.Clear();
1667 return response.GetHexBytes(buf, size, '\xdd');
1668 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001669 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001670 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001671 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001672 error.SetErrorStringWithFormat("'%s' packet unsupported", packet);
1673 else
1674 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str());
1675 }
1676 else
1677 {
1678 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet);
1679 }
1680 return 0;
1681}
1682
1683size_t
1684ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1685{
Greg Claytonc8bc1c32011-05-16 02:35:02 +00001686 if (size > m_max_memory_size)
1687 {
1688 // Keep memory read sizes down to a sane limit. This function will be
1689 // called multiple times in order to complete the task by
1690 // lldb_private::Process so it is ok to do this.
1691 size = m_max_memory_size;
1692 }
1693
Chris Lattner24943d22010-06-08 16:52:24 +00001694 StreamString packet;
1695 packet.Printf("M%llx,%zx:", addr, size);
Greg Claytoncd548032011-02-01 01:31:41 +00001696 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00001697 StringExtractorGDBRemote response;
Greg Claytonc97bfdb2011-03-10 02:26:48 +00001698 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true))
Chris Lattner24943d22010-06-08 16:52:24 +00001699 {
Greg Clayton61d043b2011-03-22 04:00:09 +00001700 if (response.IsOKResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001701 {
1702 error.Clear();
1703 return size;
1704 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001705 else if (response.IsErrorResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001706 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +00001707 else if (response.IsUnsupportedResponse())
Chris Lattner24943d22010-06-08 16:52:24 +00001708 error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str());
1709 else
1710 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
1711 }
1712 else
1713 {
1714 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str());
1715 }
1716 return 0;
1717}
1718
1719lldb::addr_t
1720ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
1721{
Greg Clayton989816b2011-05-14 01:50:35 +00001722 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
1723
Greg Clayton2f085c62011-05-15 01:25:55 +00001724 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
Greg Clayton989816b2011-05-14 01:50:35 +00001725 switch (supported)
1726 {
1727 case eLazyBoolCalculate:
1728 case eLazyBoolYes:
1729 allocated_addr = m_gdb_comm.AllocateMemory (size, permissions);
1730 if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes)
1731 return allocated_addr;
1732
1733 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001734 // Call mmap() to create memory in the inferior..
1735 unsigned prot = 0;
1736 if (permissions & lldb::ePermissionsReadable)
1737 prot |= eMmapProtRead;
1738 if (permissions & lldb::ePermissionsWritable)
1739 prot |= eMmapProtWrite;
1740 if (permissions & lldb::ePermissionsExecutable)
1741 prot |= eMmapProtExec;
Greg Clayton989816b2011-05-14 01:50:35 +00001742
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001743 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
1744 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0))
1745 m_addr_to_mmap_size[allocated_addr] = size;
1746 else
1747 allocated_addr = LLDB_INVALID_ADDRESS;
Greg Clayton989816b2011-05-14 01:50:35 +00001748 break;
1749 }
1750
Chris Lattner24943d22010-06-08 16:52:24 +00001751 if (allocated_addr == LLDB_INVALID_ADDRESS)
Greg Clayton613b8732011-05-17 03:37:42 +00001752 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
Chris Lattner24943d22010-06-08 16:52:24 +00001753 else
1754 error.Clear();
1755 return allocated_addr;
1756}
1757
1758Error
Greg Claytona9385532011-11-18 07:03:08 +00001759ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,
1760 MemoryRegionInfo &region_info)
1761{
1762
1763 Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));
1764 return error;
1765}
1766
1767Error
Chris Lattner24943d22010-06-08 16:52:24 +00001768ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
1769{
1770 Error error;
Greg Clayton2f085c62011-05-15 01:25:55 +00001771 LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();
1772
1773 switch (supported)
1774 {
1775 case eLazyBoolCalculate:
1776 // We should never be deallocating memory without allocating memory
1777 // first so we should never get eLazyBoolCalculate
1778 error.SetErrorString ("tried to deallocate memory without ever allocating memory");
1779 break;
1780
1781 case eLazyBoolYes:
1782 if (!m_gdb_comm.DeallocateMemory (addr))
1783 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
1784 break;
1785
1786 case eLazyBoolNo:
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001787 // Call munmap() to deallocate memory in the inferior..
Greg Clayton2f085c62011-05-15 01:25:55 +00001788 {
1789 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
Peter Collingbourne4d623e82011-06-03 20:40:38 +00001790 if (pos != m_addr_to_mmap_size.end() &&
1791 InferiorCallMunmap(this, addr, pos->second))
1792 m_addr_to_mmap_size.erase (pos);
1793 else
1794 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
Greg Clayton2f085c62011-05-15 01:25:55 +00001795 }
1796 break;
1797 }
1798
Chris Lattner24943d22010-06-08 16:52:24 +00001799 return error;
1800}
1801
1802
1803//------------------------------------------------------------------
1804// Process STDIO
1805//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001806size_t
1807ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
1808{
1809 if (m_stdio_communication.IsConnected())
1810 {
1811 ConnectionStatus status;
1812 m_stdio_communication.Write(src, src_len, status, NULL);
1813 }
1814 return 0;
1815}
1816
1817Error
1818ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site)
1819{
1820 Error error;
1821 assert (bp_site != NULL);
1822
Greg Claytone005f2c2010-11-06 01:53:30 +00001823 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001824 user_id_t site_id = bp_site->GetID();
1825 const addr_t addr = bp_site->GetLoadAddress();
1826 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001827 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001828
1829 if (bp_site->IsEnabled())
1830 {
1831 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001832 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 +00001833 return error;
1834 }
1835 else
1836 {
1837 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1838
1839 if (bp_site->HardwarePreferred())
1840 {
1841 // Try and set hardware breakpoint, and if that fails, fall through
1842 // and set a software breakpoint?
Greg Claytonb72d0f02011-04-12 05:54:46 +00001843 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware))
Chris Lattner24943d22010-06-08 16:52:24 +00001844 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001845 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001846 {
1847 bp_site->SetEnabled(true);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001848 bp_site->SetType (BreakpointSite::eHardware);
Chris Lattner24943d22010-06-08 16:52:24 +00001849 return error;
1850 }
Chris Lattner24943d22010-06-08 16:52:24 +00001851 }
1852 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001853
1854 if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware))
Chris Lattner24943d22010-06-08 16:52:24 +00001855 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001856 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0)
1857 {
1858 bp_site->SetEnabled(true);
1859 bp_site->SetType (BreakpointSite::eExternal);
1860 return error;
1861 }
Chris Lattner24943d22010-06-08 16:52:24 +00001862 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001863
1864 return EnableSoftwareBreakpoint (bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +00001865 }
1866
1867 if (log)
1868 {
1869 const char *err_string = error.AsCString();
1870 log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s",
1871 bp_site->GetLoadAddress(),
1872 err_string ? err_string : "NULL");
1873 }
1874 // We shouldn't reach here on a successful breakpoint enable...
1875 if (error.Success())
1876 error.SetErrorToGenericError();
1877 return error;
1878}
1879
1880Error
1881ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site)
1882{
1883 Error error;
1884 assert (bp_site != NULL);
1885 addr_t addr = bp_site->GetLoadAddress();
1886 user_id_t site_id = bp_site->GetID();
Greg Claytone005f2c2010-11-06 01:53:30 +00001887 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001888 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001889 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx", site_id, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001890
1891 if (bp_site->IsEnabled())
1892 {
1893 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1894
Greg Claytonb72d0f02011-04-12 05:54:46 +00001895 BreakpointSite::Type bp_type = bp_site->GetType();
1896 switch (bp_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001897 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001898 case BreakpointSite::eSoftware:
1899 error = DisableSoftwareBreakpoint (bp_site);
1900 break;
1901
1902 case BreakpointSite::eHardware:
1903 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1904 error.SetErrorToGenericError();
1905 break;
1906
1907 case BreakpointSite::eExternal:
1908 if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size))
1909 error.SetErrorToGenericError();
1910 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001911 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001912 if (error.Success())
1913 bp_site->SetEnabled(false);
Chris Lattner24943d22010-06-08 16:52:24 +00001914 }
1915 else
1916 {
1917 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001918 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 +00001919 return error;
1920 }
1921
1922 if (error.Success())
1923 error.SetErrorToGenericError();
1924 return error;
1925}
1926
Johnny Chen21900fb2011-09-06 22:38:36 +00001927// Pre-requisite: wp != NULL.
1928static GDBStoppointType
Johnny Chenecd4feb2011-10-14 00:42:25 +00001929GetGDBStoppointType (Watchpoint *wp)
Johnny Chen21900fb2011-09-06 22:38:36 +00001930{
1931 assert(wp);
1932 bool watch_read = wp->WatchpointRead();
1933 bool watch_write = wp->WatchpointWrite();
1934
1935 // watch_read and watch_write cannot both be false.
1936 assert(watch_read || watch_write);
1937 if (watch_read && watch_write)
1938 return eWatchpointReadWrite;
Johnny Chen48a5e852011-09-09 20:35:15 +00001939 else if (watch_read)
Johnny Chen21900fb2011-09-06 22:38:36 +00001940 return eWatchpointRead;
Johnny Chen48a5e852011-09-09 20:35:15 +00001941 else // Must be watch_write, then.
Johnny Chen21900fb2011-09-06 22:38:36 +00001942 return eWatchpointWrite;
1943}
1944
Chris Lattner24943d22010-06-08 16:52:24 +00001945Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001946ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001947{
1948 Error error;
1949 if (wp)
1950 {
1951 user_id_t watchID = wp->GetID();
1952 addr_t addr = wp->GetLoadAddress();
Greg Claytone005f2c2010-11-06 01:53:30 +00001953 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001954 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001955 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %llu)", watchID);
Chris Lattner24943d22010-06-08 16:52:24 +00001956 if (wp->IsEnabled())
1957 {
1958 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001959 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %llu) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001960 return error;
1961 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001962
1963 GDBStoppointType type = GetGDBStoppointType(wp);
1964 // Pass down an appropriate z/Z packet...
1965 if (m_gdb_comm.SupportsGDBStoppointPacket (type))
Chris Lattner24943d22010-06-08 16:52:24 +00001966 {
Johnny Chen21900fb2011-09-06 22:38:36 +00001967 if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
1968 {
1969 wp->SetEnabled(true);
1970 return error;
1971 }
1972 else
1973 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00001974 }
Johnny Chen21900fb2011-09-06 22:38:36 +00001975 else
1976 error.SetErrorString("watchpoints not supported");
Chris Lattner24943d22010-06-08 16:52:24 +00001977 }
1978 else
1979 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001980 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00001981 }
1982 if (error.Success())
1983 error.SetErrorToGenericError();
1984 return error;
1985}
1986
1987Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00001988ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp)
Chris Lattner24943d22010-06-08 16:52:24 +00001989{
1990 Error error;
1991 if (wp)
1992 {
1993 user_id_t watchID = wp->GetID();
1994
Greg Claytone005f2c2010-11-06 01:53:30 +00001995 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001996
1997 addr_t addr = wp->GetLoadAddress();
1998 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001999 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx", watchID, (uint64_t)addr);
Chris Lattner24943d22010-06-08 16:52:24 +00002000
Johnny Chen21900fb2011-09-06 22:38:36 +00002001 if (!wp->IsEnabled())
2002 {
2003 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002004 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", watchID, (uint64_t)addr);
Johnny Chen21900fb2011-09-06 22:38:36 +00002005 return error;
2006 }
2007
Chris Lattner24943d22010-06-08 16:52:24 +00002008 if (wp->IsHardware())
2009 {
Johnny Chen21900fb2011-09-06 22:38:36 +00002010 GDBStoppointType type = GetGDBStoppointType(wp);
Chris Lattner24943d22010-06-08 16:52:24 +00002011 // Pass down an appropriate z/Z packet...
Johnny Chen21900fb2011-09-06 22:38:36 +00002012 if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
2013 {
2014 wp->SetEnabled(false);
2015 return error;
2016 }
2017 else
2018 error.SetErrorString("sending gdb watchpoint packet failed");
Chris Lattner24943d22010-06-08 16:52:24 +00002019 }
2020 // TODO: clear software watchpoints if we implement them
2021 }
2022 else
2023 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00002024 error.SetErrorString("Watchpoint argument was NULL.");
Chris Lattner24943d22010-06-08 16:52:24 +00002025 }
2026 if (error.Success())
2027 error.SetErrorToGenericError();
2028 return error;
2029}
2030
2031void
2032ProcessGDBRemote::Clear()
2033{
2034 m_flags = 0;
2035 m_thread_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002036}
2037
2038Error
2039ProcessGDBRemote::DoSignal (int signo)
2040{
2041 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00002042 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002043 if (log)
2044 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
2045
2046 if (!m_gdb_comm.SendAsyncSignal (signo))
2047 error.SetErrorStringWithFormat("failed to send signal %i", signo);
2048 return error;
2049}
2050
Chris Lattner24943d22010-06-08 16:52:24 +00002051Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002052ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url)
2053{
2054 ProcessLaunchInfo launch_info;
2055 return StartDebugserverProcess(debugserver_url, launch_info);
2056}
2057
2058Error
2059ProcessGDBRemote::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 +00002060{
2061 Error error;
2062 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
2063 {
2064 // If we locate debugserver, keep that located version around
2065 static FileSpec g_debugserver_file_spec;
2066
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002067 ProcessLaunchInfo debugserver_launch_info;
Chris Lattner24943d22010-06-08 16:52:24 +00002068 char debugserver_path[PATH_MAX];
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002069 FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile();
Chris Lattner24943d22010-06-08 16:52:24 +00002070
2071 // Always check to see if we have an environment override for the path
2072 // to the debugserver to use and use it if we do.
2073 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
2074 if (env_debugserver_path)
Greg Clayton537a7a82010-10-20 20:54:39 +00002075 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +00002076 else
2077 debugserver_file_spec = g_debugserver_file_spec;
2078 bool debugserver_exists = debugserver_file_spec.Exists();
2079 if (!debugserver_exists)
2080 {
2081 // The debugserver binary is in the LLDB.framework/Resources
2082 // directory.
Greg Clayton24b48ff2010-10-17 22:03:32 +00002083 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner24943d22010-06-08 16:52:24 +00002084 {
Greg Clayton24b48ff2010-10-17 22:03:32 +00002085 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002086 debugserver_exists = debugserver_file_spec.Exists();
Greg Clayton24b48ff2010-10-17 22:03:32 +00002087 if (debugserver_exists)
2088 {
2089 g_debugserver_file_spec = debugserver_file_spec;
2090 }
2091 else
2092 {
2093 g_debugserver_file_spec.Clear();
2094 debugserver_file_spec.Clear();
2095 }
Chris Lattner24943d22010-06-08 16:52:24 +00002096 }
2097 }
2098
2099 if (debugserver_exists)
2100 {
2101 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
2102
2103 m_stdio_communication.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00002104
Greg Claytone005f2c2010-11-06 01:53:30 +00002105 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002106
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002107 Args &debugserver_args = debugserver_launch_info.GetArguments();
Chris Lattner24943d22010-06-08 16:52:24 +00002108 char arg_cstr[PATH_MAX];
Chris Lattner24943d22010-06-08 16:52:24 +00002109
Chris Lattner24943d22010-06-08 16:52:24 +00002110 // Start args with "debugserver /file/path -r --"
2111 debugserver_args.AppendArgument(debugserver_path);
2112 debugserver_args.AppendArgument(debugserver_url);
Greg Clayton24b48ff2010-10-17 22:03:32 +00002113 // use native registers, not the GDB registers
2114 debugserver_args.AppendArgument("--native-regs");
2115 // make debugserver run in its own session so signals generated by
2116 // special terminal key sequences (^C) don't affect debugserver
2117 debugserver_args.AppendArgument("--setsid");
Chris Lattner24943d22010-06-08 16:52:24 +00002118
Chris Lattner24943d22010-06-08 16:52:24 +00002119 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
2120 if (env_debugserver_log_file)
2121 {
2122 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
2123 debugserver_args.AppendArgument(arg_cstr);
2124 }
2125
2126 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
2127 if (env_debugserver_log_flags)
2128 {
2129 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
2130 debugserver_args.AppendArgument(arg_cstr);
2131 }
Greg Claytoncc3e6402011-01-25 06:55:13 +00002132// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002133// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner24943d22010-06-08 16:52:24 +00002134
Greg Claytonb72d0f02011-04-12 05:54:46 +00002135 // We currently send down all arguments, attach pids, or attach
2136 // process names in dedicated GDB server packets, so we don't need
2137 // to pass them as arguments. This is currently because of all the
2138 // things we need to setup prior to launching: the environment,
2139 // current working dir, file actions, etc.
2140#if 0
Chris Lattner24943d22010-06-08 16:52:24 +00002141 // Now append the program arguments
Greg Claytona2f74232011-02-24 22:24:29 +00002142 if (inferior_argv)
Chris Lattner24943d22010-06-08 16:52:24 +00002143 {
Greg Claytona2f74232011-02-24 22:24:29 +00002144 // Terminate the debugserver args so we can now append the inferior args
2145 debugserver_args.AppendArgument("--");
Chris Lattner24943d22010-06-08 16:52:24 +00002146
Greg Claytona2f74232011-02-24 22:24:29 +00002147 for (int i = 0; inferior_argv[i] != NULL; ++i)
2148 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner24943d22010-06-08 16:52:24 +00002149 }
2150 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
2151 {
2152 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
2153 debugserver_args.AppendArgument (arg_cstr);
2154 }
2155 else if (attach_name && attach_name[0])
2156 {
2157 if (wait_for_launch)
2158 debugserver_args.AppendArgument ("--waitfor");
2159 else
2160 debugserver_args.AppendArgument ("--attach");
2161 debugserver_args.AppendArgument (attach_name);
2162 }
Chris Lattner24943d22010-06-08 16:52:24 +00002163#endif
Greg Claytonb72d0f02011-04-12 05:54:46 +00002164
2165 ProcessLaunchInfo::FileAction file_action;
2166
2167 // Close STDIN, STDOUT and STDERR. We might need to redirect them
2168 // to "/dev/null" if we run into any problems.
2169 file_action.Close (STDIN_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002170 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002171 file_action.Close (STDOUT_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002172 debugserver_launch_info.AppendFileAction (file_action);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002173 file_action.Close (STDERR_FILENO);
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002174 debugserver_launch_info.AppendFileAction (file_action);
Chris Lattner24943d22010-06-08 16:52:24 +00002175
2176 if (log)
2177 {
2178 StreamString strm;
2179 debugserver_args.Dump (&strm);
2180 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2181 }
2182
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002183 debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
2184 debugserver_launch_info.SetUserID(process_info.GetUserID());
Greg Clayton1c4642c2011-11-16 05:37:56 +00002185
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002186 error = Host::LaunchProcess(debugserver_launch_info);
Greg Claytone9d0df42010-07-02 01:29:13 +00002187
Greg Claytonb72d0f02011-04-12 05:54:46 +00002188 if (error.Success ())
Han Ming Ongd1040dd2012-02-25 01:07:38 +00002189 m_debugserver_pid = debugserver_launch_info.GetProcessID();
Greg Claytonb72d0f02011-04-12 05:54:46 +00002190 else
Chris Lattner24943d22010-06-08 16:52:24 +00002191 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2192
2193 if (error.Fail() || log)
Greg Claytond9919d32011-12-01 23:28:38 +00002194 error.PutToLog(log.get(), "Host::LaunchProcess (launch_info) => pid=%llu, path='%s'", m_debugserver_pid, debugserver_path);
Chris Lattner24943d22010-06-08 16:52:24 +00002195 }
2196 else
2197 {
Greg Clayton9c236732011-10-26 00:56:27 +00002198 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00002199 }
2200
2201 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2202 StartAsyncThread ();
2203 }
2204 return error;
2205}
2206
2207bool
2208ProcessGDBRemote::MonitorDebugserverProcess
2209(
2210 void *callback_baton,
2211 lldb::pid_t debugserver_pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +00002212 bool exited, // True if the process did exit
Chris Lattner24943d22010-06-08 16:52:24 +00002213 int signo, // Zero for no signal
2214 int exit_status // Exit value of process if signal is zero
2215)
2216{
Greg Clayton1c4642c2011-11-16 05:37:56 +00002217 // The baton is a "ProcessGDBRemote *". Now this class might be gone
2218 // and might not exist anymore, so we need to carefully try to get the
2219 // target for this process first since we have a race condition when
2220 // we are done running between getting the notice that the inferior
2221 // process has died and the debugserver that was debugging this process.
2222 // In our test suite, we are also continually running process after
2223 // process, so we must be very careful to make sure:
2224 // 1 - process object hasn't been deleted already
2225 // 2 - that a new process object hasn't been recreated in its place
Chris Lattner24943d22010-06-08 16:52:24 +00002226
2227 // "debugserver_pid" argument passed in is the process ID for
2228 // debugserver that we are tracking...
Greg Clayton1c4642c2011-11-16 05:37:56 +00002229 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002230
Greg Clayton75ccf502010-08-21 02:22:51 +00002231 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton72e1c782011-01-22 23:43:18 +00002232
Greg Clayton1c4642c2011-11-16 05:37:56 +00002233 // Get a shared pointer to the target that has a matching process pointer.
2234 // This target could be gone, or the target could already have a new process
2235 // object inside of it
2236 TargetSP target_sp (Debugger::FindTargetWithProcess(process));
2237
Greg Clayton72e1c782011-01-22 23:43:18 +00002238 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00002239 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 +00002240
Greg Clayton1c4642c2011-11-16 05:37:56 +00002241 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002242 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002243 // We found a process in a target that matches, but another thread
2244 // might be in the process of launching a new process that will
2245 // soon replace it, so get a shared pointer to the process so we
2246 // can keep it alive.
2247 ProcessSP process_sp (target_sp->GetProcessSP());
2248 // Now we have a shared pointer to the process that can't go away on us
2249 // so we now make sure it was the same as the one passed in, and also make
2250 // sure that our previous "process *" didn't get deleted and have a new
2251 // "process *" created in its place with the same pointer. To verify this
2252 // we make sure the process has our debugserver process ID. If we pass all
2253 // of these tests, then we are sure that this process is the one we were
2254 // looking for.
2255 if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid)
Chris Lattner24943d22010-06-08 16:52:24 +00002256 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002257 // Sleep for a half a second to make sure our inferior process has
2258 // time to set its exit status before we set it incorrectly when
2259 // both the debugserver and the inferior process shut down.
2260 usleep (500000);
2261 // If our process hasn't yet exited, debugserver might have died.
2262 // If the process did exit, the we are reaping it.
2263 const StateType state = process->GetState();
2264
2265 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2266 state != eStateInvalid &&
2267 state != eStateUnloaded &&
2268 state != eStateExited &&
2269 state != eStateDetached)
Chris Lattner24943d22010-06-08 16:52:24 +00002270 {
Greg Clayton1c4642c2011-11-16 05:37:56 +00002271 char error_str[1024];
2272 if (signo)
2273 {
2274 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2275 if (signal_cstr)
2276 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
2277 else
2278 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
2279 }
Chris Lattner24943d22010-06-08 16:52:24 +00002280 else
Greg Clayton1c4642c2011-11-16 05:37:56 +00002281 {
2282 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
2283 }
Greg Clayton75ccf502010-08-21 02:22:51 +00002284
Greg Clayton1c4642c2011-11-16 05:37:56 +00002285 process->SetExitStatus (-1, error_str);
2286 }
2287 // Debugserver has exited we need to let our ProcessGDBRemote
2288 // know that it no longer has a debugserver instance
2289 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton75ccf502010-08-21 02:22:51 +00002290 }
Chris Lattner24943d22010-06-08 16:52:24 +00002291 }
2292 return true;
2293}
2294
2295void
2296ProcessGDBRemote::KillDebugserverProcess ()
2297{
2298 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2299 {
2300 ::kill (m_debugserver_pid, SIGINT);
2301 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2302 }
2303}
2304
2305void
2306ProcessGDBRemote::Initialize()
2307{
2308 static bool g_initialized = false;
2309
2310 if (g_initialized == false)
2311 {
2312 g_initialized = true;
2313 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2314 GetPluginDescriptionStatic(),
2315 CreateInstance);
2316
2317 Log::Callbacks log_callbacks = {
2318 ProcessGDBRemoteLog::DisableLog,
2319 ProcessGDBRemoteLog::EnableLog,
2320 ProcessGDBRemoteLog::ListLogCategories
2321 };
2322
2323 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2324 }
2325}
2326
2327bool
Chris Lattner24943d22010-06-08 16:52:24 +00002328ProcessGDBRemote::StartAsyncThread ()
2329{
Greg Claytone005f2c2010-11-06 01:53:30 +00002330 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002331
2332 if (log)
2333 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2334
2335 // Create a thread that watches our internal state and controls which
2336 // events make it to clients (into the DCProcess event queue).
2337 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002338 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002339}
2340
2341void
2342ProcessGDBRemote::StopAsyncThread ()
2343{
Greg Claytone005f2c2010-11-06 01:53:30 +00002344 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002345
2346 if (log)
2347 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2348
2349 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
Jim Ingham8226e942011-10-28 01:11:35 +00002350
2351 // This will shut down the async thread.
2352 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Chris Lattner24943d22010-06-08 16:52:24 +00002353
2354 // Stop the stdio thread
Greg Clayton09c81ef2011-02-08 01:34:25 +00002355 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002356 {
2357 Host::ThreadJoin (m_async_thread, NULL, NULL);
2358 }
2359}
2360
2361
2362void *
2363ProcessGDBRemote::AsyncThread (void *arg)
2364{
2365 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2366
Greg Claytone005f2c2010-11-06 01:53:30 +00002367 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002368 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002369 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002370
2371 Listener listener ("ProcessGDBRemote::AsyncThread");
2372 EventSP event_sp;
2373 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2374 eBroadcastBitAsyncThreadShouldExit;
2375
2376 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2377 {
Greg Claytona2f74232011-02-24 22:24:29 +00002378 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2379
Chris Lattner24943d22010-06-08 16:52:24 +00002380 bool done = false;
2381 while (!done)
2382 {
2383 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002384 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002385 if (listener.WaitForEvent (NULL, event_sp))
2386 {
2387 const uint32_t event_type = event_sp->GetType();
Greg Claytona2f74232011-02-24 22:24:29 +00002388 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner24943d22010-06-08 16:52:24 +00002389 {
Greg Claytona2f74232011-02-24 22:24:29 +00002390 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002391 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 +00002392
Greg Claytona2f74232011-02-24 22:24:29 +00002393 switch (event_type)
2394 {
2395 case eBroadcastBitAsyncContinue:
Chris Lattner24943d22010-06-08 16:52:24 +00002396 {
Greg Claytona2f74232011-02-24 22:24:29 +00002397 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002398
Greg Claytona2f74232011-02-24 22:24:29 +00002399 if (continue_packet)
Chris Lattner24943d22010-06-08 16:52:24 +00002400 {
Greg Claytona2f74232011-02-24 22:24:29 +00002401 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2402 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2403 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002404 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002405
Greg Claytona2f74232011-02-24 22:24:29 +00002406 if (::strstr (continue_cstr, "vAttach") == NULL)
2407 process->SetPrivateState(eStateRunning);
2408 StringExtractorGDBRemote response;
2409 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner24943d22010-06-08 16:52:24 +00002410
Greg Claytona2f74232011-02-24 22:24:29 +00002411 switch (stop_state)
2412 {
2413 case eStateStopped:
2414 case eStateCrashed:
2415 case eStateSuspended:
Greg Clayton06709002011-12-06 04:51:14 +00002416 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002417 process->SetPrivateState (stop_state);
2418 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002419
Greg Claytona2f74232011-02-24 22:24:29 +00002420 case eStateExited:
Greg Clayton06709002011-12-06 04:51:14 +00002421 process->SetLastStopPacket (response);
Greg Claytona2f74232011-02-24 22:24:29 +00002422 response.SetFilePos(1);
2423 process->SetExitStatus(response.GetHexU8(), NULL);
2424 done = true;
2425 break;
2426
2427 case eStateInvalid:
2428 process->SetExitStatus(-1, "lost connection");
2429 break;
2430
2431 default:
2432 process->SetPrivateState (stop_state);
2433 break;
2434 }
Chris Lattner24943d22010-06-08 16:52:24 +00002435 }
2436 }
Greg Claytona2f74232011-02-24 22:24:29 +00002437 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002438
Greg Claytona2f74232011-02-24 22:24:29 +00002439 case eBroadcastBitAsyncThreadShouldExit:
2440 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002441 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Claytona2f74232011-02-24 22:24:29 +00002442 done = true;
2443 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002444
Greg Claytona2f74232011-02-24 22:24:29 +00002445 default:
2446 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002447 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 +00002448 done = true;
2449 break;
2450 }
2451 }
2452 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2453 {
2454 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2455 {
2456 process->SetExitStatus (-1, "lost connection");
Chris Lattner24943d22010-06-08 16:52:24 +00002457 done = true;
Greg Claytona2f74232011-02-24 22:24:29 +00002458 }
Chris Lattner24943d22010-06-08 16:52:24 +00002459 }
2460 }
2461 else
2462 {
2463 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002464 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 +00002465 done = true;
2466 }
2467 }
2468 }
2469
2470 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00002471 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00002472
2473 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2474 return NULL;
2475}
2476
Chris Lattner24943d22010-06-08 16:52:24 +00002477const char *
2478ProcessGDBRemote::GetDispatchQueueNameForThread
2479(
2480 addr_t thread_dispatch_qaddr,
2481 std::string &dispatch_queue_name
2482)
2483{
2484 dispatch_queue_name.clear();
2485 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2486 {
2487 // Cache the dispatch_queue_offsets_addr value so we don't always have
2488 // to look it up
2489 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2490 {
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002491 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
2492 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Clayton444fe992012-02-26 05:51:37 +00002493 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
2494 ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002495 if (module_sp)
2496 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2497
2498 if (dispatch_queue_offsets_symbol == NULL)
2499 {
Greg Clayton444fe992012-02-26 05:51:37 +00002500 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
2501 module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002502 if (module_sp)
2503 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2504 }
Chris Lattner24943d22010-06-08 16:52:24 +00002505 if (dispatch_queue_offsets_symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +00002506 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00002507
2508 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2509 return NULL;
2510 }
2511
2512 uint8_t memory_buffer[8];
Greg Clayton395fc332011-02-15 21:59:32 +00002513 DataExtractor data (memory_buffer,
2514 sizeof(memory_buffer),
2515 m_target.GetArchitecture().GetByteOrder(),
2516 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00002517
2518 // Excerpt from src/queue_private.h
2519 struct dispatch_queue_offsets_s
2520 {
2521 uint16_t dqo_version;
2522 uint16_t dqo_label;
2523 uint16_t dqo_label_size;
2524 } dispatch_queue_offsets;
2525
2526
2527 Error error;
2528 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
2529 {
2530 uint32_t data_offset = 0;
2531 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
2532 {
2533 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
2534 {
2535 data_offset = 0;
2536 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
2537 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
2538 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
2539 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
2540 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
2541 dispatch_queue_name.erase (bytes_read);
2542 }
2543 }
2544 }
2545 }
2546 if (dispatch_queue_name.empty())
2547 return NULL;
2548 return dispatch_queue_name.c_str();
2549}
2550
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002551//uint32_t
2552//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2553//{
2554// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
2555// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
2556// if (m_local_debugserver)
2557// {
2558// return Host::ListProcessesMatchingName (name, matches, pids);
2559// }
2560// else
2561// {
2562// // FIXME: Implement talking to the remote debugserver.
2563// return 0;
2564// }
2565//
2566//}
2567//
Jim Ingham55e01d82011-01-22 01:33:44 +00002568bool
2569ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
2570 lldb_private::StoppointCallbackContext *context,
2571 lldb::user_id_t break_id,
2572 lldb::user_id_t break_loc_id)
2573{
2574 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
2575 // run so I can stop it if that's what I want to do.
2576 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2577 if (log)
2578 log->Printf("Hit New Thread Notification breakpoint.");
2579 return false;
2580}
2581
2582
2583bool
2584ProcessGDBRemote::StartNoticingNewThreads()
2585{
2586 static const char *bp_names[] =
2587 {
2588 "start_wqthread",
Jim Inghamff276fe2011-02-08 05:19:01 +00002589 "_pthread_wqthread",
Jim Ingham55e01d82011-01-22 01:33:44 +00002590 "_pthread_start",
2591 NULL
2592 };
2593
2594 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2595 size_t num_bps = m_thread_observation_bps.size();
2596 if (num_bps != 0)
2597 {
2598 for (int i = 0; i < num_bps; i++)
2599 {
2600 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2601 if (break_sp)
2602 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002603 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002604 log->Printf("Enabled noticing new thread breakpoint.");
2605 break_sp->SetEnabled(true);
2606 }
2607 }
2608 }
2609 else
2610 {
2611 for (int i = 0; bp_names[i] != NULL; i++)
2612 {
Jim Inghamd6d47972011-09-23 00:54:11 +00002613 Breakpoint *breakpoint = m_target.CreateBreakpoint (NULL, NULL, bp_names[i], eFunctionNameTypeFull, true).get();
Jim Ingham55e01d82011-01-22 01:33:44 +00002614 if (breakpoint)
2615 {
Jim Ingham6bb73372011-10-15 00:21:37 +00002616 if (log && log->GetVerbose())
Jim Ingham55e01d82011-01-22 01:33:44 +00002617 log->Printf("Successfully created new thread notification breakpoint at \"%s\".", bp_names[i]);
2618 m_thread_observation_bps.push_back(breakpoint->GetID());
2619 breakpoint->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
2620 }
2621 else
2622 {
2623 if (log)
2624 log->Printf("Failed to create new thread notification breakpoint.");
2625 return false;
2626 }
2627 }
2628 }
2629
2630 return true;
2631}
2632
2633bool
2634ProcessGDBRemote::StopNoticingNewThreads()
2635{
Jim Inghamff276fe2011-02-08 05:19:01 +00002636 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham6bb73372011-10-15 00:21:37 +00002637 if (log && log->GetVerbose())
Jim Inghamff276fe2011-02-08 05:19:01 +00002638 log->Printf ("Disabling new thread notification breakpoint.");
Jim Ingham55e01d82011-01-22 01:33:44 +00002639 size_t num_bps = m_thread_observation_bps.size();
2640 if (num_bps != 0)
2641 {
2642 for (int i = 0; i < num_bps; i++)
2643 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002644
2645 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2646 if (break_sp)
2647 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002648 break_sp->SetEnabled(false);
2649 }
2650 }
2651 }
2652 return true;
2653}
2654
2655