blob: dea16f29681aef01f98373d33089f2ad1318a9ab [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===//
Greg Clayton363be3f2011-07-15 03:27:12 +00002//
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>
12#include <stdlib.h>
13
14// C++ Includes
15// Other libraries and framework includes
Greg Clayton8d2ea282011-07-17 20:36:25 +000016#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton234981a2011-07-20 03:41:06 +000017#include "lldb/Core/Debugger.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000018#include "lldb/Core/PluginManager.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000020#include "lldb/Core/State.h"
21#include "lldb/Host/Host.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Symbol/ObjectFile.h"
Greg Claytone76f8c42012-09-21 16:31:20 +000023#include "lldb/Target/RegisterContext.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000024#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000025#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000026
27// Project includes
28#include "ProcessKDP.h"
29#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000030#include "ThreadKDP.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000031#include "StopInfoMachException.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36const char *
37ProcessKDP::GetPluginNameStatic()
38{
39 return "kdp-remote";
40}
41
42const char *
43ProcessKDP::GetPluginDescriptionStatic()
44{
45 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
46}
47
48void
49ProcessKDP::Terminate()
50{
51 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
52}
53
54
Greg Clayton46c9a352012-02-09 06:16:32 +000055lldb::ProcessSP
56ProcessKDP::CreateInstance (Target &target,
57 Listener &listener,
58 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000059{
Greg Clayton46c9a352012-02-09 06:16:32 +000060 lldb::ProcessSP process_sp;
61 if (crash_file_path == NULL)
62 process_sp.reset(new ProcessKDP (target, listener));
63 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000064}
65
66bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000067ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000068{
Greg Clayton61ddf562011-10-21 21:41:45 +000069 if (plugin_specified_by_name)
70 return true;
71
Greg Clayton363be3f2011-07-15 03:27:12 +000072 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000073 Module *exe_module = target.GetExecutableModulePointer();
74 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000075 {
76 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Claytonb170aee2012-05-08 01:45:38 +000077 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000078 {
Greg Claytonb170aee2012-05-08 01:45:38 +000079 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
80 case llvm::Triple::MacOSX: // For desktop targets
81 case llvm::Triple::IOS: // For arm targets
82 if (triple_ref.getVendor() == llvm::Triple::Apple)
83 {
84 ObjectFile *exe_objfile = exe_module->GetObjectFile();
85 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
86 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
87 return true;
88 }
89 break;
90
91 default:
92 break;
Greg Clayton363be3f2011-07-15 03:27:12 +000093 }
94 }
Greg Clayton61ddf562011-10-21 21:41:45 +000095 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000096}
97
98//----------------------------------------------------------------------
99// ProcessKDP constructor
100//----------------------------------------------------------------------
101ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
102 Process (target, listener),
103 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000104 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton363be3f2011-07-15 03:27:12 +0000105 m_async_thread (LLDB_INVALID_HOST_THREAD)
106{
Greg Claytone76f8c42012-09-21 16:31:20 +0000107 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
108 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton363be3f2011-07-15 03:27:12 +0000109}
110
111//----------------------------------------------------------------------
112// Destructor
113//----------------------------------------------------------------------
114ProcessKDP::~ProcessKDP()
115{
116 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000117 // We need to call finalize on the process before destroying ourselves
118 // to make sure all of the broadcaster cleanup goes as planned. If we
119 // destruct this class, then Process::~Process() might have problems
120 // trying to fully destroy the broadcaster.
121 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000122}
123
124//----------------------------------------------------------------------
125// PluginInterface
126//----------------------------------------------------------------------
127const char *
128ProcessKDP::GetPluginName()
129{
130 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
131}
132
133const char *
134ProcessKDP::GetShortPluginName()
135{
136 return GetPluginNameStatic();
137}
138
139uint32_t
140ProcessKDP::GetPluginVersion()
141{
142 return 1;
143}
144
145Error
146ProcessKDP::WillLaunch (Module* module)
147{
148 Error error;
149 error.SetErrorString ("launching not supported in kdp-remote plug-in");
150 return error;
151}
152
153Error
154ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
155{
156 Error error;
157 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
158 return error;
159}
160
161Error
162ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
163{
164 Error error;
165 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
166 return error;
167}
168
169Error
170ProcessKDP::DoConnectRemote (const char *remote_url)
171{
Greg Clayton363be3f2011-07-15 03:27:12 +0000172 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000173
174 // Don't let any JIT happen when doing KDP as we can't allocate
175 // memory and we don't want to be mucking with threads that might
176 // already be handling exceptions
177 SetCanJIT(false);
178
Greg Clayton8d2ea282011-07-17 20:36:25 +0000179 if (remote_url == NULL || remote_url[0] == '\0')
Greg Claytone76f8c42012-09-21 16:31:20 +0000180 {
181 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
182 return error;
183 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000184
185 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
186 if (conn_ap.get())
187 {
188 // Only try once for now.
189 // TODO: check if we should be retrying?
190 const uint32_t max_retry_count = 1;
191 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
192 {
193 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
194 break;
195 usleep (100000);
196 }
197 }
198
199 if (conn_ap->IsConnected())
200 {
201 const uint16_t reply_port = conn_ap->GetReadPort ();
202
203 if (reply_port != 0)
204 {
205 m_comm.SetConnection(conn_ap.release());
206
207 if (m_comm.SendRequestReattach(reply_port))
208 {
209 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
210 {
211 m_comm.GetVersion();
212 uint32_t cpu = m_comm.GetCPUType();
213 uint32_t sub = m_comm.GetCPUSubtype();
214 ArchSpec kernel_arch;
215 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
216 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000217 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000218 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000219 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000220 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
221 if (async_strm_sp)
222 {
Greg Clayton7b139222011-07-21 01:12:01 +0000223 const char *cstr;
224 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000225 {
Greg Clayton7b139222011-07-21 01:12:01 +0000226 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000227 async_strm_sp->Flush();
228 }
Greg Clayton7b139222011-07-21 01:12:01 +0000229// if ((cstr = m_comm.GetImagePath ()) != NULL)
230// {
231// async_strm_sp->Printf ("Image Path: %s\n", cstr);
232// async_strm_sp->Flush();
233// }
Greg Clayton234981a2011-07-20 03:41:06 +0000234 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000235 }
236 }
237 else
238 {
239 error.SetErrorString("KDP reattach failed");
240 }
241 }
242 else
243 {
244 error.SetErrorString("invalid reply port from UDP connection");
245 }
246 }
247 else
248 {
249 if (error.Success())
250 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
251 }
252 if (error.Fail())
253 m_comm.Disconnect();
254
Greg Clayton363be3f2011-07-15 03:27:12 +0000255 return error;
256}
257
258//----------------------------------------------------------------------
259// Process Control
260//----------------------------------------------------------------------
261Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000262ProcessKDP::DoLaunch (Module *exe_module,
263 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000264{
265 Error error;
266 error.SetErrorString ("launching not supported in kdp-remote plug-in");
267 return error;
268}
269
270
271Error
272ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
273{
274 Error error;
275 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
276 return error;
277}
278
Greg Clayton363be3f2011-07-15 03:27:12 +0000279Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000280ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
281{
282 Error error;
283 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
284 return error;
285}
286
287Error
288ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000289{
290 Error error;
291 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
292 return error;
293}
294
295
296void
297ProcessKDP::DidAttach ()
298{
299 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
300 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000301 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000302 if (GetID() != LLDB_INVALID_PROCESS_ID)
303 {
304 // TODO: figure out the register context that we will use
305 }
306}
307
308Error
309ProcessKDP::WillResume ()
310{
311 return Error();
312}
313
314Error
315ProcessKDP::DoResume ()
316{
317 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000318 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
319 // Only start the async thread if we try to do any process control
320 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
321 StartAsyncThread ();
322
323 const uint32_t num_threads = m_thread_list.GetSize();
324 uint32_t resume_cpu_mask = 0;
325
326 for (uint32_t idx = 0; idx < num_threads; ++idx)
Greg Claytonea636012012-09-21 01:55:30 +0000327 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000328 ThreadSP thread_sp (m_thread_list.GetThreadAtIndex(idx));
329 const StateType thread_resume_state = thread_sp->GetState();
330 switch (thread_resume_state)
Greg Claytonea636012012-09-21 01:55:30 +0000331 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000332 case eStateStopped:
333 case eStateSuspended:
334 // Nothing to do here when a thread will stay suspended
335 // we just leave the CPU mask bit set to zero for the thread
336 break;
337
338 case eStateStepping:
339 case eStateRunning:
340 thread_sp->GetRegisterContext()->HardwareSingleStep (thread_resume_state == eStateStepping);
341 // Thread ID is the bit we need for the CPU mask
342 resume_cpu_mask |= thread_sp->GetID();
343 break;
344
345 break;
346
347 default:
348 assert (!"invalid thread resume state");
349 break;
Greg Claytonea636012012-09-21 01:55:30 +0000350 }
351 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000352 if (log)
353 log->Printf ("ProcessKDP::DoResume () sending resume with cpu_mask = 0x%8.8x",
354 resume_cpu_mask);
355 if (resume_cpu_mask)
356 {
357
358 if (m_comm.SendRequestResume (resume_cpu_mask))
359 {
360 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
361 SetPrivateState(eStateRunning);
362 }
363 else
364 error.SetErrorString ("KDP resume failed");
365 }
Greg Claytonea636012012-09-21 01:55:30 +0000366 else
Greg Claytone76f8c42012-09-21 16:31:20 +0000367 {
368 error.SetErrorString ("all threads suspended");
369 }
370
Greg Clayton363be3f2011-07-15 03:27:12 +0000371 return error;
372}
373
Greg Claytonae932352012-04-10 00:18:59 +0000374bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000375ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000376{
377 // locker will keep a mutex locked until it goes out of scope
378 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
379 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000380 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000381
Greg Clayton37f962e2011-08-22 02:49:39 +0000382 // We currently are making only one thread per core and we
383 // actually don't know about actual threads. Eventually we
384 // want to get the thread list from memory and note which
385 // threads are on CPU as those are the only ones that we
386 // will be able to resume.
387 const uint32_t cpu_mask = m_comm.GetCPUMask();
388 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000389 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000390 lldb::tid_t tid = cpu_mask_bit;
391 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
392 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000393 thread_sp.reset(new ThreadKDP (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +0000394 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000395 }
Greg Claytonae932352012-04-10 00:18:59 +0000396 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000397}
398
Greg Clayton363be3f2011-07-15 03:27:12 +0000399void
400ProcessKDP::RefreshStateAfterStop ()
401{
402 // Let all threads recover from stopping and do any clean up based
403 // on the previous thread state (if any).
404 m_thread_list.RefreshStateAfterStop();
Greg Clayton363be3f2011-07-15 03:27:12 +0000405}
406
407Error
408ProcessKDP::DoHalt (bool &caused_stop)
409{
410 Error error;
411
412// bool timed_out = false;
413 Mutex::Locker locker;
414
415 if (m_public_state.GetValue() == eStateAttaching)
416 {
417 // We are being asked to halt during an attach. We need to just close
418 // our file handle and debugserver will go away, and we can be done...
419 m_comm.Disconnect();
420 }
421 else
422 {
Greg Clayton234981a2011-07-20 03:41:06 +0000423 if (!m_comm.SendRequestSuspend ())
424 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000425 }
426 return error;
427}
428
429Error
430ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
431 bool catch_stop_event,
432 EventSP &stop_event_sp)
433{
434 Error error;
435
436 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
437
438 bool paused_private_state_thread = false;
439 const bool is_running = m_comm.IsRunning();
440 if (log)
441 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
442 discard_thread_plans,
443 catch_stop_event,
444 is_running);
445
446 if (discard_thread_plans)
447 {
448 if (log)
449 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
450 m_thread_list.DiscardThreadPlans();
451 }
452 if (is_running)
453 {
454 if (catch_stop_event)
455 {
456 if (log)
457 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
458 PausePrivateStateThread();
459 paused_private_state_thread = true;
460 }
461
462 bool timed_out = false;
463// bool sent_interrupt = false;
464 Mutex::Locker locker;
465
466 // TODO: implement halt in CommunicationKDP
467// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
468// {
469// if (timed_out)
470// error.SetErrorString("timed out sending interrupt packet");
471// else
472// error.SetErrorString("unknown error sending interrupt packet");
473// if (paused_private_state_thread)
474// ResumePrivateStateThread();
475// return error;
476// }
477
478 if (catch_stop_event)
479 {
480 // LISTEN HERE
481 TimeValue timeout_time;
482 timeout_time = TimeValue::Now();
483 timeout_time.OffsetWithSeconds(5);
484 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
485
486 timed_out = state == eStateInvalid;
487 if (log)
488 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
489
490 if (timed_out)
491 error.SetErrorString("unable to verify target stopped");
492 }
493
494 if (paused_private_state_thread)
495 {
496 if (log)
497 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
498 ResumePrivateStateThread();
499 }
500 }
501 return error;
502}
503
504Error
505ProcessKDP::WillDetach ()
506{
507 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
508 if (log)
509 log->Printf ("ProcessKDP::WillDetach()");
510
511 bool discard_thread_plans = true;
512 bool catch_stop_event = true;
513 EventSP event_sp;
514 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
515}
516
517Error
518ProcessKDP::DoDetach()
519{
520 Error error;
521 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
522 if (log)
523 log->Printf ("ProcessKDP::DoDetach()");
524
525 DisableAllBreakpointSites ();
526
527 m_thread_list.DiscardThreadPlans();
528
Greg Clayton8d2ea282011-07-17 20:36:25 +0000529 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000530 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000531
532 m_comm.SendRequestDisconnect();
533
534 size_t response_size = m_comm.Disconnect ();
535 if (log)
536 {
537 if (response_size)
538 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
539 else
540 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
541 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000542 }
543 // Sleep for one second to let the process get all detached...
544 StopAsyncThread ();
545
Greg Claytondb9d6f42012-01-31 04:56:17 +0000546 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000547
548 SetPrivateState (eStateDetached);
549 ResumePrivateStateThread();
550
551 //KillDebugserverProcess ();
552 return error;
553}
554
555Error
556ProcessKDP::DoDestroy ()
557{
Greg Claytone76f8c42012-09-21 16:31:20 +0000558 // For KDP there really is no difference between destroy and detach
559 return DoDetach();
Greg Clayton363be3f2011-07-15 03:27:12 +0000560}
561
562//------------------------------------------------------------------
563// Process Queries
564//------------------------------------------------------------------
565
566bool
567ProcessKDP::IsAlive ()
568{
569 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
570}
571
572//------------------------------------------------------------------
573// Process Memory
574//------------------------------------------------------------------
575size_t
576ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
577{
Greg Clayton0fa51242011-07-19 03:57:15 +0000578 if (m_comm.IsConnected())
579 return m_comm.SendRequestReadMemory (addr, buf, size, error);
580 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000581 return 0;
582}
583
584size_t
585ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
586{
Greg Claytone76f8c42012-09-21 16:31:20 +0000587 if (m_comm.IsConnected())
588 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
589 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000590 return 0;
591}
592
593lldb::addr_t
594ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
595{
596 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
597 return LLDB_INVALID_ADDRESS;
598}
599
600Error
601ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
602{
603 Error error;
604 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
605 return error;
606}
607
608Error
609ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
610{
Greg Clayton234981a2011-07-20 03:41:06 +0000611 if (m_comm.LocalBreakpointsAreSupported ())
612 {
613 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000614 if (!bp_site->IsEnabled())
615 {
616 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
617 {
618 bp_site->SetEnabled(true);
619 bp_site->SetType (BreakpointSite::eExternal);
620 }
621 else
622 {
623 error.SetErrorString ("KDP set breakpoint failed");
624 }
625 }
Greg Clayton234981a2011-07-20 03:41:06 +0000626 return error;
627 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000628 return EnableSoftwareBreakpoint (bp_site);
629}
630
631Error
632ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
633{
Greg Clayton234981a2011-07-20 03:41:06 +0000634 if (m_comm.LocalBreakpointsAreSupported ())
635 {
636 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000637 if (bp_site->IsEnabled())
638 {
639 BreakpointSite::Type bp_type = bp_site->GetType();
640 if (bp_type == BreakpointSite::eExternal)
641 {
642 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
643 bp_site->SetEnabled(false);
644 else
645 error.SetErrorString ("KDP remove breakpoint failed");
646 }
647 else
648 {
649 error = DisableSoftwareBreakpoint (bp_site);
650 }
651 }
Greg Clayton234981a2011-07-20 03:41:06 +0000652 return error;
653 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000654 return DisableSoftwareBreakpoint (bp_site);
655}
656
657Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000658ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000659{
660 Error error;
661 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
662 return error;
663}
664
665Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000666ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000667{
668 Error error;
669 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
670 return error;
671}
672
673void
674ProcessKDP::Clear()
675{
Greg Clayton363be3f2011-07-15 03:27:12 +0000676 m_thread_list.Clear();
677}
678
679Error
680ProcessKDP::DoSignal (int signo)
681{
682 Error error;
683 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
684 return error;
685}
686
687void
688ProcessKDP::Initialize()
689{
690 static bool g_initialized = false;
691
692 if (g_initialized == false)
693 {
694 g_initialized = true;
695 PluginManager::RegisterPlugin (GetPluginNameStatic(),
696 GetPluginDescriptionStatic(),
697 CreateInstance);
698
699 Log::Callbacks log_callbacks = {
700 ProcessKDPLog::DisableLog,
701 ProcessKDPLog::EnableLog,
702 ProcessKDPLog::ListLogCategories
703 };
704
705 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
706 }
707}
708
709bool
710ProcessKDP::StartAsyncThread ()
711{
712 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
713
714 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000715 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000716
Greg Claytone76f8c42012-09-21 16:31:20 +0000717 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
718 return true;
719
Greg Clayton363be3f2011-07-15 03:27:12 +0000720 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
721 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
722}
723
724void
725ProcessKDP::StopAsyncThread ()
726{
727 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
728
729 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000730 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000731
732 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
733
734 // Stop the stdio thread
735 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
736 {
737 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Claytone76f8c42012-09-21 16:31:20 +0000738 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton363be3f2011-07-15 03:27:12 +0000739 }
740}
741
742
743void *
744ProcessKDP::AsyncThread (void *arg)
745{
746 ProcessKDP *process = (ProcessKDP*) arg;
747
Greg Claytone76f8c42012-09-21 16:31:20 +0000748 const lldb::pid_t pid = process->GetID();
749
Greg Clayton363be3f2011-07-15 03:27:12 +0000750 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
751 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000752 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000753
754 Listener listener ("ProcessKDP::AsyncThread");
755 EventSP event_sp;
756 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
757 eBroadcastBitAsyncThreadShouldExit;
758
Greg Claytone76f8c42012-09-21 16:31:20 +0000759
Greg Clayton363be3f2011-07-15 03:27:12 +0000760 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
761 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000762 bool done = false;
763 while (!done)
764 {
765 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000766 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
767 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000768 if (listener.WaitForEvent (NULL, event_sp))
769 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000770 uint32_t event_type = event_sp->GetType();
771 if (log)
772 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
773 pid,
774 event_type);
775
776 // When we are running, poll for 1 second to try and get an exception
777 // to indicate the process has stopped. If we don't get one, check to
778 // make sure no one asked us to exit
779 bool is_running = false;
780 DataExtractor exc_reply_packet;
781 do
Greg Clayton363be3f2011-07-15 03:27:12 +0000782 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000783 switch (event_type)
784 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000785 case eBroadcastBitAsyncContinue:
Greg Clayton363be3f2011-07-15 03:27:12 +0000786 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000787 is_running = true;
788 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Clayton363be3f2011-07-15 03:27:12 +0000789 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000790 // TODO: parse the stop reply packet
791 is_running = false;
792 process->SetPrivateState(eStateStopped);
793 }
794 else
795 {
796 // Check to see if we are supposed to exit. There is no way to
797 // interrupt a running kernel, so all we can do is wait for an
798 // exception or detach...
799 if (listener.GetNextEvent(event_sp))
800 {
801 // We got an event, go through the loop again
802 event_type = event_sp->GetType();
803 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000804 }
805 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000806 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000807
Greg Claytone76f8c42012-09-21 16:31:20 +0000808 case eBroadcastBitAsyncThreadShouldExit:
809 if (log)
810 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
811 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000812 done = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000813 is_running = false;
814 break;
815
816 default:
817 if (log)
818 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
819 pid,
820 event_type);
821 done = true;
822 is_running = false;
823 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000824 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000825 } while (is_running);
Greg Clayton363be3f2011-07-15 03:27:12 +0000826 }
827 else
828 {
829 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000830 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
831 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000832 done = true;
833 }
834 }
835 }
836
837 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000838 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
839 arg,
840 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000841
842 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
843 return NULL;
844}
845
846