blob: 3c08a1472462e5bf69e50cfd1664e878b66c1c92 [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();
Greg Claytonb170aee2012-05-08 01:45:38 +000074 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000075 {
Greg Claytonb170aee2012-05-08 01:45:38 +000076 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
77 case llvm::Triple::MacOSX: // For desktop targets
78 case llvm::Triple::IOS: // For arm targets
79 if (triple_ref.getVendor() == llvm::Triple::Apple)
80 {
81 ObjectFile *exe_objfile = exe_module->GetObjectFile();
82 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
83 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
84 return true;
85 }
86 break;
87
88 default:
89 break;
Greg Clayton363be3f2011-07-15 03:27:12 +000090 }
91 }
Greg Clayton61ddf562011-10-21 21:41:45 +000092 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000093}
94
95//----------------------------------------------------------------------
96// ProcessKDP constructor
97//----------------------------------------------------------------------
98ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
99 Process (target, listener),
100 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000101 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton363be3f2011-07-15 03:27:12 +0000102 m_async_thread (LLDB_INVALID_HOST_THREAD)
103{
104// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
105// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
106}
107
108//----------------------------------------------------------------------
109// Destructor
110//----------------------------------------------------------------------
111ProcessKDP::~ProcessKDP()
112{
113 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000114 // We need to call finalize on the process before destroying ourselves
115 // to make sure all of the broadcaster cleanup goes as planned. If we
116 // destruct this class, then Process::~Process() might have problems
117 // trying to fully destroy the broadcaster.
118 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000119}
120
121//----------------------------------------------------------------------
122// PluginInterface
123//----------------------------------------------------------------------
124const char *
125ProcessKDP::GetPluginName()
126{
127 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
128}
129
130const char *
131ProcessKDP::GetShortPluginName()
132{
133 return GetPluginNameStatic();
134}
135
136uint32_t
137ProcessKDP::GetPluginVersion()
138{
139 return 1;
140}
141
142Error
143ProcessKDP::WillLaunch (Module* module)
144{
145 Error error;
146 error.SetErrorString ("launching not supported in kdp-remote plug-in");
147 return error;
148}
149
150Error
151ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
152{
153 Error error;
154 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
155 return error;
156}
157
158Error
159ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
160{
161 Error error;
162 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
163 return error;
164}
165
166Error
167ProcessKDP::DoConnectRemote (const char *remote_url)
168{
169 // TODO: fill in the remote connection to the remote KDP here!
170 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000171
172 if (remote_url == NULL || remote_url[0] == '\0')
173 remote_url = "udp://localhost:41139";
174
175 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
176 if (conn_ap.get())
177 {
178 // Only try once for now.
179 // TODO: check if we should be retrying?
180 const uint32_t max_retry_count = 1;
181 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
182 {
183 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
184 break;
185 usleep (100000);
186 }
187 }
188
189 if (conn_ap->IsConnected())
190 {
191 const uint16_t reply_port = conn_ap->GetReadPort ();
192
193 if (reply_port != 0)
194 {
195 m_comm.SetConnection(conn_ap.release());
196
197 if (m_comm.SendRequestReattach(reply_port))
198 {
199 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
200 {
201 m_comm.GetVersion();
202 uint32_t cpu = m_comm.GetCPUType();
203 uint32_t sub = m_comm.GetCPUSubtype();
204 ArchSpec kernel_arch;
205 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
206 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000207 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000208 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000209 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000210 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
211 if (async_strm_sp)
212 {
Greg Clayton7b139222011-07-21 01:12:01 +0000213 const char *cstr;
214 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000215 {
Greg Clayton7b139222011-07-21 01:12:01 +0000216 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000217 async_strm_sp->Flush();
218 }
Greg Clayton7b139222011-07-21 01:12:01 +0000219// if ((cstr = m_comm.GetImagePath ()) != NULL)
220// {
221// async_strm_sp->Printf ("Image Path: %s\n", cstr);
222// async_strm_sp->Flush();
223// }
Greg Clayton234981a2011-07-20 03:41:06 +0000224 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000225 }
226 }
227 else
228 {
229 error.SetErrorString("KDP reattach failed");
230 }
231 }
232 else
233 {
234 error.SetErrorString("invalid reply port from UDP connection");
235 }
236 }
237 else
238 {
239 if (error.Success())
240 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
241 }
242 if (error.Fail())
243 m_comm.Disconnect();
244
Greg Clayton363be3f2011-07-15 03:27:12 +0000245 return error;
246}
247
248//----------------------------------------------------------------------
249// Process Control
250//----------------------------------------------------------------------
251Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000252ProcessKDP::DoLaunch (Module *exe_module,
253 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000254{
255 Error error;
256 error.SetErrorString ("launching not supported in kdp-remote plug-in");
257 return error;
258}
259
260
261Error
262ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
263{
264 Error error;
265 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
266 return error;
267}
268
Greg Clayton363be3f2011-07-15 03:27:12 +0000269Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000270ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
271{
272 Error error;
273 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
274 return error;
275}
276
277Error
278ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000279{
280 Error error;
281 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
282 return error;
283}
284
285
286void
287ProcessKDP::DidAttach ()
288{
289 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
290 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000291 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000292 if (GetID() != LLDB_INVALID_PROCESS_ID)
293 {
294 // TODO: figure out the register context that we will use
295 }
296}
297
298Error
299ProcessKDP::WillResume ()
300{
301 return Error();
302}
303
304Error
305ProcessKDP::DoResume ()
306{
307 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000308 if (!m_comm.SendRequestResume ())
309 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000310 return error;
311}
312
Greg Claytonae932352012-04-10 00:18:59 +0000313bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000314ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000315{
316 // locker will keep a mutex locked until it goes out of scope
317 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
318 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000319 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000320
Greg Clayton37f962e2011-08-22 02:49:39 +0000321 // We currently are making only one thread per core and we
322 // actually don't know about actual threads. Eventually we
323 // want to get the thread list from memory and note which
324 // threads are on CPU as those are the only ones that we
325 // will be able to resume.
326 const uint32_t cpu_mask = m_comm.GetCPUMask();
327 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000328 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000329 lldb::tid_t tid = cpu_mask_bit;
330 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
331 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000332 thread_sp.reset(new ThreadKDP (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +0000333 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000334 }
Greg Claytonae932352012-04-10 00:18:59 +0000335 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000336}
337
338
339StateType
340ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
341{
342 // TODO: figure out why we stopped given the packet that tells us we stopped...
343 return eStateStopped;
344}
345
346void
347ProcessKDP::RefreshStateAfterStop ()
348{
349 // Let all threads recover from stopping and do any clean up based
350 // on the previous thread state (if any).
351 m_thread_list.RefreshStateAfterStop();
352 //SetThreadStopInfo (m_last_stop_packet);
353}
354
355Error
356ProcessKDP::DoHalt (bool &caused_stop)
357{
358 Error error;
359
360// bool timed_out = false;
361 Mutex::Locker locker;
362
363 if (m_public_state.GetValue() == eStateAttaching)
364 {
365 // We are being asked to halt during an attach. We need to just close
366 // our file handle and debugserver will go away, and we can be done...
367 m_comm.Disconnect();
368 }
369 else
370 {
Greg Clayton234981a2011-07-20 03:41:06 +0000371 if (!m_comm.SendRequestSuspend ())
372 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000373 }
374 return error;
375}
376
377Error
378ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
379 bool catch_stop_event,
380 EventSP &stop_event_sp)
381{
382 Error error;
383
384 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
385
386 bool paused_private_state_thread = false;
387 const bool is_running = m_comm.IsRunning();
388 if (log)
389 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
390 discard_thread_plans,
391 catch_stop_event,
392 is_running);
393
394 if (discard_thread_plans)
395 {
396 if (log)
397 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
398 m_thread_list.DiscardThreadPlans();
399 }
400 if (is_running)
401 {
402 if (catch_stop_event)
403 {
404 if (log)
405 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
406 PausePrivateStateThread();
407 paused_private_state_thread = true;
408 }
409
410 bool timed_out = false;
411// bool sent_interrupt = false;
412 Mutex::Locker locker;
413
414 // TODO: implement halt in CommunicationKDP
415// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
416// {
417// if (timed_out)
418// error.SetErrorString("timed out sending interrupt packet");
419// else
420// error.SetErrorString("unknown error sending interrupt packet");
421// if (paused_private_state_thread)
422// ResumePrivateStateThread();
423// return error;
424// }
425
426 if (catch_stop_event)
427 {
428 // LISTEN HERE
429 TimeValue timeout_time;
430 timeout_time = TimeValue::Now();
431 timeout_time.OffsetWithSeconds(5);
432 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
433
434 timed_out = state == eStateInvalid;
435 if (log)
436 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
437
438 if (timed_out)
439 error.SetErrorString("unable to verify target stopped");
440 }
441
442 if (paused_private_state_thread)
443 {
444 if (log)
445 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
446 ResumePrivateStateThread();
447 }
448 }
449 return error;
450}
451
452Error
453ProcessKDP::WillDetach ()
454{
455 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
456 if (log)
457 log->Printf ("ProcessKDP::WillDetach()");
458
459 bool discard_thread_plans = true;
460 bool catch_stop_event = true;
461 EventSP event_sp;
462 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
463}
464
465Error
466ProcessKDP::DoDetach()
467{
468 Error error;
469 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
470 if (log)
471 log->Printf ("ProcessKDP::DoDetach()");
472
473 DisableAllBreakpointSites ();
474
475 m_thread_list.DiscardThreadPlans();
476
Greg Clayton8d2ea282011-07-17 20:36:25 +0000477 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000478 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000479
480 m_comm.SendRequestDisconnect();
481
482 size_t response_size = m_comm.Disconnect ();
483 if (log)
484 {
485 if (response_size)
486 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
487 else
488 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
489 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000490 }
491 // Sleep for one second to let the process get all detached...
492 StopAsyncThread ();
493
Greg Claytondb9d6f42012-01-31 04:56:17 +0000494 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000495
496 SetPrivateState (eStateDetached);
497 ResumePrivateStateThread();
498
499 //KillDebugserverProcess ();
500 return error;
501}
502
503Error
504ProcessKDP::DoDestroy ()
505{
506 Error error;
507 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
508 if (log)
509 log->Printf ("ProcessKDP::DoDestroy()");
510
511 // Interrupt if our inferior is running...
512 if (m_comm.IsConnected())
513 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000514 if (m_public_state.GetValue() == eStateAttaching)
515 {
516 // We are being asked to halt during an attach. We need to just close
517 // our file handle and debugserver will go away, and we can be done...
518 m_comm.Disconnect();
519 }
520 else
521 {
Greg Clayton7b139222011-07-21 01:12:01 +0000522 DisableAllBreakpointSites ();
523
524 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000525
526 StringExtractor response;
527 // TODO: Send kill packet?
528 SetExitStatus(SIGABRT, NULL);
529 }
530 }
531 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000532 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000533 return error;
534}
535
536//------------------------------------------------------------------
537// Process Queries
538//------------------------------------------------------------------
539
540bool
541ProcessKDP::IsAlive ()
542{
543 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
544}
545
546//------------------------------------------------------------------
547// Process Memory
548//------------------------------------------------------------------
549size_t
550ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
551{
Greg Clayton0fa51242011-07-19 03:57:15 +0000552 if (m_comm.IsConnected())
553 return m_comm.SendRequestReadMemory (addr, buf, size, error);
554 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000555 return 0;
556}
557
558size_t
559ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
560{
561 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
562 return 0;
563}
564
565lldb::addr_t
566ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
567{
568 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
569 return LLDB_INVALID_ADDRESS;
570}
571
572Error
573ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
574{
575 Error error;
576 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
577 return error;
578}
579
580Error
581ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
582{
Greg Clayton234981a2011-07-20 03:41:06 +0000583 if (m_comm.LocalBreakpointsAreSupported ())
584 {
585 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000586 if (!bp_site->IsEnabled())
587 {
588 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
589 {
590 bp_site->SetEnabled(true);
591 bp_site->SetType (BreakpointSite::eExternal);
592 }
593 else
594 {
595 error.SetErrorString ("KDP set breakpoint failed");
596 }
597 }
Greg Clayton234981a2011-07-20 03:41:06 +0000598 return error;
599 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000600 return EnableSoftwareBreakpoint (bp_site);
601}
602
603Error
604ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
605{
Greg Clayton234981a2011-07-20 03:41:06 +0000606 if (m_comm.LocalBreakpointsAreSupported ())
607 {
608 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000609 if (bp_site->IsEnabled())
610 {
611 BreakpointSite::Type bp_type = bp_site->GetType();
612 if (bp_type == BreakpointSite::eExternal)
613 {
614 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
615 bp_site->SetEnabled(false);
616 else
617 error.SetErrorString ("KDP remove breakpoint failed");
618 }
619 else
620 {
621 error = DisableSoftwareBreakpoint (bp_site);
622 }
623 }
Greg Clayton234981a2011-07-20 03:41:06 +0000624 return error;
625 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000626 return DisableSoftwareBreakpoint (bp_site);
627}
628
629Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000630ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000631{
632 Error error;
633 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
634 return error;
635}
636
637Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000638ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000639{
640 Error error;
641 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
642 return error;
643}
644
645void
646ProcessKDP::Clear()
647{
Greg Clayton363be3f2011-07-15 03:27:12 +0000648 m_thread_list.Clear();
649}
650
651Error
652ProcessKDP::DoSignal (int signo)
653{
654 Error error;
655 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
656 return error;
657}
658
659void
660ProcessKDP::Initialize()
661{
662 static bool g_initialized = false;
663
664 if (g_initialized == false)
665 {
666 g_initialized = true;
667 PluginManager::RegisterPlugin (GetPluginNameStatic(),
668 GetPluginDescriptionStatic(),
669 CreateInstance);
670
671 Log::Callbacks log_callbacks = {
672 ProcessKDPLog::DisableLog,
673 ProcessKDPLog::EnableLog,
674 ProcessKDPLog::ListLogCategories
675 };
676
677 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
678 }
679}
680
681bool
682ProcessKDP::StartAsyncThread ()
683{
684 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
685
686 if (log)
687 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
688
689 // Create a thread that watches our internal state and controls which
690 // events make it to clients (into the DCProcess event queue).
691 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
692 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
693}
694
695void
696ProcessKDP::StopAsyncThread ()
697{
698 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
699
700 if (log)
701 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
702
703 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
704
705 // Stop the stdio thread
706 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
707 {
708 Host::ThreadJoin (m_async_thread, NULL, NULL);
709 }
710}
711
712
713void *
714ProcessKDP::AsyncThread (void *arg)
715{
716 ProcessKDP *process = (ProcessKDP*) arg;
717
718 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
719 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000720 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000721
722 Listener listener ("ProcessKDP::AsyncThread");
723 EventSP event_sp;
724 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
725 eBroadcastBitAsyncThreadShouldExit;
726
727 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
728 {
729 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
730
731 bool done = false;
732 while (!done)
733 {
734 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000735 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000736 if (listener.WaitForEvent (NULL, event_sp))
737 {
738 const uint32_t event_type = event_sp->GetType();
739 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
740 {
741 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000742 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 +0000743
744 switch (event_type)
745 {
746 case eBroadcastBitAsyncContinue:
747 {
748 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
749
750 if (continue_packet)
751 {
752 // TODO: do continue support here
753
754// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
755// const size_t continue_cstr_len = continue_packet->GetByteSize ();
756// if (log)
757// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
758//
759// if (::strstr (continue_cstr, "vAttach") == NULL)
760// process->SetPrivateState(eStateRunning);
761// StringExtractor response;
762// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
763//
764// switch (stop_state)
765// {
766// case eStateStopped:
767// case eStateCrashed:
768// case eStateSuspended:
769// process->m_last_stop_packet = response;
770// process->SetPrivateState (stop_state);
771// break;
772//
773// case eStateExited:
774// process->m_last_stop_packet = response;
775// response.SetFilePos(1);
776// process->SetExitStatus(response.GetHexU8(), NULL);
777// done = true;
778// break;
779//
780// case eStateInvalid:
781// process->SetExitStatus(-1, "lost connection");
782// break;
783//
784// default:
785// process->SetPrivateState (stop_state);
786// break;
787// }
788 }
789 }
790 break;
791
792 case eBroadcastBitAsyncThreadShouldExit:
793 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000794 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000795 done = true;
796 break;
797
798 default:
799 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000800 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 +0000801 done = true;
802 break;
803 }
804 }
805 else if (event_sp->BroadcasterIs (&process->m_comm))
806 {
807 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
808 {
809 process->SetExitStatus (-1, "lost connection");
810 done = true;
811 }
812 }
813 }
814 else
815 {
816 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000817 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 +0000818 done = true;
819 }
820 }
821 }
822
823 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000824 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000825
826 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
827 return NULL;
828}
829
830