blob: 948ce9fec369f5c6d204ce427ec5605f5629da35 [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"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000020#include "lldb/Core/State.h"
21#include "lldb/Host/Host.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Symbol/ObjectFile.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000023#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000024#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000025
26// Project includes
27#include "ProcessKDP.h"
28#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000029#include "ThreadKDP.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000030#include "StopInfoMachException.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35const char *
36ProcessKDP::GetPluginNameStatic()
37{
38 return "kdp-remote";
39}
40
41const char *
42ProcessKDP::GetPluginDescriptionStatic()
43{
44 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
45}
46
47void
48ProcessKDP::Terminate()
49{
50 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
51}
52
53
Greg Clayton46c9a352012-02-09 06:16:32 +000054lldb::ProcessSP
55ProcessKDP::CreateInstance (Target &target,
56 Listener &listener,
57 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000058{
Greg Clayton46c9a352012-02-09 06:16:32 +000059 lldb::ProcessSP process_sp;
60 if (crash_file_path == NULL)
61 process_sp.reset(new ProcessKDP (target, listener));
62 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000063}
64
65bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000066ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000067{
Greg Clayton61ddf562011-10-21 21:41:45 +000068 if (plugin_specified_by_name)
69 return true;
70
Greg Clayton363be3f2011-07-15 03:27:12 +000071 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000072 Module *exe_module = target.GetExecutableModulePointer();
73 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000074 {
75 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Claytonb170aee2012-05-08 01:45:38 +000076 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000077 {
Greg Claytonb170aee2012-05-08 01:45:38 +000078 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
79 case llvm::Triple::MacOSX: // For desktop targets
80 case llvm::Triple::IOS: // For arm targets
81 if (triple_ref.getVendor() == llvm::Triple::Apple)
82 {
83 ObjectFile *exe_objfile = exe_module->GetObjectFile();
84 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
85 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
86 return true;
87 }
88 break;
89
90 default:
91 break;
Greg Clayton363be3f2011-07-15 03:27:12 +000092 }
93 }
Greg Clayton61ddf562011-10-21 21:41:45 +000094 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000095}
96
97//----------------------------------------------------------------------
98// ProcessKDP constructor
99//----------------------------------------------------------------------
100ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
101 Process (target, listener),
102 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000103 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton363be3f2011-07-15 03:27:12 +0000104 m_async_thread (LLDB_INVALID_HOST_THREAD)
105{
106// m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
107// m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
108}
109
110//----------------------------------------------------------------------
111// Destructor
112//----------------------------------------------------------------------
113ProcessKDP::~ProcessKDP()
114{
115 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000116 // We need to call finalize on the process before destroying ourselves
117 // to make sure all of the broadcaster cleanup goes as planned. If we
118 // destruct this class, then Process::~Process() might have problems
119 // trying to fully destroy the broadcaster.
120 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000121}
122
123//----------------------------------------------------------------------
124// PluginInterface
125//----------------------------------------------------------------------
126const char *
127ProcessKDP::GetPluginName()
128{
129 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
130}
131
132const char *
133ProcessKDP::GetShortPluginName()
134{
135 return GetPluginNameStatic();
136}
137
138uint32_t
139ProcessKDP::GetPluginVersion()
140{
141 return 1;
142}
143
144Error
145ProcessKDP::WillLaunch (Module* module)
146{
147 Error error;
148 error.SetErrorString ("launching not supported in kdp-remote plug-in");
149 return error;
150}
151
152Error
153ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
154{
155 Error error;
156 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
157 return error;
158}
159
160Error
161ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
162{
163 Error error;
164 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
165 return error;
166}
167
168Error
169ProcessKDP::DoConnectRemote (const char *remote_url)
170{
171 // TODO: fill in the remote connection to the remote KDP here!
172 Error error;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000173
174 if (remote_url == NULL || remote_url[0] == '\0')
175 remote_url = "udp://localhost:41139";
176
177 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
178 if (conn_ap.get())
179 {
180 // Only try once for now.
181 // TODO: check if we should be retrying?
182 const uint32_t max_retry_count = 1;
183 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
184 {
185 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
186 break;
187 usleep (100000);
188 }
189 }
190
191 if (conn_ap->IsConnected())
192 {
193 const uint16_t reply_port = conn_ap->GetReadPort ();
194
195 if (reply_port != 0)
196 {
197 m_comm.SetConnection(conn_ap.release());
198
199 if (m_comm.SendRequestReattach(reply_port))
200 {
201 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
202 {
203 m_comm.GetVersion();
204 uint32_t cpu = m_comm.GetCPUType();
205 uint32_t sub = m_comm.GetCPUSubtype();
206 ArchSpec kernel_arch;
207 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
208 m_target.SetArchitecture(kernel_arch);
Greg Clayton0fa51242011-07-19 03:57:15 +0000209 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000210 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000211 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000212 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
213 if (async_strm_sp)
214 {
Greg Clayton7b139222011-07-21 01:12:01 +0000215 const char *cstr;
216 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000217 {
Greg Clayton7b139222011-07-21 01:12:01 +0000218 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000219 async_strm_sp->Flush();
220 }
Greg Clayton7b139222011-07-21 01:12:01 +0000221// if ((cstr = m_comm.GetImagePath ()) != NULL)
222// {
223// async_strm_sp->Printf ("Image Path: %s\n", cstr);
224// async_strm_sp->Flush();
225// }
Greg Clayton234981a2011-07-20 03:41:06 +0000226 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000227 }
228 }
229 else
230 {
231 error.SetErrorString("KDP reattach failed");
232 }
233 }
234 else
235 {
236 error.SetErrorString("invalid reply port from UDP connection");
237 }
238 }
239 else
240 {
241 if (error.Success())
242 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
243 }
244 if (error.Fail())
245 m_comm.Disconnect();
246
Greg Clayton363be3f2011-07-15 03:27:12 +0000247 return error;
248}
249
250//----------------------------------------------------------------------
251// Process Control
252//----------------------------------------------------------------------
253Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000254ProcessKDP::DoLaunch (Module *exe_module,
255 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000256{
257 Error error;
258 error.SetErrorString ("launching not supported in kdp-remote plug-in");
259 return error;
260}
261
262
263Error
264ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
265{
266 Error error;
267 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
268 return error;
269}
270
Greg Clayton363be3f2011-07-15 03:27:12 +0000271Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000272ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
273{
274 Error error;
275 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
276 return error;
277}
278
279Error
280ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000281{
282 Error error;
283 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
284 return error;
285}
286
287
288void
289ProcessKDP::DidAttach ()
290{
291 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
292 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000293 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000294 if (GetID() != LLDB_INVALID_PROCESS_ID)
295 {
296 // TODO: figure out the register context that we will use
297 }
298}
299
300Error
301ProcessKDP::WillResume ()
302{
303 return Error();
304}
305
306Error
307ProcessKDP::DoResume ()
308{
309 Error error;
Greg Clayton234981a2011-07-20 03:41:06 +0000310 if (!m_comm.SendRequestResume ())
311 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000312 return error;
313}
314
Greg Claytonae932352012-04-10 00:18:59 +0000315bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000316ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000317{
318 // locker will keep a mutex locked until it goes out of scope
319 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
320 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000321 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000322
Greg Clayton37f962e2011-08-22 02:49:39 +0000323 // We currently are making only one thread per core and we
324 // actually don't know about actual threads. Eventually we
325 // want to get the thread list from memory and note which
326 // threads are on CPU as those are the only ones that we
327 // will be able to resume.
328 const uint32_t cpu_mask = m_comm.GetCPUMask();
329 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000330 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000331 lldb::tid_t tid = cpu_mask_bit;
332 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
333 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000334 thread_sp.reset(new ThreadKDP (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +0000335 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000336 }
Greg Claytonae932352012-04-10 00:18:59 +0000337 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000338}
339
340
341StateType
342ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
343{
344 // TODO: figure out why we stopped given the packet that tells us we stopped...
345 return eStateStopped;
346}
347
348void
349ProcessKDP::RefreshStateAfterStop ()
350{
351 // Let all threads recover from stopping and do any clean up based
352 // on the previous thread state (if any).
353 m_thread_list.RefreshStateAfterStop();
354 //SetThreadStopInfo (m_last_stop_packet);
355}
356
357Error
358ProcessKDP::DoHalt (bool &caused_stop)
359{
360 Error error;
361
362// bool timed_out = false;
363 Mutex::Locker locker;
364
365 if (m_public_state.GetValue() == eStateAttaching)
366 {
367 // We are being asked to halt during an attach. We need to just close
368 // our file handle and debugserver will go away, and we can be done...
369 m_comm.Disconnect();
370 }
371 else
372 {
Greg Clayton234981a2011-07-20 03:41:06 +0000373 if (!m_comm.SendRequestSuspend ())
374 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000375 }
376 return error;
377}
378
379Error
380ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
381 bool catch_stop_event,
382 EventSP &stop_event_sp)
383{
384 Error error;
385
386 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
387
388 bool paused_private_state_thread = false;
389 const bool is_running = m_comm.IsRunning();
390 if (log)
391 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
392 discard_thread_plans,
393 catch_stop_event,
394 is_running);
395
396 if (discard_thread_plans)
397 {
398 if (log)
399 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
400 m_thread_list.DiscardThreadPlans();
401 }
402 if (is_running)
403 {
404 if (catch_stop_event)
405 {
406 if (log)
407 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
408 PausePrivateStateThread();
409 paused_private_state_thread = true;
410 }
411
412 bool timed_out = false;
413// bool sent_interrupt = false;
414 Mutex::Locker locker;
415
416 // TODO: implement halt in CommunicationKDP
417// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
418// {
419// if (timed_out)
420// error.SetErrorString("timed out sending interrupt packet");
421// else
422// error.SetErrorString("unknown error sending interrupt packet");
423// if (paused_private_state_thread)
424// ResumePrivateStateThread();
425// return error;
426// }
427
428 if (catch_stop_event)
429 {
430 // LISTEN HERE
431 TimeValue timeout_time;
432 timeout_time = TimeValue::Now();
433 timeout_time.OffsetWithSeconds(5);
434 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
435
436 timed_out = state == eStateInvalid;
437 if (log)
438 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
439
440 if (timed_out)
441 error.SetErrorString("unable to verify target stopped");
442 }
443
444 if (paused_private_state_thread)
445 {
446 if (log)
447 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
448 ResumePrivateStateThread();
449 }
450 }
451 return error;
452}
453
454Error
455ProcessKDP::WillDetach ()
456{
457 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
458 if (log)
459 log->Printf ("ProcessKDP::WillDetach()");
460
461 bool discard_thread_plans = true;
462 bool catch_stop_event = true;
463 EventSP event_sp;
464 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
465}
466
467Error
468ProcessKDP::DoDetach()
469{
470 Error error;
471 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
472 if (log)
473 log->Printf ("ProcessKDP::DoDetach()");
474
475 DisableAllBreakpointSites ();
476
477 m_thread_list.DiscardThreadPlans();
478
Greg Clayton8d2ea282011-07-17 20:36:25 +0000479 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000480 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000481
482 m_comm.SendRequestDisconnect();
483
484 size_t response_size = m_comm.Disconnect ();
485 if (log)
486 {
487 if (response_size)
488 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
489 else
490 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
491 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000492 }
493 // Sleep for one second to let the process get all detached...
494 StopAsyncThread ();
495
Greg Claytondb9d6f42012-01-31 04:56:17 +0000496 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000497
498 SetPrivateState (eStateDetached);
499 ResumePrivateStateThread();
500
501 //KillDebugserverProcess ();
502 return error;
503}
504
505Error
506ProcessKDP::DoDestroy ()
507{
508 Error error;
509 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
510 if (log)
511 log->Printf ("ProcessKDP::DoDestroy()");
512
513 // Interrupt if our inferior is running...
514 if (m_comm.IsConnected())
515 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000516 if (m_public_state.GetValue() == eStateAttaching)
517 {
518 // We are being asked to halt during an attach. We need to just close
519 // our file handle and debugserver will go away, and we can be done...
520 m_comm.Disconnect();
521 }
522 else
523 {
Greg Clayton7b139222011-07-21 01:12:01 +0000524 DisableAllBreakpointSites ();
525
526 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000527
528 StringExtractor response;
529 // TODO: Send kill packet?
530 SetExitStatus(SIGABRT, NULL);
531 }
532 }
533 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000534 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000535 return error;
536}
537
538//------------------------------------------------------------------
539// Process Queries
540//------------------------------------------------------------------
541
542bool
543ProcessKDP::IsAlive ()
544{
545 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
546}
547
548//------------------------------------------------------------------
549// Process Memory
550//------------------------------------------------------------------
551size_t
552ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
553{
Greg Clayton0fa51242011-07-19 03:57:15 +0000554 if (m_comm.IsConnected())
555 return m_comm.SendRequestReadMemory (addr, buf, size, error);
556 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000557 return 0;
558}
559
560size_t
561ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
562{
563 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
564 return 0;
565}
566
567lldb::addr_t
568ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
569{
570 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
571 return LLDB_INVALID_ADDRESS;
572}
573
574Error
575ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
576{
577 Error error;
578 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
579 return error;
580}
581
582Error
583ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
584{
Greg Clayton234981a2011-07-20 03:41:06 +0000585 if (m_comm.LocalBreakpointsAreSupported ())
586 {
587 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000588 if (!bp_site->IsEnabled())
589 {
590 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
591 {
592 bp_site->SetEnabled(true);
593 bp_site->SetType (BreakpointSite::eExternal);
594 }
595 else
596 {
597 error.SetErrorString ("KDP set breakpoint failed");
598 }
599 }
Greg Clayton234981a2011-07-20 03:41:06 +0000600 return error;
601 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000602 return EnableSoftwareBreakpoint (bp_site);
603}
604
605Error
606ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
607{
Greg Clayton234981a2011-07-20 03:41:06 +0000608 if (m_comm.LocalBreakpointsAreSupported ())
609 {
610 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000611 if (bp_site->IsEnabled())
612 {
613 BreakpointSite::Type bp_type = bp_site->GetType();
614 if (bp_type == BreakpointSite::eExternal)
615 {
616 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
617 bp_site->SetEnabled(false);
618 else
619 error.SetErrorString ("KDP remove breakpoint failed");
620 }
621 else
622 {
623 error = DisableSoftwareBreakpoint (bp_site);
624 }
625 }
Greg Clayton234981a2011-07-20 03:41:06 +0000626 return error;
627 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000628 return DisableSoftwareBreakpoint (bp_site);
629}
630
631Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000632ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000633{
634 Error error;
635 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
636 return error;
637}
638
639Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000640ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000641{
642 Error error;
643 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
644 return error;
645}
646
647void
648ProcessKDP::Clear()
649{
Greg Clayton363be3f2011-07-15 03:27:12 +0000650 m_thread_list.Clear();
651}
652
653Error
654ProcessKDP::DoSignal (int signo)
655{
656 Error error;
657 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
658 return error;
659}
660
661void
662ProcessKDP::Initialize()
663{
664 static bool g_initialized = false;
665
666 if (g_initialized == false)
667 {
668 g_initialized = true;
669 PluginManager::RegisterPlugin (GetPluginNameStatic(),
670 GetPluginDescriptionStatic(),
671 CreateInstance);
672
673 Log::Callbacks log_callbacks = {
674 ProcessKDPLog::DisableLog,
675 ProcessKDPLog::EnableLog,
676 ProcessKDPLog::ListLogCategories
677 };
678
679 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
680 }
681}
682
683bool
684ProcessKDP::StartAsyncThread ()
685{
686 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
687
688 if (log)
689 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
690
691 // Create a thread that watches our internal state and controls which
692 // events make it to clients (into the DCProcess event queue).
693 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
694 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
695}
696
697void
698ProcessKDP::StopAsyncThread ()
699{
700 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
701
702 if (log)
703 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
704
705 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
706
707 // Stop the stdio thread
708 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
709 {
710 Host::ThreadJoin (m_async_thread, NULL, NULL);
711 }
712}
713
714
715void *
716ProcessKDP::AsyncThread (void *arg)
717{
718 ProcessKDP *process = (ProcessKDP*) arg;
719
720 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
721 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000722 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000723
724 Listener listener ("ProcessKDP::AsyncThread");
725 EventSP event_sp;
726 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
727 eBroadcastBitAsyncThreadShouldExit;
728
729 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
730 {
731 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
732
733 bool done = false;
734 while (!done)
735 {
736 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000737 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000738 if (listener.WaitForEvent (NULL, event_sp))
739 {
740 const uint32_t event_type = event_sp->GetType();
741 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
742 {
743 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000744 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 +0000745
746 switch (event_type)
747 {
748 case eBroadcastBitAsyncContinue:
749 {
750 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
751
752 if (continue_packet)
753 {
754 // TODO: do continue support here
755
756// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
757// const size_t continue_cstr_len = continue_packet->GetByteSize ();
758// if (log)
759// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
760//
761// if (::strstr (continue_cstr, "vAttach") == NULL)
762// process->SetPrivateState(eStateRunning);
763// StringExtractor response;
764// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
765//
766// switch (stop_state)
767// {
768// case eStateStopped:
769// case eStateCrashed:
770// case eStateSuspended:
771// process->m_last_stop_packet = response;
772// process->SetPrivateState (stop_state);
773// break;
774//
775// case eStateExited:
776// process->m_last_stop_packet = response;
777// response.SetFilePos(1);
778// process->SetExitStatus(response.GetHexU8(), NULL);
779// done = true;
780// break;
781//
782// case eStateInvalid:
783// process->SetExitStatus(-1, "lost connection");
784// break;
785//
786// default:
787// process->SetPrivateState (stop_state);
788// break;
789// }
790 }
791 }
792 break;
793
794 case eBroadcastBitAsyncThreadShouldExit:
795 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000796 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000797 done = true;
798 break;
799
800 default:
801 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000802 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 +0000803 done = true;
804 break;
805 }
806 }
807 else if (event_sp->BroadcasterIs (&process->m_comm))
808 {
809 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
810 {
811 process->SetExitStatus (-1, "lost connection");
812 done = true;
813 }
814 }
815 }
816 else
817 {
818 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000819 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 +0000820 done = true;
821 }
822 }
823 }
824
825 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000826 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000827
828 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
829 return NULL;
830}
831
832