1 //===-- ProcessKDP.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>
12 #include <stdlib.h>
13 
14 // C++ Includes
15 // Other libraries and framework includes
16 #include "lldb/Core/ConnectionFileDescriptor.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleSpec.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Core/UUID.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Host/Symbols.h"
25 #include "lldb/Symbol/ObjectFile.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 
30 // Project includes
31 #include "ProcessKDP.h"
32 #include "ProcessKDPLog.h"
33 #include "ThreadKDP.h"
34 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 const char *
40 ProcessKDP::GetPluginNameStatic()
41 {
42     return "kdp-remote";
43 }
44 
45 const char *
46 ProcessKDP::GetPluginDescriptionStatic()
47 {
48     return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
49 }
50 
51 void
52 ProcessKDP::Terminate()
53 {
54     PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
55 }
56 
57 
58 lldb::ProcessSP
59 ProcessKDP::CreateInstance (Target &target,
60                             Listener &listener,
61                             const FileSpec *crash_file_path)
62 {
63     lldb::ProcessSP process_sp;
64     if (crash_file_path == NULL)
65         process_sp.reset(new ProcessKDP (target, listener));
66     return process_sp;
67 }
68 
69 bool
70 ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
71 {
72     if (plugin_specified_by_name)
73         return true;
74 
75     // For now we are just making sure the file exists for a given module
76     Module *exe_module = target.GetExecutableModulePointer();
77     if (exe_module)
78     {
79         const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
80         switch (triple_ref.getOS())
81         {
82             case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
83             case llvm::Triple::MacOSX:  // For desktop targets
84             case llvm::Triple::IOS:     // For arm targets
85                 if (triple_ref.getVendor() == llvm::Triple::Apple)
86                 {
87                     ObjectFile *exe_objfile = exe_module->GetObjectFile();
88                     if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
89                         exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
90                         return true;
91                 }
92                 break;
93 
94             default:
95                 break;
96         }
97     }
98     return false;
99 }
100 
101 //----------------------------------------------------------------------
102 // ProcessKDP constructor
103 //----------------------------------------------------------------------
104 ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
105     Process (target, listener),
106     m_comm("lldb.process.kdp-remote.communication"),
107     m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
108     m_async_thread (LLDB_INVALID_HOST_THREAD),
109     m_destroy_in_process (false),
110     m_dyld_plugin_name (),
111     m_kernel_load_addr (LLDB_INVALID_ADDRESS)
112 {
113     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
114     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
115 }
116 
117 //----------------------------------------------------------------------
118 // Destructor
119 //----------------------------------------------------------------------
120 ProcessKDP::~ProcessKDP()
121 {
122     Clear();
123     // We need to call finalize on the process before destroying ourselves
124     // to make sure all of the broadcaster cleanup goes as planned. If we
125     // destruct this class, then Process::~Process() might have problems
126     // trying to fully destroy the broadcaster.
127     Finalize();
128 }
129 
130 //----------------------------------------------------------------------
131 // PluginInterface
132 //----------------------------------------------------------------------
133 const char *
134 ProcessKDP::GetPluginName()
135 {
136     return "Process debugging plug-in that uses the Darwin KDP remote protocol";
137 }
138 
139 const char *
140 ProcessKDP::GetShortPluginName()
141 {
142     return GetPluginNameStatic();
143 }
144 
145 uint32_t
146 ProcessKDP::GetPluginVersion()
147 {
148     return 1;
149 }
150 
151 Error
152 ProcessKDP::WillLaunch (Module* module)
153 {
154     Error error;
155     error.SetErrorString ("launching not supported in kdp-remote plug-in");
156     return error;
157 }
158 
159 Error
160 ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
161 {
162     Error error;
163     error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
164     return error;
165 }
166 
167 Error
168 ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
169 {
170     Error error;
171     error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
172     return error;
173 }
174 
175 Error
176 ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
177 {
178     Error error;
179 
180     // Don't let any JIT happen when doing KDP as we can't allocate
181     // memory and we don't want to be mucking with threads that might
182     // already be handling exceptions
183     SetCanJIT(false);
184 
185     if (remote_url == NULL || remote_url[0] == '\0')
186     {
187         error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
188         return error;
189     }
190 
191     std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
192     if (conn_ap.get())
193     {
194         // Only try once for now.
195         // TODO: check if we should be retrying?
196         const uint32_t max_retry_count = 1;
197         for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
198         {
199             if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
200                 break;
201             usleep (100000);
202         }
203     }
204 
205     if (conn_ap->IsConnected())
206     {
207         const uint16_t reply_port = conn_ap->GetReadPort ();
208 
209         if (reply_port != 0)
210         {
211             m_comm.SetConnection(conn_ap.release());
212 
213             if (m_comm.SendRequestReattach(reply_port))
214             {
215                 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
216                 {
217                     m_comm.GetVersion();
218                     uint32_t cpu = m_comm.GetCPUType();
219                     uint32_t sub = m_comm.GetCPUSubtype();
220                     ArchSpec kernel_arch;
221                     kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
222                     m_target.SetArchitecture(kernel_arch);
223 
224                     /* Get the kernel's UUID and load address via kdp-kernelversion packet.  */
225 
226                     UUID kernel_uuid = m_comm.GetUUID ();
227                     addr_t kernel_load_addr = m_comm.GetLoadAddress ();
228 
229                     if (kernel_load_addr != LLDB_INVALID_ADDRESS)
230                     {
231                         m_kernel_load_addr = kernel_load_addr;
232                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
233                     }
234 
235                     // Set the thread ID
236                     UpdateThreadListIfNeeded ();
237                     SetID (1);
238                     GetThreadList ();
239                     SetPrivateState (eStateStopped);
240                     StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
241                     if (async_strm_sp)
242                     {
243                         const char *cstr;
244                         if ((cstr = m_comm.GetKernelVersion ()) != NULL)
245                         {
246                             async_strm_sp->Printf ("Version: %s\n", cstr);
247                             async_strm_sp->Flush();
248                         }
249 //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
250 //                      {
251 //                          async_strm_sp->Printf ("Image Path: %s\n", cstr);
252 //                          async_strm_sp->Flush();
253 //                      }
254                     }
255                 }
256                 else
257                 {
258                     error.SetErrorString("KDP_REATTACH failed");
259                 }
260             }
261             else
262             {
263                 error.SetErrorString("KDP_REATTACH failed");
264             }
265         }
266         else
267         {
268             error.SetErrorString("invalid reply port from UDP connection");
269         }
270     }
271     else
272     {
273         if (error.Success())
274             error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
275     }
276     if (error.Fail())
277         m_comm.Disconnect();
278 
279     return error;
280 }
281 
282 //----------------------------------------------------------------------
283 // Process Control
284 //----------------------------------------------------------------------
285 Error
286 ProcessKDP::DoLaunch (Module *exe_module,
287                       const ProcessLaunchInfo &launch_info)
288 {
289     Error error;
290     error.SetErrorString ("launching not supported in kdp-remote plug-in");
291     return error;
292 }
293 
294 
295 Error
296 ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
297 {
298     Error error;
299     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
300     return error;
301 }
302 
303 Error
304 ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
305 {
306     Error error;
307     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
308     return error;
309 }
310 
311 Error
312 ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
313 {
314     Error error;
315     error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
316     return error;
317 }
318 
319 
320 void
321 ProcessKDP::DidAttach ()
322 {
323     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
324     if (log)
325         log->Printf ("ProcessKDP::DidAttach()");
326     if (GetID() != LLDB_INVALID_PROCESS_ID)
327     {
328         // TODO: figure out the register context that we will use
329     }
330 }
331 
332 addr_t
333 ProcessKDP::GetImageInfoAddress()
334 {
335     return m_kernel_load_addr;
336 }
337 
338 lldb_private::DynamicLoader *
339 ProcessKDP::GetDynamicLoader ()
340 {
341     if (m_dyld_ap.get() == NULL)
342         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
343     return m_dyld_ap.get();
344 }
345 
346 Error
347 ProcessKDP::WillResume ()
348 {
349     return Error();
350 }
351 
352 Error
353 ProcessKDP::DoResume ()
354 {
355     Error error;
356     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
357     // Only start the async thread if we try to do any process control
358     if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
359         StartAsyncThread ();
360 
361     bool resume = false;
362 
363     // With KDP there is only one thread we can tell what to do
364     ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
365     if (kernel_thread_sp)
366     {
367         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
368         switch (thread_resume_state)
369         {
370             case eStateSuspended:
371                 // Nothing to do here when a thread will stay suspended
372                 // we just leave the CPU mask bit set to zero for the thread
373                 break;
374 
375             case eStateStepping:
376                 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true);
377                 resume = true;
378                 break;
379 
380             case eStateRunning:
381                 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false);
382                 resume = true;
383                 break;
384 
385             default:
386                 // The only valid thread resume states are listed above
387                 assert (!"invalid thread resume state");
388                 break;
389         }
390     }
391 
392     if (resume)
393     {
394         if (log)
395             log->Printf ("ProcessKDP::DoResume () sending resume");
396 
397         if (m_comm.SendRequestResume ())
398         {
399             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
400             SetPrivateState(eStateRunning);
401         }
402         else
403             error.SetErrorString ("KDP resume failed");
404     }
405     else
406     {
407         error.SetErrorString ("kernel thread is suspended");
408     }
409 
410     return error;
411 }
412 
413 lldb::ThreadSP
414 ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
415 {
416     // KDP only tells us about one thread/core. Any other threads will usually
417     // be the ones that are read from memory by the OS plug-ins.
418     const lldb::tid_t kernel_tid = 1;
419     ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
420     if (!thread_sp)
421     {
422         thread_sp.reset(new ThreadKDP (*this, kernel_tid));
423         new_thread_list.AddThread(thread_sp);
424     }
425     return thread_sp;
426 }
427 
428 
429 
430 
431 bool
432 ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
433 {
434     // locker will keep a mutex locked until it goes out of scope
435     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
436     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
437         log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
438 
439     // Even though there is a CPU mask, it doesn't mean to can see each CPU
440     // indivudually, there is really only one. Lets call this thread 1.
441     GetKernelThread (old_thread_list, new_thread_list);
442 
443     return new_thread_list.GetSize(false) > 0;
444 }
445 
446 void
447 ProcessKDP::RefreshStateAfterStop ()
448 {
449     // Let all threads recover from stopping and do any clean up based
450     // on the previous thread state (if any).
451     m_thread_list.RefreshStateAfterStop();
452 }
453 
454 Error
455 ProcessKDP::DoHalt (bool &caused_stop)
456 {
457     Error error;
458 
459     if (m_comm.IsRunning())
460     {
461         if (m_destroy_in_process)
462         {
463             // If we are attemping to destroy, we need to not return an error to
464             // Halt or DoDestroy won't get called.
465             // We are also currently running, so send a process stopped event
466             SetPrivateState (eStateStopped);
467         }
468         else
469         {
470             error.SetErrorString ("KDP cannot interrupt a running kernel");
471         }
472     }
473     return error;
474 }
475 
476 Error
477 ProcessKDP::DoDetach()
478 {
479     Error error;
480     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
481     if (log)
482         log->Printf ("ProcessKDP::DoDetach()");
483 
484     if (m_comm.IsRunning())
485     {
486         // We are running and we can't interrupt a running kernel, so we need
487         // to just close the connection to the kernel and hope for the best
488     }
489     else
490     {
491         DisableAllBreakpointSites ();
492 
493         m_thread_list.DiscardThreadPlans();
494 
495         if (m_comm.IsConnected())
496         {
497 
498             m_comm.SendRequestDisconnect();
499 
500             size_t response_size = m_comm.Disconnect ();
501             if (log)
502             {
503                 if (response_size)
504                     log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
505                 else
506                     log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
507             }
508         }
509     }
510     StopAsyncThread ();
511     m_comm.Clear();
512 
513     SetPrivateState (eStateDetached);
514     ResumePrivateStateThread();
515 
516     //KillDebugserverProcess ();
517     return error;
518 }
519 
520 Error
521 ProcessKDP::WillDestroy ()
522 {
523     Error error;
524     m_destroy_in_process = true;
525     return error;
526 }
527 
528 Error
529 ProcessKDP::DoDestroy ()
530 {
531     // For KDP there really is no difference between destroy and detach
532     return DoDetach();
533 }
534 
535 //------------------------------------------------------------------
536 // Process Queries
537 //------------------------------------------------------------------
538 
539 bool
540 ProcessKDP::IsAlive ()
541 {
542     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
543 }
544 
545 //------------------------------------------------------------------
546 // Process Memory
547 //------------------------------------------------------------------
548 size_t
549 ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
550 {
551     if (m_comm.IsConnected())
552         return m_comm.SendRequestReadMemory (addr, buf, size, error);
553     error.SetErrorString ("not connected");
554     return 0;
555 }
556 
557 size_t
558 ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
559 {
560     if (m_comm.IsConnected())
561         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
562     error.SetErrorString ("not connected");
563     return 0;
564 }
565 
566 lldb::addr_t
567 ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
568 {
569     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
570     return LLDB_INVALID_ADDRESS;
571 }
572 
573 Error
574 ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
575 {
576     Error error;
577     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
578     return error;
579 }
580 
581 Error
582 ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
583 {
584     if (m_comm.LocalBreakpointsAreSupported ())
585     {
586         Error error;
587         if (!bp_site->IsEnabled())
588         {
589             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
590             {
591                 bp_site->SetEnabled(true);
592                 bp_site->SetType (BreakpointSite::eExternal);
593             }
594             else
595             {
596                 error.SetErrorString ("KDP set breakpoint failed");
597             }
598         }
599         return error;
600     }
601     return EnableSoftwareBreakpoint (bp_site);
602 }
603 
604 Error
605 ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
606 {
607     if (m_comm.LocalBreakpointsAreSupported ())
608     {
609         Error error;
610         if (bp_site->IsEnabled())
611         {
612             BreakpointSite::Type bp_type = bp_site->GetType();
613             if (bp_type == BreakpointSite::eExternal)
614             {
615                 if (m_destroy_in_process && m_comm.IsRunning())
616                 {
617                     // We are trying to destroy our connection and we are running
618                     bp_site->SetEnabled(false);
619                 }
620                 else
621                 {
622                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
623                         bp_site->SetEnabled(false);
624                     else
625                         error.SetErrorString ("KDP remove breakpoint failed");
626                 }
627             }
628             else
629             {
630                 error = DisableSoftwareBreakpoint (bp_site);
631             }
632         }
633         return error;
634     }
635     return DisableSoftwareBreakpoint (bp_site);
636 }
637 
638 Error
639 ProcessKDP::EnableWatchpoint (Watchpoint *wp)
640 {
641     Error error;
642     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
643     return error;
644 }
645 
646 Error
647 ProcessKDP::DisableWatchpoint (Watchpoint *wp)
648 {
649     Error error;
650     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
651     return error;
652 }
653 
654 void
655 ProcessKDP::Clear()
656 {
657     m_thread_list.Clear();
658 }
659 
660 Error
661 ProcessKDP::DoSignal (int signo)
662 {
663     Error error;
664     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
665     return error;
666 }
667 
668 void
669 ProcessKDP::Initialize()
670 {
671     static bool g_initialized = false;
672 
673     if (g_initialized == false)
674     {
675         g_initialized = true;
676         PluginManager::RegisterPlugin (GetPluginNameStatic(),
677                                        GetPluginDescriptionStatic(),
678                                        CreateInstance);
679 
680         Log::Callbacks log_callbacks = {
681             ProcessKDPLog::DisableLog,
682             ProcessKDPLog::EnableLog,
683             ProcessKDPLog::ListLogCategories
684         };
685 
686         Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
687     }
688 }
689 
690 bool
691 ProcessKDP::StartAsyncThread ()
692 {
693     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
694 
695     if (log)
696         log->Printf ("ProcessKDP::StartAsyncThread ()");
697 
698     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
699         return true;
700 
701     m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
702     return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
703 }
704 
705 void
706 ProcessKDP::StopAsyncThread ()
707 {
708     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
709 
710     if (log)
711         log->Printf ("ProcessKDP::StopAsyncThread ()");
712 
713     m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
714 
715     // Stop the stdio thread
716     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
717     {
718         Host::ThreadJoin (m_async_thread, NULL, NULL);
719         m_async_thread = LLDB_INVALID_HOST_THREAD;
720     }
721 }
722 
723 
724 void *
725 ProcessKDP::AsyncThread (void *arg)
726 {
727     ProcessKDP *process = (ProcessKDP*) arg;
728 
729     const lldb::pid_t pid = process->GetID();
730 
731     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
732     if (log)
733         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
734 
735     Listener listener ("ProcessKDP::AsyncThread");
736     EventSP event_sp;
737     const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
738                                         eBroadcastBitAsyncThreadShouldExit;
739 
740 
741     if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
742     {
743         bool done = false;
744         while (!done)
745         {
746             if (log)
747                 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
748                              pid);
749             if (listener.WaitForEvent (NULL, event_sp))
750             {
751                 uint32_t event_type = event_sp->GetType();
752                 if (log)
753                     log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
754                                  pid,
755                                  event_type);
756 
757                 // When we are running, poll for 1 second to try and get an exception
758                 // to indicate the process has stopped. If we don't get one, check to
759                 // make sure no one asked us to exit
760                 bool is_running = false;
761                 DataExtractor exc_reply_packet;
762                 do
763                 {
764                     switch (event_type)
765                     {
766                     case eBroadcastBitAsyncContinue:
767                         {
768                             is_running = true;
769                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
770                             {
771                                 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
772                                 thread_sp->GetRegisterContext()->InvalidateAllRegisters();
773                                 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
774 
775                                 // TODO: parse the stop reply packet
776                                 is_running = false;
777                                 process->SetPrivateState(eStateStopped);
778                             }
779                             else
780                             {
781                                 // Check to see if we are supposed to exit. There is no way to
782                                 // interrupt a running kernel, so all we can do is wait for an
783                                 // exception or detach...
784                                 if (listener.GetNextEvent(event_sp))
785                                 {
786                                     // We got an event, go through the loop again
787                                     event_type = event_sp->GetType();
788                                 }
789                             }
790                         }
791                         break;
792 
793                     case eBroadcastBitAsyncThreadShouldExit:
794                         if (log)
795                             log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
796                                          pid);
797                         done = true;
798                         is_running = false;
799                         break;
800 
801                     default:
802                         if (log)
803                             log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
804                                          pid,
805                                          event_type);
806                         done = true;
807                         is_running = false;
808                         break;
809                     }
810                 } while (is_running);
811             }
812             else
813             {
814                 if (log)
815                     log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
816                                  pid);
817                 done = true;
818             }
819         }
820     }
821 
822     if (log)
823         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
824                      arg,
825                      pid);
826 
827     process->m_async_thread = LLDB_INVALID_HOST_THREAD;
828     return NULL;
829 }
830 
831 
832