blob: 8fa7cc67c69ae436aa3e893985c4d20c7ae4c9b2 [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 Claytonea636012012-09-21 01:55:30 +0000310 if (m_comm.SendRequestResume ())
311 {
312 SetPrivateState(eStateRunning);
313 DataExtractor exc_reply_packet;
314 if (m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 60 * USEC_PER_SEC))
315 {
316 SetPrivateState(eStateStopped);
317 }
318 }
319 else
Greg Clayton234981a2011-07-20 03:41:06 +0000320 error.SetErrorString ("KDP resume failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000321 return error;
322}
323
Greg Claytonae932352012-04-10 00:18:59 +0000324bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000325ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000326{
327 // locker will keep a mutex locked until it goes out of scope
328 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
329 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000330 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000331
Greg Clayton37f962e2011-08-22 02:49:39 +0000332 // We currently are making only one thread per core and we
333 // actually don't know about actual threads. Eventually we
334 // want to get the thread list from memory and note which
335 // threads are on CPU as those are the only ones that we
336 // will be able to resume.
337 const uint32_t cpu_mask = m_comm.GetCPUMask();
338 for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1)
Greg Clayton363be3f2011-07-15 03:27:12 +0000339 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000340 lldb::tid_t tid = cpu_mask_bit;
341 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
342 if (!thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000343 thread_sp.reset(new ThreadKDP (shared_from_this(), tid));
Greg Clayton37f962e2011-08-22 02:49:39 +0000344 new_thread_list.AddThread(thread_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000345 }
Greg Claytonae932352012-04-10 00:18:59 +0000346 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000347}
348
349
350StateType
351ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet)
352{
353 // TODO: figure out why we stopped given the packet that tells us we stopped...
354 return eStateStopped;
355}
356
357void
358ProcessKDP::RefreshStateAfterStop ()
359{
360 // Let all threads recover from stopping and do any clean up based
361 // on the previous thread state (if any).
362 m_thread_list.RefreshStateAfterStop();
363 //SetThreadStopInfo (m_last_stop_packet);
364}
365
366Error
367ProcessKDP::DoHalt (bool &caused_stop)
368{
369 Error error;
370
371// bool timed_out = false;
372 Mutex::Locker locker;
373
374 if (m_public_state.GetValue() == eStateAttaching)
375 {
376 // We are being asked to halt during an attach. We need to just close
377 // our file handle and debugserver will go away, and we can be done...
378 m_comm.Disconnect();
379 }
380 else
381 {
Greg Clayton234981a2011-07-20 03:41:06 +0000382 if (!m_comm.SendRequestSuspend ())
383 error.SetErrorString ("KDP halt failed");
Greg Clayton363be3f2011-07-15 03:27:12 +0000384 }
385 return error;
386}
387
388Error
389ProcessKDP::InterruptIfRunning (bool discard_thread_plans,
390 bool catch_stop_event,
391 EventSP &stop_event_sp)
392{
393 Error error;
394
395 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
396
397 bool paused_private_state_thread = false;
398 const bool is_running = m_comm.IsRunning();
399 if (log)
400 log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i",
401 discard_thread_plans,
402 catch_stop_event,
403 is_running);
404
405 if (discard_thread_plans)
406 {
407 if (log)
408 log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans");
409 m_thread_list.DiscardThreadPlans();
410 }
411 if (is_running)
412 {
413 if (catch_stop_event)
414 {
415 if (log)
416 log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread");
417 PausePrivateStateThread();
418 paused_private_state_thread = true;
419 }
420
421 bool timed_out = false;
422// bool sent_interrupt = false;
423 Mutex::Locker locker;
424
425 // TODO: implement halt in CommunicationKDP
426// if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out))
427// {
428// if (timed_out)
429// error.SetErrorString("timed out sending interrupt packet");
430// else
431// error.SetErrorString("unknown error sending interrupt packet");
432// if (paused_private_state_thread)
433// ResumePrivateStateThread();
434// return error;
435// }
436
437 if (catch_stop_event)
438 {
439 // LISTEN HERE
440 TimeValue timeout_time;
441 timeout_time = TimeValue::Now();
442 timeout_time.OffsetWithSeconds(5);
443 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp);
444
445 timed_out = state == eStateInvalid;
446 if (log)
447 log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out);
448
449 if (timed_out)
450 error.SetErrorString("unable to verify target stopped");
451 }
452
453 if (paused_private_state_thread)
454 {
455 if (log)
456 log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread");
457 ResumePrivateStateThread();
458 }
459 }
460 return error;
461}
462
463Error
464ProcessKDP::WillDetach ()
465{
466 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
467 if (log)
468 log->Printf ("ProcessKDP::WillDetach()");
469
470 bool discard_thread_plans = true;
471 bool catch_stop_event = true;
472 EventSP event_sp;
473 return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp);
474}
475
476Error
477ProcessKDP::DoDetach()
478{
479 Error error;
480 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
481 if (log)
482 log->Printf ("ProcessKDP::DoDetach()");
483
484 DisableAllBreakpointSites ();
485
486 m_thread_list.DiscardThreadPlans();
487
Greg Clayton8d2ea282011-07-17 20:36:25 +0000488 if (m_comm.IsConnected())
Greg Clayton363be3f2011-07-15 03:27:12 +0000489 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000490
491 m_comm.SendRequestDisconnect();
492
493 size_t response_size = m_comm.Disconnect ();
494 if (log)
495 {
496 if (response_size)
497 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
498 else
499 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
500 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000501 }
502 // Sleep for one second to let the process get all detached...
503 StopAsyncThread ();
504
Greg Claytondb9d6f42012-01-31 04:56:17 +0000505 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000506
507 SetPrivateState (eStateDetached);
508 ResumePrivateStateThread();
509
510 //KillDebugserverProcess ();
511 return error;
512}
513
514Error
515ProcessKDP::DoDestroy ()
516{
517 Error error;
518 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
519 if (log)
520 log->Printf ("ProcessKDP::DoDestroy()");
521
522 // Interrupt if our inferior is running...
523 if (m_comm.IsConnected())
524 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000525 if (m_public_state.GetValue() == eStateAttaching)
526 {
527 // We are being asked to halt during an attach. We need to just close
528 // our file handle and debugserver will go away, and we can be done...
529 m_comm.Disconnect();
530 }
531 else
532 {
Greg Clayton7b139222011-07-21 01:12:01 +0000533 DisableAllBreakpointSites ();
534
535 m_comm.SendRequestDisconnect();
Greg Clayton363be3f2011-07-15 03:27:12 +0000536
537 StringExtractor response;
538 // TODO: Send kill packet?
539 SetExitStatus(SIGABRT, NULL);
540 }
541 }
542 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000543 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000544 return error;
545}
546
547//------------------------------------------------------------------
548// Process Queries
549//------------------------------------------------------------------
550
551bool
552ProcessKDP::IsAlive ()
553{
554 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
555}
556
557//------------------------------------------------------------------
558// Process Memory
559//------------------------------------------------------------------
560size_t
561ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
562{
Greg Clayton0fa51242011-07-19 03:57:15 +0000563 if (m_comm.IsConnected())
564 return m_comm.SendRequestReadMemory (addr, buf, size, error);
565 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000566 return 0;
567}
568
569size_t
570ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
571{
572 error.SetErrorString ("ProcessKDP::DoReadMemory not implemented");
573 return 0;
574}
575
576lldb::addr_t
577ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
578{
579 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
580 return LLDB_INVALID_ADDRESS;
581}
582
583Error
584ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
585{
586 Error error;
587 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
588 return error;
589}
590
591Error
592ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
593{
Greg Clayton234981a2011-07-20 03:41:06 +0000594 if (m_comm.LocalBreakpointsAreSupported ())
595 {
596 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000597 if (!bp_site->IsEnabled())
598 {
599 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
600 {
601 bp_site->SetEnabled(true);
602 bp_site->SetType (BreakpointSite::eExternal);
603 }
604 else
605 {
606 error.SetErrorString ("KDP set breakpoint failed");
607 }
608 }
Greg Clayton234981a2011-07-20 03:41:06 +0000609 return error;
610 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000611 return EnableSoftwareBreakpoint (bp_site);
612}
613
614Error
615ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
616{
Greg Clayton234981a2011-07-20 03:41:06 +0000617 if (m_comm.LocalBreakpointsAreSupported ())
618 {
619 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000620 if (bp_site->IsEnabled())
621 {
622 BreakpointSite::Type bp_type = bp_site->GetType();
623 if (bp_type == BreakpointSite::eExternal)
624 {
625 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
626 bp_site->SetEnabled(false);
627 else
628 error.SetErrorString ("KDP remove breakpoint failed");
629 }
630 else
631 {
632 error = DisableSoftwareBreakpoint (bp_site);
633 }
634 }
Greg Clayton234981a2011-07-20 03:41:06 +0000635 return error;
636 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000637 return DisableSoftwareBreakpoint (bp_site);
638}
639
640Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000641ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000642{
643 Error error;
644 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
645 return error;
646}
647
648Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000649ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000650{
651 Error error;
652 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
653 return error;
654}
655
656void
657ProcessKDP::Clear()
658{
Greg Clayton363be3f2011-07-15 03:27:12 +0000659 m_thread_list.Clear();
660}
661
662Error
663ProcessKDP::DoSignal (int signo)
664{
665 Error error;
666 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
667 return error;
668}
669
670void
671ProcessKDP::Initialize()
672{
673 static bool g_initialized = false;
674
675 if (g_initialized == false)
676 {
677 g_initialized = true;
678 PluginManager::RegisterPlugin (GetPluginNameStatic(),
679 GetPluginDescriptionStatic(),
680 CreateInstance);
681
682 Log::Callbacks log_callbacks = {
683 ProcessKDPLog::DisableLog,
684 ProcessKDPLog::EnableLog,
685 ProcessKDPLog::ListLogCategories
686 };
687
688 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
689 }
690}
691
692bool
693ProcessKDP::StartAsyncThread ()
694{
695 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
696
697 if (log)
698 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
699
700 // Create a thread that watches our internal state and controls which
701 // events make it to clients (into the DCProcess event queue).
702 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
703 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
704}
705
706void
707ProcessKDP::StopAsyncThread ()
708{
709 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
710
711 if (log)
712 log->Printf ("ProcessKDP::%s ()", __FUNCTION__);
713
714 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
715
716 // Stop the stdio thread
717 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
718 {
719 Host::ThreadJoin (m_async_thread, NULL, NULL);
720 }
721}
722
723
724void *
725ProcessKDP::AsyncThread (void *arg)
726{
727 ProcessKDP *process = (ProcessKDP*) arg;
728
729 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
730 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000731 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000732
733 Listener listener ("ProcessKDP::AsyncThread");
734 EventSP event_sp;
735 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
736 eBroadcastBitAsyncThreadShouldExit;
737
738 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
739 {
740 listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit);
741
742 bool done = false;
743 while (!done)
744 {
745 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000746 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000747 if (listener.WaitForEvent (NULL, event_sp))
748 {
749 const uint32_t event_type = event_sp->GetType();
750 if (event_sp->BroadcasterIs (&process->m_async_broadcaster))
751 {
752 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000753 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 +0000754
755 switch (event_type)
756 {
757 case eBroadcastBitAsyncContinue:
758 {
759 const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get());
760
761 if (continue_packet)
762 {
763 // TODO: do continue support here
764
765// const char *continue_cstr = (const char *)continue_packet->GetBytes ();
766// const size_t continue_cstr_len = continue_packet->GetByteSize ();
767// if (log)
768// log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr);
769//
770// if (::strstr (continue_cstr, "vAttach") == NULL)
771// process->SetPrivateState(eStateRunning);
772// StringExtractor response;
773// StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response);
774//
775// switch (stop_state)
776// {
777// case eStateStopped:
778// case eStateCrashed:
779// case eStateSuspended:
780// process->m_last_stop_packet = response;
781// process->SetPrivateState (stop_state);
782// break;
783//
784// case eStateExited:
785// process->m_last_stop_packet = response;
786// response.SetFilePos(1);
787// process->SetExitStatus(response.GetHexU8(), NULL);
788// done = true;
789// break;
790//
791// case eStateInvalid:
792// process->SetExitStatus(-1, "lost connection");
793// break;
794//
795// default:
796// process->SetPrivateState (stop_state);
797// break;
798// }
799 }
800 }
801 break;
802
803 case eBroadcastBitAsyncThreadShouldExit:
804 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000805 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000806 done = true;
807 break;
808
809 default:
810 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000811 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 +0000812 done = true;
813 break;
814 }
815 }
816 else if (event_sp->BroadcasterIs (&process->m_comm))
817 {
818 if (event_type & Communication::eBroadcastBitReadThreadDidExit)
819 {
820 process->SetExitStatus (-1, "lost connection");
821 done = true;
822 }
823 }
824 }
825 else
826 {
827 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000828 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 +0000829 done = true;
830 }
831 }
832 }
833
834 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000835 log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000836
837 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
838 return NULL;
839}
840
841