blob: c5be91435d7dc8b32c523b2b7d008c4c6d45296f [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 Claytone76f8c42012-09-21 16:31:20 +000023#include "lldb/Target/RegisterContext.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000024#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000025#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000026
27// Project includes
28#include "ProcessKDP.h"
29#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000030#include "ThreadKDP.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000031
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 Clayton3acaa922012-09-25 02:40:06 +0000104 m_async_thread (LLDB_INVALID_HOST_THREAD),
105 m_destroy_in_process (false)
Greg Clayton363be3f2011-07-15 03:27:12 +0000106{
Greg Claytone76f8c42012-09-21 16:31:20 +0000107 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
108 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton363be3f2011-07-15 03:27:12 +0000109}
110
111//----------------------------------------------------------------------
112// Destructor
113//----------------------------------------------------------------------
114ProcessKDP::~ProcessKDP()
115{
116 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000117 // We need to call finalize on the process before destroying ourselves
118 // to make sure all of the broadcaster cleanup goes as planned. If we
119 // destruct this class, then Process::~Process() might have problems
120 // trying to fully destroy the broadcaster.
121 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000122}
123
124//----------------------------------------------------------------------
125// PluginInterface
126//----------------------------------------------------------------------
127const char *
128ProcessKDP::GetPluginName()
129{
130 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
131}
132
133const char *
134ProcessKDP::GetShortPluginName()
135{
136 return GetPluginNameStatic();
137}
138
139uint32_t
140ProcessKDP::GetPluginVersion()
141{
142 return 1;
143}
144
145Error
146ProcessKDP::WillLaunch (Module* module)
147{
148 Error error;
149 error.SetErrorString ("launching not supported in kdp-remote plug-in");
150 return error;
151}
152
153Error
154ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
155{
156 Error error;
157 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
158 return error;
159}
160
161Error
162ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
163{
164 Error error;
165 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
166 return error;
167}
168
169Error
170ProcessKDP::DoConnectRemote (const char *remote_url)
171{
Greg Clayton363be3f2011-07-15 03:27:12 +0000172 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000173
174 // Don't let any JIT happen when doing KDP as we can't allocate
175 // memory and we don't want to be mucking with threads that might
176 // already be handling exceptions
177 SetCanJIT(false);
178
Greg Clayton8d2ea282011-07-17 20:36:25 +0000179 if (remote_url == NULL || remote_url[0] == '\0')
Greg Claytone76f8c42012-09-21 16:31:20 +0000180 {
181 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
182 return error;
183 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000184
185 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
186 if (conn_ap.get())
187 {
188 // Only try once for now.
189 // TODO: check if we should be retrying?
190 const uint32_t max_retry_count = 1;
191 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
192 {
193 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
194 break;
195 usleep (100000);
196 }
197 }
198
199 if (conn_ap->IsConnected())
200 {
201 const uint16_t reply_port = conn_ap->GetReadPort ();
202
203 if (reply_port != 0)
204 {
205 m_comm.SetConnection(conn_ap.release());
206
207 if (m_comm.SendRequestReattach(reply_port))
208 {
209 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
210 {
211 m_comm.GetVersion();
212 uint32_t cpu = m_comm.GetCPUType();
213 uint32_t sub = m_comm.GetCPUSubtype();
214 ArchSpec kernel_arch;
215 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
216 m_target.SetArchitecture(kernel_arch);
Greg Clayton3acaa922012-09-25 02:40:06 +0000217 // Set the thread ID
218 UpdateThreadListIfNeeded ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000219 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000220 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000221 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000222 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
223 if (async_strm_sp)
224 {
Greg Clayton7b139222011-07-21 01:12:01 +0000225 const char *cstr;
226 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000227 {
Greg Clayton7b139222011-07-21 01:12:01 +0000228 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000229 async_strm_sp->Flush();
230 }
Greg Clayton7b139222011-07-21 01:12:01 +0000231// if ((cstr = m_comm.GetImagePath ()) != NULL)
232// {
233// async_strm_sp->Printf ("Image Path: %s\n", cstr);
234// async_strm_sp->Flush();
235// }
Greg Clayton234981a2011-07-20 03:41:06 +0000236 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000237 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000238 else
239 {
240 puts ("KDP_CONNECT failed"); // REMOVE THIS
241 error.SetErrorString("KDP_REATTACH failed");
242 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000243 }
244 else
245 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000246 puts ("KDP_REATTACH failed"); // REMOVE THIS
247 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton8d2ea282011-07-17 20:36:25 +0000248 }
249 }
250 else
251 {
252 error.SetErrorString("invalid reply port from UDP connection");
253 }
254 }
255 else
256 {
257 if (error.Success())
258 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
259 }
260 if (error.Fail())
261 m_comm.Disconnect();
262
Greg Clayton363be3f2011-07-15 03:27:12 +0000263 return error;
264}
265
266//----------------------------------------------------------------------
267// Process Control
268//----------------------------------------------------------------------
269Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000270ProcessKDP::DoLaunch (Module *exe_module,
271 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000272{
273 Error error;
274 error.SetErrorString ("launching not supported in kdp-remote plug-in");
275 return error;
276}
277
278
279Error
280ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
281{
282 Error error;
283 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
284 return error;
285}
286
Greg Clayton363be3f2011-07-15 03:27:12 +0000287Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000288ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
289{
290 Error error;
291 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
292 return error;
293}
294
295Error
296ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000297{
298 Error error;
299 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
300 return error;
301}
302
303
304void
305ProcessKDP::DidAttach ()
306{
307 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
308 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000309 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000310 if (GetID() != LLDB_INVALID_PROCESS_ID)
311 {
312 // TODO: figure out the register context that we will use
313 }
314}
315
316Error
317ProcessKDP::WillResume ()
318{
319 return Error();
320}
321
322Error
323ProcessKDP::DoResume ()
324{
325 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000326 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
327 // Only start the async thread if we try to do any process control
328 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
329 StartAsyncThread ();
330
Greg Clayton3acaa922012-09-25 02:40:06 +0000331 bool resume = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000332
Greg Clayton3acaa922012-09-25 02:40:06 +0000333 // With KDP there is only one thread we can tell what to do
334 ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
335 if (kernel_thread_sp)
Greg Claytonea636012012-09-21 01:55:30 +0000336 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000337 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Claytone76f8c42012-09-21 16:31:20 +0000338 switch (thread_resume_state)
Greg Claytonea636012012-09-21 01:55:30 +0000339 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000340 case eStateSuspended:
341 // Nothing to do here when a thread will stay suspended
342 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton3acaa922012-09-25 02:40:06 +0000343 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000344 break;
345
346 case eStateStepping:
Greg Clayton3acaa922012-09-25 02:40:06 +0000347 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread stepping");
348 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true);
349 resume = true;
350 break;
351
Greg Claytone76f8c42012-09-21 16:31:20 +0000352 case eStateRunning:
Greg Clayton3acaa922012-09-25 02:40:06 +0000353 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread running");
354 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false);
355 resume = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000356 break;
Greg Clayton3acaa922012-09-25 02:40:06 +0000357
Greg Claytone76f8c42012-09-21 16:31:20 +0000358 default:
Greg Clayton3acaa922012-09-25 02:40:06 +0000359 // The only valid thread resume states are listed above
Greg Claytone76f8c42012-09-21 16:31:20 +0000360 assert (!"invalid thread resume state");
361 break;
Greg Claytonea636012012-09-21 01:55:30 +0000362 }
363 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000364
365 if (resume)
Greg Claytone76f8c42012-09-21 16:31:20 +0000366 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000367 if (log)
368 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Claytone76f8c42012-09-21 16:31:20 +0000369
Greg Clayton3acaa922012-09-25 02:40:06 +0000370 if (m_comm.SendRequestResume ())
Greg Claytone76f8c42012-09-21 16:31:20 +0000371 {
372 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
373 SetPrivateState(eStateRunning);
374 }
375 else
376 error.SetErrorString ("KDP resume failed");
377 }
Greg Claytonea636012012-09-21 01:55:30 +0000378 else
Greg Claytone76f8c42012-09-21 16:31:20 +0000379 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000380 error.SetErrorString ("kernel thread is suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000381 }
382
Greg Clayton363be3f2011-07-15 03:27:12 +0000383 return error;
384}
385
Greg Clayton3acaa922012-09-25 02:40:06 +0000386lldb::ThreadSP
387ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
388{
389 // KDP only tells us about one thread/core. Any other threads will usually
390 // be the ones that are read from memory by the OS plug-ins.
391 const lldb::tid_t kernel_tid = 1;
392 ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
393 if (!thread_sp)
394 {
395 thread_sp.reset(new ThreadKDP (shared_from_this(), kernel_tid));
396 new_thread_list.AddThread(thread_sp);
397 }
398 return thread_sp;
399}
400
401
402
403
Greg Claytonae932352012-04-10 00:18:59 +0000404bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000405ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000406{
407 // locker will keep a mutex locked until it goes out of scope
408 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
409 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000410 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000411
Greg Clayton3acaa922012-09-25 02:40:06 +0000412 // Even though there is a CPU mask, it doesn't mean to can see each CPU
413 // indivudually, there is really only one. Lets call this thread 1.
414 GetKernelThread (old_thread_list, new_thread_list);
415
Greg Claytonae932352012-04-10 00:18:59 +0000416 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000417}
418
Greg Clayton363be3f2011-07-15 03:27:12 +0000419void
420ProcessKDP::RefreshStateAfterStop ()
421{
422 // Let all threads recover from stopping and do any clean up based
423 // on the previous thread state (if any).
424 m_thread_list.RefreshStateAfterStop();
Greg Clayton363be3f2011-07-15 03:27:12 +0000425}
426
427Error
428ProcessKDP::DoHalt (bool &caused_stop)
429{
430 Error error;
431
Greg Clayton3acaa922012-09-25 02:40:06 +0000432 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000433 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000434 if (m_destroy_in_process)
Greg Clayton363be3f2011-07-15 03:27:12 +0000435 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000436 // If we are attemping to destroy, we need to not return an error to
437 // Halt or DoDestroy won't get called.
438 // We are also currently running, so send a process stopped event
439 SetPrivateState (eStateStopped);
Greg Clayton363be3f2011-07-15 03:27:12 +0000440 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000441 else
Greg Clayton363be3f2011-07-15 03:27:12 +0000442 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000443 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Clayton363be3f2011-07-15 03:27:12 +0000444 }
445 }
446 return error;
447}
448
449Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000450ProcessKDP::DoDetach()
451{
452 Error error;
453 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
454 if (log)
455 log->Printf ("ProcessKDP::DoDetach()");
456
Greg Clayton3acaa922012-09-25 02:40:06 +0000457 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000458 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000459 // We are running and we can't interrupt a running kernel, so we need
460 // to just close the connection to the kernel and hope for the best
461 }
462 else
463 {
464 DisableAllBreakpointSites ();
465
466 m_thread_list.DiscardThreadPlans();
467
468 if (m_comm.IsConnected())
Greg Clayton8d2ea282011-07-17 20:36:25 +0000469 {
Greg Clayton3acaa922012-09-25 02:40:06 +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 Clayton8d2ea282011-07-17 20:36:25 +0000481 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000482 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000483 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000484 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000485
486 SetPrivateState (eStateDetached);
487 ResumePrivateStateThread();
488
489 //KillDebugserverProcess ();
490 return error;
491}
492
493Error
Greg Clayton3acaa922012-09-25 02:40:06 +0000494ProcessKDP::WillDestroy ()
495{
496 Error error;
497 m_destroy_in_process = true;
498 return error;
499}
500
501Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000502ProcessKDP::DoDestroy ()
503{
Greg Claytone76f8c42012-09-21 16:31:20 +0000504 // For KDP there really is no difference between destroy and detach
505 return DoDetach();
Greg Clayton363be3f2011-07-15 03:27:12 +0000506}
507
508//------------------------------------------------------------------
509// Process Queries
510//------------------------------------------------------------------
511
512bool
513ProcessKDP::IsAlive ()
514{
515 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
516}
517
518//------------------------------------------------------------------
519// Process Memory
520//------------------------------------------------------------------
521size_t
522ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
523{
Greg Clayton0fa51242011-07-19 03:57:15 +0000524 if (m_comm.IsConnected())
525 return m_comm.SendRequestReadMemory (addr, buf, size, error);
526 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000527 return 0;
528}
529
530size_t
531ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
532{
Greg Claytone76f8c42012-09-21 16:31:20 +0000533 if (m_comm.IsConnected())
534 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
535 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000536 return 0;
537}
538
539lldb::addr_t
540ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
541{
542 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
543 return LLDB_INVALID_ADDRESS;
544}
545
546Error
547ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
548{
549 Error error;
550 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
551 return error;
552}
553
554Error
555ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
556{
Greg Clayton234981a2011-07-20 03:41:06 +0000557 if (m_comm.LocalBreakpointsAreSupported ())
558 {
559 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000560 if (!bp_site->IsEnabled())
561 {
562 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
563 {
564 bp_site->SetEnabled(true);
565 bp_site->SetType (BreakpointSite::eExternal);
566 }
567 else
568 {
569 error.SetErrorString ("KDP set breakpoint failed");
570 }
571 }
Greg Clayton234981a2011-07-20 03:41:06 +0000572 return error;
573 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000574 return EnableSoftwareBreakpoint (bp_site);
575}
576
577Error
578ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
579{
Greg Clayton234981a2011-07-20 03:41:06 +0000580 if (m_comm.LocalBreakpointsAreSupported ())
581 {
582 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000583 if (bp_site->IsEnabled())
584 {
585 BreakpointSite::Type bp_type = bp_site->GetType();
586 if (bp_type == BreakpointSite::eExternal)
587 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000588 if (m_destroy_in_process && m_comm.IsRunning())
589 {
590 // We are trying to destroy our connection and we are running
Greg Clayton7b139222011-07-21 01:12:01 +0000591 bp_site->SetEnabled(false);
Greg Clayton3acaa922012-09-25 02:40:06 +0000592 }
Greg Clayton7b139222011-07-21 01:12:01 +0000593 else
Greg Clayton3acaa922012-09-25 02:40:06 +0000594 {
595 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
596 bp_site->SetEnabled(false);
597 else
598 error.SetErrorString ("KDP remove breakpoint failed");
599 }
Greg Clayton7b139222011-07-21 01:12:01 +0000600 }
601 else
602 {
603 error = DisableSoftwareBreakpoint (bp_site);
604 }
605 }
Greg Clayton234981a2011-07-20 03:41:06 +0000606 return error;
607 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000608 return DisableSoftwareBreakpoint (bp_site);
609}
610
611Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000612ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000613{
614 Error error;
615 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
616 return error;
617}
618
619Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000620ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000621{
622 Error error;
623 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
624 return error;
625}
626
627void
628ProcessKDP::Clear()
629{
Greg Clayton363be3f2011-07-15 03:27:12 +0000630 m_thread_list.Clear();
631}
632
633Error
634ProcessKDP::DoSignal (int signo)
635{
636 Error error;
637 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
638 return error;
639}
640
641void
642ProcessKDP::Initialize()
643{
644 static bool g_initialized = false;
645
646 if (g_initialized == false)
647 {
648 g_initialized = true;
649 PluginManager::RegisterPlugin (GetPluginNameStatic(),
650 GetPluginDescriptionStatic(),
651 CreateInstance);
652
653 Log::Callbacks log_callbacks = {
654 ProcessKDPLog::DisableLog,
655 ProcessKDPLog::EnableLog,
656 ProcessKDPLog::ListLogCategories
657 };
658
659 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
660 }
661}
662
663bool
664ProcessKDP::StartAsyncThread ()
665{
666 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
667
668 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000669 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000670
Greg Claytone76f8c42012-09-21 16:31:20 +0000671 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
672 return true;
673
Greg Clayton363be3f2011-07-15 03:27:12 +0000674 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
675 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
676}
677
678void
679ProcessKDP::StopAsyncThread ()
680{
681 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
682
683 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000684 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000685
686 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
687
688 // Stop the stdio thread
689 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
690 {
691 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Claytone76f8c42012-09-21 16:31:20 +0000692 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton363be3f2011-07-15 03:27:12 +0000693 }
694}
695
696
697void *
698ProcessKDP::AsyncThread (void *arg)
699{
700 ProcessKDP *process = (ProcessKDP*) arg;
701
Greg Claytone76f8c42012-09-21 16:31:20 +0000702 const lldb::pid_t pid = process->GetID();
703
Greg Clayton363be3f2011-07-15 03:27:12 +0000704 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
705 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000706 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000707
708 Listener listener ("ProcessKDP::AsyncThread");
709 EventSP event_sp;
710 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
711 eBroadcastBitAsyncThreadShouldExit;
712
Greg Claytone76f8c42012-09-21 16:31:20 +0000713
Greg Clayton363be3f2011-07-15 03:27:12 +0000714 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
715 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000716 bool done = false;
717 while (!done)
718 {
719 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000720 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
721 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000722 if (listener.WaitForEvent (NULL, event_sp))
723 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000724 uint32_t event_type = event_sp->GetType();
725 if (log)
726 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
727 pid,
728 event_type);
729
730 // When we are running, poll for 1 second to try and get an exception
731 // to indicate the process has stopped. If we don't get one, check to
732 // make sure no one asked us to exit
733 bool is_running = false;
734 DataExtractor exc_reply_packet;
735 do
Greg Clayton363be3f2011-07-15 03:27:12 +0000736 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000737 switch (event_type)
738 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000739 case eBroadcastBitAsyncContinue:
Greg Clayton363be3f2011-07-15 03:27:12 +0000740 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000741 is_running = true;
742 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Clayton363be3f2011-07-15 03:27:12 +0000743 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000744 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
745 thread_sp->GetRegisterContext()->InvalidateAllRegisters();
746 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
747
Greg Claytone76f8c42012-09-21 16:31:20 +0000748 // TODO: parse the stop reply packet
Greg Clayton3acaa922012-09-25 02:40:06 +0000749 is_running = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000750 process->SetPrivateState(eStateStopped);
751 }
752 else
753 {
754 // Check to see if we are supposed to exit. There is no way to
755 // interrupt a running kernel, so all we can do is wait for an
756 // exception or detach...
757 if (listener.GetNextEvent(event_sp))
758 {
759 // We got an event, go through the loop again
760 event_type = event_sp->GetType();
761 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000762 }
763 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000764 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000765
Greg Claytone76f8c42012-09-21 16:31:20 +0000766 case eBroadcastBitAsyncThreadShouldExit:
767 if (log)
768 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
769 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000770 done = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000771 is_running = false;
772 break;
773
774 default:
775 if (log)
776 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
777 pid,
778 event_type);
779 done = true;
780 is_running = false;
781 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000782 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000783 } while (is_running);
Greg Clayton363be3f2011-07-15 03:27:12 +0000784 }
785 else
786 {
787 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000788 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
789 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000790 done = true;
791 }
792 }
793 }
794
795 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000796 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
797 arg,
798 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000799
800 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
801 return NULL;
802}
803
804