blob: fed40e0ff1bb02f7bd51c862d043e14761dba31f [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();
Greg Claytonffa43a62011-11-17 04:46:02 +0000100 // We need to call finalize on the process before destroying ourselves
101 // to make sure all of the broadcaster cleanup goes as planned. If we
102 // destruct this class, then Process::~Process() might have problems
103 // trying to fully destroy the broadcaster.
104 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000105}
106
107//----------------------------------------------------------------------
108// PluginInterface
109//----------------------------------------------------------------------
110const char *
111ProcessKDP::GetPluginName()
112{
113 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
114}
115
116const char *
117ProcessKDP::GetShortPluginName()
118{
119 return GetPluginNameStatic();
120}
121
122uint32_t
123ProcessKDP::GetPluginVersion()
124{
125 return 1;
126}
127
128Error
129ProcessKDP::WillLaunch (Module* module)
130{
131 Error error;
132 error.SetErrorString ("launching not supported in kdp-remote plug-in");
133 return error;
134}
135
136Error
137ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
138{
139 Error error;
140 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
141 return error;
142}
143
144Error
145ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
146{
147 Error error;
148 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
149 return error;
150}
151
152Error
153ProcessKDP::DoConnectRemote (const char *remote_url)
154{
155 // TODO: fill in the remote connection to the remote KDP here!
156 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000157
158 if (remote_url == NULL || remote_url[0] == '\0')
159 remote_url = "udp://localhost:41139";
160
161 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
162 if (conn_ap.get())
163 {
164 // Only try once for now.
165 // TODO: check if we should be retrying?
166 const uint32_t max_retry_count = 1;
167 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
168 {
169 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
170 break;
171 usleep (100000);
172 }
173 }
174
175 if (conn_ap->IsConnected())
176 {
177 const uint16_t reply_port = conn_ap->GetReadPort ();
178
179 if (reply_port != 0)
180 {
181 m_comm.SetConnection(conn_ap.release());
182
183 if (m_comm.SendRequestReattach(reply_port))
184 {
185 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
186 {
187 m_comm.GetVersion();
188 uint32_t cpu = m_comm.GetCPUType();
189 uint32_t sub = m_comm.GetCPUSubtype();
190 ArchSpec kernel_arch;
191 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
192 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000193 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000194 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000195 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000196 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
197 if (async_strm_sp)
198 {
Greg Clayton7b139222011-07-21 01:12:01 +0000199 const char *cstr;
200 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000201 {
Greg Clayton7b139222011-07-21 01:12:01 +0000202 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000203 async_strm_sp->Flush();
204 }
Greg Clayton7b139222011-07-21 01:12:01 +0000205// if ((cstr = m_comm.GetImagePath ()) != NULL)
206// {
207// async_strm_sp->Printf ("Image Path: %s\n", cstr);
208// async_strm_sp->Flush();
209// }
Greg Clayton234981a2011-07-20 03:41:06 +0000210 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000211 }
212 }
213 else
214 {
215 error.SetErrorString("KDP reattach failed");
216 }
217 }
218 else
219 {
220 error.SetErrorString("invalid reply port from UDP connection");
221 }
222 }
223 else
224 {
225 if (error.Success())
226 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
227 }
228 if (error.Fail())
229 m_comm.Disconnect();
230
Greg Clayton363be3f2011-07-15 03:27:12 +0000231 return error;
232}
233
234//----------------------------------------------------------------------
235// Process Control
236//----------------------------------------------------------------------
237Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000238ProcessKDP::DoLaunch (Module *exe_module,
239 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000240{
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)
Johnny Chen01df0572011-10-11 21:17:10 +0000269 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000270 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
Greg Clayton37f962e2011-08-22 02:49:39 +0000292ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000293{
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))
Greg Clayton444e35b2011-10-19 18:09:39 +0000297 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000298
Greg Clayton37f962e2011-08-22 02:49:39 +0000299 // We currently are making only one thread per core and we
300 // actually don't know about actual threads. Eventually we
301 // want to get the thread list from memory and note which
302 // threads are on CPU as those are the only ones that we
303 // will be able to resume.
304 const uint32_t cpu_mask = m_comm.GetCPUMask();
305 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000306 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000307 lldb::tid_t tid = cpu_mask_bit;
308 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
309 if (!thread_sp)
310 thread_sp.reset(new ThreadKDP (*this, tid));
311 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000312 }
Greg Clayton37f962e2011-08-22 02:49:39 +0000313 return new_thread_list.GetSize(false);
Greg Clayton363be3f2011-07-15 03:27:12 +0000314}
315
316
317StateType
318ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
319{
320 // TODO: figure out why we stopped given the packet that tells us we stopped...
321 return eStateStopped;
322}
323
324void
325ProcessKDP::RefreshStateAfterStop ()
326{
327 // Let all threads recover from stopping and do any clean up based
328 // on the previous thread state (if any).
329 m_thread_list.RefreshStateAfterStop();
330 //SetThreadStopInfo (m_last_stop_packet);
331}
332
333Error
334ProcessKDP::DoHalt (bool &caused_stop)
335{
336 Error error;
337
338// bool timed_out = false;
339 Mutex::Locker locker;
340
341 if (m_public_state.GetValue() == eStateAttaching)
342 {
343 // We are being asked to halt during an attach. We need to just close
344 // our file handle and debugserver will go away, and we can be done...
345 m_comm.Disconnect();
346 }
347 else
348 {
Greg Clayton234981a2011-07-20 03:41:06 +0000349 if (!m_comm.SendRequestSuspend ())
350 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000351 }
352 return error;
353}
354
355Error
356ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
357 bool catch_stop_event,
358 EventSP &stop_event_sp)
359{
360 Error error;
361
362 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
363
364 bool paused_private_state_thread = false;
365 const bool is_running = m_comm.IsRunning();
366 if (log)
367 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
368 discard_thread_plans,
369 catch_stop_event,
370 is_running);
371
372 if (discard_thread_plans)
373 {
374 if (log)
375 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
376 m_thread_list.DiscardThreadPlans();
377 }
378 if (is_running)
379 {
380 if (catch_stop_event)
381 {
382 if (log)
383 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
384 PausePrivateStateThread();
385 paused_private_state_thread = true;
386 }
387
388 bool timed_out = false;
389// bool sent_interrupt = false;
390 Mutex::Locker locker;
391
392 // TODO: implement halt in CommunicationKDP
393// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
394// {
395// if (timed_out)
396// error.SetErrorString("timed out sending interrupt packet");
397// else
398// error.SetErrorString("unknown error sending interrupt packet");
399// if (paused_private_state_thread)
400// ResumePrivateStateThread();
401// return error;
402// }
403
404 if (catch_stop_event)
405 {
406 // LISTEN HERE
407 TimeValue timeout_time;
408 timeout_time = TimeValue::Now();
409 timeout_time.OffsetWithSeconds(5);
410 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
411
412 timed_out = state == eStateInvalid;
413 if (log)
414 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
415
416 if (timed_out)
417 error.SetErrorString("unable to verify target stopped");
418 }
419
420 if (paused_private_state_thread)
421 {
422 if (log)
423 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
424 ResumePrivateStateThread();
425 }
426 }
427 return error;
428}
429
430Error
431ProcessKDP::WillDetach ()
432{
433 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
434 if (log)
435 log->Printf ("ProcessKDP::WillDetach()");
436
437 bool discard_thread_plans = true;
438 bool catch_stop_event = true;
439 EventSP event_sp;
440 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
441}
442
443Error
444ProcessKDP::DoDetach()
445{
446 Error error;
447 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
448 if (log)
449 log->Printf ("ProcessKDP::DoDetach()");
450
451 DisableAllBreakpointSites ();
452
453 m_thread_list.DiscardThreadPlans();
454
Greg Clayton8d2ea282011-07-17 20:36:25 +0000455 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000456 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000457
458 m_comm.SendRequestDisconnect();
459
460 size_t response_size = m_comm.Disconnect ();
461 if (log)
462 {
463 if (response_size)
464 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
465 else
466 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
467 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000468 }
469 // Sleep for one second to let the process get all detached...
470 StopAsyncThread ();
471
472 m_comm.StopReadThread();
473 m_comm.Disconnect(); // Disconnect from the debug server.
474
475 SetPrivateState (eStateDetached);
476 ResumePrivateStateThread();
477
478 //KillDebugserverProcess ();
479 return error;
480}
481
482Error
483ProcessKDP::DoDestroy ()
484{
485 Error error;
486 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
487 if (log)
488 log->Printf ("ProcessKDP::DoDestroy()");
489
490 // Interrupt if our inferior is running...
491 if (m_comm.IsConnected())
492 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000493 if (m_public_state.GetValue() == eStateAttaching)
494 {
495 // We are being asked to halt during an attach. We need to just close
496 // our file handle and debugserver will go away, and we can be done...
497 m_comm.Disconnect();
498 }
499 else
500 {
Greg Clayton7b139222011-07-21 01:12:01 +0000501 DisableAllBreakpointSites ();
502
503 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000504
505 StringExtractor response;
506 // TODO: Send kill packet?
507 SetExitStatus(SIGABRT, NULL);
508 }
509 }
510 StopAsyncThread ();
511 m_comm.StopReadThread();
512 m_comm.Disconnect(); // Disconnect from the debug server.
513 return error;
514}
515
516//------------------------------------------------------------------
517// Process Queries
518//------------------------------------------------------------------
519
520bool
521ProcessKDP::IsAlive ()
522{
523 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
524}
525
526//------------------------------------------------------------------
527// Process Memory
528//------------------------------------------------------------------
529size_t
530ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
531{
Greg Clayton0fa51242011-07-19 03:57:15 +0000532 if (m_comm.IsConnected())
533 return m_comm.SendRequestReadMemory (addr, buf, size, error);
534 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000535 return 0;
536}
537
538size_t
539ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
540{
541 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
542 return 0;
543}
544
545lldb::addr_t
546ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
547{
548 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
549 return LLDB_INVALID_ADDRESS;
550}
551
552Error
553ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
554{
555 Error error;
556 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
557 return error;
558}
559
560Error
561ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
562{
Greg Clayton234981a2011-07-20 03:41:06 +0000563 if (m_comm.LocalBreakpointsAreSupported ())
564 {
565 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000566 if (!bp_site->IsEnabled())
567 {
568 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
569 {
570 bp_site->SetEnabled(true);
571 bp_site->SetType (BreakpointSite::eExternal);
572 }
573 else
574 {
575 error.SetErrorString ("KDP set breakpoint failed");
576 }
577 }
Greg Clayton234981a2011-07-20 03:41:06 +0000578 return error;
579 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000580 return EnableSoftwareBreakpoint (bp_site);
581}
582
583Error
584ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
585{
Greg Clayton234981a2011-07-20 03:41:06 +0000586 if (m_comm.LocalBreakpointsAreSupported ())
587 {
588 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000589 if (bp_site->IsEnabled())
590 {
591 BreakpointSite::Type bp_type = bp_site->GetType();
592 if (bp_type == BreakpointSite::eExternal)
593 {
594 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
595 bp_site->SetEnabled(false);
596 else
597 error.SetErrorString ("KDP remove breakpoint failed");
598 }
599 else
600 {
601 error = DisableSoftwareBreakpoint (bp_site);
602 }
603 }
Greg Clayton234981a2011-07-20 03:41:06 +0000604 return error;
605 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000606 return DisableSoftwareBreakpoint (bp_site);
607}
608
609Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000610ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000611{
612 Error error;
613 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
614 return error;
615}
616
617Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000618ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000619{
620 Error error;
621 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
622 return error;
623}
624
625void
626ProcessKDP::Clear()
627{
Greg Clayton363be3f2011-07-15 03:27:12 +0000628 m_thread_list.Clear();
629}
630
631Error
632ProcessKDP::DoSignal (int signo)
633{
634 Error error;
635 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
636 return error;
637}
638
639void
640ProcessKDP::Initialize()
641{
642 static bool g_initialized = false;
643
644 if (g_initialized == false)
645 {
646 g_initialized = true;
647 PluginManager::RegisterPlugin (GetPluginNameStatic(),
648 GetPluginDescriptionStatic(),
649 CreateInstance);
650
651 Log::Callbacks log_callbacks = {
652 ProcessKDPLog::DisableLog,
653 ProcessKDPLog::EnableLog,
654 ProcessKDPLog::ListLogCategories
655 };
656
657 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
658 }
659}
660
661bool
662ProcessKDP::StartAsyncThread ()
663{
664 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
665
666 if (log)
667 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
668
669 // Create a thread that watches our internal state and controls which
670 // events make it to clients (into the DCProcess event queue).
671 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
672 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
673}
674
675void
676ProcessKDP::StopAsyncThread ()
677{
678 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
679
680 if (log)
681 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
682
683 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
684
685 // Stop the stdio thread
686 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
687 {
688 Host::ThreadJoin (m_async_thread, NULL, NULL);
689 }
690}
691
692
693void *
694ProcessKDP::AsyncThread (void *arg)
695{
696 ProcessKDP *process = (ProcessKDP*) arg;
697
698 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
699 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000700 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000701
702 Listener listener ("ProcessKDP::AsyncThread");
703 EventSP event_sp;
704 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
705 eBroadcastBitAsyncThreadShouldExit;
706
707 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
708 {
709 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
710
711 bool done = false;
712 while (!done)
713 {
714 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000715 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000716 if (listener.WaitForEvent (NULL, event_sp))
717 {
718 const uint32_t event_type = event_sp->GetType();
719 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
720 {
721 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000722 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 +0000723
724 switch (event_type)
725 {
726 case eBroadcastBitAsyncContinue:
727 {
728 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
729
730 if (continue_packet)
731 {
732 // TODO: do continue support here
733
734// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
735// const size_t continue_cstr_len = continue_packet->GetByteSize ();
736// if (log)
737// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
738//
739// if (::strstr (continue_cstr, "vAttach") == NULL)
740// process->SetPrivateState(eStateRunning);
741// StringExtractor response;
742// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
743//
744// switch (stop_state)
745// {
746// case eStateStopped:
747// case eStateCrashed:
748// case eStateSuspended:
749// process->m_last_stop_packet = response;
750// process->SetPrivateState (stop_state);
751// break;
752//
753// case eStateExited:
754// process->m_last_stop_packet = response;
755// response.SetFilePos(1);
756// process->SetExitStatus(response.GetHexU8(), NULL);
757// done = true;
758// break;
759//
760// case eStateInvalid:
761// process->SetExitStatus(-1, "lost connection");
762// break;
763//
764// default:
765// process->SetPrivateState (stop_state);
766// break;
767// }
768 }
769 }
770 break;
771
772 case eBroadcastBitAsyncThreadShouldExit:
773 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000774 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000775 done = true;
776 break;
777
778 default:
779 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000780 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 +0000781 done = true;
782 break;
783 }
784 }
785 else if (event_sp->BroadcasterIs (&process->m_comm))
786 {
787 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
788 {
789 process->SetExitStatus (-1, "lost connection");
790 done = true;
791 }
792 }
793 }
794 else
795 {
796 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000797 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 +0000798 done = true;
799 }
800 }
801 }
802
803 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000804 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000805
806 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
807 return NULL;
808}
809
810