blob: ab6c9150b9732197856b54978c9c046f072cddc1 [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>
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <sys/types.h>
Chris Lattner24943d22010-06-08 16:52:24 +000014#include <sys/stat.h>
Chris Lattner24943d22010-06-08 16:52:24 +000015
16// C++ Includes
17#include <algorithm>
18#include <map>
19
20// Other libraries and framework includes
21
22#include "lldb/Breakpoint/WatchpointLocation.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000023#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/ArchSpec.h"
25#include "lldb/Core/Debugger.h"
26#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000027#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Core/InputReader.h"
29#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/State.h"
32#include "lldb/Core/StreamString.h"
33#include "lldb/Core/Timer.h"
34#include "lldb/Host/TimeValue.h"
35#include "lldb/Symbol/ObjectFile.h"
36#include "lldb/Target/DynamicLoader.h"
37#include "lldb/Target/Target.h"
38#include "lldb/Target/TargetList.h"
Jason Molendadea5ea72010-06-09 21:28:42 +000039#include "lldb/Utility/PseudoTerminal.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
41// Project includes
42#include "lldb/Host/Host.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000043#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000044#include "GDBRemoteRegisterContext.h"
45#include "ProcessGDBRemote.h"
46#include "ProcessGDBRemoteLog.h"
47#include "ThreadGDBRemote.h"
Greg Clayton643ee732010-08-04 01:40:35 +000048#include "StopInfoMachException.h"
49
Chris Lattner24943d22010-06-08 16:52:24 +000050
Chris Lattner24943d22010-06-08 16:52:24 +000051
52#define DEBUGSERVER_BASENAME "debugserver"
53using namespace lldb;
54using namespace lldb_private;
55
56static inline uint16_t
57get_random_port ()
58{
59 return (arc4random() % (UINT16_MAX - 1000u)) + 1000u;
60}
61
62
63const char *
64ProcessGDBRemote::GetPluginNameStatic()
65{
66 return "process.gdb-remote";
67}
68
69const char *
70ProcessGDBRemote::GetPluginDescriptionStatic()
71{
72 return "GDB Remote protocol based debugging plug-in.";
73}
74
75void
76ProcessGDBRemote::Terminate()
77{
78 PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance);
79}
80
81
82Process*
83ProcessGDBRemote::CreateInstance (Target &target, Listener &listener)
84{
85 return new ProcessGDBRemote (target, listener);
86}
87
88bool
89ProcessGDBRemote::CanDebug(Target &target)
90{
91 // For now we are just making sure the file exists for a given module
92 ModuleSP exe_module_sp(target.GetExecutableModule());
93 if (exe_module_sp.get())
94 return exe_module_sp->GetFileSpec().Exists();
Jim Ingham7508e732010-08-09 23:31:02 +000095 // However, if there is no executable module, we return true since we might be preparing to attach.
96 return true;
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
99//----------------------------------------------------------------------
100// ProcessGDBRemote constructor
101//----------------------------------------------------------------------
102ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
103 Process (target, listener),
Chris Lattner24943d22010-06-08 16:52:24 +0000104 m_flags (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000105 m_stdio_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000106 m_gdb_comm(),
107 m_debugserver_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton75ccf502010-08-21 02:22:51 +0000108 m_debugserver_thread (LLDB_INVALID_HOST_THREAD),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000109 m_last_stop_packet (),
Chris Lattner24943d22010-06-08 16:52:24 +0000110 m_register_info (),
Chris Lattner24943d22010-06-08 16:52:24 +0000111 m_async_broadcaster ("lldb.process.gdb-remote.async-broadcaster"),
112 m_async_thread (LLDB_INVALID_HOST_THREAD),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000113 m_curr_tid (LLDB_INVALID_THREAD_ID),
114 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Chris Lattner24943d22010-06-08 16:52:24 +0000115 m_z0_supported (1),
Greg Claytonc1f45872011-02-12 06:28:37 +0000116 m_continue_c_tids (),
117 m_continue_C_tids (),
118 m_continue_s_tids (),
119 m_continue_S_tids (),
Chris Lattner24943d22010-06-08 16:52:24 +0000120 m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000121 m_packet_timeout (1),
122 m_max_memory_size (512),
Jim Ingham7508e732010-08-09 23:31:02 +0000123 m_waiting_for_attach (false),
Jim Ingham55e01d82011-01-22 01:33:44 +0000124 m_local_debugserver (true),
125 m_thread_observation_bps()
Chris Lattner24943d22010-06-08 16:52:24 +0000126{
127}
128
129//----------------------------------------------------------------------
130// Destructor
131//----------------------------------------------------------------------
132ProcessGDBRemote::~ProcessGDBRemote()
133{
Greg Clayton09c81ef2011-02-08 01:34:25 +0000134 if (IS_VALID_LLDB_HOST_THREAD(m_debugserver_thread))
Greg Clayton75ccf502010-08-21 02:22:51 +0000135 {
136 Host::ThreadCancel (m_debugserver_thread, NULL);
137 thread_result_t thread_result;
138 Host::ThreadJoin (m_debugserver_thread, &thread_result, NULL);
139 m_debugserver_thread = LLDB_INVALID_HOST_THREAD;
140 }
Chris Lattner24943d22010-06-08 16:52:24 +0000141 // m_mach_process.UnregisterNotificationCallbacks (this);
142 Clear();
143}
144
145//----------------------------------------------------------------------
146// PluginInterface
147//----------------------------------------------------------------------
148const char *
149ProcessGDBRemote::GetPluginName()
150{
151 return "Process debugging plug-in that uses the GDB remote protocol";
152}
153
154const char *
155ProcessGDBRemote::GetShortPluginName()
156{
157 return GetPluginNameStatic();
158}
159
160uint32_t
161ProcessGDBRemote::GetPluginVersion()
162{
163 return 1;
164}
165
166void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000167ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)
Chris Lattner24943d22010-06-08 16:52:24 +0000168{
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000169 if (!force && m_register_info.GetNumRegisters() > 0)
170 return;
171
172 char packet[128];
Chris Lattner24943d22010-06-08 16:52:24 +0000173 m_register_info.Clear();
174 StringExtractorGDBRemote::Type packet_type = StringExtractorGDBRemote::eResponse;
175 uint32_t reg_offset = 0;
176 uint32_t reg_num = 0;
177 for (; packet_type == StringExtractorGDBRemote::eResponse; ++reg_num)
178 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000179 const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num);
180 assert (packet_len < sizeof(packet));
Chris Lattner24943d22010-06-08 16:52:24 +0000181 StringExtractorGDBRemote response;
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000182 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, false))
Chris Lattner24943d22010-06-08 16:52:24 +0000183 {
184 packet_type = response.GetType();
185 if (packet_type == StringExtractorGDBRemote::eResponse)
186 {
187 std::string name;
188 std::string value;
189 ConstString reg_name;
190 ConstString alt_name;
191 ConstString set_name;
192 RegisterInfo reg_info = { NULL, // Name
193 NULL, // Alt name
194 0, // byte size
195 reg_offset, // offset
196 eEncodingUint, // encoding
197 eFormatHex, // formate
Chris Lattner24943d22010-06-08 16:52:24 +0000198 {
199 LLDB_INVALID_REGNUM, // GCC reg num
200 LLDB_INVALID_REGNUM, // DWARF reg num
201 LLDB_INVALID_REGNUM, // generic reg num
Jason Molenda3a4ea242010-09-10 07:49:16 +0000202 reg_num, // GDB reg num
203 reg_num // native register number
Chris Lattner24943d22010-06-08 16:52:24 +0000204 }
205 };
206
207 while (response.GetNameColonValue(name, value))
208 {
209 if (name.compare("name") == 0)
210 {
211 reg_name.SetCString(value.c_str());
212 }
213 else if (name.compare("alt-name") == 0)
214 {
215 alt_name.SetCString(value.c_str());
216 }
217 else if (name.compare("bitsize") == 0)
218 {
219 reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT;
220 }
221 else if (name.compare("offset") == 0)
222 {
223 uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molenda53d96862010-06-11 23:44:18 +0000224 if (reg_offset != offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000225 {
226 reg_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000227 }
228 }
229 else if (name.compare("encoding") == 0)
230 {
231 if (value.compare("uint") == 0)
232 reg_info.encoding = eEncodingUint;
233 else if (value.compare("sint") == 0)
234 reg_info.encoding = eEncodingSint;
235 else if (value.compare("ieee754") == 0)
236 reg_info.encoding = eEncodingIEEE754;
237 else if (value.compare("vector") == 0)
238 reg_info.encoding = eEncodingVector;
239 }
240 else if (name.compare("format") == 0)
241 {
242 if (value.compare("binary") == 0)
243 reg_info.format = eFormatBinary;
244 else if (value.compare("decimal") == 0)
245 reg_info.format = eFormatDecimal;
246 else if (value.compare("hex") == 0)
247 reg_info.format = eFormatHex;
248 else if (value.compare("float") == 0)
249 reg_info.format = eFormatFloat;
250 else if (value.compare("vector-sint8") == 0)
251 reg_info.format = eFormatVectorOfSInt8;
252 else if (value.compare("vector-uint8") == 0)
253 reg_info.format = eFormatVectorOfUInt8;
254 else if (value.compare("vector-sint16") == 0)
255 reg_info.format = eFormatVectorOfSInt16;
256 else if (value.compare("vector-uint16") == 0)
257 reg_info.format = eFormatVectorOfUInt16;
258 else if (value.compare("vector-sint32") == 0)
259 reg_info.format = eFormatVectorOfSInt32;
260 else if (value.compare("vector-uint32") == 0)
261 reg_info.format = eFormatVectorOfUInt32;
262 else if (value.compare("vector-float32") == 0)
263 reg_info.format = eFormatVectorOfFloat32;
264 else if (value.compare("vector-uint128") == 0)
265 reg_info.format = eFormatVectorOfUInt128;
266 }
267 else if (name.compare("set") == 0)
268 {
269 set_name.SetCString(value.c_str());
270 }
271 else if (name.compare("gcc") == 0)
272 {
273 reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
274 }
275 else if (name.compare("dwarf") == 0)
276 {
277 reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0);
278 }
279 else if (name.compare("generic") == 0)
280 {
281 if (value.compare("pc") == 0)
282 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
283 else if (value.compare("sp") == 0)
284 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
285 else if (value.compare("fp") == 0)
286 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
287 else if (value.compare("ra") == 0)
288 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
289 else if (value.compare("flags") == 0)
290 reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
291 }
292 }
293
Jason Molenda53d96862010-06-11 23:44:18 +0000294 reg_info.byte_offset = reg_offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000295 assert (reg_info.byte_size != 0);
296 reg_offset += reg_info.byte_size;
297 m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name);
298 }
299 }
300 else
301 {
302 packet_type = StringExtractorGDBRemote::eError;
303 }
304 }
305
306 if (reg_num == 0)
307 {
308 // We didn't get anything. See if we are debugging ARM and fill with
309 // a hard coded register set until we can get an updated debugserver
310 // down on the devices.
Greg Clayton940b1032011-02-23 00:35:02 +0000311 if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::arm)
Chris Lattner24943d22010-06-08 16:52:24 +0000312 m_register_info.HardcodeARMRegisters();
313 }
314 m_register_info.Finalize ();
315}
316
317Error
318ProcessGDBRemote::WillLaunch (Module* module)
319{
320 return WillLaunchOrAttach ();
321}
322
323Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000324ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000325{
326 return WillLaunchOrAttach ();
327}
328
329Error
Greg Clayton20d338f2010-11-18 05:57:03 +0000330ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +0000331{
332 return WillLaunchOrAttach ();
333}
334
335Error
Greg Claytone71e2582011-02-04 01:58:07 +0000336ProcessGDBRemote::DoConnectRemote (const char *remote_url)
337{
338 Error error (WillLaunchOrAttach ());
339
340 if (error.Fail())
341 return error;
342
343 if (strncmp (remote_url, "connect://", strlen ("connect://")) == 0)
344 {
345 error = ConnectToDebugserver (remote_url);
346 }
347 else
348 {
349 error.SetErrorStringWithFormat ("unsupported remote url: %s", remote_url);
350 }
351
352 if (error.Fail())
353 return error;
354 StartAsyncThread ();
355
356 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (m_packet_timeout);
357 if (pid == LLDB_INVALID_PROCESS_ID)
358 {
359 // We don't have a valid process ID, so note that we are connected
360 // and could now request to launch or attach, or get remote process
361 // listings...
362 SetPrivateState (eStateConnected);
363 }
364 else
365 {
366 // We have a valid process
367 SetID (pid);
368 StringExtractorGDBRemote response;
369 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, response, m_packet_timeout, false))
370 {
371 const StateType state = SetThreadStopInfo (response);
372 if (state == eStateStopped)
373 {
374 SetPrivateState (state);
375 }
376 else
377 error.SetErrorStringWithFormat ("Process %i was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state));
378 }
379 else
380 error.SetErrorStringWithFormat ("Process %i was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url);
381 }
382 return error;
383}
384
385Error
Chris Lattner24943d22010-06-08 16:52:24 +0000386ProcessGDBRemote::WillLaunchOrAttach ()
387{
388 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000389 m_stdio_communication.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000390 return error;
391}
392
393//----------------------------------------------------------------------
394// Process Control
395//----------------------------------------------------------------------
396Error
397ProcessGDBRemote::DoLaunch
398(
399 Module* module,
400 char const *argv[],
401 char const *envp[],
Greg Clayton452bf612010-08-31 18:35:14 +0000402 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +0000403 const char *stdin_path,
404 const char *stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +0000405 const char *stderr_path,
406 const char *working_dir
Chris Lattner24943d22010-06-08 16:52:24 +0000407)
408{
Greg Clayton4b407112010-09-30 21:49:03 +0000409 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000410 // ::LogSetBitMask (GDBR_LOG_DEFAULT);
411 // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD);
412 // ::LogSetLogFile ("/dev/stdout");
Chris Lattner24943d22010-06-08 16:52:24 +0000413
414 ObjectFile * object_file = module->GetObjectFile();
415 if (object_file)
416 {
417 ArchSpec inferior_arch(module->GetArchitecture());
418 char host_port[128];
419 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
Greg Claytone71e2582011-02-04 01:58:07 +0000420 char connect_url[128];
421 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000422
Greg Claytona2f74232011-02-24 22:24:29 +0000423 // Make sure we aren't already connected?
424 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000425 {
426 error = StartDebugserverProcess (host_port,
427 NULL,
428 NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000429 LLDB_INVALID_PROCESS_ID,
Greg Claytonde915be2011-01-23 05:56:20 +0000430 NULL,
431 false,
Chris Lattner24943d22010-06-08 16:52:24 +0000432 inferior_arch);
433 if (error.Fail())
434 return error;
435
Greg Claytone71e2582011-02-04 01:58:07 +0000436 error = ConnectToDebugserver (connect_url);
Greg Claytona2f74232011-02-24 22:24:29 +0000437 }
438
439 if (error.Success())
440 {
441 lldb_utility::PseudoTerminal pty;
442 const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0;
Greg Claytonafb81862011-03-02 21:34:46 +0000443
444 // If the debugserver is local and we aren't disabling STDIO, lets use
445 // a pseudo terminal to instead of relying on the 'O' packets for stdio
446 // since 'O' packets can really slow down debugging if the inferior
447 // does a lot of output.
448 if (m_local_debugserver && !disable_stdio)
Greg Claytona2f74232011-02-24 22:24:29 +0000449 {
450 const char *slave_name = NULL;
451 if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000452 {
Greg Claytona2f74232011-02-24 22:24:29 +0000453 if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0))
454 slave_name = pty.GetSlaveName (NULL, 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000455 }
Greg Claytona2f74232011-02-24 22:24:29 +0000456 if (stdin_path == NULL)
457 stdin_path = slave_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000458
Greg Claytona2f74232011-02-24 22:24:29 +0000459 if (stdout_path == NULL)
460 stdout_path = slave_name;
461
462 if (stderr_path == NULL)
463 stderr_path = slave_name;
464 }
465
Greg Claytonafb81862011-03-02 21:34:46 +0000466 // Set STDIN to /dev/null if we want STDIO disabled or if either
467 // STDOUT or STDERR have been set to something and STDIN hasn't
468 if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000469 stdin_path = "/dev/null";
470
Greg Claytonafb81862011-03-02 21:34:46 +0000471 // Set STDOUT to /dev/null if we want STDIO disabled or if either
472 // STDIN or STDERR have been set to something and STDOUT hasn't
473 if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000474 stdout_path = "/dev/null";
475
Greg Claytonafb81862011-03-02 21:34:46 +0000476 // Set STDERR to /dev/null if we want STDIO disabled or if either
477 // STDIN or STDOUT have been set to something and STDERR hasn't
478 if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path)))
Greg Claytona2f74232011-02-24 22:24:29 +0000479 stderr_path = "/dev/null";
480
481 if (stdin_path)
482 m_gdb_comm.SetSTDIN (stdin_path);
483 if (stdout_path)
484 m_gdb_comm.SetSTDOUT (stdout_path);
485 if (stderr_path)
486 m_gdb_comm.SetSTDERR (stderr_path);
487
488 m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR);
489
490
491 if (working_dir && working_dir[0])
492 {
493 m_gdb_comm.SetWorkingDir (working_dir);
494 }
495
496 // Send the environment and the program + arguments after we connect
497 if (envp)
498 {
499 const char *env_entry;
500 for (int i=0; (env_entry = envp[i]); ++i)
Greg Clayton960d6a42010-08-03 00:35:52 +0000501 {
Greg Claytona2f74232011-02-24 22:24:29 +0000502 if (m_gdb_comm.SendEnvironmentPacket(env_entry, m_packet_timeout) != 0)
503 break;
Greg Clayton960d6a42010-08-03 00:35:52 +0000504 }
Greg Claytona2f74232011-02-24 22:24:29 +0000505 }
Greg Clayton960d6a42010-08-03 00:35:52 +0000506
Greg Claytona2f74232011-02-24 22:24:29 +0000507 const uint32_t arg_timeout_seconds = 10;
508 int arg_packet_err = m_gdb_comm.SendArgumentsPacket (argv, arg_timeout_seconds);
509 if (arg_packet_err == 0)
510 {
511 std::string error_str;
512 if (m_gdb_comm.GetLaunchSuccess (m_packet_timeout, error_str))
Chris Lattner24943d22010-06-08 16:52:24 +0000513 {
Greg Claytona2f74232011-02-24 22:24:29 +0000514 SetID (m_gdb_comm.GetCurrentProcessID (m_packet_timeout));
Chris Lattner24943d22010-06-08 16:52:24 +0000515 }
516 else
517 {
Greg Claytona2f74232011-02-24 22:24:29 +0000518 error.SetErrorString (error_str.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000519 }
Greg Claytona2f74232011-02-24 22:24:29 +0000520 }
521 else
522 {
523 error.SetErrorStringWithFormat("'A' packet returned an error: %i.\n", arg_packet_err);
524 }
Chris Lattner24943d22010-06-08 16:52:24 +0000525
Greg Claytona2f74232011-02-24 22:24:29 +0000526 if (GetID() == LLDB_INVALID_PROCESS_ID)
527 {
528 KillDebugserverProcess ();
529 return error;
530 }
531
532 StringExtractorGDBRemote response;
533 if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, response, m_packet_timeout, false))
534 {
535 SetPrivateState (SetThreadStopInfo (response));
536
537 if (!disable_stdio)
538 {
539 if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd)
540 SetUpProcessInputReader (pty.ReleaseMasterFileDescriptor());
541 }
Chris Lattner24943d22010-06-08 16:52:24 +0000542 }
543 }
Chris Lattner24943d22010-06-08 16:52:24 +0000544 }
545 else
546 {
547 // Set our user ID to an invalid process ID.
548 SetID(LLDB_INVALID_PROCESS_ID);
Greg Clayton940b1032011-02-23 00:35:02 +0000549 error.SetErrorStringWithFormat("Failed to get object file from '%s' for arch %s.\n",
550 module->GetFileSpec().GetFilename().AsCString(),
551 module->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000552 }
Chris Lattner24943d22010-06-08 16:52:24 +0000553 return error;
Greg Clayton4b407112010-09-30 21:49:03 +0000554
Chris Lattner24943d22010-06-08 16:52:24 +0000555}
556
557
558Error
Greg Claytone71e2582011-02-04 01:58:07 +0000559ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)
Chris Lattner24943d22010-06-08 16:52:24 +0000560{
561 Error error;
562 // Sleep and wait a bit for debugserver to start to listen...
563 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
564 if (conn_ap.get())
565 {
Chris Lattner24943d22010-06-08 16:52:24 +0000566 const uint32_t max_retry_count = 50;
567 uint32_t retry_count = 0;
568 while (!m_gdb_comm.IsConnected())
569 {
Greg Claytone71e2582011-02-04 01:58:07 +0000570 if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess)
Chris Lattner24943d22010-06-08 16:52:24 +0000571 {
572 m_gdb_comm.SetConnection (conn_ap.release());
573 break;
574 }
575 retry_count++;
576
577 if (retry_count >= max_retry_count)
578 break;
579
580 usleep (100000);
581 }
582 }
583
584 if (!m_gdb_comm.IsConnected())
585 {
586 if (error.Success())
587 error.SetErrorString("not connected to remote gdb server");
588 return error;
589 }
590
Chris Lattner24943d22010-06-08 16:52:24 +0000591 if (m_gdb_comm.StartReadThread(&error))
592 {
593 // Send an initial ack
Greg Claytona4881d02011-01-22 07:12:45 +0000594 m_gdb_comm.SendAck();
Chris Lattner24943d22010-06-08 16:52:24 +0000595
596 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
Greg Clayton75ccf502010-08-21 02:22:51 +0000597 m_debugserver_thread = Host::StartMonitoringChildProcess (MonitorDebugserverProcess,
598 this,
599 m_debugserver_pid,
600 false);
601
Greg Claytonc1f45872011-02-12 06:28:37 +0000602 m_gdb_comm.ResetDiscoverableSettings();
603 m_gdb_comm.GetSendAcks ();
604 m_gdb_comm.GetThreadSuffixSupported ();
605 m_gdb_comm.GetHostInfo ();
606 m_gdb_comm.GetVContSupported ('c');
Chris Lattner24943d22010-06-08 16:52:24 +0000607 }
608 return error;
609}
610
611void
612ProcessGDBRemote::DidLaunchOrAttach ()
613{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000614 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
615 if (log)
616 log->Printf ("ProcessGDBRemote::DidLaunch()");
Greg Clayton75c703d2011-02-16 04:46:07 +0000617 if (GetID() != LLDB_INVALID_PROCESS_ID)
Chris Lattner24943d22010-06-08 16:52:24 +0000618 {
619 m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
620
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000621 BuildDynamicRegisterInfo (false);
Greg Clayton20d338f2010-11-18 05:57:03 +0000622
Greg Clayton395fc332011-02-15 21:59:32 +0000623 m_target.GetArchitecture().SetByteOrder (m_gdb_comm.GetByteOrder());
Greg Clayton20d338f2010-11-18 05:57:03 +0000624
Chris Lattner24943d22010-06-08 16:52:24 +0000625 StreamString strm;
626
Chris Lattner24943d22010-06-08 16:52:24 +0000627 // See if the GDB server supports the qHostInfo information
628 const char *vendor = m_gdb_comm.GetVendorString().AsCString();
629 const char *os_type = m_gdb_comm.GetOSString().AsCString();
Greg Claytonfc7920f2011-02-09 03:09:55 +0000630 ArchSpec target_arch (GetTarget().GetArchitecture());
631 ArchSpec gdb_remote_arch (m_gdb_comm.GetHostArchitecture());
632
Greg Claytonc62176d2011-02-09 03:12:09 +0000633 // If the remote host is ARM and we have apple as the vendor, then
Greg Claytonfc7920f2011-02-09 03:09:55 +0000634 // ARM executables and shared libraries can have mixed ARM architectures.
635 // You can have an armv6 executable, and if the host is armv7, then the
636 // system will load the best possible architecture for all shared libraries
637 // it has, so we really need to take the remote host architecture as our
638 // defacto architecture in this case.
639
Greg Clayton940b1032011-02-23 00:35:02 +0000640 if (gdb_remote_arch.GetMachine() == llvm::Triple::arm &&
641 gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple)
Greg Claytonfc7920f2011-02-09 03:09:55 +0000642 {
643 GetTarget().SetArchitecture (gdb_remote_arch);
644 target_arch = gdb_remote_arch;
645 }
646
Greg Clayton395fc332011-02-15 21:59:32 +0000647 if (vendor)
648 m_target.GetArchitecture().GetTriple().setVendorName(vendor);
649 if (os_type)
650 m_target.GetArchitecture().GetTriple().setOSName(os_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000651 }
652}
653
654void
655ProcessGDBRemote::DidLaunch ()
656{
657 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000658}
659
660Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000661ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid)
Chris Lattner24943d22010-06-08 16:52:24 +0000662{
663 Error error;
664 // Clear out and clean up from any current state
665 Clear();
Greg Claytona2f74232011-02-24 22:24:29 +0000666 const ArchSpec &arch_spec = GetTarget().GetArchitecture();
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000667
Chris Lattner24943d22010-06-08 16:52:24 +0000668 if (attach_pid != LLDB_INVALID_PROCESS_ID)
669 {
Greg Claytona2f74232011-02-24 22:24:29 +0000670 // Make sure we aren't already connected?
671 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000672 {
Greg Claytona2f74232011-02-24 22:24:29 +0000673 char host_port[128];
674 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
675 char connect_url[128];
676 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
Chris Lattner24943d22010-06-08 16:52:24 +0000677
Greg Claytona2f74232011-02-24 22:24:29 +0000678 error = StartDebugserverProcess (host_port, // debugserver_url
679 NULL, // inferior_argv
680 NULL, // inferior_envp
681 LLDB_INVALID_PROCESS_ID, // Don't send any attach to pid options to debugserver
682 NULL, // Don't send any attach by process name option to debugserver
683 false, // Don't send any attach wait_for_launch flag as an option to debugserver
684 arch_spec);
685
686 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000687 {
Greg Claytona2f74232011-02-24 22:24:29 +0000688 const char *error_string = error.AsCString();
689 if (error_string == NULL)
690 error_string = "unable to launch " DEBUGSERVER_BASENAME;
691
692 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000693 }
Greg Claytona2f74232011-02-24 22:24:29 +0000694 else
695 {
696 error = ConnectToDebugserver (connect_url);
697 }
698 }
699
700 if (error.Success())
701 {
702 char packet[64];
703 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%x", attach_pid);
704
705 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));
Chris Lattner24943d22010-06-08 16:52:24 +0000706 }
707 }
Chris Lattner24943d22010-06-08 16:52:24 +0000708 return error;
709}
710
711size_t
712ProcessGDBRemote::AttachInputReaderCallback
713(
714 void *baton,
715 InputReader *reader,
716 lldb::InputReaderAction notification,
717 const char *bytes,
718 size_t bytes_len
719)
720{
721 if (notification == eInputReaderGotToken)
722 {
723 ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton;
724 if (gdb_process->m_waiting_for_attach)
725 gdb_process->m_waiting_for_attach = false;
726 reader->SetIsDone(true);
727 return 1;
728 }
729 return 0;
730}
731
732Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000733ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +0000734{
735 Error error;
736 // Clear out and clean up from any current state
737 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000738
Chris Lattner24943d22010-06-08 16:52:24 +0000739 if (process_name && process_name[0])
740 {
Greg Claytona2f74232011-02-24 22:24:29 +0000741 // Make sure we aren't already connected?
742 if (!m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +0000743 {
Chris Lattner24943d22010-06-08 16:52:24 +0000744
Greg Claytona2f74232011-02-24 22:24:29 +0000745 const ArchSpec &arch_spec = GetTarget().GetArchitecture();
746
747 char host_port[128];
748 snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ());
749 char connect_url[128];
750 snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port);
751
752 error = StartDebugserverProcess (host_port, // debugserver_url
753 NULL, // inferior_argv
754 NULL, // inferior_envp
755 LLDB_INVALID_PROCESS_ID, // Don't send any attach to pid options to debugserver
756 NULL, // Don't send any attach by process name option to debugserver
757 false, // Don't send any attach wait_for_launch flag as an option to debugserver
758 arch_spec);
759 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000760 {
Greg Claytona2f74232011-02-24 22:24:29 +0000761 const char *error_string = error.AsCString();
762 if (error_string == NULL)
763 error_string = "unable to launch " DEBUGSERVER_BASENAME;
Chris Lattner24943d22010-06-08 16:52:24 +0000764
Greg Claytona2f74232011-02-24 22:24:29 +0000765 SetExitStatus (-1, error_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000766 }
Greg Claytona2f74232011-02-24 22:24:29 +0000767 else
768 {
769 error = ConnectToDebugserver (connect_url);
770 }
771 }
772
773 if (error.Success())
774 {
775 StreamString packet;
776
777 if (wait_for_launch)
778 packet.PutCString("vAttachWait");
779 else
780 packet.PutCString("vAttachName");
781 packet.PutChar(';');
782 packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
783
784 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));
785
Chris Lattner24943d22010-06-08 16:52:24 +0000786 }
787 }
Chris Lattner24943d22010-06-08 16:52:24 +0000788 return error;
789}
790
Chris Lattner24943d22010-06-08 16:52:24 +0000791
792void
793ProcessGDBRemote::DidAttach ()
794{
Greg Claytone71e2582011-02-04 01:58:07 +0000795 DidLaunchOrAttach ();
Chris Lattner24943d22010-06-08 16:52:24 +0000796}
797
798Error
799ProcessGDBRemote::WillResume ()
800{
Greg Claytonc1f45872011-02-12 06:28:37 +0000801 m_continue_c_tids.clear();
802 m_continue_C_tids.clear();
803 m_continue_s_tids.clear();
804 m_continue_S_tids.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000805 return Error();
806}
807
808Error
809ProcessGDBRemote::DoResume ()
810{
Jim Ingham3ae449a2010-11-17 02:32:00 +0000811 Error error;
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000812 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
813 if (log)
814 log->Printf ("ProcessGDBRemote::Resume()");
Greg Claytonb749a262010-12-03 06:02:24 +0000815
816 Listener listener ("gdb-remote.resume-packet-sent");
817 if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))
818 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000819 StreamString continue_packet;
820 bool continue_packet_error = false;
821 if (m_gdb_comm.HasAnyVContSupport ())
822 {
823 continue_packet.PutCString ("vCont");
824
825 if (!m_continue_c_tids.empty())
826 {
827 if (m_gdb_comm.GetVContSupported ('c'))
828 {
829 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)
830 continue_packet.Printf(";c:%4.4x", *t_pos);
831 }
832 else
833 continue_packet_error = true;
834 }
835
836 if (!continue_packet_error && !m_continue_C_tids.empty())
837 {
838 if (m_gdb_comm.GetVContSupported ('C'))
839 {
840 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)
841 continue_packet.Printf(";C%2.2x:%4.4x", s_pos->second, s_pos->first);
842 }
843 else
844 continue_packet_error = true;
845 }
Greg Claytonb749a262010-12-03 06:02:24 +0000846
Greg Claytonc1f45872011-02-12 06:28:37 +0000847 if (!continue_packet_error && !m_continue_s_tids.empty())
848 {
849 if (m_gdb_comm.GetVContSupported ('s'))
850 {
851 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)
852 continue_packet.Printf(";s:%4.4x", *t_pos);
853 }
854 else
855 continue_packet_error = true;
856 }
857
858 if (!continue_packet_error && !m_continue_S_tids.empty())
859 {
860 if (m_gdb_comm.GetVContSupported ('S'))
861 {
862 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)
863 continue_packet.Printf(";S%2.2x:%4.4x", s_pos->second, s_pos->first);
864 }
865 else
866 continue_packet_error = true;
867 }
868
869 if (continue_packet_error)
870 continue_packet.GetString().clear();
871 }
872 else
873 continue_packet_error = true;
874
875 if (continue_packet_error)
876 {
877 continue_packet_error = false;
878 // Either no vCont support, or we tried to use part of the vCont
879 // packet that wasn't supported by the remote GDB server.
880 // We need to try and make a simple packet that can do our continue
881 const size_t num_threads = GetThreadList().GetSize();
882 const size_t num_continue_c_tids = m_continue_c_tids.size();
883 const size_t num_continue_C_tids = m_continue_C_tids.size();
884 const size_t num_continue_s_tids = m_continue_s_tids.size();
885 const size_t num_continue_S_tids = m_continue_S_tids.size();
886 if (num_continue_c_tids > 0)
887 {
888 if (num_continue_c_tids == num_threads)
889 {
890 // All threads are resuming...
891 SetCurrentGDBRemoteThreadForRun (-1);
892 continue_packet.PutChar ('c');
893 }
894 else if (num_continue_c_tids == 1 &&
895 num_continue_C_tids == 0 &&
896 num_continue_s_tids == 0 &&
897 num_continue_S_tids == 0 )
898 {
899 // Only one thread is continuing
900 SetCurrentGDBRemoteThreadForRun (m_continue_c_tids.front());
901 continue_packet.PutChar ('c');
902 }
903 else
904 {
905 // We can't represent this continue packet....
906 continue_packet_error = true;
907 }
908 }
909
910 if (!continue_packet_error && num_continue_C_tids > 0)
911 {
912 if (num_continue_C_tids == num_threads)
913 {
914 const int continue_signo = m_continue_C_tids.front().second;
915 if (num_continue_C_tids > 1)
916 {
917 for (size_t i=1; i<num_threads; ++i)
918 {
919 if (m_continue_C_tids[i].second != continue_signo)
920 continue_packet_error = true;
921 }
922 }
923 if (!continue_packet_error)
924 {
925 // Add threads continuing with the same signo...
926 SetCurrentGDBRemoteThreadForRun (-1);
927 continue_packet.Printf("C%2.2x", continue_signo);
928 }
929 }
930 else if (num_continue_c_tids == 0 &&
931 num_continue_C_tids == 1 &&
932 num_continue_s_tids == 0 &&
933 num_continue_S_tids == 0 )
934 {
935 // Only one thread is continuing with signal
936 SetCurrentGDBRemoteThreadForRun (m_continue_C_tids.front().first);
937 continue_packet.Printf("C%2.2x", m_continue_C_tids.front().second);
938 }
939 else
940 {
941 // We can't represent this continue packet....
942 continue_packet_error = true;
943 }
944 }
945
946 if (!continue_packet_error && num_continue_s_tids > 0)
947 {
948 if (num_continue_s_tids == num_threads)
949 {
950 // All threads are resuming...
951 SetCurrentGDBRemoteThreadForRun (-1);
952 continue_packet.PutChar ('s');
953 }
954 else if (num_continue_c_tids == 0 &&
955 num_continue_C_tids == 0 &&
956 num_continue_s_tids == 1 &&
957 num_continue_S_tids == 0 )
958 {
959 // Only one thread is stepping
960 SetCurrentGDBRemoteThreadForRun (m_continue_s_tids.front());
961 continue_packet.PutChar ('s');
962 }
963 else
964 {
965 // We can't represent this continue packet....
966 continue_packet_error = true;
967 }
968 }
969
970 if (!continue_packet_error && num_continue_S_tids > 0)
971 {
972 if (num_continue_S_tids == num_threads)
973 {
974 const int step_signo = m_continue_S_tids.front().second;
975 // Are all threads trying to step with the same signal?
976 if (num_continue_S_tids > 1)
977 {
978 for (size_t i=1; i<num_threads; ++i)
979 {
980 if (m_continue_S_tids[i].second != step_signo)
981 continue_packet_error = true;
982 }
983 }
984 if (!continue_packet_error)
985 {
986 // Add threads stepping with the same signo...
987 SetCurrentGDBRemoteThreadForRun (-1);
988 continue_packet.Printf("S%2.2x", step_signo);
989 }
990 }
991 else if (num_continue_c_tids == 0 &&
992 num_continue_C_tids == 0 &&
993 num_continue_s_tids == 0 &&
994 num_continue_S_tids == 1 )
995 {
996 // Only one thread is stepping with signal
997 SetCurrentGDBRemoteThreadForRun (m_continue_S_tids.front().first);
998 continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second);
999 }
1000 else
1001 {
1002 // We can't represent this continue packet....
1003 continue_packet_error = true;
1004 }
1005 }
1006 }
1007
1008 if (continue_packet_error)
1009 {
1010 error.SetErrorString ("can't make continue packet for this resume");
1011 }
1012 else
1013 {
1014 EventSP event_sp;
1015 TimeValue timeout;
1016 timeout = TimeValue::Now();
1017 timeout.OffsetWithSeconds (5);
1018 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));
1019
1020 if (listener.WaitForEvent (&timeout, event_sp) == false)
1021 error.SetErrorString("Resume timed out.");
1022 }
Greg Claytonb749a262010-12-03 06:02:24 +00001023 }
1024
Jim Ingham3ae449a2010-11-17 02:32:00 +00001025 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001026}
1027
1028size_t
1029ProcessGDBRemote::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
1030{
1031 const uint8_t *trap_opcode = NULL;
1032 uint32_t trap_opcode_size = 0;
1033
1034 static const uint8_t g_arm_breakpoint_opcode[] = { 0xFE, 0xDE, 0xFF, 0xE7 };
1035 //static const uint8_t g_thumb_breakpooint_opcode[] = { 0xFE, 0xDE };
1036 static const uint8_t g_ppc_breakpoint_opcode[] = { 0x7F, 0xC0, 0x00, 0x08 };
1037 static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
1038
Greg Clayton940b1032011-02-23 00:35:02 +00001039 const llvm::Triple::ArchType machine = GetTarget().GetArchitecture().GetMachine();
1040 switch (machine)
Chris Lattner24943d22010-06-08 16:52:24 +00001041 {
Greg Clayton940b1032011-02-23 00:35:02 +00001042 case llvm::Triple::x86:
1043 case llvm::Triple::x86_64:
Greg Claytoncf015052010-06-11 03:25:34 +00001044 trap_opcode = g_i386_breakpoint_opcode;
1045 trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
1046 break;
1047
Greg Clayton940b1032011-02-23 00:35:02 +00001048 case llvm::Triple::arm:
Greg Claytoncf015052010-06-11 03:25:34 +00001049 // TODO: fill this in for ARM. We need to dig up the symbol for
1050 // the address in the breakpoint locaiton and figure out if it is
1051 // an ARM or Thumb breakpoint.
1052 trap_opcode = g_arm_breakpoint_opcode;
1053 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1054 break;
1055
Greg Clayton940b1032011-02-23 00:35:02 +00001056 case llvm::Triple::ppc:
1057 case llvm::Triple::ppc64:
Greg Claytoncf015052010-06-11 03:25:34 +00001058 trap_opcode = g_ppc_breakpoint_opcode;
1059 trap_opcode_size = sizeof(g_ppc_breakpoint_opcode);
1060 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001061
Greg Claytoncf015052010-06-11 03:25:34 +00001062 default:
1063 assert(!"Unhandled architecture in ProcessMacOSX::GetSoftwareBreakpointTrapOpcode()");
1064 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001065 }
1066
1067 if (trap_opcode && trap_opcode_size)
1068 {
1069 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1070 return trap_opcode_size;
1071 }
1072 return 0;
1073}
1074
1075uint32_t
1076ProcessGDBRemote::UpdateThreadListIfNeeded ()
1077{
1078 // locker will keep a mutex locked until it goes out of scope
Greg Claytone005f2c2010-11-06 01:53:30 +00001079 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001080 if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))
Chris Lattner24943d22010-06-08 16:52:24 +00001081 log->Printf ("ProcessGDBRemote::%s (pid = %i)", __FUNCTION__, GetID());
1082
Greg Clayton5205f0b2010-09-03 17:10:42 +00001083 Mutex::Locker locker (m_thread_list.GetMutex ());
Chris Lattner24943d22010-06-08 16:52:24 +00001084 const uint32_t stop_id = GetStopID();
1085 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1086 {
1087 // Update the thread list's stop id immediately so we don't recurse into this function.
1088 ThreadList curr_thread_list (this);
1089 curr_thread_list.SetStopID(stop_id);
1090
1091 Error err;
1092 StringExtractorGDBRemote response;
1093 for (m_gdb_comm.SendPacketAndWaitForResponse("qfThreadInfo", response, 1, false);
1094 response.IsNormalPacket();
1095 m_gdb_comm.SendPacketAndWaitForResponse("qsThreadInfo", response, 1, false))
1096 {
1097 char ch = response.GetChar();
1098 if (ch == 'l')
1099 break;
1100 if (ch == 'm')
1101 {
1102 do
1103 {
1104 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1105
1106 if (tid != LLDB_INVALID_THREAD_ID)
1107 {
1108 ThreadSP thread_sp (GetThreadList().FindThreadByID (tid, false));
Greg Claytona875b642011-01-09 21:07:35 +00001109 if (!thread_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001110 thread_sp.reset (new ThreadGDBRemote (*this, tid));
1111 curr_thread_list.AddThread(thread_sp);
1112 }
1113
1114 ch = response.GetChar();
1115 } while (ch == ',');
1116 }
1117 }
1118
1119 m_thread_list = curr_thread_list;
1120
1121 SetThreadStopInfo (m_last_stop_packet);
1122 }
1123 return GetThreadList().GetSize(false);
1124}
1125
1126
1127StateType
1128ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)
1129{
1130 const char stop_type = stop_packet.GetChar();
1131 switch (stop_type)
1132 {
1133 case 'T':
1134 case 'S':
1135 {
Greg Claytonc3c46612011-02-15 00:19:15 +00001136 if (GetStopID() == 0)
1137 {
1138 // Our first stop, make sure we have a process ID, and also make
1139 // sure we know about our registers
1140 if (GetID() == LLDB_INVALID_PROCESS_ID)
1141 {
1142 lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (1);
1143 if (pid != LLDB_INVALID_PROCESS_ID)
1144 SetID (pid);
1145 }
1146 BuildDynamicRegisterInfo (true);
1147 }
Chris Lattner24943d22010-06-08 16:52:24 +00001148 // Stop with signal and thread info
1149 const uint8_t signo = stop_packet.GetHexU8();
1150 std::string name;
1151 std::string value;
1152 std::string thread_name;
1153 uint32_t exc_type = 0;
Greg Clayton7661a982010-07-23 16:45:51 +00001154 std::vector<addr_t> exc_data;
Chris Lattner24943d22010-06-08 16:52:24 +00001155 uint32_t tid = LLDB_INVALID_THREAD_ID;
1156 addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS;
1157 uint32_t exc_data_count = 0;
Greg Claytona875b642011-01-09 21:07:35 +00001158 ThreadSP thread_sp;
1159
Chris Lattner24943d22010-06-08 16:52:24 +00001160 while (stop_packet.GetNameColonValue(name, value))
1161 {
1162 if (name.compare("metype") == 0)
1163 {
1164 // exception type in big endian hex
1165 exc_type = Args::StringToUInt32 (value.c_str(), 0, 16);
1166 }
1167 else if (name.compare("mecount") == 0)
1168 {
1169 // exception count in big endian hex
1170 exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16);
1171 }
1172 else if (name.compare("medata") == 0)
1173 {
1174 // exception data in big endian hex
1175 exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16));
1176 }
1177 else if (name.compare("thread") == 0)
1178 {
1179 // thread in big endian hex
1180 tid = Args::StringToUInt32 (value.c_str(), 0, 16);
Greg Claytonc3c46612011-02-15 00:19:15 +00001181 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytona875b642011-01-09 21:07:35 +00001182 thread_sp = m_thread_list.FindThreadByID(tid, false);
Greg Claytonc3c46612011-02-15 00:19:15 +00001183 if (!thread_sp)
1184 {
1185 // Create the thread if we need to
1186 thread_sp.reset (new ThreadGDBRemote (*this, tid));
1187 m_thread_list.AddThread(thread_sp);
1188 }
Chris Lattner24943d22010-06-08 16:52:24 +00001189 }
Greg Clayton4862fa22011-01-08 03:17:57 +00001190 else if (name.compare("hexname") == 0)
1191 {
1192 StringExtractor name_extractor;
1193 // Swap "value" over into "name_extractor"
1194 name_extractor.GetStringRef().swap(value);
1195 // Now convert the HEX bytes into a string value
1196 name_extractor.GetHexByteString (value);
1197 thread_name.swap (value);
1198 }
Chris Lattner24943d22010-06-08 16:52:24 +00001199 else if (name.compare("name") == 0)
1200 {
1201 thread_name.swap (value);
1202 }
Greg Clayton0a7f75f2010-09-09 06:32:46 +00001203 else if (name.compare("qaddr") == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001204 {
1205 thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16);
1206 }
Greg Claytona875b642011-01-09 21:07:35 +00001207 else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1]))
1208 {
1209 // We have a register number that contains an expedited
1210 // register value. Lets supply this register to our thread
1211 // so it won't have to go and read it.
1212 if (thread_sp)
1213 {
1214 uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16);
1215
1216 if (reg != UINT32_MAX)
1217 {
1218 StringExtractor reg_value_extractor;
1219 // Swap "value" over into "reg_value_extractor"
1220 reg_value_extractor.GetStringRef().swap(value);
Greg Claytonc3c46612011-02-15 00:19:15 +00001221 if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor))
1222 {
1223 Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'",
1224 name.c_str(),
1225 reg,
1226 reg,
1227 reg_value_extractor.GetStringRef().c_str(),
1228 stop_packet.GetStringRef().c_str());
1229 }
Greg Claytona875b642011-01-09 21:07:35 +00001230 }
1231 }
1232 }
Chris Lattner24943d22010-06-08 16:52:24 +00001233 }
Chris Lattner24943d22010-06-08 16:52:24 +00001234
1235 if (thread_sp)
1236 {
1237 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get());
1238
1239 gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr);
Jim Ingham9082c8a2011-01-28 02:23:12 +00001240 gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001241 if (exc_type != 0)
1242 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001243 const size_t exc_data_size = exc_data.size();
Greg Clayton643ee732010-08-04 01:40:35 +00001244
1245 gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp,
1246 exc_type,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001247 exc_data_size,
1248 exc_data_size >= 1 ? exc_data[0] : 0,
1249 exc_data_size >= 2 ? exc_data[1] : 0));
Chris Lattner24943d22010-06-08 16:52:24 +00001250 }
1251 else if (signo)
1252 {
Greg Clayton643ee732010-08-04 01:40:35 +00001253 gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo));
Chris Lattner24943d22010-06-08 16:52:24 +00001254 }
1255 else
1256 {
Greg Clayton643ee732010-08-04 01:40:35 +00001257 StopInfoSP invalid_stop_info_sp;
1258 gdb_thread->SetStopInfo (invalid_stop_info_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001259 }
1260 }
1261 return eStateStopped;
1262 }
1263 break;
1264
1265 case 'W':
1266 // process exited
1267 return eStateExited;
1268
1269 default:
1270 break;
1271 }
1272 return eStateInvalid;
1273}
1274
1275void
1276ProcessGDBRemote::RefreshStateAfterStop ()
1277{
Jim Ingham7508e732010-08-09 23:31:02 +00001278 // FIXME - add a variable to tell that we're in the middle of attaching if we
1279 // need to know that.
Chris Lattner24943d22010-06-08 16:52:24 +00001280 // We must be attaching if we don't already have a valid architecture
Jim Ingham7508e732010-08-09 23:31:02 +00001281// if (!GetTarget().GetArchitecture().IsValid())
1282// {
1283// Module *exe_module = GetTarget().GetExecutableModule().get();
1284// if (exe_module)
1285// m_arch_spec = exe_module->GetArchitecture();
1286// }
1287
Chris Lattner24943d22010-06-08 16:52:24 +00001288 // Let all threads recover from stopping and do any clean up based
1289 // on the previous thread state (if any).
1290 m_thread_list.RefreshStateAfterStop();
1291
1292 // Discover new threads:
1293 UpdateThreadListIfNeeded ();
1294}
1295
1296Error
Jim Ingham3ae449a2010-11-17 02:32:00 +00001297ProcessGDBRemote::DoHalt (bool &caused_stop)
Chris Lattner24943d22010-06-08 16:52:24 +00001298{
1299 Error error;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001300
Greg Claytona4881d02011-01-22 07:12:45 +00001301 bool timed_out = false;
1302 Mutex::Locker locker;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001303
1304 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton20d338f2010-11-18 05:57:03 +00001305 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001306 // We are being asked to halt during an attach. We need to just close
1307 // our file handle and debugserver will go away, and we can be done...
1308 m_gdb_comm.Disconnect();
Greg Clayton20d338f2010-11-18 05:57:03 +00001309 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001310 else
1311 {
1312 if (!m_gdb_comm.SendInterrupt (locker, 2, caused_stop, timed_out))
1313 {
1314 if (timed_out)
1315 error.SetErrorString("timed out sending interrupt packet");
1316 else
1317 error.SetErrorString("unknown error sending interrupt packet");
1318 }
1319 }
Chris Lattner24943d22010-06-08 16:52:24 +00001320 return error;
1321}
1322
1323Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001324ProcessGDBRemote::InterruptIfRunning
1325(
1326 bool discard_thread_plans,
1327 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +00001328 EventSP &stop_event_sp
1329)
Chris Lattner24943d22010-06-08 16:52:24 +00001330{
1331 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001332
Greg Clayton2860ba92011-01-23 19:58:49 +00001333 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1334
Greg Clayton68ca8232011-01-25 02:58:48 +00001335 bool paused_private_state_thread = false;
Greg Clayton2860ba92011-01-23 19:58:49 +00001336 const bool is_running = m_gdb_comm.IsRunning();
1337 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00001338 log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
Greg Clayton2860ba92011-01-23 19:58:49 +00001339 discard_thread_plans,
Greg Clayton68ca8232011-01-25 02:58:48 +00001340 catch_stop_event,
Greg Clayton2860ba92011-01-23 19:58:49 +00001341 is_running);
1342
Greg Clayton2860ba92011-01-23 19:58:49 +00001343 if (discard_thread_plans)
1344 {
1345 if (log)
1346 log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans");
1347 m_thread_list.DiscardThreadPlans();
1348 }
1349 if (is_running)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001350 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001351 if (catch_stop_event)
1352 {
1353 if (log)
1354 log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread");
1355 PausePrivateStateThread();
1356 paused_private_state_thread = true;
1357 }
1358
Greg Clayton4fb400f2010-09-27 21:07:38 +00001359 bool timed_out = false;
Greg Claytona4881d02011-01-22 07:12:45 +00001360 bool sent_interrupt = false;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001361 Mutex::Locker locker;
Greg Clayton72e1c782011-01-22 23:43:18 +00001362
Greg Clayton72e1c782011-01-22 23:43:18 +00001363 if (!m_gdb_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
Greg Clayton4fb400f2010-09-27 21:07:38 +00001364 {
1365 if (timed_out)
1366 error.SetErrorString("timed out sending interrupt packet");
1367 else
1368 error.SetErrorString("unknown error sending interrupt packet");
Greg Clayton68ca8232011-01-25 02:58:48 +00001369 if (paused_private_state_thread)
Greg Clayton72e1c782011-01-22 23:43:18 +00001370 ResumePrivateStateThread();
1371 return error;
Greg Clayton4fb400f2010-09-27 21:07:38 +00001372 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001373
Greg Clayton72e1c782011-01-22 23:43:18 +00001374 if (catch_stop_event)
1375 {
Greg Clayton68ca8232011-01-25 02:58:48 +00001376 // LISTEN HERE
Greg Clayton72e1c782011-01-22 23:43:18 +00001377 TimeValue timeout_time;
1378 timeout_time = TimeValue::Now();
Greg Clayton68ca8232011-01-25 02:58:48 +00001379 timeout_time.OffsetWithSeconds(5);
1380 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
Greg Clayton2860ba92011-01-23 19:58:49 +00001381
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001382 timed_out = state == eStateInvalid;
Greg Clayton2860ba92011-01-23 19:58:49 +00001383 if (log)
1384 log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001385
Greg Clayton2860ba92011-01-23 19:58:49 +00001386 if (timed_out)
Greg Clayton72e1c782011-01-22 23:43:18 +00001387 error.SetErrorString("unable to verify target stopped");
1388 }
1389
Greg Clayton68ca8232011-01-25 02:58:48 +00001390 if (paused_private_state_thread)
Greg Clayton2860ba92011-01-23 19:58:49 +00001391 {
1392 if (log)
1393 log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread");
Greg Clayton72e1c782011-01-22 23:43:18 +00001394 ResumePrivateStateThread();
Greg Clayton2860ba92011-01-23 19:58:49 +00001395 }
Greg Clayton4fb400f2010-09-27 21:07:38 +00001396 }
Chris Lattner24943d22010-06-08 16:52:24 +00001397 return error;
1398}
1399
Greg Clayton4fb400f2010-09-27 21:07:38 +00001400Error
Greg Clayton72e1c782011-01-22 23:43:18 +00001401ProcessGDBRemote::WillDetach ()
1402{
Greg Clayton2860ba92011-01-23 19:58:49 +00001403 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
1404 if (log)
1405 log->Printf ("ProcessGDBRemote::WillDetach()");
1406
Greg Clayton72e1c782011-01-22 23:43:18 +00001407 bool discard_thread_plans = true;
1408 bool catch_stop_event = true;
Greg Clayton72e1c782011-01-22 23:43:18 +00001409 EventSP event_sp;
Greg Clayton68ca8232011-01-25 02:58:48 +00001410 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
Greg Clayton72e1c782011-01-22 23:43:18 +00001411}
1412
1413Error
Greg Clayton4fb400f2010-09-27 21:07:38 +00001414ProcessGDBRemote::DoDetach()
1415{
1416 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001417 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Greg Clayton4fb400f2010-09-27 21:07:38 +00001418 if (log)
1419 log->Printf ("ProcessGDBRemote::DoDetach()");
1420
1421 DisableAllBreakpointSites ();
1422
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001423 m_thread_list.DiscardThreadPlans();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001424
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001425 size_t response_size = m_gdb_comm.SendPacket ("D", 1);
1426 if (log)
Greg Clayton4fb400f2010-09-27 21:07:38 +00001427 {
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001428 if (response_size)
1429 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully");
1430 else
1431 log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed");
Greg Clayton4fb400f2010-09-27 21:07:38 +00001432 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001433 // Sleep for one second to let the process get all detached...
Greg Clayton4fb400f2010-09-27 21:07:38 +00001434 StopAsyncThread ();
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001435
Greg Clayton4fb400f2010-09-27 21:07:38 +00001436 m_gdb_comm.StopReadThread();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001437 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001438
1439 SetPrivateState (eStateDetached);
1440 ResumePrivateStateThread();
1441
1442 //KillDebugserverProcess ();
Greg Clayton4fb400f2010-09-27 21:07:38 +00001443 return error;
1444}
Chris Lattner24943d22010-06-08 16:52:24 +00001445
1446Error
1447ProcessGDBRemote::DoDestroy ()
1448{
1449 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001450 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001451 if (log)
1452 log->Printf ("ProcessGDBRemote::DoDestroy()");
1453
1454 // Interrupt if our inferior is running...
Greg Claytona4881d02011-01-22 07:12:45 +00001455 if (m_gdb_comm.IsConnected())
Chris Lattner24943d22010-06-08 16:52:24 +00001456 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001457 if (m_public_state.GetValue() == eStateAttaching)
Greg Clayton27a8dd72011-01-25 04:57:42 +00001458 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001459 // We are being asked to halt during an attach. We need to just close
1460 // our file handle and debugserver will go away, and we can be done...
1461 m_gdb_comm.Disconnect();
Greg Clayton27a8dd72011-01-25 04:57:42 +00001462 }
1463 else
Greg Clayton72e1c782011-01-22 23:43:18 +00001464 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001465
1466 StringExtractorGDBRemote response;
1467 bool send_async = true;
1468 if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, 2, send_async))
1469 {
1470 char packet_cmd = response.GetChar(0);
1471
1472 if (packet_cmd == 'W' || packet_cmd == 'X')
1473 {
1474 m_last_stop_packet = response;
1475 SetExitStatus(response.GetHexU8(), NULL);
1476 }
1477 }
1478 else
1479 {
1480 SetExitStatus(SIGABRT, NULL);
1481 //error.SetErrorString("kill packet failed");
1482 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001483 }
1484 }
Chris Lattner24943d22010-06-08 16:52:24 +00001485 StopAsyncThread ();
1486 m_gdb_comm.StopReadThread();
1487 KillDebugserverProcess ();
Johnny Chenc5b15db2010-09-03 22:35:47 +00001488 m_gdb_comm.Disconnect(); // Disconnect from the debug server.
Chris Lattner24943d22010-06-08 16:52:24 +00001489 return error;
1490}
1491
Chris Lattner24943d22010-06-08 16:52:24 +00001492//------------------------------------------------------------------
1493// Process Queries
1494//------------------------------------------------------------------
1495
1496bool
1497ProcessGDBRemote::IsAlive ()
1498{
Greg Clayton58e844b2010-12-08 05:08:21 +00001499 return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
Chris Lattner24943d22010-06-08 16:52:24 +00001500}
1501
1502addr_t
1503ProcessGDBRemote::GetImageInfoAddress()
1504{
1505 if (!m_gdb_comm.IsRunning())
1506 {
1507 StringExtractorGDBRemote response;
1508 if (m_gdb_comm.SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, 2, false))
1509 {
1510 if (response.IsNormalPacket())
1511 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1512 }
1513 }
1514 return LLDB_INVALID_ADDRESS;
1515}
1516
Chris Lattner24943d22010-06-08 16:52:24 +00001517//------------------------------------------------------------------
1518// Process Memory
1519//------------------------------------------------------------------
1520size_t
1521ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1522{
1523 if (size > m_max_memory_size)
1524 {
1525 // Keep memory read sizes down to a sane limit. This function will be
1526 // called multiple times in order to complete the task by
1527 // lldb_private::Process so it is ok to do this.
1528 size = m_max_memory_size;
1529 }
1530
1531 char packet[64];
1532 const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size);
1533 assert (packet_len + 1 < sizeof(packet));
1534 StringExtractorGDBRemote response;
1535 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, true))
1536 {
1537 if (response.IsNormalPacket())
1538 {
1539 error.Clear();
1540 return response.GetHexBytes(buf, size, '\xdd');
1541 }
1542 else if (response.IsErrorPacket())
1543 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
1544 else if (response.IsUnsupportedPacket())
1545 error.SetErrorStringWithFormat("'%s' packet unsupported", packet);
1546 else
1547 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str());
1548 }
1549 else
1550 {
1551 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet);
1552 }
1553 return 0;
1554}
1555
1556size_t
1557ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1558{
1559 StreamString packet;
1560 packet.Printf("M%llx,%zx:", addr, size);
Greg Claytoncd548032011-02-01 01:31:41 +00001561 packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00001562 StringExtractorGDBRemote response;
1563 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, 2, true))
1564 {
1565 if (response.IsOKPacket())
1566 {
1567 error.Clear();
1568 return size;
1569 }
1570 else if (response.IsErrorPacket())
1571 error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str());
1572 else if (response.IsUnsupportedPacket())
1573 error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str());
1574 else
1575 error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str());
1576 }
1577 else
1578 {
1579 error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str());
1580 }
1581 return 0;
1582}
1583
1584lldb::addr_t
1585ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
1586{
1587 addr_t allocated_addr = m_gdb_comm.AllocateMemory (size, permissions, m_packet_timeout);
1588 if (allocated_addr == LLDB_INVALID_ADDRESS)
1589 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %u", size, permissions);
1590 else
1591 error.Clear();
1592 return allocated_addr;
1593}
1594
1595Error
1596ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)
1597{
1598 Error error;
1599 if (!m_gdb_comm.DeallocateMemory (addr, m_packet_timeout))
1600 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr);
1601 return error;
1602}
1603
1604
1605//------------------------------------------------------------------
1606// Process STDIO
1607//------------------------------------------------------------------
1608
1609size_t
1610ProcessGDBRemote::GetSTDOUT (char *buf, size_t buf_size, Error &error)
1611{
1612 Mutex::Locker locker(m_stdio_mutex);
1613 size_t bytes_available = m_stdout_data.size();
1614 if (bytes_available > 0)
1615 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +00001616 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
1617 if (log)
1618 log->Printf ("ProcessGDBRemote::%s (&%p[%u]) ...", __FUNCTION__, buf, buf_size);
Chris Lattner24943d22010-06-08 16:52:24 +00001619 if (bytes_available > buf_size)
1620 {
Greg Clayton53d68e72010-07-20 22:52:08 +00001621 memcpy(buf, m_stdout_data.c_str(), buf_size);
Chris Lattner24943d22010-06-08 16:52:24 +00001622 m_stdout_data.erase(0, buf_size);
1623 bytes_available = buf_size;
1624 }
1625 else
1626 {
Greg Clayton53d68e72010-07-20 22:52:08 +00001627 memcpy(buf, m_stdout_data.c_str(), bytes_available);
Chris Lattner24943d22010-06-08 16:52:24 +00001628 m_stdout_data.clear();
1629
1630 //ResetEventBits(eBroadcastBitSTDOUT);
1631 }
1632 }
1633 return bytes_available;
1634}
1635
1636size_t
1637ProcessGDBRemote::GetSTDERR (char *buf, size_t buf_size, Error &error)
1638{
1639 // Can we get STDERR through the remote protocol?
1640 return 0;
1641}
1642
1643size_t
1644ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error)
1645{
1646 if (m_stdio_communication.IsConnected())
1647 {
1648 ConnectionStatus status;
1649 m_stdio_communication.Write(src, src_len, status, NULL);
1650 }
1651 return 0;
1652}
1653
1654Error
1655ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site)
1656{
1657 Error error;
1658 assert (bp_site != NULL);
1659
Greg Claytone005f2c2010-11-06 01:53:30 +00001660 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001661 user_id_t site_id = bp_site->GetID();
1662 const addr_t addr = bp_site->GetLoadAddress();
1663 if (log)
1664 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %d) address = 0x%llx", site_id, (uint64_t)addr);
1665
1666 if (bp_site->IsEnabled())
1667 {
1668 if (log)
1669 log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %d) address = 0x%llx -- SUCCESS (already enabled)", site_id, (uint64_t)addr);
1670 return error;
1671 }
1672 else
1673 {
1674 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1675
1676 if (bp_site->HardwarePreferred())
1677 {
1678 // Try and set hardware breakpoint, and if that fails, fall through
1679 // and set a software breakpoint?
1680 }
1681
1682 if (m_z0_supported)
1683 {
1684 char packet[64];
1685 const int packet_len = ::snprintf (packet, sizeof(packet), "Z0,%llx,%zx", addr, bp_op_size);
1686 assert (packet_len + 1 < sizeof(packet));
1687 StringExtractorGDBRemote response;
1688 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, true))
1689 {
1690 if (response.IsUnsupportedPacket())
1691 {
1692 // Disable z packet support and try again
1693 m_z0_supported = 0;
1694 return EnableBreakpoint (bp_site);
1695 }
1696 else if (response.IsOKPacket())
1697 {
1698 bp_site->SetEnabled(true);
1699 bp_site->SetType (BreakpointSite::eExternal);
1700 return error;
1701 }
1702 else
1703 {
1704 uint8_t error_byte = response.GetError();
1705 if (error_byte)
1706 error.SetErrorStringWithFormat("%x packet failed with error: %i (0x%2.2x).\n", packet, error_byte, error_byte);
1707 }
1708 }
1709 }
1710 else
1711 {
1712 return EnableSoftwareBreakpoint (bp_site);
1713 }
1714 }
1715
1716 if (log)
1717 {
1718 const char *err_string = error.AsCString();
1719 log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s",
1720 bp_site->GetLoadAddress(),
1721 err_string ? err_string : "NULL");
1722 }
1723 // We shouldn't reach here on a successful breakpoint enable...
1724 if (error.Success())
1725 error.SetErrorToGenericError();
1726 return error;
1727}
1728
1729Error
1730ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site)
1731{
1732 Error error;
1733 assert (bp_site != NULL);
1734 addr_t addr = bp_site->GetLoadAddress();
1735 user_id_t site_id = bp_site->GetID();
Greg Claytone005f2c2010-11-06 01:53:30 +00001736 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001737 if (log)
1738 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %d) addr = 0x%8.8llx", site_id, (uint64_t)addr);
1739
1740 if (bp_site->IsEnabled())
1741 {
1742 const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site);
1743
1744 if (bp_site->IsHardware())
1745 {
1746 // TODO: disable hardware breakpoint...
1747 }
1748 else
1749 {
1750 if (m_z0_supported)
1751 {
1752 char packet[64];
1753 const int packet_len = ::snprintf (packet, sizeof(packet), "z0,%llx,%zx", addr, bp_op_size);
1754 assert (packet_len + 1 < sizeof(packet));
1755 StringExtractorGDBRemote response;
1756 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, true))
1757 {
1758 if (response.IsUnsupportedPacket())
1759 {
1760 error.SetErrorString("Breakpoint site was set with Z packet, yet remote debugserver states z packets are not supported.");
1761 }
1762 else if (response.IsOKPacket())
1763 {
1764 if (log)
1765 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %d) addr = 0x%8.8llx -- SUCCESS", site_id, (uint64_t)addr);
1766 bp_site->SetEnabled(false);
1767 return error;
1768 }
1769 else
1770 {
1771 uint8_t error_byte = response.GetError();
1772 if (error_byte)
1773 error.SetErrorStringWithFormat("%x packet failed with error: %i (0x%2.2x).\n", packet, error_byte, error_byte);
1774 }
1775 }
1776 }
1777 else
1778 {
1779 return DisableSoftwareBreakpoint (bp_site);
1780 }
1781 }
1782 }
1783 else
1784 {
1785 if (log)
1786 log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %d) addr = 0x%8.8llx -- SUCCESS (already disabled)", site_id, (uint64_t)addr);
1787 return error;
1788 }
1789
1790 if (error.Success())
1791 error.SetErrorToGenericError();
1792 return error;
1793}
1794
1795Error
1796ProcessGDBRemote::EnableWatchpoint (WatchpointLocation *wp)
1797{
1798 Error error;
1799 if (wp)
1800 {
1801 user_id_t watchID = wp->GetID();
1802 addr_t addr = wp->GetLoadAddress();
Greg Claytone005f2c2010-11-06 01:53:30 +00001803 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001804 if (log)
1805 log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %d)", watchID);
1806 if (wp->IsEnabled())
1807 {
1808 if (log)
1809 log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %d) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
1810 return error;
1811 }
1812 else
1813 {
1814 // Pass down an appropriate z/Z packet...
1815 error.SetErrorString("watchpoints not supported");
1816 }
1817 }
1818 else
1819 {
1820 error.SetErrorString("Watchpoint location argument was NULL.");
1821 }
1822 if (error.Success())
1823 error.SetErrorToGenericError();
1824 return error;
1825}
1826
1827Error
1828ProcessGDBRemote::DisableWatchpoint (WatchpointLocation *wp)
1829{
1830 Error error;
1831 if (wp)
1832 {
1833 user_id_t watchID = wp->GetID();
1834
Greg Claytone005f2c2010-11-06 01:53:30 +00001835 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001836
1837 addr_t addr = wp->GetLoadAddress();
1838 if (log)
1839 log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %d) addr = 0x%8.8llx", watchID, (uint64_t)addr);
1840
1841 if (wp->IsHardware())
1842 {
1843 // Pass down an appropriate z/Z packet...
1844 error.SetErrorString("watchpoints not supported");
1845 }
1846 // TODO: clear software watchpoints if we implement them
1847 }
1848 else
1849 {
1850 error.SetErrorString("Watchpoint location argument was NULL.");
1851 }
1852 if (error.Success())
1853 error.SetErrorToGenericError();
1854 return error;
1855}
1856
1857void
1858ProcessGDBRemote::Clear()
1859{
1860 m_flags = 0;
1861 m_thread_list.Clear();
1862 {
1863 Mutex::Locker locker(m_stdio_mutex);
1864 m_stdout_data.clear();
1865 }
Chris Lattner24943d22010-06-08 16:52:24 +00001866}
1867
1868Error
1869ProcessGDBRemote::DoSignal (int signo)
1870{
1871 Error error;
Greg Claytone005f2c2010-11-06 01:53:30 +00001872 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001873 if (log)
1874 log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo);
1875
1876 if (!m_gdb_comm.SendAsyncSignal (signo))
1877 error.SetErrorStringWithFormat("failed to send signal %i", signo);
1878 return error;
1879}
1880
Chris Lattner24943d22010-06-08 16:52:24 +00001881Error
1882ProcessGDBRemote::StartDebugserverProcess
1883(
1884 const char *debugserver_url, // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
1885 char const *inferior_argv[], // Arguments for the inferior program including the path to the inferior itself as the first argument
1886 char const *inferior_envp[], // Environment to pass along to the inferior program
Greg Clayton23cf0c72010-11-08 04:29:11 +00001887 lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, and attach_pid != LLDB_INVALID_PROCESS_ID send this pid as an argument to debugserver
Chris Lattner24943d22010-06-08 16:52:24 +00001888 const char *attach_name, // Wait for the next process to launch whose basename matches "attach_name"
1889 bool wait_for_launch, // Wait for the process named "attach_name" to launch
Greg Claytona2f74232011-02-24 22:24:29 +00001890 const ArchSpec& inferior_arch // The arch of the inferior that we will launch
Chris Lattner24943d22010-06-08 16:52:24 +00001891)
1892{
1893 Error error;
1894 if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
1895 {
1896 // If we locate debugserver, keep that located version around
1897 static FileSpec g_debugserver_file_spec;
1898
1899 FileSpec debugserver_file_spec;
1900 char debugserver_path[PATH_MAX];
1901
1902 // Always check to see if we have an environment override for the path
1903 // to the debugserver to use and use it if we do.
1904 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
1905 if (env_debugserver_path)
Greg Clayton537a7a82010-10-20 20:54:39 +00001906 debugserver_file_spec.SetFile (env_debugserver_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001907 else
1908 debugserver_file_spec = g_debugserver_file_spec;
1909 bool debugserver_exists = debugserver_file_spec.Exists();
1910 if (!debugserver_exists)
1911 {
1912 // The debugserver binary is in the LLDB.framework/Resources
1913 // directory.
Greg Clayton24b48ff2010-10-17 22:03:32 +00001914 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
Chris Lattner24943d22010-06-08 16:52:24 +00001915 {
Greg Clayton24b48ff2010-10-17 22:03:32 +00001916 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
Chris Lattner24943d22010-06-08 16:52:24 +00001917 debugserver_exists = debugserver_file_spec.Exists();
Greg Clayton24b48ff2010-10-17 22:03:32 +00001918 if (debugserver_exists)
1919 {
1920 g_debugserver_file_spec = debugserver_file_spec;
1921 }
1922 else
1923 {
1924 g_debugserver_file_spec.Clear();
1925 debugserver_file_spec.Clear();
1926 }
Chris Lattner24943d22010-06-08 16:52:24 +00001927 }
1928 }
1929
1930 if (debugserver_exists)
1931 {
1932 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
1933
1934 m_stdio_communication.Clear();
1935 posix_spawnattr_t attr;
1936
Greg Claytone005f2c2010-11-06 01:53:30 +00001937 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001938
1939 Error local_err; // Errors that don't affect the spawning.
1940 if (log)
Greg Clayton940b1032011-02-23 00:35:02 +00001941 log->Printf ("%s ( path='%s', argv=%p, envp=%p, arch=%s )",
1942 __FUNCTION__,
1943 debugserver_path,
1944 inferior_argv,
1945 inferior_envp,
1946 inferior_arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +00001947 error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX);
1948 if (error.Fail() || log)
Greg Claytone005f2c2010-11-06 01:53:30 +00001949 error.PutToLog(log.get(), "::posix_spawnattr_init ( &attr )");
Chris Lattner24943d22010-06-08 16:52:24 +00001950 if (error.Fail())
Greg Clayton940b1032011-02-23 00:35:02 +00001951 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001952
Chris Lattner24943d22010-06-08 16:52:24 +00001953 Args debugserver_args;
1954 char arg_cstr[PATH_MAX];
Chris Lattner24943d22010-06-08 16:52:24 +00001955
Chris Lattner24943d22010-06-08 16:52:24 +00001956 // Start args with "debugserver /file/path -r --"
1957 debugserver_args.AppendArgument(debugserver_path);
1958 debugserver_args.AppendArgument(debugserver_url);
Greg Clayton24b48ff2010-10-17 22:03:32 +00001959 // use native registers, not the GDB registers
1960 debugserver_args.AppendArgument("--native-regs");
1961 // make debugserver run in its own session so signals generated by
1962 // special terminal key sequences (^C) don't affect debugserver
1963 debugserver_args.AppendArgument("--setsid");
Chris Lattner24943d22010-06-08 16:52:24 +00001964
Chris Lattner24943d22010-06-08 16:52:24 +00001965 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
1966 if (env_debugserver_log_file)
1967 {
1968 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
1969 debugserver_args.AppendArgument(arg_cstr);
1970 }
1971
1972 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
1973 if (env_debugserver_log_flags)
1974 {
1975 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
1976 debugserver_args.AppendArgument(arg_cstr);
1977 }
Greg Claytoncc3e6402011-01-25 06:55:13 +00001978// debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001979// debugserver_args.AppendArgument("--log-flags=0x802e0e");
Chris Lattner24943d22010-06-08 16:52:24 +00001980
1981 // Now append the program arguments
Greg Claytona2f74232011-02-24 22:24:29 +00001982 if (inferior_argv)
Chris Lattner24943d22010-06-08 16:52:24 +00001983 {
Greg Claytona2f74232011-02-24 22:24:29 +00001984 // Terminate the debugserver args so we can now append the inferior args
1985 debugserver_args.AppendArgument("--");
Chris Lattner24943d22010-06-08 16:52:24 +00001986
Greg Claytona2f74232011-02-24 22:24:29 +00001987 for (int i = 0; inferior_argv[i] != NULL; ++i)
1988 debugserver_args.AppendArgument (inferior_argv[i]);
Chris Lattner24943d22010-06-08 16:52:24 +00001989 }
1990 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
1991 {
1992 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
1993 debugserver_args.AppendArgument (arg_cstr);
1994 }
1995 else if (attach_name && attach_name[0])
1996 {
1997 if (wait_for_launch)
1998 debugserver_args.AppendArgument ("--waitfor");
1999 else
2000 debugserver_args.AppendArgument ("--attach");
2001 debugserver_args.AppendArgument (attach_name);
2002 }
2003
2004 Error file_actions_err;
2005 posix_spawn_file_actions_t file_actions;
2006#if DONT_CLOSE_DEBUGSERVER_STDIO
2007 file_actions_err.SetErrorString ("Remove this after uncommenting the code block below.");
2008#else
2009 file_actions_err.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX);
2010 if (file_actions_err.Success())
2011 {
2012 ::posix_spawn_file_actions_addclose (&file_actions, STDIN_FILENO);
2013 ::posix_spawn_file_actions_addclose (&file_actions, STDOUT_FILENO);
2014 ::posix_spawn_file_actions_addclose (&file_actions, STDERR_FILENO);
2015 }
2016#endif
2017
2018 if (log)
2019 {
2020 StreamString strm;
2021 debugserver_args.Dump (&strm);
2022 log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData());
2023 }
2024
Greg Clayton72e1c782011-01-22 23:43:18 +00002025 error.SetError (::posix_spawnp (&m_debugserver_pid,
2026 debugserver_path,
2027 file_actions_err.Success() ? &file_actions : NULL,
2028 &attr,
2029 debugserver_args.GetArgumentVector(),
2030 (char * const*)inferior_envp),
2031 eErrorTypePOSIX);
2032
Greg Claytone9d0df42010-07-02 01:29:13 +00002033
2034 ::posix_spawnattr_destroy (&attr);
2035
Chris Lattner24943d22010-06-08 16:52:24 +00002036 if (file_actions_err.Success())
2037 ::posix_spawn_file_actions_destroy (&file_actions);
2038
2039 // We have seen some cases where posix_spawnp was returning a valid
2040 // looking pid even when an error was returned, so clear it out
2041 if (error.Fail())
2042 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2043
2044 if (error.Fail() || log)
Greg Claytone005f2c2010-11-06 01:53:30 +00002045 error.PutToLog(log.get(), "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", m_debugserver_pid, debugserver_path, NULL, &attr, inferior_argv, inferior_envp);
Chris Lattner24943d22010-06-08 16:52:24 +00002046
Chris Lattner24943d22010-06-08 16:52:24 +00002047 }
2048 else
2049 {
2050 error.SetErrorStringWithFormat ("Unable to locate " DEBUGSERVER_BASENAME ".\n");
2051 }
2052
2053 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2054 StartAsyncThread ();
2055 }
2056 return error;
2057}
2058
2059bool
2060ProcessGDBRemote::MonitorDebugserverProcess
2061(
2062 void *callback_baton,
2063 lldb::pid_t debugserver_pid,
2064 int signo, // Zero for no signal
2065 int exit_status // Exit value of process if signal is zero
2066)
2067{
2068 // We pass in the ProcessGDBRemote inferior process it and name it
2069 // "gdb_remote_pid". The process ID is passed in the "callback_baton"
2070 // pointer value itself, thus we need the double cast...
2071
2072 // "debugserver_pid" argument passed in is the process ID for
2073 // debugserver that we are tracking...
2074
Greg Clayton75ccf502010-08-21 02:22:51 +00002075 ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
Greg Clayton72e1c782011-01-22 23:43:18 +00002076
2077 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
2078 if (log)
2079 log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%i, signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status);
2080
Greg Clayton75ccf502010-08-21 02:22:51 +00002081 if (process)
Chris Lattner24943d22010-06-08 16:52:24 +00002082 {
Greg Clayton75ccf502010-08-21 02:22:51 +00002083 // Sleep for a half a second to make sure our inferior process has
2084 // time to set its exit status before we set it incorrectly when
2085 // both the debugserver and the inferior process shut down.
2086 usleep (500000);
2087 // If our process hasn't yet exited, debugserver might have died.
2088 // If the process did exit, the we are reaping it.
Greg Clayton3b2c41c2010-10-18 04:14:23 +00002089 const StateType state = process->GetState();
2090
2091 if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&
2092 state != eStateInvalid &&
2093 state != eStateUnloaded &&
2094 state != eStateExited &&
2095 state != eStateDetached)
Chris Lattner24943d22010-06-08 16:52:24 +00002096 {
Greg Clayton75ccf502010-08-21 02:22:51 +00002097 char error_str[1024];
2098 if (signo)
Chris Lattner24943d22010-06-08 16:52:24 +00002099 {
Greg Clayton75ccf502010-08-21 02:22:51 +00002100 const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo);
2101 if (signal_cstr)
2102 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002103 else
Greg Clayton75ccf502010-08-21 02:22:51 +00002104 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo);
Chris Lattner24943d22010-06-08 16:52:24 +00002105 }
2106 else
2107 {
Greg Clayton75ccf502010-08-21 02:22:51 +00002108 ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status);
Chris Lattner24943d22010-06-08 16:52:24 +00002109 }
Greg Clayton75ccf502010-08-21 02:22:51 +00002110
2111 process->SetExitStatus (-1, error_str);
2112 }
Greg Clayton3b2c41c2010-10-18 04:14:23 +00002113 // Debugserver has exited we need to let our ProcessGDBRemote
2114 // know that it no longer has a debugserver instance
2115 process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2116 // We are returning true to this function below, so we can
2117 // forget about the monitor handle.
2118 process->m_debugserver_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002119 }
2120 return true;
2121}
2122
2123void
2124ProcessGDBRemote::KillDebugserverProcess ()
2125{
2126 if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)
2127 {
2128 ::kill (m_debugserver_pid, SIGINT);
2129 m_debugserver_pid = LLDB_INVALID_PROCESS_ID;
2130 }
2131}
2132
2133void
2134ProcessGDBRemote::Initialize()
2135{
2136 static bool g_initialized = false;
2137
2138 if (g_initialized == false)
2139 {
2140 g_initialized = true;
2141 PluginManager::RegisterPlugin (GetPluginNameStatic(),
2142 GetPluginDescriptionStatic(),
2143 CreateInstance);
2144
2145 Log::Callbacks log_callbacks = {
2146 ProcessGDBRemoteLog::DisableLog,
2147 ProcessGDBRemoteLog::EnableLog,
2148 ProcessGDBRemoteLog::ListLogCategories
2149 };
2150
2151 Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks);
2152 }
2153}
2154
2155bool
2156ProcessGDBRemote::SetCurrentGDBRemoteThread (int tid)
2157{
2158 if (m_curr_tid == tid)
2159 return true;
2160
2161 char packet[32];
Greg Claytonc1f45872011-02-12 06:28:37 +00002162 int packet_len;
2163 if (tid <= 0)
2164 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
2165 else
2166 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
Chris Lattner24943d22010-06-08 16:52:24 +00002167 assert (packet_len + 1 < sizeof(packet));
2168 StringExtractorGDBRemote response;
2169 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, false))
2170 {
2171 if (response.IsOKPacket())
2172 {
2173 m_curr_tid = tid;
2174 return true;
2175 }
2176 }
2177 return false;
2178}
2179
2180bool
2181ProcessGDBRemote::SetCurrentGDBRemoteThreadForRun (int tid)
2182{
2183 if (m_curr_tid_run == tid)
2184 return true;
2185
2186 char packet[32];
Greg Claytonc1f45872011-02-12 06:28:37 +00002187 int packet_len;
2188 if (tid <= 0)
2189 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
2190 else
2191 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
2192
Chris Lattner24943d22010-06-08 16:52:24 +00002193 assert (packet_len + 1 < sizeof(packet));
2194 StringExtractorGDBRemote response;
2195 if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, 2, false))
2196 {
2197 if (response.IsOKPacket())
2198 {
2199 m_curr_tid_run = tid;
2200 return true;
2201 }
2202 }
2203 return false;
2204}
2205
2206void
2207ProcessGDBRemote::ResetGDBRemoteState ()
2208{
2209 // Reset and GDB remote state
2210 m_curr_tid = LLDB_INVALID_THREAD_ID;
2211 m_curr_tid_run = LLDB_INVALID_THREAD_ID;
2212 m_z0_supported = 1;
2213}
2214
2215
2216bool
2217ProcessGDBRemote::StartAsyncThread ()
2218{
2219 ResetGDBRemoteState ();
2220
Greg Claytone005f2c2010-11-06 01:53:30 +00002221 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002222
2223 if (log)
2224 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2225
2226 // Create a thread that watches our internal state and controls which
2227 // events make it to clients (into the DCProcess event queue).
2228 m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002229 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002230}
2231
2232void
2233ProcessGDBRemote::StopAsyncThread ()
2234{
Greg Claytone005f2c2010-11-06 01:53:30 +00002235 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002236
2237 if (log)
2238 log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
2239
2240 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
2241
2242 // Stop the stdio thread
Greg Clayton09c81ef2011-02-08 01:34:25 +00002243 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002244 {
2245 Host::ThreadJoin (m_async_thread, NULL, NULL);
2246 }
2247}
2248
2249
2250void *
2251ProcessGDBRemote::AsyncThread (void *arg)
2252{
2253 ProcessGDBRemote *process = (ProcessGDBRemote*) arg;
2254
Greg Claytone005f2c2010-11-06 01:53:30 +00002255 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002256 if (log)
2257 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID());
2258
2259 Listener listener ("ProcessGDBRemote::AsyncThread");
2260 EventSP event_sp;
2261 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
2262 eBroadcastBitAsyncThreadShouldExit;
2263
2264 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
2265 {
Greg Claytona2f74232011-02-24 22:24:29 +00002266 listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit);
2267
Chris Lattner24943d22010-06-08 16:52:24 +00002268 bool done = false;
2269 while (!done)
2270 {
2271 if (log)
2272 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
2273 if (listener.WaitForEvent (NULL, event_sp))
2274 {
2275 const uint32_t event_type = event_sp->GetType();
Greg Claytona2f74232011-02-24 22:24:29 +00002276 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
Chris Lattner24943d22010-06-08 16:52:24 +00002277 {
Greg Claytona2f74232011-02-24 22:24:29 +00002278 if (log)
2279 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002280
Greg Claytona2f74232011-02-24 22:24:29 +00002281 switch (event_type)
2282 {
2283 case eBroadcastBitAsyncContinue:
Chris Lattner24943d22010-06-08 16:52:24 +00002284 {
Greg Claytona2f74232011-02-24 22:24:29 +00002285 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002286
Greg Claytona2f74232011-02-24 22:24:29 +00002287 if (continue_packet)
Chris Lattner24943d22010-06-08 16:52:24 +00002288 {
Greg Claytona2f74232011-02-24 22:24:29 +00002289 const char *continue_cstr = (const char *)continue_packet->GetBytes ();
2290 const size_t continue_cstr_len = continue_packet->GetByteSize ();
2291 if (log)
2292 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +00002293
Greg Claytona2f74232011-02-24 22:24:29 +00002294 if (::strstr (continue_cstr, "vAttach") == NULL)
2295 process->SetPrivateState(eStateRunning);
2296 StringExtractorGDBRemote response;
2297 StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
Chris Lattner24943d22010-06-08 16:52:24 +00002298
Greg Claytona2f74232011-02-24 22:24:29 +00002299 switch (stop_state)
2300 {
2301 case eStateStopped:
2302 case eStateCrashed:
2303 case eStateSuspended:
2304 process->m_last_stop_packet = response;
2305 process->m_last_stop_packet.SetFilePos (0);
2306 process->SetPrivateState (stop_state);
2307 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002308
Greg Claytona2f74232011-02-24 22:24:29 +00002309 case eStateExited:
2310 process->m_last_stop_packet = response;
2311 process->m_last_stop_packet.SetFilePos (0);
2312 response.SetFilePos(1);
2313 process->SetExitStatus(response.GetHexU8(), NULL);
2314 done = true;
2315 break;
2316
2317 case eStateInvalid:
2318 process->SetExitStatus(-1, "lost connection");
2319 break;
2320
2321 default:
2322 process->SetPrivateState (stop_state);
2323 break;
2324 }
Chris Lattner24943d22010-06-08 16:52:24 +00002325 }
2326 }
Greg Claytona2f74232011-02-24 22:24:29 +00002327 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002328
Greg Claytona2f74232011-02-24 22:24:29 +00002329 case eBroadcastBitAsyncThreadShouldExit:
2330 if (log)
2331 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
2332 done = true;
2333 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002334
Greg Claytona2f74232011-02-24 22:24:29 +00002335 default:
2336 if (log)
2337 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type);
2338 done = true;
2339 break;
2340 }
2341 }
2342 else if (event_sp->BroadcasterIs (&process->m_gdb_comm))
2343 {
2344 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
2345 {
2346 process->SetExitStatus (-1, "lost connection");
Chris Lattner24943d22010-06-08 16:52:24 +00002347 done = true;
Greg Claytona2f74232011-02-24 22:24:29 +00002348 }
Chris Lattner24943d22010-06-08 16:52:24 +00002349 }
2350 }
2351 else
2352 {
2353 if (log)
2354 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID());
2355 done = true;
2356 }
2357 }
2358 }
2359
2360 if (log)
2361 log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, arg, process->GetID());
2362
2363 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
2364 return NULL;
2365}
2366
Chris Lattner24943d22010-06-08 16:52:24 +00002367const char *
2368ProcessGDBRemote::GetDispatchQueueNameForThread
2369(
2370 addr_t thread_dispatch_qaddr,
2371 std::string &dispatch_queue_name
2372)
2373{
2374 dispatch_queue_name.clear();
2375 if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
2376 {
2377 // Cache the dispatch_queue_offsets_addr value so we don't always have
2378 // to look it up
2379 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2380 {
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002381 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
2382 const Symbol *dispatch_queue_offsets_symbol = NULL;
Greg Clayton537a7a82010-10-20 20:54:39 +00002383 ModuleSP module_sp(GetTarget().GetImages().FindFirstModuleForFileSpec (FileSpec("libSystem.B.dylib", false)));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002384 if (module_sp)
2385 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2386
2387 if (dispatch_queue_offsets_symbol == NULL)
2388 {
Greg Clayton537a7a82010-10-20 20:54:39 +00002389 module_sp = GetTarget().GetImages().FindFirstModuleForFileSpec (FileSpec("libdispatch.dylib", false));
Greg Claytonaf6e9e42010-10-12 17:33:06 +00002390 if (module_sp)
2391 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
2392 }
Chris Lattner24943d22010-06-08 16:52:24 +00002393 if (dispatch_queue_offsets_symbol)
Greg Claytoneea26402010-09-14 23:36:40 +00002394 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetValue().GetLoadAddress(&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00002395
2396 if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS)
2397 return NULL;
2398 }
2399
2400 uint8_t memory_buffer[8];
Greg Clayton395fc332011-02-15 21:59:32 +00002401 DataExtractor data (memory_buffer,
2402 sizeof(memory_buffer),
2403 m_target.GetArchitecture().GetByteOrder(),
2404 m_target.GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00002405
2406 // Excerpt from src/queue_private.h
2407 struct dispatch_queue_offsets_s
2408 {
2409 uint16_t dqo_version;
2410 uint16_t dqo_label;
2411 uint16_t dqo_label_size;
2412 } dispatch_queue_offsets;
2413
2414
2415 Error error;
2416 if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets))
2417 {
2418 uint32_t data_offset = 0;
2419 if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t)))
2420 {
2421 if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize())
2422 {
2423 data_offset = 0;
2424 lldb::addr_t queue_addr = data.GetAddress(&data_offset);
2425 lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label;
2426 dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0');
2427 size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error);
2428 if (bytes_read < dispatch_queue_offsets.dqo_label_size)
2429 dispatch_queue_name.erase (bytes_read);
2430 }
2431 }
2432 }
2433 }
2434 if (dispatch_queue_name.empty())
2435 return NULL;
2436 return dispatch_queue_name.c_str();
2437}
2438
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002439//uint32_t
2440//ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2441//{
2442// // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver
2443// // process and ask it for the list of processes. But if we are local, we can let the Host do it.
2444// if (m_local_debugserver)
2445// {
2446// return Host::ListProcessesMatchingName (name, matches, pids);
2447// }
2448// else
2449// {
2450// // FIXME: Implement talking to the remote debugserver.
2451// return 0;
2452// }
2453//
2454//}
2455//
Jim Ingham55e01d82011-01-22 01:33:44 +00002456bool
2457ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,
2458 lldb_private::StoppointCallbackContext *context,
2459 lldb::user_id_t break_id,
2460 lldb::user_id_t break_loc_id)
2461{
2462 // I don't think I have to do anything here, just make sure I notice the new thread when it starts to
2463 // run so I can stop it if that's what I want to do.
2464 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2465 if (log)
2466 log->Printf("Hit New Thread Notification breakpoint.");
2467 return false;
2468}
2469
2470
2471bool
2472ProcessGDBRemote::StartNoticingNewThreads()
2473{
2474 static const char *bp_names[] =
2475 {
2476 "start_wqthread",
Jim Inghamff276fe2011-02-08 05:19:01 +00002477 "_pthread_wqthread",
Jim Ingham55e01d82011-01-22 01:33:44 +00002478 "_pthread_start",
2479 NULL
2480 };
2481
2482 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2483 size_t num_bps = m_thread_observation_bps.size();
2484 if (num_bps != 0)
2485 {
2486 for (int i = 0; i < num_bps; i++)
2487 {
2488 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2489 if (break_sp)
2490 {
2491 if (log)
2492 log->Printf("Enabled noticing new thread breakpoint.");
2493 break_sp->SetEnabled(true);
2494 }
2495 }
2496 }
2497 else
2498 {
2499 for (int i = 0; bp_names[i] != NULL; i++)
2500 {
2501 Breakpoint *breakpoint = m_target.CreateBreakpoint (NULL, bp_names[i], eFunctionNameTypeFull, true).get();
2502 if (breakpoint)
2503 {
2504 if (log)
2505 log->Printf("Successfully created new thread notification breakpoint at \"%s\".", bp_names[i]);
2506 m_thread_observation_bps.push_back(breakpoint->GetID());
2507 breakpoint->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true);
2508 }
2509 else
2510 {
2511 if (log)
2512 log->Printf("Failed to create new thread notification breakpoint.");
2513 return false;
2514 }
2515 }
2516 }
2517
2518 return true;
2519}
2520
2521bool
2522ProcessGDBRemote::StopNoticingNewThreads()
2523{
Jim Inghamff276fe2011-02-08 05:19:01 +00002524 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2525 if (log)
2526 log->Printf ("Disabling new thread notification breakpoint.");
Jim Ingham55e01d82011-01-22 01:33:44 +00002527 size_t num_bps = m_thread_observation_bps.size();
2528 if (num_bps != 0)
2529 {
2530 for (int i = 0; i < num_bps; i++)
2531 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002532
2533 lldb::BreakpointSP break_sp = m_target.GetBreakpointByID(m_thread_observation_bps[i]);
2534 if (break_sp)
2535 {
Jim Ingham55e01d82011-01-22 01:33:44 +00002536 break_sp->SetEnabled(false);
2537 }
2538 }
2539 }
2540 return true;
2541}
2542
2543