blob: 9ae965ec84fe780672f95c2c577a81a43ca2ef7a [file] [log] [blame]
Greg Clayton363be3f2011-07-15 03:27:12 +00001//===-- ProcessKDP.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <errno.h>
12#include <stdlib.h>
13
14// C++ Includes
15// Other libraries and framework includes
16#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/State.h"
18#include "lldb/Host/Host.h"
19
20// Project includes
21#include "ProcessKDP.h"
22#include "ProcessKDPLog.h"
23//#include "ThreadKDP.h"
24#include "StopInfoMachException.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29const char *
30ProcessKDP::GetPluginNameStatic()
31{
32 return "kdp-remote";
33}
34
35const char *
36ProcessKDP::GetPluginDescriptionStatic()
37{
38 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
39}
40
41void
42ProcessKDP::Terminate()
43{
44 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
45}
46
47
48Process*
49ProcessKDP::CreateInstance (Target &target, Listener &listener)
50{
51 return new ProcessKDP (target, listener);
52}
53
54bool
55ProcessKDP::CanDebug(Target &target)
56{
57 // For now we are just making sure the file exists for a given module
58 ModuleSP exe_module_sp(target.GetExecutableModule());
59 if (exe_module_sp.get())
60 {
61 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
62 if (triple_ref.getOS() == llvm::Triple::Darwin &&
63 triple_ref.getVendor() == llvm::Triple::Apple)
64 {
65
66 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
67 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
68 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
69 return true;
70 }
71 }
72 return false;
73}
74
75//----------------------------------------------------------------------
76// ProcessKDP constructor
77//----------------------------------------------------------------------
78ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
79 Process (target, listener),
80 m_comm("lldb.process.kdp-remote.communication"),
81 m_async_broadcaster ("lldb.process.kdp-remote.async-broadcaster"),
82 m_async_thread (LLDB_INVALID_HOST_THREAD)
83{
84// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
85// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
86}
87
88//----------------------------------------------------------------------
89// Destructor
90//----------------------------------------------------------------------
91ProcessKDP::~ProcessKDP()
92{
93 Clear();
94}
95
96//----------------------------------------------------------------------
97// PluginInterface
98//----------------------------------------------------------------------
99const char *
100ProcessKDP::GetPluginName()
101{
102 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
103}
104
105const char *
106ProcessKDP::GetShortPluginName()
107{
108 return GetPluginNameStatic();
109}
110
111uint32_t
112ProcessKDP::GetPluginVersion()
113{
114 return 1;
115}
116
117Error
118ProcessKDP::WillLaunch (Module* module)
119{
120 Error error;
121 error.SetErrorString ("launching not supported in kdp-remote plug-in");
122 return error;
123}
124
125Error
126ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
127{
128 Error error;
129 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
130 return error;
131}
132
133Error
134ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
135{
136 Error error;
137 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
138 return error;
139}
140
141Error
142ProcessKDP::DoConnectRemote (const char *remote_url)
143{
144 // TODO: fill in the remote connection to the remote KDP here!
145 Error error;
146 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
147 return error;
148}
149
150//----------------------------------------------------------------------
151// Process Control
152//----------------------------------------------------------------------
153Error
154ProcessKDP::DoLaunch (Module* module,
155 char const *argv[],
156 char const *envp[],
157 uint32_t launch_flags,
158 const char *stdin_path,
159 const char *stdout_path,
160 const char *stderr_path,
161 const char *working_dir)
162{
163 Error error;
164 error.SetErrorString ("launching not supported in kdp-remote plug-in");
165 return error;
166}
167
168
169Error
170ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
171{
172 Error error;
173 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
174 return error;
175}
176
177size_t
178ProcessKDP::AttachInputReaderCallback (void *baton,
179 InputReader *reader,
180 lldb::InputReaderAction notification,
181 const char *bytes,
182 size_t bytes_len)
183{
184 if (notification == eInputReaderGotToken)
185 {
186// ProcessKDP *process = (ProcessKDP *)baton;
187// if (process->m_waiting_for_attach)
188// process->m_waiting_for_attach = false;
189 reader->SetIsDone(true);
190 return 1;
191 }
192 return 0;
193}
194
195Error
196ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch)
197{
198 Error error;
199 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
200 return error;
201}
202
203
204void
205ProcessKDP::DidAttach ()
206{
207 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
208 if (log)
209 log->Printf ("ProcessKDP::DidLaunch()");
210 if (GetID() != LLDB_INVALID_PROCESS_ID)
211 {
212 // TODO: figure out the register context that we will use
213 }
214}
215
216Error
217ProcessKDP::WillResume ()
218{
219 return Error();
220}
221
222Error
223ProcessKDP::DoResume ()
224{
225 Error error;
226 error.SetErrorString ("ProcessKDP::DoResume () is not implemented yet");
227 return error;
228}
229
230uint32_t
231ProcessKDP::UpdateThreadListIfNeeded ()
232{
233 // locker will keep a mutex locked until it goes out of scope
234 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
235 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
236 log->Printf ("ProcessKDP::%s (pid = %i)", __FUNCTION__, GetID());
237
238 Mutex::Locker locker (m_thread_list.GetMutex ());
239 // TODO: get the thread list here!
240 const uint32_t stop_id = GetStopID();
241 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
242 {
243 // Update the thread list's stop id immediately so we don't recurse into this function.
244// ThreadList curr_thread_list (this);
245// curr_thread_list.SetStopID(stop_id);
246//
247// std::vector<lldb::tid_t> thread_ids;
248// bool sequence_mutex_unavailable = false;
249// const size_t num_thread_ids = m_comm.GetCurrentThreadIDs (thread_ids, sequence_mutex_unavailable);
250// if (num_thread_ids > 0)
251// {
252// for (size_t i=0; i<num_thread_ids; ++i)
253// {
254// tid_t tid = thread_ids[i];
255// ThreadSP thread_sp (GetThreadList().FindThreadByID (tid, false));
256// if (!thread_sp)
257// thread_sp.reset (new ThreadGDBRemote (*this, tid));
258// curr_thread_list.AddThread(thread_sp);
259// }
260// }
261//
262// if (sequence_mutex_unavailable == false)
263// {
264// m_thread_list = curr_thread_list;
265// SetThreadStopInfo (m_last_stop_packet);
266// }
267 }
268 return GetThreadList().GetSize(false);
269}
270
271
272StateType
273ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
274{
275 // TODO: figure out why we stopped given the packet that tells us we stopped...
276 return eStateStopped;
277}
278
279void
280ProcessKDP::RefreshStateAfterStop ()
281{
282 // Let all threads recover from stopping and do any clean up based
283 // on the previous thread state (if any).
284 m_thread_list.RefreshStateAfterStop();
285 //SetThreadStopInfo (m_last_stop_packet);
286}
287
288Error
289ProcessKDP::DoHalt (bool &caused_stop)
290{
291 Error error;
292
293// bool timed_out = false;
294 Mutex::Locker locker;
295
296 if (m_public_state.GetValue() == eStateAttaching)
297 {
298 // We are being asked to halt during an attach. We need to just close
299 // our file handle and debugserver will go away, and we can be done...
300 m_comm.Disconnect();
301 }
302 else
303 {
304 // TODO: add the ability to halt a running kernel
305 error.SetErrorString ("halt not supported in kdp-remote plug-in");
306// if (!m_comm.SendInterrupt (locker, 2, caused_stop, timed_out))
307// {
308// if (timed_out)
309// error.SetErrorString("timed out sending interrupt packet");
310// else
311// error.SetErrorString("unknown error sending interrupt packet");
312// }
313 }
314 return error;
315}
316
317Error
318ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
319 bool catch_stop_event,
320 EventSP &stop_event_sp)
321{
322 Error error;
323
324 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
325
326 bool paused_private_state_thread = false;
327 const bool is_running = m_comm.IsRunning();
328 if (log)
329 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
330 discard_thread_plans,
331 catch_stop_event,
332 is_running);
333
334 if (discard_thread_plans)
335 {
336 if (log)
337 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
338 m_thread_list.DiscardThreadPlans();
339 }
340 if (is_running)
341 {
342 if (catch_stop_event)
343 {
344 if (log)
345 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
346 PausePrivateStateThread();
347 paused_private_state_thread = true;
348 }
349
350 bool timed_out = false;
351// bool sent_interrupt = false;
352 Mutex::Locker locker;
353
354 // TODO: implement halt in CommunicationKDP
355// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
356// {
357// if (timed_out)
358// error.SetErrorString("timed out sending interrupt packet");
359// else
360// error.SetErrorString("unknown error sending interrupt packet");
361// if (paused_private_state_thread)
362// ResumePrivateStateThread();
363// return error;
364// }
365
366 if (catch_stop_event)
367 {
368 // LISTEN HERE
369 TimeValue timeout_time;
370 timeout_time = TimeValue::Now();
371 timeout_time.OffsetWithSeconds(5);
372 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
373
374 timed_out = state == eStateInvalid;
375 if (log)
376 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
377
378 if (timed_out)
379 error.SetErrorString("unable to verify target stopped");
380 }
381
382 if (paused_private_state_thread)
383 {
384 if (log)
385 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
386 ResumePrivateStateThread();
387 }
388 }
389 return error;
390}
391
392Error
393ProcessKDP::WillDetach ()
394{
395 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
396 if (log)
397 log->Printf ("ProcessKDP::WillDetach()");
398
399 bool discard_thread_plans = true;
400 bool catch_stop_event = true;
401 EventSP event_sp;
402 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
403}
404
405Error
406ProcessKDP::DoDetach()
407{
408 Error error;
409 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
410 if (log)
411 log->Printf ("ProcessKDP::DoDetach()");
412
413 DisableAllBreakpointSites ();
414
415 m_thread_list.DiscardThreadPlans();
416
417 size_t response_size = m_comm.SendPacket ("D", 1);
418 if (log)
419 {
420 if (response_size)
421 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
422 else
423 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
424 }
425 // Sleep for one second to let the process get all detached...
426 StopAsyncThread ();
427
428 m_comm.StopReadThread();
429 m_comm.Disconnect(); // Disconnect from the debug server.
430
431 SetPrivateState (eStateDetached);
432 ResumePrivateStateThread();
433
434 //KillDebugserverProcess ();
435 return error;
436}
437
438Error
439ProcessKDP::DoDestroy ()
440{
441 Error error;
442 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
443 if (log)
444 log->Printf ("ProcessKDP::DoDestroy()");
445
446 // Interrupt if our inferior is running...
447 if (m_comm.IsConnected())
448 {
449 if (m_public_state.GetValue() == eStateAttaching)
450 {
451 // We are being asked to halt during an attach. We need to just close
452 // our file handle and debugserver will go away, and we can be done...
453 m_comm.Disconnect();
454 }
455 else
456 {
457
458 StringExtractor response;
459 // TODO: Send kill packet?
460 SetExitStatus(SIGABRT, NULL);
461 }
462 }
463 StopAsyncThread ();
464 m_comm.StopReadThread();
465 m_comm.Disconnect(); // Disconnect from the debug server.
466 return error;
467}
468
469//------------------------------------------------------------------
470// Process Queries
471//------------------------------------------------------------------
472
473bool
474ProcessKDP::IsAlive ()
475{
476 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
477}
478
479//------------------------------------------------------------------
480// Process Memory
481//------------------------------------------------------------------
482size_t
483ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
484{
485 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
486 return 0;
487}
488
489size_t
490ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
491{
492 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
493 return 0;
494}
495
496lldb::addr_t
497ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
498{
499 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
500 return LLDB_INVALID_ADDRESS;
501}
502
503Error
504ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
505{
506 Error error;
507 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
508 return error;
509}
510
511Error
512ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
513{
514 return EnableSoftwareBreakpoint (bp_site);
515}
516
517Error
518ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
519{
520 return DisableSoftwareBreakpoint (bp_site);
521}
522
523Error
524ProcessKDP::EnableWatchpoint (WatchpointLocation *wp)
525{
526 Error error;
527 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
528 return error;
529}
530
531Error
532ProcessKDP::DisableWatchpoint (WatchpointLocation *wp)
533{
534 Error error;
535 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
536 return error;
537}
538
539void
540ProcessKDP::Clear()
541{
542 Mutex::Locker locker (m_thread_list.GetMutex ());
543 m_thread_list.Clear();
544}
545
546Error
547ProcessKDP::DoSignal (int signo)
548{
549 Error error;
550 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
551 return error;
552}
553
554void
555ProcessKDP::Initialize()
556{
557 static bool g_initialized = false;
558
559 if (g_initialized == false)
560 {
561 g_initialized = true;
562 PluginManager::RegisterPlugin (GetPluginNameStatic(),
563 GetPluginDescriptionStatic(),
564 CreateInstance);
565
566 Log::Callbacks log_callbacks = {
567 ProcessKDPLog::DisableLog,
568 ProcessKDPLog::EnableLog,
569 ProcessKDPLog::ListLogCategories
570 };
571
572 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
573 }
574}
575
576bool
577ProcessKDP::StartAsyncThread ()
578{
579 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
580
581 if (log)
582 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
583
584 // Create a thread that watches our internal state and controls which
585 // events make it to clients (into the DCProcess event queue).
586 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
587 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
588}
589
590void
591ProcessKDP::StopAsyncThread ()
592{
593 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
594
595 if (log)
596 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
597
598 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
599
600 // Stop the stdio thread
601 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
602 {
603 Host::ThreadJoin (m_async_thread, NULL, NULL);
604 }
605}
606
607
608void *
609ProcessKDP::AsyncThread (void *arg)
610{
611 ProcessKDP *process = (ProcessKDP*) arg;
612
613 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
614 if (log)
615 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID());
616
617 Listener listener ("ProcessKDP::AsyncThread");
618 EventSP event_sp;
619 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
620 eBroadcastBitAsyncThreadShouldExit;
621
622 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
623 {
624 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
625
626 bool done = false;
627 while (!done)
628 {
629 if (log)
630 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
631 if (listener.WaitForEvent (NULL, event_sp))
632 {
633 const uint32_t event_type = event_sp->GetType();
634 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
635 {
636 if (log)
637 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type);
638
639 switch (event_type)
640 {
641 case eBroadcastBitAsyncContinue:
642 {
643 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
644
645 if (continue_packet)
646 {
647 // TODO: do continue support here
648
649// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
650// const size_t continue_cstr_len = continue_packet->GetByteSize ();
651// if (log)
652// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
653//
654// if (::strstr (continue_cstr, "vAttach") == NULL)
655// process->SetPrivateState(eStateRunning);
656// StringExtractor response;
657// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
658//
659// switch (stop_state)
660// {
661// case eStateStopped:
662// case eStateCrashed:
663// case eStateSuspended:
664// process->m_last_stop_packet = response;
665// process->SetPrivateState (stop_state);
666// break;
667//
668// case eStateExited:
669// process->m_last_stop_packet = response;
670// response.SetFilePos(1);
671// process->SetExitStatus(response.GetHexU8(), NULL);
672// done = true;
673// break;
674//
675// case eStateInvalid:
676// process->SetExitStatus(-1, "lost connection");
677// break;
678//
679// default:
680// process->SetPrivateState (stop_state);
681// break;
682// }
683 }
684 }
685 break;
686
687 case eBroadcastBitAsyncThreadShouldExit:
688 if (log)
689 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
690 done = true;
691 break;
692
693 default:
694 if (log)
695 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type);
696 done = true;
697 break;
698 }
699 }
700 else if (event_sp->BroadcasterIs (&process->m_comm))
701 {
702 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
703 {
704 process->SetExitStatus (-1, "lost connection");
705 done = true;
706 }
707 }
708 }
709 else
710 {
711 if (log)
712 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID());
713 done = true;
714 }
715 }
716 }
717
718 if (log)
719 log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, arg, process->GetID());
720
721 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
722 return NULL;
723}
724
725