blob: ab872f8e7d78e62aced3f997f8b80087aa7240eb [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"
19#include "lldb/Core/State.h"
20#include "lldb/Host/Host.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000021#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000022#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000023
24// Project includes
25#include "ProcessKDP.h"
26#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000027#include "ThreadKDP.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000028#include "StopInfoMachException.h"
29
30using namespace lldb;
31using namespace lldb_private;
32
33const char *
34ProcessKDP::GetPluginNameStatic()
35{
36 return "kdp-remote";
37}
38
39const char *
40ProcessKDP::GetPluginDescriptionStatic()
41{
42 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
43}
44
45void
46ProcessKDP::Terminate()
47{
48 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
49}
50
51
Greg Clayton46c9a352012-02-09 06:16:32 +000052lldb::ProcessSP
53ProcessKDP::CreateInstance (Target &target,
54 Listener &listener,
55 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000056{
Greg Clayton46c9a352012-02-09 06:16:32 +000057 lldb::ProcessSP process_sp;
58 if (crash_file_path == NULL)
59 process_sp.reset(new ProcessKDP (target, listener));
60 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000061}
62
63bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000064ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000065{
Greg Clayton61ddf562011-10-21 21:41:45 +000066 if (plugin_specified_by_name)
67 return true;
68
Greg Clayton363be3f2011-07-15 03:27:12 +000069 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000070 Module *exe_module = target.GetExecutableModulePointer();
71 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000072 {
73 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
74 if (triple_ref.getOS() == llvm::Triple::Darwin &&
75 triple_ref.getVendor() == llvm::Triple::Apple)
76 {
Greg Clayton5beb99d2011-08-11 02:48:45 +000077 ObjectFile *exe_objfile = exe_module->GetObjectFile();
Greg Clayton363be3f2011-07-15 03:27:12 +000078 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
79 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
80 return true;
81 }
82 }
Greg Clayton61ddf562011-10-21 21:41:45 +000083 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000084}
85
86//----------------------------------------------------------------------
87// ProcessKDP constructor
88//----------------------------------------------------------------------
89ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
90 Process (target, listener),
91 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +000092 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton363be3f2011-07-15 03:27:12 +000093 m_async_thread (LLDB_INVALID_HOST_THREAD)
94{
95// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
96// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
97}
98
99//----------------------------------------------------------------------
100// Destructor
101//----------------------------------------------------------------------
102ProcessKDP::~ProcessKDP()
103{
104 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000105 // We need to call finalize on the process before destroying ourselves
106 // to make sure all of the broadcaster cleanup goes as planned. If we
107 // destruct this class, then Process::~Process() might have problems
108 // trying to fully destroy the broadcaster.
109 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000110}
111
112//----------------------------------------------------------------------
113// PluginInterface
114//----------------------------------------------------------------------
115const char *
116ProcessKDP::GetPluginName()
117{
118 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
119}
120
121const char *
122ProcessKDP::GetShortPluginName()
123{
124 return GetPluginNameStatic();
125}
126
127uint32_t
128ProcessKDP::GetPluginVersion()
129{
130 return 1;
131}
132
133Error
134ProcessKDP::WillLaunch (Module* module)
135{
136 Error error;
137 error.SetErrorString ("launching not supported in kdp-remote plug-in");
138 return error;
139}
140
141Error
142ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
143{
144 Error error;
145 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
146 return error;
147}
148
149Error
150ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
151{
152 Error error;
153 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
154 return error;
155}
156
157Error
158ProcessKDP::DoConnectRemote (const char *remote_url)
159{
160 // TODO: fill in the remote connection to the remote KDP here!
161 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000162
163 if (remote_url == NULL || remote_url[0] == '\0')
164 remote_url = "udp://localhost:41139";
165
166 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
167 if (conn_ap.get())
168 {
169 // Only try once for now.
170 // TODO: check if we should be retrying?
171 const uint32_t max_retry_count = 1;
172 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
173 {
174 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
175 break;
176 usleep (100000);
177 }
178 }
179
180 if (conn_ap->IsConnected())
181 {
182 const uint16_t reply_port = conn_ap->GetReadPort ();
183
184 if (reply_port != 0)
185 {
186 m_comm.SetConnection(conn_ap.release());
187
188 if (m_comm.SendRequestReattach(reply_port))
189 {
190 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
191 {
192 m_comm.GetVersion();
193 uint32_t cpu = m_comm.GetCPUType();
194 uint32_t sub = m_comm.GetCPUSubtype();
195 ArchSpec kernel_arch;
196 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
197 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000198 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000199 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000200 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000201 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
202 if (async_strm_sp)
203 {
Greg Clayton7b139222011-07-21 01:12:01 +0000204 const char *cstr;
205 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000206 {
Greg Clayton7b139222011-07-21 01:12:01 +0000207 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000208 async_strm_sp->Flush();
209 }
Greg Clayton7b139222011-07-21 01:12:01 +0000210// if ((cstr = m_comm.GetImagePath ()) != NULL)
211// {
212// async_strm_sp->Printf ("Image Path: %s\n", cstr);
213// async_strm_sp->Flush();
214// }
Greg Clayton234981a2011-07-20 03:41:06 +0000215 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000216 }
217 }
218 else
219 {
220 error.SetErrorString("KDP reattach failed");
221 }
222 }
223 else
224 {
225 error.SetErrorString("invalid reply port from UDP connection");
226 }
227 }
228 else
229 {
230 if (error.Success())
231 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
232 }
233 if (error.Fail())
234 m_comm.Disconnect();
235
Greg Clayton363be3f2011-07-15 03:27:12 +0000236 return error;
237}
238
239//----------------------------------------------------------------------
240// Process Control
241//----------------------------------------------------------------------
242Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000243ProcessKDP::DoLaunch (Module *exe_module,
244 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000245{
246 Error error;
247 error.SetErrorString ("launching not supported in kdp-remote plug-in");
248 return error;
249}
250
251
252Error
253ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
254{
255 Error error;
256 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
257 return error;
258}
259
Greg Clayton363be3f2011-07-15 03:27:12 +0000260Error
261ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch)
262{
263 Error error;
264 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
265 return error;
266}
267
268
269void
270ProcessKDP::DidAttach ()
271{
272 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
273 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000274 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000275 if (GetID() != LLDB_INVALID_PROCESS_ID)
276 {
277 // TODO: figure out the register context that we will use
278 }
279}
280
281Error
282ProcessKDP::WillResume ()
283{
284 return Error();
285}
286
287Error
288ProcessKDP::DoResume ()
289{
290 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000291 if (!m_comm.SendRequestResume ())
292 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000293 return error;
294}
295
296uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +0000297ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000298{
299 // locker will keep a mutex locked until it goes out of scope
300 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
301 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000302 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000303
Greg Clayton37f962e2011-08-22 02:49:39 +0000304 // We currently are making only one thread per core and we
305 // actually don't know about actual threads. Eventually we
306 // want to get the thread list from memory and note which
307 // threads are on CPU as those are the only ones that we
308 // will be able to resume.
309 const uint32_t cpu_mask = m_comm.GetCPUMask();
310 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000311 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000312 lldb::tid_t tid = cpu_mask_bit;
313 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
314 if (!thread_sp)
315 thread_sp.reset(new ThreadKDP (*this, tid));
316 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000317 }
Greg Clayton37f962e2011-08-22 02:49:39 +0000318 return new_thread_list.GetSize(false);
Greg Clayton363be3f2011-07-15 03:27:12 +0000319}
320
321
322StateType
323ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
324{
325 // TODO: figure out why we stopped given the packet that tells us we stopped...
326 return eStateStopped;
327}
328
329void
330ProcessKDP::RefreshStateAfterStop ()
331{
332 // Let all threads recover from stopping and do any clean up based
333 // on the previous thread state (if any).
334 m_thread_list.RefreshStateAfterStop();
335 //SetThreadStopInfo (m_last_stop_packet);
336}
337
338Error
339ProcessKDP::DoHalt (bool &caused_stop)
340{
341 Error error;
342
343// bool timed_out = false;
344 Mutex::Locker locker;
345
346 if (m_public_state.GetValue() == eStateAttaching)
347 {
348 // We are being asked to halt during an attach. We need to just close
349 // our file handle and debugserver will go away, and we can be done...
350 m_comm.Disconnect();
351 }
352 else
353 {
Greg Clayton234981a2011-07-20 03:41:06 +0000354 if (!m_comm.SendRequestSuspend ())
355 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000356 }
357 return error;
358}
359
360Error
361ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
362 bool catch_stop_event,
363 EventSP &stop_event_sp)
364{
365 Error error;
366
367 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
368
369 bool paused_private_state_thread = false;
370 const bool is_running = m_comm.IsRunning();
371 if (log)
372 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
373 discard_thread_plans,
374 catch_stop_event,
375 is_running);
376
377 if (discard_thread_plans)
378 {
379 if (log)
380 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
381 m_thread_list.DiscardThreadPlans();
382 }
383 if (is_running)
384 {
385 if (catch_stop_event)
386 {
387 if (log)
388 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
389 PausePrivateStateThread();
390 paused_private_state_thread = true;
391 }
392
393 bool timed_out = false;
394// bool sent_interrupt = false;
395 Mutex::Locker locker;
396
397 // TODO: implement halt in CommunicationKDP
398// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
399// {
400// if (timed_out)
401// error.SetErrorString("timed out sending interrupt packet");
402// else
403// error.SetErrorString("unknown error sending interrupt packet");
404// if (paused_private_state_thread)
405// ResumePrivateStateThread();
406// return error;
407// }
408
409 if (catch_stop_event)
410 {
411 // LISTEN HERE
412 TimeValue timeout_time;
413 timeout_time = TimeValue::Now();
414 timeout_time.OffsetWithSeconds(5);
415 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
416
417 timed_out = state == eStateInvalid;
418 if (log)
419 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
420
421 if (timed_out)
422 error.SetErrorString("unable to verify target stopped");
423 }
424
425 if (paused_private_state_thread)
426 {
427 if (log)
428 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
429 ResumePrivateStateThread();
430 }
431 }
432 return error;
433}
434
435Error
436ProcessKDP::WillDetach ()
437{
438 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
439 if (log)
440 log->Printf ("ProcessKDP::WillDetach()");
441
442 bool discard_thread_plans = true;
443 bool catch_stop_event = true;
444 EventSP event_sp;
445 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
446}
447
448Error
449ProcessKDP::DoDetach()
450{
451 Error error;
452 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
453 if (log)
454 log->Printf ("ProcessKDP::DoDetach()");
455
456 DisableAllBreakpointSites ();
457
458 m_thread_list.DiscardThreadPlans();
459
Greg Clayton8d2ea282011-07-17 20:36:25 +0000460 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000461 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000462
463 m_comm.SendRequestDisconnect();
464
465 size_t response_size = m_comm.Disconnect ();
466 if (log)
467 {
468 if (response_size)
469 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
470 else
471 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
472 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000473 }
474 // Sleep for one second to let the process get all detached...
475 StopAsyncThread ();
476
Greg Claytondb9d6f42012-01-31 04:56:17 +0000477 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000478
479 SetPrivateState (eStateDetached);
480 ResumePrivateStateThread();
481
482 //KillDebugserverProcess ();
483 return error;
484}
485
486Error
487ProcessKDP::DoDestroy ()
488{
489 Error error;
490 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
491 if (log)
492 log->Printf ("ProcessKDP::DoDestroy()");
493
494 // Interrupt if our inferior is running...
495 if (m_comm.IsConnected())
496 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000497 if (m_public_state.GetValue() == eStateAttaching)
498 {
499 // We are being asked to halt during an attach. We need to just close
500 // our file handle and debugserver will go away, and we can be done...
501 m_comm.Disconnect();
502 }
503 else
504 {
Greg Clayton7b139222011-07-21 01:12:01 +0000505 DisableAllBreakpointSites ();
506
507 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000508
509 StringExtractor response;
510 // TODO: Send kill packet?
511 SetExitStatus(SIGABRT, NULL);
512 }
513 }
514 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000515 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000516 return error;
517}
518
519//------------------------------------------------------------------
520// Process Queries
521//------------------------------------------------------------------
522
523bool
524ProcessKDP::IsAlive ()
525{
526 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
527}
528
529//------------------------------------------------------------------
530// Process Memory
531//------------------------------------------------------------------
532size_t
533ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
534{
Greg Clayton0fa51242011-07-19 03:57:15 +0000535 if (m_comm.IsConnected())
536 return m_comm.SendRequestReadMemory (addr, buf, size, error);
537 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000538 return 0;
539}
540
541size_t
542ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
543{
544 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
545 return 0;
546}
547
548lldb::addr_t
549ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
550{
551 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
552 return LLDB_INVALID_ADDRESS;
553}
554
555Error
556ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
557{
558 Error error;
559 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
560 return error;
561}
562
563Error
564ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
565{
Greg Clayton234981a2011-07-20 03:41:06 +0000566 if (m_comm.LocalBreakpointsAreSupported ())
567 {
568 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000569 if (!bp_site->IsEnabled())
570 {
571 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
572 {
573 bp_site->SetEnabled(true);
574 bp_site->SetType (BreakpointSite::eExternal);
575 }
576 else
577 {
578 error.SetErrorString ("KDP set breakpoint failed");
579 }
580 }
Greg Clayton234981a2011-07-20 03:41:06 +0000581 return error;
582 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000583 return EnableSoftwareBreakpoint (bp_site);
584}
585
586Error
587ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
588{
Greg Clayton234981a2011-07-20 03:41:06 +0000589 if (m_comm.LocalBreakpointsAreSupported ())
590 {
591 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000592 if (bp_site->IsEnabled())
593 {
594 BreakpointSite::Type bp_type = bp_site->GetType();
595 if (bp_type == BreakpointSite::eExternal)
596 {
597 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
598 bp_site->SetEnabled(false);
599 else
600 error.SetErrorString ("KDP remove breakpoint failed");
601 }
602 else
603 {
604 error = DisableSoftwareBreakpoint (bp_site);
605 }
606 }
Greg Clayton234981a2011-07-20 03:41:06 +0000607 return error;
608 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000609 return DisableSoftwareBreakpoint (bp_site);
610}
611
612Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000613ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000614{
615 Error error;
616 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
617 return error;
618}
619
620Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000621ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000622{
623 Error error;
624 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
625 return error;
626}
627
628void
629ProcessKDP::Clear()
630{
Greg Clayton363be3f2011-07-15 03:27:12 +0000631 m_thread_list.Clear();
632}
633
634Error
635ProcessKDP::DoSignal (int signo)
636{
637 Error error;
638 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
639 return error;
640}
641
642void
643ProcessKDP::Initialize()
644{
645 static bool g_initialized = false;
646
647 if (g_initialized == false)
648 {
649 g_initialized = true;
650 PluginManager::RegisterPlugin (GetPluginNameStatic(),
651 GetPluginDescriptionStatic(),
652 CreateInstance);
653
654 Log::Callbacks log_callbacks = {
655 ProcessKDPLog::DisableLog,
656 ProcessKDPLog::EnableLog,
657 ProcessKDPLog::ListLogCategories
658 };
659
660 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
661 }
662}
663
664bool
665ProcessKDP::StartAsyncThread ()
666{
667 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
668
669 if (log)
670 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
671
672 // Create a thread that watches our internal state and controls which
673 // events make it to clients (into the DCProcess event queue).
674 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
675 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
676}
677
678void
679ProcessKDP::StopAsyncThread ()
680{
681 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
682
683 if (log)
684 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
685
686 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
687
688 // Stop the stdio thread
689 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
690 {
691 Host::ThreadJoin (m_async_thread, NULL, NULL);
692 }
693}
694
695
696void *
697ProcessKDP::AsyncThread (void *arg)
698{
699 ProcessKDP *process = (ProcessKDP*) arg;
700
701 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
702 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000703 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000704
705 Listener listener ("ProcessKDP::AsyncThread");
706 EventSP event_sp;
707 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
708 eBroadcastBitAsyncThreadShouldExit;
709
710 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
711 {
712 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
713
714 bool done = false;
715 while (!done)
716 {
717 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000718 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000719 if (listener.WaitForEvent (NULL, event_sp))
720 {
721 const uint32_t event_type = event_sp->GetType();
722 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
723 {
724 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000725 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type);
Greg Clayton363be3f2011-07-15 03:27:12 +0000726
727 switch (event_type)
728 {
729 case eBroadcastBitAsyncContinue:
730 {
731 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
732
733 if (continue_packet)
734 {
735 // TODO: do continue support here
736
737// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
738// const size_t continue_cstr_len = continue_packet->GetByteSize ();
739// if (log)
740// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
741//
742// if (::strstr (continue_cstr, "vAttach") == NULL)
743// process->SetPrivateState(eStateRunning);
744// StringExtractor response;
745// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
746//
747// switch (stop_state)
748// {
749// case eStateStopped:
750// case eStateCrashed:
751// case eStateSuspended:
752// process->m_last_stop_packet = response;
753// process->SetPrivateState (stop_state);
754// break;
755//
756// case eStateExited:
757// process->m_last_stop_packet = response;
758// response.SetFilePos(1);
759// process->SetExitStatus(response.GetHexU8(), NULL);
760// done = true;
761// break;
762//
763// case eStateInvalid:
764// process->SetExitStatus(-1, "lost connection");
765// break;
766//
767// default:
768// process->SetPrivateState (stop_state);
769// break;
770// }
771 }
772 }
773 break;
774
775 case eBroadcastBitAsyncThreadShouldExit:
776 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000777 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000778 done = true;
779 break;
780
781 default:
782 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000783 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type);
Greg Clayton363be3f2011-07-15 03:27:12 +0000784 done = true;
785 break;
786 }
787 }
788 else if (event_sp->BroadcasterIs (&process->m_comm))
789 {
790 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
791 {
792 process->SetExitStatus (-1, "lost connection");
793 done = true;
794 }
795 }
796 }
797 else
798 {
799 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000800 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000801 done = true;
802 }
803 }
804 }
805
806 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000807 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000808
809 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
810 return NULL;
811}
812
813