blob: 8f327d6220dc75238cb3d9069f2f710d5b2ad296 [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
Greg Claytondb9d6f42012-01-31 04:56:17 +0000472 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000473
474 SetPrivateState (eStateDetached);
475 ResumePrivateStateThread();
476
477 //KillDebugserverProcess ();
478 return error;
479}
480
481Error
482ProcessKDP::DoDestroy ()
483{
484 Error error;
485 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
486 if (log)
487 log->Printf ("ProcessKDP::DoDestroy()");
488
489 // Interrupt if our inferior is running...
490 if (m_comm.IsConnected())
491 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000492 if (m_public_state.GetValue() == eStateAttaching)
493 {
494 // We are being asked to halt during an attach. We need to just close
495 // our file handle and debugserver will go away, and we can be done...
496 m_comm.Disconnect();
497 }
498 else
499 {
Greg Clayton7b139222011-07-21 01:12:01 +0000500 DisableAllBreakpointSites ();
501
502 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000503
504 StringExtractor response;
505 // TODO: Send kill packet?
506 SetExitStatus(SIGABRT, NULL);
507 }
508 }
509 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000510 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000511 return error;
512}
513
514//------------------------------------------------------------------
515// Process Queries
516//------------------------------------------------------------------
517
518bool
519ProcessKDP::IsAlive ()
520{
521 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
522}
523
524//------------------------------------------------------------------
525// Process Memory
526//------------------------------------------------------------------
527size_t
528ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
529{
Greg Clayton0fa51242011-07-19 03:57:15 +0000530 if (m_comm.IsConnected())
531 return m_comm.SendRequestReadMemory (addr, buf, size, error);
532 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000533 return 0;
534}
535
536size_t
537ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
538{
539 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
540 return 0;
541}
542
543lldb::addr_t
544ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
545{
546 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
547 return LLDB_INVALID_ADDRESS;
548}
549
550Error
551ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
552{
553 Error error;
554 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
555 return error;
556}
557
558Error
559ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
560{
Greg Clayton234981a2011-07-20 03:41:06 +0000561 if (m_comm.LocalBreakpointsAreSupported ())
562 {
563 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000564 if (!bp_site->IsEnabled())
565 {
566 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
567 {
568 bp_site->SetEnabled(true);
569 bp_site->SetType (BreakpointSite::eExternal);
570 }
571 else
572 {
573 error.SetErrorString ("KDP set breakpoint failed");
574 }
575 }
Greg Clayton234981a2011-07-20 03:41:06 +0000576 return error;
577 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000578 return EnableSoftwareBreakpoint (bp_site);
579}
580
581Error
582ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
583{
Greg Clayton234981a2011-07-20 03:41:06 +0000584 if (m_comm.LocalBreakpointsAreSupported ())
585 {
586 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000587 if (bp_site->IsEnabled())
588 {
589 BreakpointSite::Type bp_type = bp_site->GetType();
590 if (bp_type == BreakpointSite::eExternal)
591 {
592 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
593 bp_site->SetEnabled(false);
594 else
595 error.SetErrorString ("KDP remove breakpoint failed");
596 }
597 else
598 {
599 error = DisableSoftwareBreakpoint (bp_site);
600 }
601 }
Greg Clayton234981a2011-07-20 03:41:06 +0000602 return error;
603 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000604 return DisableSoftwareBreakpoint (bp_site);
605}
606
607Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000608ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000609{
610 Error error;
611 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
612 return error;
613}
614
615Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000616ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000617{
618 Error error;
619 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
620 return error;
621}
622
623void
624ProcessKDP::Clear()
625{
Greg Clayton363be3f2011-07-15 03:27:12 +0000626 m_thread_list.Clear();
627}
628
629Error
630ProcessKDP::DoSignal (int signo)
631{
632 Error error;
633 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
634 return error;
635}
636
637void
638ProcessKDP::Initialize()
639{
640 static bool g_initialized = false;
641
642 if (g_initialized == false)
643 {
644 g_initialized = true;
645 PluginManager::RegisterPlugin (GetPluginNameStatic(),
646 GetPluginDescriptionStatic(),
647 CreateInstance);
648
649 Log::Callbacks log_callbacks = {
650 ProcessKDPLog::DisableLog,
651 ProcessKDPLog::EnableLog,
652 ProcessKDPLog::ListLogCategories
653 };
654
655 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
656 }
657}
658
659bool
660ProcessKDP::StartAsyncThread ()
661{
662 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
663
664 if (log)
665 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
666
667 // Create a thread that watches our internal state and controls which
668 // events make it to clients (into the DCProcess event queue).
669 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
670 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
671}
672
673void
674ProcessKDP::StopAsyncThread ()
675{
676 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
677
678 if (log)
679 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
680
681 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
682
683 // Stop the stdio thread
684 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
685 {
686 Host::ThreadJoin (m_async_thread, NULL, NULL);
687 }
688}
689
690
691void *
692ProcessKDP::AsyncThread (void *arg)
693{
694 ProcessKDP *process = (ProcessKDP*) arg;
695
696 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
697 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000698 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000699
700 Listener listener ("ProcessKDP::AsyncThread");
701 EventSP event_sp;
702 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
703 eBroadcastBitAsyncThreadShouldExit;
704
705 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
706 {
707 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
708
709 bool done = false;
710 while (!done)
711 {
712 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000713 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000714 if (listener.WaitForEvent (NULL, event_sp))
715 {
716 const uint32_t event_type = event_sp->GetType();
717 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
718 {
719 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000720 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 +0000721
722 switch (event_type)
723 {
724 case eBroadcastBitAsyncContinue:
725 {
726 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
727
728 if (continue_packet)
729 {
730 // TODO: do continue support here
731
732// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
733// const size_t continue_cstr_len = continue_packet->GetByteSize ();
734// if (log)
735// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
736//
737// if (::strstr (continue_cstr, "vAttach") == NULL)
738// process->SetPrivateState(eStateRunning);
739// StringExtractor response;
740// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
741//
742// switch (stop_state)
743// {
744// case eStateStopped:
745// case eStateCrashed:
746// case eStateSuspended:
747// process->m_last_stop_packet = response;
748// process->SetPrivateState (stop_state);
749// break;
750//
751// case eStateExited:
752// process->m_last_stop_packet = response;
753// response.SetFilePos(1);
754// process->SetExitStatus(response.GetHexU8(), NULL);
755// done = true;
756// break;
757//
758// case eStateInvalid:
759// process->SetExitStatus(-1, "lost connection");
760// break;
761//
762// default:
763// process->SetPrivateState (stop_state);
764// break;
765// }
766 }
767 }
768 break;
769
770 case eBroadcastBitAsyncThreadShouldExit:
771 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000772 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000773 done = true;
774 break;
775
776 default:
777 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000778 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 +0000779 done = true;
780 break;
781 }
782 }
783 else if (event_sp->BroadcasterIs (&process->m_comm))
784 {
785 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
786 {
787 process->SetExitStatus (-1, "lost connection");
788 done = true;
789 }
790 }
791 }
792 else
793 {
794 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000795 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 +0000796 done = true;
797 }
798 }
799 }
800
801 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000802 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000803
804 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
805 return NULL;
806}
807
808