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