blob: 8f7021a64e0d940094d56b176718f514322221cc [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"
Jason Molendafac2e622012-09-29 04:02:01 +000020#include "lldb/Core/ModuleSpec.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000021#include "lldb/Core/State.h"
Jason Molendafac2e622012-09-29 04:02:01 +000022#include "lldb/Core/UUID.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000023#include "lldb/Host/Host.h"
Jason Molendafac2e622012-09-29 04:02:01 +000024#include "lldb/Host/Symbols.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000025#include "lldb/Symbol/ObjectFile.h"
Greg Claytone76f8c42012-09-21 16:31:20 +000026#include "lldb/Target/RegisterContext.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000027#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000028#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000029
30// Project includes
31#include "ProcessKDP.h"
32#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000033#include "ThreadKDP.h"
Jason Molendab46937c2012-10-03 01:29:34 +000034#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39const char *
40ProcessKDP::GetPluginNameStatic()
41{
42 return "kdp-remote";
43}
44
45const char *
46ProcessKDP::GetPluginDescriptionStatic()
47{
48 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
49}
50
51void
52ProcessKDP::Terminate()
53{
54 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
55}
56
57
Greg Clayton46c9a352012-02-09 06:16:32 +000058lldb::ProcessSP
59ProcessKDP::CreateInstance (Target &target,
60 Listener &listener,
61 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000062{
Greg Clayton46c9a352012-02-09 06:16:32 +000063 lldb::ProcessSP process_sp;
64 if (crash_file_path == NULL)
65 process_sp.reset(new ProcessKDP (target, listener));
66 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000067}
68
69bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000070ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000071{
Greg Clayton61ddf562011-10-21 21:41:45 +000072 if (plugin_specified_by_name)
73 return true;
74
Greg Clayton363be3f2011-07-15 03:27:12 +000075 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000076 Module *exe_module = target.GetExecutableModulePointer();
77 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000078 {
79 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Claytonb170aee2012-05-08 01:45:38 +000080 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000081 {
Greg Claytonb170aee2012-05-08 01:45:38 +000082 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
83 case llvm::Triple::MacOSX: // For desktop targets
84 case llvm::Triple::IOS: // For arm targets
85 if (triple_ref.getVendor() == llvm::Triple::Apple)
86 {
87 ObjectFile *exe_objfile = exe_module->GetObjectFile();
88 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
89 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
90 return true;
91 }
92 break;
93
94 default:
95 break;
Greg Clayton363be3f2011-07-15 03:27:12 +000096 }
97 }
Greg Clayton61ddf562011-10-21 21:41:45 +000098 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000099}
100
101//----------------------------------------------------------------------
102// ProcessKDP constructor
103//----------------------------------------------------------------------
104ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
105 Process (target, listener),
106 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000107 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton3acaa922012-09-25 02:40:06 +0000108 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molendab46937c2012-10-03 01:29:34 +0000109 m_destroy_in_process (false),
110 m_dyld_plugin_name (),
111 m_kernel_load_addr (LLDB_INVALID_ADDRESS)
Greg Clayton363be3f2011-07-15 03:27:12 +0000112{
Greg Claytone76f8c42012-09-21 16:31:20 +0000113 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
114 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton363be3f2011-07-15 03:27:12 +0000115}
116
117//----------------------------------------------------------------------
118// Destructor
119//----------------------------------------------------------------------
120ProcessKDP::~ProcessKDP()
121{
122 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000123 // We need to call finalize on the process before destroying ourselves
124 // to make sure all of the broadcaster cleanup goes as planned. If we
125 // destruct this class, then Process::~Process() might have problems
126 // trying to fully destroy the broadcaster.
127 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000128}
129
130//----------------------------------------------------------------------
131// PluginInterface
132//----------------------------------------------------------------------
133const char *
134ProcessKDP::GetPluginName()
135{
136 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
137}
138
139const char *
140ProcessKDP::GetShortPluginName()
141{
142 return GetPluginNameStatic();
143}
144
145uint32_t
146ProcessKDP::GetPluginVersion()
147{
148 return 1;
149}
150
151Error
152ProcessKDP::WillLaunch (Module* module)
153{
154 Error error;
155 error.SetErrorString ("launching not supported in kdp-remote plug-in");
156 return error;
157}
158
159Error
160ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
161{
162 Error error;
163 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
164 return error;
165}
166
167Error
168ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
169{
170 Error error;
171 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
172 return error;
173}
174
175Error
Jason Molendafac2e622012-09-29 04:02:01 +0000176ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Clayton363be3f2011-07-15 03:27:12 +0000177{
Greg Clayton363be3f2011-07-15 03:27:12 +0000178 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000179
180 // Don't let any JIT happen when doing KDP as we can't allocate
181 // memory and we don't want to be mucking with threads that might
182 // already be handling exceptions
183 SetCanJIT(false);
184
Greg Clayton8d2ea282011-07-17 20:36:25 +0000185 if (remote_url == NULL || remote_url[0] == '\0')
Greg Claytone76f8c42012-09-21 16:31:20 +0000186 {
187 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
188 return error;
189 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000190
191 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
192 if (conn_ap.get())
193 {
194 // Only try once for now.
195 // TODO: check if we should be retrying?
196 const uint32_t max_retry_count = 1;
197 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
198 {
199 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
200 break;
201 usleep (100000);
202 }
203 }
204
205 if (conn_ap->IsConnected())
206 {
207 const uint16_t reply_port = conn_ap->GetReadPort ();
208
209 if (reply_port != 0)
210 {
211 m_comm.SetConnection(conn_ap.release());
212
213 if (m_comm.SendRequestReattach(reply_port))
214 {
215 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
216 {
217 m_comm.GetVersion();
218 uint32_t cpu = m_comm.GetCPUType();
219 uint32_t sub = m_comm.GetCPUSubtype();
220 ArchSpec kernel_arch;
221 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
222 m_target.SetArchitecture(kernel_arch);
Jason Molendafac2e622012-09-29 04:02:01 +0000223
224 /* Get the kernel's UUID and load address via kdp-kernelversion packet. */
225
226 UUID kernel_uuid = m_comm.GetUUID ();
227 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molendafac2e622012-09-29 04:02:01 +0000228
Jason Molendab46937c2012-10-03 01:29:34 +0000229 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
Jason Molendafac2e622012-09-29 04:02:01 +0000230 {
Jason Molendab46937c2012-10-03 01:29:34 +0000231 m_kernel_load_addr = kernel_load_addr;
232 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molendafac2e622012-09-29 04:02:01 +0000233 }
234
Greg Clayton3acaa922012-09-25 02:40:06 +0000235 // Set the thread ID
236 UpdateThreadListIfNeeded ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000237 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000238 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000239 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000240 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
241 if (async_strm_sp)
242 {
Greg Clayton7b139222011-07-21 01:12:01 +0000243 const char *cstr;
244 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000245 {
Greg Clayton7b139222011-07-21 01:12:01 +0000246 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000247 async_strm_sp->Flush();
248 }
Greg Clayton7b139222011-07-21 01:12:01 +0000249// if ((cstr = m_comm.GetImagePath ()) != NULL)
250// {
251// async_strm_sp->Printf ("Image Path: %s\n", cstr);
252// async_strm_sp->Flush();
253// }
Greg Clayton234981a2011-07-20 03:41:06 +0000254 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000255 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000256 else
257 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000258 error.SetErrorString("KDP_REATTACH failed");
259 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000260 }
261 else
262 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000263 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton8d2ea282011-07-17 20:36:25 +0000264 }
265 }
266 else
267 {
268 error.SetErrorString("invalid reply port from UDP connection");
269 }
270 }
271 else
272 {
273 if (error.Success())
274 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
275 }
276 if (error.Fail())
277 m_comm.Disconnect();
278
Greg Clayton363be3f2011-07-15 03:27:12 +0000279 return error;
280}
281
282//----------------------------------------------------------------------
283// Process Control
284//----------------------------------------------------------------------
285Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000286ProcessKDP::DoLaunch (Module *exe_module,
287 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000288{
289 Error error;
290 error.SetErrorString ("launching not supported in kdp-remote plug-in");
291 return error;
292}
293
294
295Error
296ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
297{
298 Error error;
299 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
300 return error;
301}
302
Greg Clayton363be3f2011-07-15 03:27:12 +0000303Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000304ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
305{
306 Error error;
307 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
308 return error;
309}
310
311Error
312ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000313{
314 Error error;
315 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
316 return error;
317}
318
319
320void
321ProcessKDP::DidAttach ()
322{
323 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
324 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000325 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000326 if (GetID() != LLDB_INVALID_PROCESS_ID)
327 {
328 // TODO: figure out the register context that we will use
329 }
330}
331
Jason Molendab46937c2012-10-03 01:29:34 +0000332addr_t
333ProcessKDP::GetImageInfoAddress()
334{
335 return m_kernel_load_addr;
336}
337
338lldb_private::DynamicLoader *
339ProcessKDP::GetDynamicLoader ()
340{
341 if (m_dyld_ap.get() == NULL)
342 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
343 return m_dyld_ap.get();
344}
345
Greg Clayton363be3f2011-07-15 03:27:12 +0000346Error
347ProcessKDP::WillResume ()
348{
349 return Error();
350}
351
352Error
353ProcessKDP::DoResume ()
354{
355 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000356 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
357 // Only start the async thread if we try to do any process control
358 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
359 StartAsyncThread ();
360
Greg Clayton3acaa922012-09-25 02:40:06 +0000361 bool resume = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000362
Greg Clayton3acaa922012-09-25 02:40:06 +0000363 // With KDP there is only one thread we can tell what to do
364 ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
365 if (kernel_thread_sp)
Greg Claytonea636012012-09-21 01:55:30 +0000366 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000367 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Claytone76f8c42012-09-21 16:31:20 +0000368 switch (thread_resume_state)
Greg Claytonea636012012-09-21 01:55:30 +0000369 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000370 case eStateSuspended:
371 // Nothing to do here when a thread will stay suspended
372 // we just leave the CPU mask bit set to zero for the thread
373 break;
374
375 case eStateStepping:
Greg Clayton3acaa922012-09-25 02:40:06 +0000376 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true);
377 resume = true;
378 break;
379
Greg Claytone76f8c42012-09-21 16:31:20 +0000380 case eStateRunning:
Greg Clayton3acaa922012-09-25 02:40:06 +0000381 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false);
382 resume = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000383 break;
Greg Clayton3acaa922012-09-25 02:40:06 +0000384
Greg Claytone76f8c42012-09-21 16:31:20 +0000385 default:
Greg Clayton3acaa922012-09-25 02:40:06 +0000386 // The only valid thread resume states are listed above
Greg Claytone76f8c42012-09-21 16:31:20 +0000387 assert (!"invalid thread resume state");
388 break;
Greg Claytonea636012012-09-21 01:55:30 +0000389 }
390 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000391
392 if (resume)
Greg Claytone76f8c42012-09-21 16:31:20 +0000393 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000394 if (log)
395 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Claytone76f8c42012-09-21 16:31:20 +0000396
Greg Clayton3acaa922012-09-25 02:40:06 +0000397 if (m_comm.SendRequestResume ())
Greg Claytone76f8c42012-09-21 16:31:20 +0000398 {
399 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
400 SetPrivateState(eStateRunning);
401 }
402 else
403 error.SetErrorString ("KDP resume failed");
404 }
Greg Claytonea636012012-09-21 01:55:30 +0000405 else
Greg Claytone76f8c42012-09-21 16:31:20 +0000406 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000407 error.SetErrorString ("kernel thread is suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000408 }
409
Greg Clayton363be3f2011-07-15 03:27:12 +0000410 return error;
411}
412
Greg Clayton3acaa922012-09-25 02:40:06 +0000413lldb::ThreadSP
414ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
415{
416 // KDP only tells us about one thread/core. Any other threads will usually
417 // be the ones that are read from memory by the OS plug-ins.
418 const lldb::tid_t kernel_tid = 1;
419 ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
420 if (!thread_sp)
421 {
422 thread_sp.reset(new ThreadKDP (shared_from_this(), kernel_tid));
423 new_thread_list.AddThread(thread_sp);
424 }
425 return thread_sp;
426}
427
428
429
430
Greg Claytonae932352012-04-10 00:18:59 +0000431bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000432ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000433{
434 // locker will keep a mutex locked until it goes out of scope
435 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
436 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000437 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000438
Greg Clayton3acaa922012-09-25 02:40:06 +0000439 // Even though there is a CPU mask, it doesn't mean to can see each CPU
440 // indivudually, there is really only one. Lets call this thread 1.
441 GetKernelThread (old_thread_list, new_thread_list);
442
Greg Claytonae932352012-04-10 00:18:59 +0000443 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000444}
445
Greg Clayton363be3f2011-07-15 03:27:12 +0000446void
447ProcessKDP::RefreshStateAfterStop ()
448{
449 // Let all threads recover from stopping and do any clean up based
450 // on the previous thread state (if any).
451 m_thread_list.RefreshStateAfterStop();
Greg Clayton363be3f2011-07-15 03:27:12 +0000452}
453
454Error
455ProcessKDP::DoHalt (bool &caused_stop)
456{
457 Error error;
458
Greg Clayton3acaa922012-09-25 02:40:06 +0000459 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000460 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000461 if (m_destroy_in_process)
Greg Clayton363be3f2011-07-15 03:27:12 +0000462 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000463 // If we are attemping to destroy, we need to not return an error to
464 // Halt or DoDestroy won't get called.
465 // We are also currently running, so send a process stopped event
466 SetPrivateState (eStateStopped);
Greg Clayton363be3f2011-07-15 03:27:12 +0000467 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000468 else
Greg Clayton363be3f2011-07-15 03:27:12 +0000469 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000470 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Clayton363be3f2011-07-15 03:27:12 +0000471 }
472 }
473 return error;
474}
475
476Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000477ProcessKDP::DoDetach()
478{
479 Error error;
480 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
481 if (log)
482 log->Printf ("ProcessKDP::DoDetach()");
483
Greg Clayton3acaa922012-09-25 02:40:06 +0000484 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000485 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000486 // We are running and we can't interrupt a running kernel, so we need
487 // to just close the connection to the kernel and hope for the best
488 }
489 else
490 {
491 DisableAllBreakpointSites ();
492
493 m_thread_list.DiscardThreadPlans();
494
495 if (m_comm.IsConnected())
Greg Clayton8d2ea282011-07-17 20:36:25 +0000496 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000497
498 m_comm.SendRequestDisconnect();
499
500 size_t response_size = m_comm.Disconnect ();
501 if (log)
502 {
503 if (response_size)
504 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
505 else
506 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
507 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000508 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000509 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000510 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000511 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000512
513 SetPrivateState (eStateDetached);
514 ResumePrivateStateThread();
515
516 //KillDebugserverProcess ();
517 return error;
518}
519
520Error
Greg Clayton3acaa922012-09-25 02:40:06 +0000521ProcessKDP::WillDestroy ()
522{
523 Error error;
524 m_destroy_in_process = true;
525 return error;
526}
527
528Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000529ProcessKDP::DoDestroy ()
530{
Greg Claytone76f8c42012-09-21 16:31:20 +0000531 // For KDP there really is no difference between destroy and detach
532 return DoDetach();
Greg Clayton363be3f2011-07-15 03:27:12 +0000533}
534
535//------------------------------------------------------------------
536// Process Queries
537//------------------------------------------------------------------
538
539bool
540ProcessKDP::IsAlive ()
541{
542 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
543}
544
545//------------------------------------------------------------------
546// Process Memory
547//------------------------------------------------------------------
548size_t
549ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
550{
Greg Clayton0fa51242011-07-19 03:57:15 +0000551 if (m_comm.IsConnected())
552 return m_comm.SendRequestReadMemory (addr, buf, size, error);
553 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000554 return 0;
555}
556
557size_t
558ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
559{
Greg Claytone76f8c42012-09-21 16:31:20 +0000560 if (m_comm.IsConnected())
561 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
562 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000563 return 0;
564}
565
566lldb::addr_t
567ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
568{
569 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
570 return LLDB_INVALID_ADDRESS;
571}
572
573Error
574ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
575{
576 Error error;
577 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
578 return error;
579}
580
581Error
582ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
583{
Greg Clayton234981a2011-07-20 03:41:06 +0000584 if (m_comm.LocalBreakpointsAreSupported ())
585 {
586 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000587 if (!bp_site->IsEnabled())
588 {
589 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
590 {
591 bp_site->SetEnabled(true);
592 bp_site->SetType (BreakpointSite::eExternal);
593 }
594 else
595 {
596 error.SetErrorString ("KDP set breakpoint failed");
597 }
598 }
Greg Clayton234981a2011-07-20 03:41:06 +0000599 return error;
600 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000601 return EnableSoftwareBreakpoint (bp_site);
602}
603
604Error
605ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
606{
Greg Clayton234981a2011-07-20 03:41:06 +0000607 if (m_comm.LocalBreakpointsAreSupported ())
608 {
609 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000610 if (bp_site->IsEnabled())
611 {
612 BreakpointSite::Type bp_type = bp_site->GetType();
613 if (bp_type == BreakpointSite::eExternal)
614 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000615 if (m_destroy_in_process && m_comm.IsRunning())
616 {
617 // We are trying to destroy our connection and we are running
Greg Clayton7b139222011-07-21 01:12:01 +0000618 bp_site->SetEnabled(false);
Greg Clayton3acaa922012-09-25 02:40:06 +0000619 }
Greg Clayton7b139222011-07-21 01:12:01 +0000620 else
Greg Clayton3acaa922012-09-25 02:40:06 +0000621 {
622 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
623 bp_site->SetEnabled(false);
624 else
625 error.SetErrorString ("KDP remove breakpoint failed");
626 }
Greg Clayton7b139222011-07-21 01:12:01 +0000627 }
628 else
629 {
630 error = DisableSoftwareBreakpoint (bp_site);
631 }
632 }
Greg Clayton234981a2011-07-20 03:41:06 +0000633 return error;
634 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000635 return DisableSoftwareBreakpoint (bp_site);
636}
637
638Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000639ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000640{
641 Error error;
642 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
643 return error;
644}
645
646Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000647ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000648{
649 Error error;
650 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
651 return error;
652}
653
654void
655ProcessKDP::Clear()
656{
Greg Clayton363be3f2011-07-15 03:27:12 +0000657 m_thread_list.Clear();
658}
659
660Error
661ProcessKDP::DoSignal (int signo)
662{
663 Error error;
664 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
665 return error;
666}
667
668void
669ProcessKDP::Initialize()
670{
671 static bool g_initialized = false;
672
673 if (g_initialized == false)
674 {
675 g_initialized = true;
676 PluginManager::RegisterPlugin (GetPluginNameStatic(),
677 GetPluginDescriptionStatic(),
678 CreateInstance);
679
680 Log::Callbacks log_callbacks = {
681 ProcessKDPLog::DisableLog,
682 ProcessKDPLog::EnableLog,
683 ProcessKDPLog::ListLogCategories
684 };
685
686 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
687 }
688}
689
690bool
691ProcessKDP::StartAsyncThread ()
692{
693 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
694
695 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000696 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000697
Greg Claytone76f8c42012-09-21 16:31:20 +0000698 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
699 return true;
700
Greg Clayton363be3f2011-07-15 03:27:12 +0000701 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
702 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
703}
704
705void
706ProcessKDP::StopAsyncThread ()
707{
708 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
709
710 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000711 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000712
713 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
714
715 // Stop the stdio thread
716 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
717 {
718 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Claytone76f8c42012-09-21 16:31:20 +0000719 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton363be3f2011-07-15 03:27:12 +0000720 }
721}
722
723
724void *
725ProcessKDP::AsyncThread (void *arg)
726{
727 ProcessKDP *process = (ProcessKDP*) arg;
728
Greg Claytone76f8c42012-09-21 16:31:20 +0000729 const lldb::pid_t pid = process->GetID();
730
Greg Clayton363be3f2011-07-15 03:27:12 +0000731 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
732 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000733 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000734
735 Listener listener ("ProcessKDP::AsyncThread");
736 EventSP event_sp;
737 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
738 eBroadcastBitAsyncThreadShouldExit;
739
Greg Claytone76f8c42012-09-21 16:31:20 +0000740
Greg Clayton363be3f2011-07-15 03:27:12 +0000741 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
742 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000743 bool done = false;
744 while (!done)
745 {
746 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000747 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
748 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000749 if (listener.WaitForEvent (NULL, event_sp))
750 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000751 uint32_t event_type = event_sp->GetType();
752 if (log)
753 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
754 pid,
755 event_type);
756
757 // When we are running, poll for 1 second to try and get an exception
758 // to indicate the process has stopped. If we don't get one, check to
759 // make sure no one asked us to exit
760 bool is_running = false;
761 DataExtractor exc_reply_packet;
762 do
Greg Clayton363be3f2011-07-15 03:27:12 +0000763 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000764 switch (event_type)
765 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000766 case eBroadcastBitAsyncContinue:
Greg Clayton363be3f2011-07-15 03:27:12 +0000767 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000768 is_running = true;
769 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Clayton363be3f2011-07-15 03:27:12 +0000770 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000771 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
772 thread_sp->GetRegisterContext()->InvalidateAllRegisters();
773 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
774
Greg Claytone76f8c42012-09-21 16:31:20 +0000775 // TODO: parse the stop reply packet
Greg Clayton3acaa922012-09-25 02:40:06 +0000776 is_running = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000777 process->SetPrivateState(eStateStopped);
778 }
779 else
780 {
781 // Check to see if we are supposed to exit. There is no way to
782 // interrupt a running kernel, so all we can do is wait for an
783 // exception or detach...
784 if (listener.GetNextEvent(event_sp))
785 {
786 // We got an event, go through the loop again
787 event_type = event_sp->GetType();
788 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000789 }
790 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000791 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000792
Greg Claytone76f8c42012-09-21 16:31:20 +0000793 case eBroadcastBitAsyncThreadShouldExit:
794 if (log)
795 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
796 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000797 done = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000798 is_running = false;
799 break;
800
801 default:
802 if (log)
803 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
804 pid,
805 event_type);
806 done = true;
807 is_running = false;
808 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000809 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000810 } while (is_running);
Greg Clayton363be3f2011-07-15 03:27:12 +0000811 }
812 else
813 {
814 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000815 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
816 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000817 done = true;
818 }
819 }
820 }
821
822 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000823 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
824 arg,
825 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000826
827 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
828 return NULL;
829}
830
831