blob: d2d8dc6a1bd05add2ce7d0d1bedd83a5af6d3bd6 [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
52Process*
53ProcessKDP::CreateInstance (Target &target, Listener &listener)
54{
55 return new ProcessKDP (target, listener);
56}
57
58bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000059ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000060{
Greg Clayton61ddf562011-10-21 21:41:45 +000061 if (plugin_specified_by_name)
62 return true;
63
Greg Clayton363be3f2011-07-15 03:27:12 +000064 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000065 Module *exe_module = target.GetExecutableModulePointer();
66 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000067 {
68 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
69 if (triple_ref.getOS() == llvm::Triple::Darwin &&
70 triple_ref.getVendor() == llvm::Triple::Apple)
71 {
Greg Clayton5beb99d2011-08-11 02:48:45 +000072 ObjectFile *exe_objfile = exe_module->GetObjectFile();
Greg Clayton363be3f2011-07-15 03:27:12 +000073 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
74 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
75 return true;
76 }
77 }
Greg Clayton61ddf562011-10-21 21:41:45 +000078 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000079}
80
81//----------------------------------------------------------------------
82// ProcessKDP constructor
83//----------------------------------------------------------------------
84ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
85 Process (target, listener),
86 m_comm("lldb.process.kdp-remote.communication"),
87 m_async_broadcaster ("lldb.process.kdp-remote.async-broadcaster"),
88 m_async_thread (LLDB_INVALID_HOST_THREAD)
89{
90// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
91// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
92}
93
94//----------------------------------------------------------------------
95// Destructor
96//----------------------------------------------------------------------
97ProcessKDP::~ProcessKDP()
98{
99 Clear();
100}
101
102//----------------------------------------------------------------------
103// PluginInterface
104//----------------------------------------------------------------------
105const char *
106ProcessKDP::GetPluginName()
107{
108 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
109}
110
111const char *
112ProcessKDP::GetShortPluginName()
113{
114 return GetPluginNameStatic();
115}
116
117uint32_t
118ProcessKDP::GetPluginVersion()
119{
120 return 1;
121}
122
123Error
124ProcessKDP::WillLaunch (Module* module)
125{
126 Error error;
127 error.SetErrorString ("launching not supported in kdp-remote plug-in");
128 return error;
129}
130
131Error
132ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
133{
134 Error error;
135 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
136 return error;
137}
138
139Error
140ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
141{
142 Error error;
143 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
144 return error;
145}
146
147Error
148ProcessKDP::DoConnectRemote (const char *remote_url)
149{
150 // TODO: fill in the remote connection to the remote KDP here!
151 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000152
153 if (remote_url == NULL || remote_url[0] == '\0')
154 remote_url = "udp://localhost:41139";
155
156 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
157 if (conn_ap.get())
158 {
159 // Only try once for now.
160 // TODO: check if we should be retrying?
161 const uint32_t max_retry_count = 1;
162 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
163 {
164 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
165 break;
166 usleep (100000);
167 }
168 }
169
170 if (conn_ap->IsConnected())
171 {
172 const uint16_t reply_port = conn_ap->GetReadPort ();
173
174 if (reply_port != 0)
175 {
176 m_comm.SetConnection(conn_ap.release());
177
178 if (m_comm.SendRequestReattach(reply_port))
179 {
180 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
181 {
182 m_comm.GetVersion();
183 uint32_t cpu = m_comm.GetCPUType();
184 uint32_t sub = m_comm.GetCPUSubtype();
185 ArchSpec kernel_arch;
186 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
187 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000188 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000189 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000190 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000191 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
192 if (async_strm_sp)
193 {
Greg Clayton7b139222011-07-21 01:12:01 +0000194 const char *cstr;
195 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000196 {
Greg Clayton7b139222011-07-21 01:12:01 +0000197 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000198 async_strm_sp->Flush();
199 }
Greg Clayton7b139222011-07-21 01:12:01 +0000200// if ((cstr = m_comm.GetImagePath ()) != NULL)
201// {
202// async_strm_sp->Printf ("Image Path: %s\n", cstr);
203// async_strm_sp->Flush();
204// }
Greg Clayton234981a2011-07-20 03:41:06 +0000205 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000206 }
207 }
208 else
209 {
210 error.SetErrorString("KDP reattach failed");
211 }
212 }
213 else
214 {
215 error.SetErrorString("invalid reply port from UDP connection");
216 }
217 }
218 else
219 {
220 if (error.Success())
221 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
222 }
223 if (error.Fail())
224 m_comm.Disconnect();
225
Greg Clayton363be3f2011-07-15 03:27:12 +0000226 return error;
227}
228
229//----------------------------------------------------------------------
230// Process Control
231//----------------------------------------------------------------------
232Error
233ProcessKDP::DoLaunch (Module* module,
234 char const *argv[],
235 char const *envp[],
236 uint32_t launch_flags,
237 const char *stdin_path,
238 const char *stdout_path,
239 const char *stderr_path,
240 const char *working_dir)
241{
242 Error error;
243 error.SetErrorString ("launching not supported in kdp-remote plug-in");
244 return error;
245}
246
247
248Error
249ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
250{
251 Error error;
252 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
253 return error;
254}
255
Greg Clayton363be3f2011-07-15 03:27:12 +0000256Error
257ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch)
258{
259 Error error;
260 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
261 return error;
262}
263
264
265void
266ProcessKDP::DidAttach ()
267{
268 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
269 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000270 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000271 if (GetID() != LLDB_INVALID_PROCESS_ID)
272 {
273 // TODO: figure out the register context that we will use
274 }
275}
276
277Error
278ProcessKDP::WillResume ()
279{
280 return Error();
281}
282
283Error
284ProcessKDP::DoResume ()
285{
286 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000287 if (!m_comm.SendRequestResume ())
288 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000289 return error;
290}
291
292uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +0000293ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000294{
295 // locker will keep a mutex locked until it goes out of scope
296 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
297 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000298 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000299
Greg Clayton37f962e2011-08-22 02:49:39 +0000300 // We currently are making only one thread per core and we
301 // actually don't know about actual threads. Eventually we
302 // want to get the thread list from memory and note which
303 // threads are on CPU as those are the only ones that we
304 // will be able to resume.
305 const uint32_t cpu_mask = m_comm.GetCPUMask();
306 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000307 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000308 lldb::tid_t tid = cpu_mask_bit;
309 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
310 if (!thread_sp)
311 thread_sp.reset(new ThreadKDP (*this, tid));
312 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000313 }
Greg Clayton37f962e2011-08-22 02:49:39 +0000314 return new_thread_list.GetSize(false);
Greg Clayton363be3f2011-07-15 03:27:12 +0000315}
316
317
318StateType
319ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
320{
321 // TODO: figure out why we stopped given the packet that tells us we stopped...
322 return eStateStopped;
323}
324
325void
326ProcessKDP::RefreshStateAfterStop ()
327{
328 // Let all threads recover from stopping and do any clean up based
329 // on the previous thread state (if any).
330 m_thread_list.RefreshStateAfterStop();
331 //SetThreadStopInfo (m_last_stop_packet);
332}
333
334Error
335ProcessKDP::DoHalt (bool &caused_stop)
336{
337 Error error;
338
339// bool timed_out = false;
340 Mutex::Locker locker;
341
342 if (m_public_state.GetValue() == eStateAttaching)
343 {
344 // We are being asked to halt during an attach. We need to just close
345 // our file handle and debugserver will go away, and we can be done...
346 m_comm.Disconnect();
347 }
348 else
349 {
Greg Clayton234981a2011-07-20 03:41:06 +0000350 if (!m_comm.SendRequestSuspend ())
351 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000352 }
353 return error;
354}
355
356Error
357ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
358 bool catch_stop_event,
359 EventSP &stop_event_sp)
360{
361 Error error;
362
363 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
364
365 bool paused_private_state_thread = false;
366 const bool is_running = m_comm.IsRunning();
367 if (log)
368 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
369 discard_thread_plans,
370 catch_stop_event,
371 is_running);
372
373 if (discard_thread_plans)
374 {
375 if (log)
376 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
377 m_thread_list.DiscardThreadPlans();
378 }
379 if (is_running)
380 {
381 if (catch_stop_event)
382 {
383 if (log)
384 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
385 PausePrivateStateThread();
386 paused_private_state_thread = true;
387 }
388
389 bool timed_out = false;
390// bool sent_interrupt = false;
391 Mutex::Locker locker;
392
393 // TODO: implement halt in CommunicationKDP
394// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
395// {
396// if (timed_out)
397// error.SetErrorString("timed out sending interrupt packet");
398// else
399// error.SetErrorString("unknown error sending interrupt packet");
400// if (paused_private_state_thread)
401// ResumePrivateStateThread();
402// return error;
403// }
404
405 if (catch_stop_event)
406 {
407 // LISTEN HERE
408 TimeValue timeout_time;
409 timeout_time = TimeValue::Now();
410 timeout_time.OffsetWithSeconds(5);
411 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
412
413 timed_out = state == eStateInvalid;
414 if (log)
415 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
416
417 if (timed_out)
418 error.SetErrorString("unable to verify target stopped");
419 }
420
421 if (paused_private_state_thread)
422 {
423 if (log)
424 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
425 ResumePrivateStateThread();
426 }
427 }
428 return error;
429}
430
431Error
432ProcessKDP::WillDetach ()
433{
434 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
435 if (log)
436 log->Printf ("ProcessKDP::WillDetach()");
437
438 bool discard_thread_plans = true;
439 bool catch_stop_event = true;
440 EventSP event_sp;
441 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
442}
443
444Error
445ProcessKDP::DoDetach()
446{
447 Error error;
448 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
449 if (log)
450 log->Printf ("ProcessKDP::DoDetach()");
451
452 DisableAllBreakpointSites ();
453
454 m_thread_list.DiscardThreadPlans();
455
Greg Clayton8d2ea282011-07-17 20:36:25 +0000456 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000457 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000458
459 m_comm.SendRequestDisconnect();
460
461 size_t response_size = m_comm.Disconnect ();
462 if (log)
463 {
464 if (response_size)
465 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
466 else
467 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
468 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000469 }
470 // Sleep for one second to let the process get all detached...
471 StopAsyncThread ();
472
473 m_comm.StopReadThread();
474 m_comm.Disconnect(); // Disconnect from the debug server.
475
476 SetPrivateState (eStateDetached);
477 ResumePrivateStateThread();
478
479 //KillDebugserverProcess ();
480 return error;
481}
482
483Error
484ProcessKDP::DoDestroy ()
485{
486 Error error;
487 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
488 if (log)
489 log->Printf ("ProcessKDP::DoDestroy()");
490
491 // Interrupt if our inferior is running...
492 if (m_comm.IsConnected())
493 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000494 if (m_public_state.GetValue() == eStateAttaching)
495 {
496 // We are being asked to halt during an attach. We need to just close
497 // our file handle and debugserver will go away, and we can be done...
498 m_comm.Disconnect();
499 }
500 else
501 {
Greg Clayton7b139222011-07-21 01:12:01 +0000502 DisableAllBreakpointSites ();
503
504 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000505
506 StringExtractor response;
507 // TODO: Send kill packet?
508 SetExitStatus(SIGABRT, NULL);
509 }
510 }
511 StopAsyncThread ();
512 m_comm.StopReadThread();
513 m_comm.Disconnect(); // Disconnect from the debug server.
514 return error;
515}
516
517//------------------------------------------------------------------
518// Process Queries
519//------------------------------------------------------------------
520
521bool
522ProcessKDP::IsAlive ()
523{
524 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
525}
526
527//------------------------------------------------------------------
528// Process Memory
529//------------------------------------------------------------------
530size_t
531ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
532{
Greg Clayton0fa51242011-07-19 03:57:15 +0000533 if (m_comm.IsConnected())
534 return m_comm.SendRequestReadMemory (addr, buf, size, error);
535 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000536 return 0;
537}
538
539size_t
540ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
541{
542 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
543 return 0;
544}
545
546lldb::addr_t
547ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
548{
549 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
550 return LLDB_INVALID_ADDRESS;
551}
552
553Error
554ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
555{
556 Error error;
557 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
558 return error;
559}
560
561Error
562ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
563{
Greg Clayton234981a2011-07-20 03:41:06 +0000564 if (m_comm.LocalBreakpointsAreSupported ())
565 {
566 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000567 if (!bp_site->IsEnabled())
568 {
569 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
570 {
571 bp_site->SetEnabled(true);
572 bp_site->SetType (BreakpointSite::eExternal);
573 }
574 else
575 {
576 error.SetErrorString ("KDP set breakpoint failed");
577 }
578 }
Greg Clayton234981a2011-07-20 03:41:06 +0000579 return error;
580 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000581 return EnableSoftwareBreakpoint (bp_site);
582}
583
584Error
585ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
586{
Greg Clayton234981a2011-07-20 03:41:06 +0000587 if (m_comm.LocalBreakpointsAreSupported ())
588 {
589 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000590 if (bp_site->IsEnabled())
591 {
592 BreakpointSite::Type bp_type = bp_site->GetType();
593 if (bp_type == BreakpointSite::eExternal)
594 {
595 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
596 bp_site->SetEnabled(false);
597 else
598 error.SetErrorString ("KDP remove breakpoint failed");
599 }
600 else
601 {
602 error = DisableSoftwareBreakpoint (bp_site);
603 }
604 }
Greg Clayton234981a2011-07-20 03:41:06 +0000605 return error;
606 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000607 return DisableSoftwareBreakpoint (bp_site);
608}
609
610Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000611ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000612{
613 Error error;
614 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
615 return error;
616}
617
618Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000619ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000620{
621 Error error;
622 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
623 return error;
624}
625
626void
627ProcessKDP::Clear()
628{
629 Mutex::Locker locker (m_thread_list.GetMutex ());
630 m_thread_list.Clear();
631}
632
633Error
634ProcessKDP::DoSignal (int signo)
635{
636 Error error;
637 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
638 return error;
639}
640
641void
642ProcessKDP::Initialize()
643{
644 static bool g_initialized = false;
645
646 if (g_initialized == false)
647 {
648 g_initialized = true;
649 PluginManager::RegisterPlugin (GetPluginNameStatic(),
650 GetPluginDescriptionStatic(),
651 CreateInstance);
652
653 Log::Callbacks log_callbacks = {
654 ProcessKDPLog::DisableLog,
655 ProcessKDPLog::EnableLog,
656 ProcessKDPLog::ListLogCategories
657 };
658
659 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
660 }
661}
662
663bool
664ProcessKDP::StartAsyncThread ()
665{
666 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
667
668 if (log)
669 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
670
671 // Create a thread that watches our internal state and controls which
672 // events make it to clients (into the DCProcess event queue).
673 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
674 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
675}
676
677void
678ProcessKDP::StopAsyncThread ()
679{
680 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
681
682 if (log)
683 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
684
685 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
686
687 // Stop the stdio thread
688 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
689 {
690 Host::ThreadJoin (m_async_thread, NULL, NULL);
691 }
692}
693
694
695void *
696ProcessKDP::AsyncThread (void *arg)
697{
698 ProcessKDP *process = (ProcessKDP*) arg;
699
700 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
701 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000702 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000703
704 Listener listener ("ProcessKDP::AsyncThread");
705 EventSP event_sp;
706 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
707 eBroadcastBitAsyncThreadShouldExit;
708
709 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
710 {
711 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
712
713 bool done = false;
714 while (!done)
715 {
716 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000717 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000718 if (listener.WaitForEvent (NULL, event_sp))
719 {
720 const uint32_t event_type = event_sp->GetType();
721 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
722 {
723 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000724 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 +0000725
726 switch (event_type)
727 {
728 case eBroadcastBitAsyncContinue:
729 {
730 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
731
732 if (continue_packet)
733 {
734 // TODO: do continue support here
735
736// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
737// const size_t continue_cstr_len = continue_packet->GetByteSize ();
738// if (log)
739// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
740//
741// if (::strstr (continue_cstr, "vAttach") == NULL)
742// process->SetPrivateState(eStateRunning);
743// StringExtractor response;
744// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
745//
746// switch (stop_state)
747// {
748// case eStateStopped:
749// case eStateCrashed:
750// case eStateSuspended:
751// process->m_last_stop_packet = response;
752// process->SetPrivateState (stop_state);
753// break;
754//
755// case eStateExited:
756// process->m_last_stop_packet = response;
757// response.SetFilePos(1);
758// process->SetExitStatus(response.GetHexU8(), NULL);
759// done = true;
760// break;
761//
762// case eStateInvalid:
763// process->SetExitStatus(-1, "lost connection");
764// break;
765//
766// default:
767// process->SetPrivateState (stop_state);
768// break;
769// }
770 }
771 }
772 break;
773
774 case eBroadcastBitAsyncThreadShouldExit:
775 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000776 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000777 done = true;
778 break;
779
780 default:
781 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000782 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 +0000783 done = true;
784 break;
785 }
786 }
787 else if (event_sp->BroadcasterIs (&process->m_comm))
788 {
789 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
790 {
791 process->SetExitStatus (-1, "lost connection");
792 done = true;
793 }
794 }
795 }
796 else
797 {
798 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000799 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 +0000800 done = true;
801 }
802 }
803 }
804
805 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000806 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000807
808 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
809 return NULL;
810}
811
812