blob: df01e94cb1436cd55a243d7c6d7c6870992eca69 [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
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000233ProcessKDP::DoLaunch (Module *exe_module,
234 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000235{
236 Error error;
237 error.SetErrorString ("launching not supported in kdp-remote plug-in");
238 return error;
239}
240
241
242Error
243ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
244{
245 Error error;
246 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
247 return error;
248}
249
Greg Clayton363be3f2011-07-15 03:27:12 +0000250Error
251ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch)
252{
253 Error error;
254 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
255 return error;
256}
257
258
259void
260ProcessKDP::DidAttach ()
261{
262 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
263 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000264 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000265 if (GetID() != LLDB_INVALID_PROCESS_ID)
266 {
267 // TODO: figure out the register context that we will use
268 }
269}
270
271Error
272ProcessKDP::WillResume ()
273{
274 return Error();
275}
276
277Error
278ProcessKDP::DoResume ()
279{
280 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000281 if (!m_comm.SendRequestResume ())
282 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000283 return error;
284}
285
286uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +0000287ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000288{
289 // locker will keep a mutex locked until it goes out of scope
290 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
291 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000292 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000293
Greg Clayton37f962e2011-08-22 02:49:39 +0000294 // We currently are making only one thread per core and we
295 // actually don't know about actual threads. Eventually we
296 // want to get the thread list from memory and note which
297 // threads are on CPU as those are the only ones that we
298 // will be able to resume.
299 const uint32_t cpu_mask = m_comm.GetCPUMask();
300 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000301 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000302 lldb::tid_t tid = cpu_mask_bit;
303 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
304 if (!thread_sp)
305 thread_sp.reset(new ThreadKDP (*this, tid));
306 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000307 }
Greg Clayton37f962e2011-08-22 02:49:39 +0000308 return new_thread_list.GetSize(false);
Greg Clayton363be3f2011-07-15 03:27:12 +0000309}
310
311
312StateType
313ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
314{
315 // TODO: figure out why we stopped given the packet that tells us we stopped...
316 return eStateStopped;
317}
318
319void
320ProcessKDP::RefreshStateAfterStop ()
321{
322 // Let all threads recover from stopping and do any clean up based
323 // on the previous thread state (if any).
324 m_thread_list.RefreshStateAfterStop();
325 //SetThreadStopInfo (m_last_stop_packet);
326}
327
328Error
329ProcessKDP::DoHalt (bool &caused_stop)
330{
331 Error error;
332
333// bool timed_out = false;
334 Mutex::Locker locker;
335
336 if (m_public_state.GetValue() == eStateAttaching)
337 {
338 // We are being asked to halt during an attach. We need to just close
339 // our file handle and debugserver will go away, and we can be done...
340 m_comm.Disconnect();
341 }
342 else
343 {
Greg Clayton234981a2011-07-20 03:41:06 +0000344 if (!m_comm.SendRequestSuspend ())
345 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000346 }
347 return error;
348}
349
350Error
351ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
352 bool catch_stop_event,
353 EventSP &stop_event_sp)
354{
355 Error error;
356
357 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
358
359 bool paused_private_state_thread = false;
360 const bool is_running = m_comm.IsRunning();
361 if (log)
362 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
363 discard_thread_plans,
364 catch_stop_event,
365 is_running);
366
367 if (discard_thread_plans)
368 {
369 if (log)
370 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
371 m_thread_list.DiscardThreadPlans();
372 }
373 if (is_running)
374 {
375 if (catch_stop_event)
376 {
377 if (log)
378 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
379 PausePrivateStateThread();
380 paused_private_state_thread = true;
381 }
382
383 bool timed_out = false;
384// bool sent_interrupt = false;
385 Mutex::Locker locker;
386
387 // TODO: implement halt in CommunicationKDP
388// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
389// {
390// if (timed_out)
391// error.SetErrorString("timed out sending interrupt packet");
392// else
393// error.SetErrorString("unknown error sending interrupt packet");
394// if (paused_private_state_thread)
395// ResumePrivateStateThread();
396// return error;
397// }
398
399 if (catch_stop_event)
400 {
401 // LISTEN HERE
402 TimeValue timeout_time;
403 timeout_time = TimeValue::Now();
404 timeout_time.OffsetWithSeconds(5);
405 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
406
407 timed_out = state == eStateInvalid;
408 if (log)
409 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
410
411 if (timed_out)
412 error.SetErrorString("unable to verify target stopped");
413 }
414
415 if (paused_private_state_thread)
416 {
417 if (log)
418 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
419 ResumePrivateStateThread();
420 }
421 }
422 return error;
423}
424
425Error
426ProcessKDP::WillDetach ()
427{
428 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
429 if (log)
430 log->Printf ("ProcessKDP::WillDetach()");
431
432 bool discard_thread_plans = true;
433 bool catch_stop_event = true;
434 EventSP event_sp;
435 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
436}
437
438Error
439ProcessKDP::DoDetach()
440{
441 Error error;
442 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
443 if (log)
444 log->Printf ("ProcessKDP::DoDetach()");
445
446 DisableAllBreakpointSites ();
447
448 m_thread_list.DiscardThreadPlans();
449
Greg Clayton8d2ea282011-07-17 20:36:25 +0000450 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000451 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000452
453 m_comm.SendRequestDisconnect();
454
455 size_t response_size = m_comm.Disconnect ();
456 if (log)
457 {
458 if (response_size)
459 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
460 else
461 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
462 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000463 }
464 // Sleep for one second to let the process get all detached...
465 StopAsyncThread ();
466
467 m_comm.StopReadThread();
468 m_comm.Disconnect(); // Disconnect from the debug server.
469
470 SetPrivateState (eStateDetached);
471 ResumePrivateStateThread();
472
473 //KillDebugserverProcess ();
474 return error;
475}
476
477Error
478ProcessKDP::DoDestroy ()
479{
480 Error error;
481 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
482 if (log)
483 log->Printf ("ProcessKDP::DoDestroy()");
484
485 // Interrupt if our inferior is running...
486 if (m_comm.IsConnected())
487 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000488 if (m_public_state.GetValue() == eStateAttaching)
489 {
490 // We are being asked to halt during an attach. We need to just close
491 // our file handle and debugserver will go away, and we can be done...
492 m_comm.Disconnect();
493 }
494 else
495 {
Greg Clayton7b139222011-07-21 01:12:01 +0000496 DisableAllBreakpointSites ();
497
498 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000499
500 StringExtractor response;
501 // TODO: Send kill packet?
502 SetExitStatus(SIGABRT, NULL);
503 }
504 }
505 StopAsyncThread ();
506 m_comm.StopReadThread();
507 m_comm.Disconnect(); // Disconnect from the debug server.
508 return error;
509}
510
511//------------------------------------------------------------------
512// Process Queries
513//------------------------------------------------------------------
514
515bool
516ProcessKDP::IsAlive ()
517{
518 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
519}
520
521//------------------------------------------------------------------
522// Process Memory
523//------------------------------------------------------------------
524size_t
525ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
526{
Greg Clayton0fa51242011-07-19 03:57:15 +0000527 if (m_comm.IsConnected())
528 return m_comm.SendRequestReadMemory (addr, buf, size, error);
529 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000530 return 0;
531}
532
533size_t
534ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
535{
536 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
537 return 0;
538}
539
540lldb::addr_t
541ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
542{
543 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
544 return LLDB_INVALID_ADDRESS;
545}
546
547Error
548ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
549{
550 Error error;
551 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
552 return error;
553}
554
555Error
556ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
557{
Greg Clayton234981a2011-07-20 03:41:06 +0000558 if (m_comm.LocalBreakpointsAreSupported ())
559 {
560 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000561 if (!bp_site->IsEnabled())
562 {
563 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
564 {
565 bp_site->SetEnabled(true);
566 bp_site->SetType (BreakpointSite::eExternal);
567 }
568 else
569 {
570 error.SetErrorString ("KDP set breakpoint failed");
571 }
572 }
Greg Clayton234981a2011-07-20 03:41:06 +0000573 return error;
574 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000575 return EnableSoftwareBreakpoint (bp_site);
576}
577
578Error
579ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
580{
Greg Clayton234981a2011-07-20 03:41:06 +0000581 if (m_comm.LocalBreakpointsAreSupported ())
582 {
583 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000584 if (bp_site->IsEnabled())
585 {
586 BreakpointSite::Type bp_type = bp_site->GetType();
587 if (bp_type == BreakpointSite::eExternal)
588 {
589 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
590 bp_site->SetEnabled(false);
591 else
592 error.SetErrorString ("KDP remove breakpoint failed");
593 }
594 else
595 {
596 error = DisableSoftwareBreakpoint (bp_site);
597 }
598 }
Greg Clayton234981a2011-07-20 03:41:06 +0000599 return error;
600 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000601 return DisableSoftwareBreakpoint (bp_site);
602}
603
604Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000605ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000606{
607 Error error;
608 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
609 return error;
610}
611
612Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000613ProcessKDP::DisableWatchpoint (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
620void
621ProcessKDP::Clear()
622{
623 Mutex::Locker locker (m_thread_list.GetMutex ());
624 m_thread_list.Clear();
625}
626
627Error
628ProcessKDP::DoSignal (int signo)
629{
630 Error error;
631 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
632 return error;
633}
634
635void
636ProcessKDP::Initialize()
637{
638 static bool g_initialized = false;
639
640 if (g_initialized == false)
641 {
642 g_initialized = true;
643 PluginManager::RegisterPlugin (GetPluginNameStatic(),
644 GetPluginDescriptionStatic(),
645 CreateInstance);
646
647 Log::Callbacks log_callbacks = {
648 ProcessKDPLog::DisableLog,
649 ProcessKDPLog::EnableLog,
650 ProcessKDPLog::ListLogCategories
651 };
652
653 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
654 }
655}
656
657bool
658ProcessKDP::StartAsyncThread ()
659{
660 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
661
662 if (log)
663 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
664
665 // Create a thread that watches our internal state and controls which
666 // events make it to clients (into the DCProcess event queue).
667 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
668 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
669}
670
671void
672ProcessKDP::StopAsyncThread ()
673{
674 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
675
676 if (log)
677 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
678
679 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
680
681 // Stop the stdio thread
682 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
683 {
684 Host::ThreadJoin (m_async_thread, NULL, NULL);
685 }
686}
687
688
689void *
690ProcessKDP::AsyncThread (void *arg)
691{
692 ProcessKDP *process = (ProcessKDP*) arg;
693
694 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
695 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000696 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000697
698 Listener listener ("ProcessKDP::AsyncThread");
699 EventSP event_sp;
700 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
701 eBroadcastBitAsyncThreadShouldExit;
702
703 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
704 {
705 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
706
707 bool done = false;
708 while (!done)
709 {
710 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000711 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000712 if (listener.WaitForEvent (NULL, event_sp))
713 {
714 const uint32_t event_type = event_sp->GetType();
715 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
716 {
717 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000718 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 +0000719
720 switch (event_type)
721 {
722 case eBroadcastBitAsyncContinue:
723 {
724 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
725
726 if (continue_packet)
727 {
728 // TODO: do continue support here
729
730// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
731// const size_t continue_cstr_len = continue_packet->GetByteSize ();
732// if (log)
733// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
734//
735// if (::strstr (continue_cstr, "vAttach") == NULL)
736// process->SetPrivateState(eStateRunning);
737// StringExtractor response;
738// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
739//
740// switch (stop_state)
741// {
742// case eStateStopped:
743// case eStateCrashed:
744// case eStateSuspended:
745// process->m_last_stop_packet = response;
746// process->SetPrivateState (stop_state);
747// break;
748//
749// case eStateExited:
750// process->m_last_stop_packet = response;
751// response.SetFilePos(1);
752// process->SetExitStatus(response.GetHexU8(), NULL);
753// done = true;
754// break;
755//
756// case eStateInvalid:
757// process->SetExitStatus(-1, "lost connection");
758// break;
759//
760// default:
761// process->SetPrivateState (stop_state);
762// break;
763// }
764 }
765 }
766 break;
767
768 case eBroadcastBitAsyncThreadShouldExit:
769 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000770 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000771 done = true;
772 break;
773
774 default:
775 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000776 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 +0000777 done = true;
778 break;
779 }
780 }
781 else if (event_sp->BroadcasterIs (&process->m_comm))
782 {
783 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
784 {
785 process->SetExitStatus (-1, "lost connection");
786 done = true;
787 }
788 }
789 }
790 else
791 {
792 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000793 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 +0000794 done = true;
795 }
796 }
797 }
798
799 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000800 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000801
802 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
803 return NULL;
804}
805
806