blob: 1db2d842dd861f44c8b6bc314919471301d56660 [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
Greg Clayton46c9a352012-02-09 06:16:32 +000052lldb::ProcessSP
53ProcessKDP::CreateInstance (Target &target,
54 Listener &listener,
55 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000056{
Greg Clayton46c9a352012-02-09 06:16:32 +000057 lldb::ProcessSP process_sp;
58 if (crash_file_path == NULL)
59 process_sp.reset(new ProcessKDP (target, listener));
60 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000061}
62
63bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000064ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000065{
Greg Clayton61ddf562011-10-21 21:41:45 +000066 if (plugin_specified_by_name)
67 return true;
68
Greg Clayton363be3f2011-07-15 03:27:12 +000069 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000070 Module *exe_module = target.GetExecutableModulePointer();
71 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000072 {
73 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
74 if (triple_ref.getOS() == llvm::Triple::Darwin &&
75 triple_ref.getVendor() == llvm::Triple::Apple)
76 {
Greg Clayton5beb99d2011-08-11 02:48:45 +000077 ObjectFile *exe_objfile = exe_module->GetObjectFile();
Greg Clayton363be3f2011-07-15 03:27:12 +000078 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
79 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
80 return true;
81 }
82 }
Greg Clayton61ddf562011-10-21 21:41:45 +000083 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000084}
85
86//----------------------------------------------------------------------
87// ProcessKDP constructor
88//----------------------------------------------------------------------
89ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
90 Process (target, listener),
91 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +000092 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton363be3f2011-07-15 03:27:12 +000093 m_async_thread (LLDB_INVALID_HOST_THREAD)
94{
95// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
96// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
97}
98
99//----------------------------------------------------------------------
100// Destructor
101//----------------------------------------------------------------------
102ProcessKDP::~ProcessKDP()
103{
104 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000105 // We need to call finalize on the process before destroying ourselves
106 // to make sure all of the broadcaster cleanup goes as planned. If we
107 // destruct this class, then Process::~Process() might have problems
108 // trying to fully destroy the broadcaster.
109 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000110}
111
112//----------------------------------------------------------------------
113// PluginInterface
114//----------------------------------------------------------------------
115const char *
116ProcessKDP::GetPluginName()
117{
118 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
119}
120
121const char *
122ProcessKDP::GetShortPluginName()
123{
124 return GetPluginNameStatic();
125}
126
127uint32_t
128ProcessKDP::GetPluginVersion()
129{
130 return 1;
131}
132
133Error
134ProcessKDP::WillLaunch (Module* module)
135{
136 Error error;
137 error.SetErrorString ("launching not supported in kdp-remote plug-in");
138 return error;
139}
140
141Error
142ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
143{
144 Error error;
145 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
146 return error;
147}
148
149Error
150ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
151{
152 Error error;
153 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
154 return error;
155}
156
157Error
158ProcessKDP::DoConnectRemote (const char *remote_url)
159{
160 // TODO: fill in the remote connection to the remote KDP here!
161 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000162
163 if (remote_url == NULL || remote_url[0] == '\0')
164 remote_url = "udp://localhost:41139";
165
166 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
167 if (conn_ap.get())
168 {
169 // Only try once for now.
170 // TODO: check if we should be retrying?
171 const uint32_t max_retry_count = 1;
172 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
173 {
174 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
175 break;
176 usleep (100000);
177 }
178 }
179
180 if (conn_ap->IsConnected())
181 {
182 const uint16_t reply_port = conn_ap->GetReadPort ();
183
184 if (reply_port != 0)
185 {
186 m_comm.SetConnection(conn_ap.release());
187
188 if (m_comm.SendRequestReattach(reply_port))
189 {
190 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
191 {
192 m_comm.GetVersion();
193 uint32_t cpu = m_comm.GetCPUType();
194 uint32_t sub = m_comm.GetCPUSubtype();
195 ArchSpec kernel_arch;
196 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
197 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000198 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000199 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000200 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000201 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
202 if (async_strm_sp)
203 {
Greg Clayton7b139222011-07-21 01:12:01 +0000204 const char *cstr;
205 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000206 {
Greg Clayton7b139222011-07-21 01:12:01 +0000207 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000208 async_strm_sp->Flush();
209 }
Greg Clayton7b139222011-07-21 01:12:01 +0000210// if ((cstr = m_comm.GetImagePath ()) != NULL)
211// {
212// async_strm_sp->Printf ("Image Path: %s\n", cstr);
213// async_strm_sp->Flush();
214// }
Greg Clayton234981a2011-07-20 03:41:06 +0000215 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000216 }
217 }
218 else
219 {
220 error.SetErrorString("KDP reattach failed");
221 }
222 }
223 else
224 {
225 error.SetErrorString("invalid reply port from UDP connection");
226 }
227 }
228 else
229 {
230 if (error.Success())
231 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
232 }
233 if (error.Fail())
234 m_comm.Disconnect();
235
Greg Clayton363be3f2011-07-15 03:27:12 +0000236 return error;
237}
238
239//----------------------------------------------------------------------
240// Process Control
241//----------------------------------------------------------------------
242Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000243ProcessKDP::DoLaunch (Module *exe_module,
244 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000245{
246 Error error;
247 error.SetErrorString ("launching not supported in kdp-remote plug-in");
248 return error;
249}
250
251
252Error
253ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
254{
255 Error error;
256 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
257 return error;
258}
259
Greg Clayton363be3f2011-07-15 03:27:12 +0000260Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000261ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
262{
263 Error error;
264 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
265 return error;
266}
267
268Error
269ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000270{
271 Error error;
272 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
273 return error;
274}
275
276
277void
278ProcessKDP::DidAttach ()
279{
280 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
281 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000282 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000283 if (GetID() != LLDB_INVALID_PROCESS_ID)
284 {
285 // TODO: figure out the register context that we will use
286 }
287}
288
289Error
290ProcessKDP::WillResume ()
291{
292 return Error();
293}
294
295Error
296ProcessKDP::DoResume ()
297{
298 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000299 if (!m_comm.SendRequestResume ())
300 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000301 return error;
302}
303
304uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +0000305ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000306{
307 // locker will keep a mutex locked until it goes out of scope
308 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
309 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000310 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000311
Greg Clayton37f962e2011-08-22 02:49:39 +0000312 // We currently are making only one thread per core and we
313 // actually don't know about actual threads. Eventually we
314 // want to get the thread list from memory and note which
315 // threads are on CPU as those are the only ones that we
316 // will be able to resume.
317 const uint32_t cpu_mask = m_comm.GetCPUMask();
318 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000319 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000320 lldb::tid_t tid = cpu_mask_bit;
321 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
322 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000323 thread_sp.reset(new ThreadKDP (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +0000324 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000325 }
Greg Clayton37f962e2011-08-22 02:49:39 +0000326 return new_thread_list.GetSize(false);
Greg Clayton363be3f2011-07-15 03:27:12 +0000327}
328
329
330StateType
331ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
332{
333 // TODO: figure out why we stopped given the packet that tells us we stopped...
334 return eStateStopped;
335}
336
337void
338ProcessKDP::RefreshStateAfterStop ()
339{
340 // Let all threads recover from stopping and do any clean up based
341 // on the previous thread state (if any).
342 m_thread_list.RefreshStateAfterStop();
343 //SetThreadStopInfo (m_last_stop_packet);
344}
345
346Error
347ProcessKDP::DoHalt (bool &caused_stop)
348{
349 Error error;
350
351// bool timed_out = false;
352 Mutex::Locker locker;
353
354 if (m_public_state.GetValue() == eStateAttaching)
355 {
356 // We are being asked to halt during an attach. We need to just close
357 // our file handle and debugserver will go away, and we can be done...
358 m_comm.Disconnect();
359 }
360 else
361 {
Greg Clayton234981a2011-07-20 03:41:06 +0000362 if (!m_comm.SendRequestSuspend ())
363 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000364 }
365 return error;
366}
367
368Error
369ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
370 bool catch_stop_event,
371 EventSP &stop_event_sp)
372{
373 Error error;
374
375 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
376
377 bool paused_private_state_thread = false;
378 const bool is_running = m_comm.IsRunning();
379 if (log)
380 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
381 discard_thread_plans,
382 catch_stop_event,
383 is_running);
384
385 if (discard_thread_plans)
386 {
387 if (log)
388 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
389 m_thread_list.DiscardThreadPlans();
390 }
391 if (is_running)
392 {
393 if (catch_stop_event)
394 {
395 if (log)
396 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
397 PausePrivateStateThread();
398 paused_private_state_thread = true;
399 }
400
401 bool timed_out = false;
402// bool sent_interrupt = false;
403 Mutex::Locker locker;
404
405 // TODO: implement halt in CommunicationKDP
406// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
407// {
408// if (timed_out)
409// error.SetErrorString("timed out sending interrupt packet");
410// else
411// error.SetErrorString("unknown error sending interrupt packet");
412// if (paused_private_state_thread)
413// ResumePrivateStateThread();
414// return error;
415// }
416
417 if (catch_stop_event)
418 {
419 // LISTEN HERE
420 TimeValue timeout_time;
421 timeout_time = TimeValue::Now();
422 timeout_time.OffsetWithSeconds(5);
423 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
424
425 timed_out = state == eStateInvalid;
426 if (log)
427 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
428
429 if (timed_out)
430 error.SetErrorString("unable to verify target stopped");
431 }
432
433 if (paused_private_state_thread)
434 {
435 if (log)
436 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
437 ResumePrivateStateThread();
438 }
439 }
440 return error;
441}
442
443Error
444ProcessKDP::WillDetach ()
445{
446 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
447 if (log)
448 log->Printf ("ProcessKDP::WillDetach()");
449
450 bool discard_thread_plans = true;
451 bool catch_stop_event = true;
452 EventSP event_sp;
453 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
454}
455
456Error
457ProcessKDP::DoDetach()
458{
459 Error error;
460 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
461 if (log)
462 log->Printf ("ProcessKDP::DoDetach()");
463
464 DisableAllBreakpointSites ();
465
466 m_thread_list.DiscardThreadPlans();
467
Greg Clayton8d2ea282011-07-17 20:36:25 +0000468 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000469 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000470
471 m_comm.SendRequestDisconnect();
472
473 size_t response_size = m_comm.Disconnect ();
474 if (log)
475 {
476 if (response_size)
477 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
478 else
479 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
480 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000481 }
482 // Sleep for one second to let the process get all detached...
483 StopAsyncThread ();
484
Greg Claytondb9d6f42012-01-31 04:56:17 +0000485 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000486
487 SetPrivateState (eStateDetached);
488 ResumePrivateStateThread();
489
490 //KillDebugserverProcess ();
491 return error;
492}
493
494Error
495ProcessKDP::DoDestroy ()
496{
497 Error error;
498 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
499 if (log)
500 log->Printf ("ProcessKDP::DoDestroy()");
501
502 // Interrupt if our inferior is running...
503 if (m_comm.IsConnected())
504 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000505 if (m_public_state.GetValue() == eStateAttaching)
506 {
507 // We are being asked to halt during an attach. We need to just close
508 // our file handle and debugserver will go away, and we can be done...
509 m_comm.Disconnect();
510 }
511 else
512 {
Greg Clayton7b139222011-07-21 01:12:01 +0000513 DisableAllBreakpointSites ();
514
515 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000516
517 StringExtractor response;
518 // TODO: Send kill packet?
519 SetExitStatus(SIGABRT, NULL);
520 }
521 }
522 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000523 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000524 return error;
525}
526
527//------------------------------------------------------------------
528// Process Queries
529//------------------------------------------------------------------
530
531bool
532ProcessKDP::IsAlive ()
533{
534 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
535}
536
537//------------------------------------------------------------------
538// Process Memory
539//------------------------------------------------------------------
540size_t
541ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
542{
Greg Clayton0fa51242011-07-19 03:57:15 +0000543 if (m_comm.IsConnected())
544 return m_comm.SendRequestReadMemory (addr, buf, size, error);
545 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000546 return 0;
547}
548
549size_t
550ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
551{
552 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
553 return 0;
554}
555
556lldb::addr_t
557ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
558{
559 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
560 return LLDB_INVALID_ADDRESS;
561}
562
563Error
564ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
565{
566 Error error;
567 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
568 return error;
569}
570
571Error
572ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
573{
Greg Clayton234981a2011-07-20 03:41:06 +0000574 if (m_comm.LocalBreakpointsAreSupported ())
575 {
576 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000577 if (!bp_site->IsEnabled())
578 {
579 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
580 {
581 bp_site->SetEnabled(true);
582 bp_site->SetType (BreakpointSite::eExternal);
583 }
584 else
585 {
586 error.SetErrorString ("KDP set breakpoint failed");
587 }
588 }
Greg Clayton234981a2011-07-20 03:41:06 +0000589 return error;
590 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000591 return EnableSoftwareBreakpoint (bp_site);
592}
593
594Error
595ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
596{
Greg Clayton234981a2011-07-20 03:41:06 +0000597 if (m_comm.LocalBreakpointsAreSupported ())
598 {
599 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000600 if (bp_site->IsEnabled())
601 {
602 BreakpointSite::Type bp_type = bp_site->GetType();
603 if (bp_type == BreakpointSite::eExternal)
604 {
605 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
606 bp_site->SetEnabled(false);
607 else
608 error.SetErrorString ("KDP remove breakpoint failed");
609 }
610 else
611 {
612 error = DisableSoftwareBreakpoint (bp_site);
613 }
614 }
Greg Clayton234981a2011-07-20 03:41:06 +0000615 return error;
616 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000617 return DisableSoftwareBreakpoint (bp_site);
618}
619
620Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000621ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000622{
623 Error error;
624 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
625 return error;
626}
627
628Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000629ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000630{
631 Error error;
632 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
633 return error;
634}
635
636void
637ProcessKDP::Clear()
638{
Greg Clayton363be3f2011-07-15 03:27:12 +0000639 m_thread_list.Clear();
640}
641
642Error
643ProcessKDP::DoSignal (int signo)
644{
645 Error error;
646 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
647 return error;
648}
649
650void
651ProcessKDP::Initialize()
652{
653 static bool g_initialized = false;
654
655 if (g_initialized == false)
656 {
657 g_initialized = true;
658 PluginManager::RegisterPlugin (GetPluginNameStatic(),
659 GetPluginDescriptionStatic(),
660 CreateInstance);
661
662 Log::Callbacks log_callbacks = {
663 ProcessKDPLog::DisableLog,
664 ProcessKDPLog::EnableLog,
665 ProcessKDPLog::ListLogCategories
666 };
667
668 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
669 }
670}
671
672bool
673ProcessKDP::StartAsyncThread ()
674{
675 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
676
677 if (log)
678 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
679
680 // Create a thread that watches our internal state and controls which
681 // events make it to clients (into the DCProcess event queue).
682 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
683 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
684}
685
686void
687ProcessKDP::StopAsyncThread ()
688{
689 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
690
691 if (log)
692 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
693
694 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
695
696 // Stop the stdio thread
697 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
698 {
699 Host::ThreadJoin (m_async_thread, NULL, NULL);
700 }
701}
702
703
704void *
705ProcessKDP::AsyncThread (void *arg)
706{
707 ProcessKDP *process = (ProcessKDP*) arg;
708
709 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
710 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000711 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000712
713 Listener listener ("ProcessKDP::AsyncThread");
714 EventSP event_sp;
715 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
716 eBroadcastBitAsyncThreadShouldExit;
717
718 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
719 {
720 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
721
722 bool done = false;
723 while (!done)
724 {
725 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000726 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000727 if (listener.WaitForEvent (NULL, event_sp))
728 {
729 const uint32_t event_type = event_sp->GetType();
730 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
731 {
732 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000733 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 +0000734
735 switch (event_type)
736 {
737 case eBroadcastBitAsyncContinue:
738 {
739 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
740
741 if (continue_packet)
742 {
743 // TODO: do continue support here
744
745// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
746// const size_t continue_cstr_len = continue_packet->GetByteSize ();
747// if (log)
748// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
749//
750// if (::strstr (continue_cstr, "vAttach") == NULL)
751// process->SetPrivateState(eStateRunning);
752// StringExtractor response;
753// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
754//
755// switch (stop_state)
756// {
757// case eStateStopped:
758// case eStateCrashed:
759// case eStateSuspended:
760// process->m_last_stop_packet = response;
761// process->SetPrivateState (stop_state);
762// break;
763//
764// case eStateExited:
765// process->m_last_stop_packet = response;
766// response.SetFilePos(1);
767// process->SetExitStatus(response.GetHexU8(), NULL);
768// done = true;
769// break;
770//
771// case eStateInvalid:
772// process->SetExitStatus(-1, "lost connection");
773// break;
774//
775// default:
776// process->SetPrivateState (stop_state);
777// break;
778// }
779 }
780 }
781 break;
782
783 case eBroadcastBitAsyncThreadShouldExit:
784 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000785 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000786 done = true;
787 break;
788
789 default:
790 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000791 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 +0000792 done = true;
793 break;
794 }
795 }
796 else if (event_sp->BroadcasterIs (&process->m_comm))
797 {
798 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
799 {
800 process->SetExitStatus (-1, "lost connection");
801 done = true;
802 }
803 }
804 }
805 else
806 {
807 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000808 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 +0000809 done = true;
810 }
811 }
812 }
813
814 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000815 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000816
817 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
818 return NULL;
819}
820
821