blob: 4e856897b901937c774f28205833fbefd8e9b707 [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"
Greg Clayton363be3f2011-07-15 03:27:12 +000034
35using namespace lldb;
36using namespace lldb_private;
37
38const char *
39ProcessKDP::GetPluginNameStatic()
40{
41 return "kdp-remote";
42}
43
44const char *
45ProcessKDP::GetPluginDescriptionStatic()
46{
47 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
48}
49
50void
51ProcessKDP::Terminate()
52{
53 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
54}
55
56
Greg Clayton46c9a352012-02-09 06:16:32 +000057lldb::ProcessSP
58ProcessKDP::CreateInstance (Target &target,
59 Listener &listener,
60 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000061{
Greg Clayton46c9a352012-02-09 06:16:32 +000062 lldb::ProcessSP process_sp;
63 if (crash_file_path == NULL)
64 process_sp.reset(new ProcessKDP (target, listener));
65 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000066}
67
68bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000069ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000070{
Greg Clayton61ddf562011-10-21 21:41:45 +000071 if (plugin_specified_by_name)
72 return true;
73
Greg Clayton363be3f2011-07-15 03:27:12 +000074 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000075 Module *exe_module = target.GetExecutableModulePointer();
76 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000077 {
78 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Claytonb170aee2012-05-08 01:45:38 +000079 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000080 {
Greg Claytonb170aee2012-05-08 01:45:38 +000081 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
82 case llvm::Triple::MacOSX: // For desktop targets
83 case llvm::Triple::IOS: // For arm targets
84 if (triple_ref.getVendor() == llvm::Triple::Apple)
85 {
86 ObjectFile *exe_objfile = exe_module->GetObjectFile();
87 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
88 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
89 return true;
90 }
91 break;
92
93 default:
94 break;
Greg Clayton363be3f2011-07-15 03:27:12 +000095 }
96 }
Greg Clayton61ddf562011-10-21 21:41:45 +000097 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +000098}
99
100//----------------------------------------------------------------------
101// ProcessKDP constructor
102//----------------------------------------------------------------------
103ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
104 Process (target, listener),
105 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000106 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton3acaa922012-09-25 02:40:06 +0000107 m_async_thread (LLDB_INVALID_HOST_THREAD),
108 m_destroy_in_process (false)
Greg Clayton363be3f2011-07-15 03:27:12 +0000109{
Greg Claytone76f8c42012-09-21 16:31:20 +0000110 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
111 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton363be3f2011-07-15 03:27:12 +0000112}
113
114//----------------------------------------------------------------------
115// Destructor
116//----------------------------------------------------------------------
117ProcessKDP::~ProcessKDP()
118{
119 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000120 // We need to call finalize on the process before destroying ourselves
121 // to make sure all of the broadcaster cleanup goes as planned. If we
122 // destruct this class, then Process::~Process() might have problems
123 // trying to fully destroy the broadcaster.
124 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000125}
126
127//----------------------------------------------------------------------
128// PluginInterface
129//----------------------------------------------------------------------
130const char *
131ProcessKDP::GetPluginName()
132{
133 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
134}
135
136const char *
137ProcessKDP::GetShortPluginName()
138{
139 return GetPluginNameStatic();
140}
141
142uint32_t
143ProcessKDP::GetPluginVersion()
144{
145 return 1;
146}
147
148Error
149ProcessKDP::WillLaunch (Module* module)
150{
151 Error error;
152 error.SetErrorString ("launching not supported in kdp-remote plug-in");
153 return error;
154}
155
156Error
157ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
158{
159 Error error;
160 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
161 return error;
162}
163
164Error
165ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
166{
167 Error error;
168 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
169 return error;
170}
171
172Error
Jason Molendafac2e622012-09-29 04:02:01 +0000173ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Clayton363be3f2011-07-15 03:27:12 +0000174{
Greg Clayton363be3f2011-07-15 03:27:12 +0000175 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000176
177 // Don't let any JIT happen when doing KDP as we can't allocate
178 // memory and we don't want to be mucking with threads that might
179 // already be handling exceptions
180 SetCanJIT(false);
181
Greg Clayton8d2ea282011-07-17 20:36:25 +0000182 if (remote_url == NULL || remote_url[0] == '\0')
Greg Claytone76f8c42012-09-21 16:31:20 +0000183 {
184 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
185 return error;
186 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000187
188 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
189 if (conn_ap.get())
190 {
191 // Only try once for now.
192 // TODO: check if we should be retrying?
193 const uint32_t max_retry_count = 1;
194 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
195 {
196 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
197 break;
198 usleep (100000);
199 }
200 }
201
202 if (conn_ap->IsConnected())
203 {
204 const uint16_t reply_port = conn_ap->GetReadPort ();
205
206 if (reply_port != 0)
207 {
208 m_comm.SetConnection(conn_ap.release());
209
210 if (m_comm.SendRequestReattach(reply_port))
211 {
212 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
213 {
214 m_comm.GetVersion();
215 uint32_t cpu = m_comm.GetCPUType();
216 uint32_t sub = m_comm.GetCPUSubtype();
217 ArchSpec kernel_arch;
218 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
219 m_target.SetArchitecture(kernel_arch);
Jason Molendafac2e622012-09-29 04:02:01 +0000220
221 /* Get the kernel's UUID and load address via kdp-kernelversion packet. */
222
223 UUID kernel_uuid = m_comm.GetUUID ();
224 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
225 if (strm)
226 {
227 char uuidbuf[64];
228 strm->Printf ("Kernel UUID: %s\n", kernel_uuid.GetAsCString (uuidbuf, sizeof (uuidbuf)));
229 strm->Printf ("Load Address: 0x%llx\n", kernel_load_addr);
230 strm->Flush ();
231 }
232
233 /* Set the kernel's LoadAddress based on the information from kdp.
234 This would normally be handled by the DynamicLoaderDarwinKernel plugin but there's no easy
235 way to communicate the UUID / load addr from kdp back up to that plugin so we'll set it here. */
236 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
237 bool find_and_load_kernel = true;
238 if (exe_module_sp.get ())
239 {
240 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
241 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
242 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
243 {
244 UUID exe_objfile_uuid;
245 if (exe_objfile->GetUUID (&exe_objfile_uuid) && kernel_uuid == exe_objfile_uuid
246 && exe_objfile->GetHeaderAddress().IsValid())
247 {
248 find_and_load_kernel = false;
249 addr_t slide = kernel_load_addr - exe_objfile->GetHeaderAddress().GetFileAddress();
250 if (slide != 0)
251 {
252 bool changed = false;
253 exe_module_sp->SetLoadAddress (m_target, slide, changed);
254 if (changed)
255 {
256 ModuleList modlist;
257 modlist.Append (exe_module_sp);
258 m_target.ModulesDidLoad (modlist);
259 }
260 }
261 }
262 }
263 }
264
265 // If the executable binary is not the same as the kernel being run on the remote host,
266 // see if Symbols::DownloadObjectAndSymbolFile can find us a symbol file based on the UUID
267 // and if so, load it at the correct address.
268 if (find_and_load_kernel && kernel_load_addr != LLDB_INVALID_ADDRESS && kernel_uuid.IsValid())
269 {
270 ModuleSpec sym_spec;
271 sym_spec.GetUUID() = kernel_uuid;
272 if (Symbols::DownloadObjectAndSymbolFile (sym_spec)
273 && sym_spec.GetArchitecture().IsValid()
274 && sym_spec.GetSymbolFileSpec().Exists())
275 {
276 ModuleSP kernel_sp = m_target.GetSharedModule (sym_spec);
277 if (kernel_sp.get())
278 {
279 m_target.SetExecutableModule(kernel_sp, false);
280 if (kernel_sp->GetObjectFile() && kernel_sp->GetObjectFile()->GetHeaderAddress().IsValid())
281 {
282 addr_t slide = kernel_load_addr - kernel_sp->GetObjectFile()->GetHeaderAddress().GetFileAddress();
283 bool changed = false;
284 kernel_sp->SetLoadAddress (m_target, slide, changed);
285 if (changed)
286 {
287 ModuleList modlist;
288 modlist.Append (kernel_sp);
289 m_target.ModulesDidLoad (modlist);
290 }
291 if (strm)
292 {
293 strm->Printf ("Loaded kernel file %s/%s\n",
294 kernel_sp->GetFileSpec().GetDirectory().AsCString(),
295 kernel_sp->GetFileSpec().GetFilename().AsCString());
296 strm->Flush ();
297 }
298 }
299 }
300 }
301 }
302
303
Greg Clayton3acaa922012-09-25 02:40:06 +0000304 // Set the thread ID
305 UpdateThreadListIfNeeded ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000306 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000307 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000308 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000309 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
310 if (async_strm_sp)
311 {
Greg Clayton7b139222011-07-21 01:12:01 +0000312 const char *cstr;
313 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000314 {
Greg Clayton7b139222011-07-21 01:12:01 +0000315 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000316 async_strm_sp->Flush();
317 }
Greg Clayton7b139222011-07-21 01:12:01 +0000318// if ((cstr = m_comm.GetImagePath ()) != NULL)
319// {
320// async_strm_sp->Printf ("Image Path: %s\n", cstr);
321// async_strm_sp->Flush();
322// }
Greg Clayton234981a2011-07-20 03:41:06 +0000323 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000324 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000325 else
326 {
327 puts ("KDP_CONNECT failed"); // REMOVE THIS
328 error.SetErrorString("KDP_REATTACH failed");
329 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000330 }
331 else
332 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000333 puts ("KDP_REATTACH failed"); // REMOVE THIS
334 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton8d2ea282011-07-17 20:36:25 +0000335 }
336 }
337 else
338 {
339 error.SetErrorString("invalid reply port from UDP connection");
340 }
341 }
342 else
343 {
344 if (error.Success())
345 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
346 }
347 if (error.Fail())
348 m_comm.Disconnect();
349
Greg Clayton363be3f2011-07-15 03:27:12 +0000350 return error;
351}
352
353//----------------------------------------------------------------------
354// Process Control
355//----------------------------------------------------------------------
356Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000357ProcessKDP::DoLaunch (Module *exe_module,
358 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000359{
360 Error error;
361 error.SetErrorString ("launching not supported in kdp-remote plug-in");
362 return error;
363}
364
365
366Error
367ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
368{
369 Error error;
370 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
371 return error;
372}
373
Greg Clayton363be3f2011-07-15 03:27:12 +0000374Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000375ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
376{
377 Error error;
378 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
379 return error;
380}
381
382Error
383ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000384{
385 Error error;
386 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
387 return error;
388}
389
390
391void
392ProcessKDP::DidAttach ()
393{
394 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
395 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000396 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000397 if (GetID() != LLDB_INVALID_PROCESS_ID)
398 {
399 // TODO: figure out the register context that we will use
400 }
401}
402
403Error
404ProcessKDP::WillResume ()
405{
406 return Error();
407}
408
409Error
410ProcessKDP::DoResume ()
411{
412 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000413 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
414 // Only start the async thread if we try to do any process control
415 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
416 StartAsyncThread ();
417
Greg Clayton3acaa922012-09-25 02:40:06 +0000418 bool resume = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000419
Greg Clayton3acaa922012-09-25 02:40:06 +0000420 // With KDP there is only one thread we can tell what to do
421 ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
422 if (kernel_thread_sp)
Greg Claytonea636012012-09-21 01:55:30 +0000423 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000424 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Claytone76f8c42012-09-21 16:31:20 +0000425 switch (thread_resume_state)
Greg Claytonea636012012-09-21 01:55:30 +0000426 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000427 case eStateSuspended:
428 // Nothing to do here when a thread will stay suspended
429 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton3acaa922012-09-25 02:40:06 +0000430 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000431 break;
432
433 case eStateStepping:
Greg Clayton3acaa922012-09-25 02:40:06 +0000434 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread stepping");
435 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true);
436 resume = true;
437 break;
438
Greg Claytone76f8c42012-09-21 16:31:20 +0000439 case eStateRunning:
Greg Clayton3acaa922012-09-25 02:40:06 +0000440 puts("REMOVE THIS: ProcessKDP::DoResume () -- thread running");
441 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false);
442 resume = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000443 break;
Greg Clayton3acaa922012-09-25 02:40:06 +0000444
Greg Claytone76f8c42012-09-21 16:31:20 +0000445 default:
Greg Clayton3acaa922012-09-25 02:40:06 +0000446 // The only valid thread resume states are listed above
Greg Claytone76f8c42012-09-21 16:31:20 +0000447 assert (!"invalid thread resume state");
448 break;
Greg Claytonea636012012-09-21 01:55:30 +0000449 }
450 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000451
452 if (resume)
Greg Claytone76f8c42012-09-21 16:31:20 +0000453 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000454 if (log)
455 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Claytone76f8c42012-09-21 16:31:20 +0000456
Greg Clayton3acaa922012-09-25 02:40:06 +0000457 if (m_comm.SendRequestResume ())
Greg Claytone76f8c42012-09-21 16:31:20 +0000458 {
459 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
460 SetPrivateState(eStateRunning);
461 }
462 else
463 error.SetErrorString ("KDP resume failed");
464 }
Greg Claytonea636012012-09-21 01:55:30 +0000465 else
Greg Claytone76f8c42012-09-21 16:31:20 +0000466 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000467 error.SetErrorString ("kernel thread is suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000468 }
469
Greg Clayton363be3f2011-07-15 03:27:12 +0000470 return error;
471}
472
Greg Clayton3acaa922012-09-25 02:40:06 +0000473lldb::ThreadSP
474ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
475{
476 // KDP only tells us about one thread/core. Any other threads will usually
477 // be the ones that are read from memory by the OS plug-ins.
478 const lldb::tid_t kernel_tid = 1;
479 ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
480 if (!thread_sp)
481 {
482 thread_sp.reset(new ThreadKDP (shared_from_this(), kernel_tid));
483 new_thread_list.AddThread(thread_sp);
484 }
485 return thread_sp;
486}
487
488
489
490
Greg Claytonae932352012-04-10 00:18:59 +0000491bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000492ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000493{
494 // locker will keep a mutex locked until it goes out of scope
495 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
496 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000497 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000498
Greg Clayton3acaa922012-09-25 02:40:06 +0000499 // Even though there is a CPU mask, it doesn't mean to can see each CPU
500 // indivudually, there is really only one. Lets call this thread 1.
501 GetKernelThread (old_thread_list, new_thread_list);
502
Greg Claytonae932352012-04-10 00:18:59 +0000503 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000504}
505
Greg Clayton363be3f2011-07-15 03:27:12 +0000506void
507ProcessKDP::RefreshStateAfterStop ()
508{
509 // Let all threads recover from stopping and do any clean up based
510 // on the previous thread state (if any).
511 m_thread_list.RefreshStateAfterStop();
Greg Clayton363be3f2011-07-15 03:27:12 +0000512}
513
514Error
515ProcessKDP::DoHalt (bool &caused_stop)
516{
517 Error error;
518
Greg Clayton3acaa922012-09-25 02:40:06 +0000519 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000520 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000521 if (m_destroy_in_process)
Greg Clayton363be3f2011-07-15 03:27:12 +0000522 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000523 // If we are attemping to destroy, we need to not return an error to
524 // Halt or DoDestroy won't get called.
525 // We are also currently running, so send a process stopped event
526 SetPrivateState (eStateStopped);
Greg Clayton363be3f2011-07-15 03:27:12 +0000527 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000528 else
Greg Clayton363be3f2011-07-15 03:27:12 +0000529 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000530 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Clayton363be3f2011-07-15 03:27:12 +0000531 }
532 }
533 return error;
534}
535
536Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000537ProcessKDP::DoDetach()
538{
539 Error error;
540 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
541 if (log)
542 log->Printf ("ProcessKDP::DoDetach()");
543
Greg Clayton3acaa922012-09-25 02:40:06 +0000544 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000545 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000546 // We are running and we can't interrupt a running kernel, so we need
547 // to just close the connection to the kernel and hope for the best
548 }
549 else
550 {
551 DisableAllBreakpointSites ();
552
553 m_thread_list.DiscardThreadPlans();
554
555 if (m_comm.IsConnected())
Greg Clayton8d2ea282011-07-17 20:36:25 +0000556 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000557
558 m_comm.SendRequestDisconnect();
559
560 size_t response_size = m_comm.Disconnect ();
561 if (log)
562 {
563 if (response_size)
564 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
565 else
566 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
567 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000568 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000569 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000570 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000571 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000572
573 SetPrivateState (eStateDetached);
574 ResumePrivateStateThread();
575
576 //KillDebugserverProcess ();
577 return error;
578}
579
580Error
Greg Clayton3acaa922012-09-25 02:40:06 +0000581ProcessKDP::WillDestroy ()
582{
583 Error error;
584 m_destroy_in_process = true;
585 return error;
586}
587
588Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000589ProcessKDP::DoDestroy ()
590{
Greg Claytone76f8c42012-09-21 16:31:20 +0000591 // For KDP there really is no difference between destroy and detach
592 return DoDetach();
Greg Clayton363be3f2011-07-15 03:27:12 +0000593}
594
595//------------------------------------------------------------------
596// Process Queries
597//------------------------------------------------------------------
598
599bool
600ProcessKDP::IsAlive ()
601{
602 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
603}
604
605//------------------------------------------------------------------
606// Process Memory
607//------------------------------------------------------------------
608size_t
609ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
610{
Greg Clayton0fa51242011-07-19 03:57:15 +0000611 if (m_comm.IsConnected())
612 return m_comm.SendRequestReadMemory (addr, buf, size, error);
613 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000614 return 0;
615}
616
617size_t
618ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
619{
Greg Claytone76f8c42012-09-21 16:31:20 +0000620 if (m_comm.IsConnected())
621 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
622 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000623 return 0;
624}
625
626lldb::addr_t
627ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
628{
629 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
630 return LLDB_INVALID_ADDRESS;
631}
632
633Error
634ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
635{
636 Error error;
637 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
638 return error;
639}
640
641Error
642ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
643{
Greg Clayton234981a2011-07-20 03:41:06 +0000644 if (m_comm.LocalBreakpointsAreSupported ())
645 {
646 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000647 if (!bp_site->IsEnabled())
648 {
649 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
650 {
651 bp_site->SetEnabled(true);
652 bp_site->SetType (BreakpointSite::eExternal);
653 }
654 else
655 {
656 error.SetErrorString ("KDP set breakpoint failed");
657 }
658 }
Greg Clayton234981a2011-07-20 03:41:06 +0000659 return error;
660 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000661 return EnableSoftwareBreakpoint (bp_site);
662}
663
664Error
665ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
666{
Greg Clayton234981a2011-07-20 03:41:06 +0000667 if (m_comm.LocalBreakpointsAreSupported ())
668 {
669 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000670 if (bp_site->IsEnabled())
671 {
672 BreakpointSite::Type bp_type = bp_site->GetType();
673 if (bp_type == BreakpointSite::eExternal)
674 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000675 if (m_destroy_in_process && m_comm.IsRunning())
676 {
677 // We are trying to destroy our connection and we are running
Greg Clayton7b139222011-07-21 01:12:01 +0000678 bp_site->SetEnabled(false);
Greg Clayton3acaa922012-09-25 02:40:06 +0000679 }
Greg Clayton7b139222011-07-21 01:12:01 +0000680 else
Greg Clayton3acaa922012-09-25 02:40:06 +0000681 {
682 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
683 bp_site->SetEnabled(false);
684 else
685 error.SetErrorString ("KDP remove breakpoint failed");
686 }
Greg Clayton7b139222011-07-21 01:12:01 +0000687 }
688 else
689 {
690 error = DisableSoftwareBreakpoint (bp_site);
691 }
692 }
Greg Clayton234981a2011-07-20 03:41:06 +0000693 return error;
694 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000695 return DisableSoftwareBreakpoint (bp_site);
696}
697
698Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000699ProcessKDP::EnableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000700{
701 Error error;
702 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
703 return error;
704}
705
706Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000707ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000708{
709 Error error;
710 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
711 return error;
712}
713
714void
715ProcessKDP::Clear()
716{
Greg Clayton363be3f2011-07-15 03:27:12 +0000717 m_thread_list.Clear();
718}
719
720Error
721ProcessKDP::DoSignal (int signo)
722{
723 Error error;
724 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
725 return error;
726}
727
728void
729ProcessKDP::Initialize()
730{
731 static bool g_initialized = false;
732
733 if (g_initialized == false)
734 {
735 g_initialized = true;
736 PluginManager::RegisterPlugin (GetPluginNameStatic(),
737 GetPluginDescriptionStatic(),
738 CreateInstance);
739
740 Log::Callbacks log_callbacks = {
741 ProcessKDPLog::DisableLog,
742 ProcessKDPLog::EnableLog,
743 ProcessKDPLog::ListLogCategories
744 };
745
746 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
747 }
748}
749
750bool
751ProcessKDP::StartAsyncThread ()
752{
753 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
754
755 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000756 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000757
Greg Claytone76f8c42012-09-21 16:31:20 +0000758 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
759 return true;
760
Greg Clayton363be3f2011-07-15 03:27:12 +0000761 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
762 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
763}
764
765void
766ProcessKDP::StopAsyncThread ()
767{
768 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
769
770 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000771 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000772
773 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
774
775 // Stop the stdio thread
776 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
777 {
778 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Claytone76f8c42012-09-21 16:31:20 +0000779 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton363be3f2011-07-15 03:27:12 +0000780 }
781}
782
783
784void *
785ProcessKDP::AsyncThread (void *arg)
786{
787 ProcessKDP *process = (ProcessKDP*) arg;
788
Greg Claytone76f8c42012-09-21 16:31:20 +0000789 const lldb::pid_t pid = process->GetID();
790
Greg Clayton363be3f2011-07-15 03:27:12 +0000791 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
792 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000793 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000794
795 Listener listener ("ProcessKDP::AsyncThread");
796 EventSP event_sp;
797 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
798 eBroadcastBitAsyncThreadShouldExit;
799
Greg Claytone76f8c42012-09-21 16:31:20 +0000800
Greg Clayton363be3f2011-07-15 03:27:12 +0000801 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
802 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000803 bool done = false;
804 while (!done)
805 {
806 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000807 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
808 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000809 if (listener.WaitForEvent (NULL, event_sp))
810 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000811 uint32_t event_type = event_sp->GetType();
812 if (log)
813 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
814 pid,
815 event_type);
816
817 // When we are running, poll for 1 second to try and get an exception
818 // to indicate the process has stopped. If we don't get one, check to
819 // make sure no one asked us to exit
820 bool is_running = false;
821 DataExtractor exc_reply_packet;
822 do
Greg Clayton363be3f2011-07-15 03:27:12 +0000823 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000824 switch (event_type)
825 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000826 case eBroadcastBitAsyncContinue:
Greg Clayton363be3f2011-07-15 03:27:12 +0000827 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000828 is_running = true;
829 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Clayton363be3f2011-07-15 03:27:12 +0000830 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000831 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
832 thread_sp->GetRegisterContext()->InvalidateAllRegisters();
833 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
834
Greg Claytone76f8c42012-09-21 16:31:20 +0000835 // TODO: parse the stop reply packet
Greg Clayton3acaa922012-09-25 02:40:06 +0000836 is_running = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000837 process->SetPrivateState(eStateStopped);
838 }
839 else
840 {
841 // Check to see if we are supposed to exit. There is no way to
842 // interrupt a running kernel, so all we can do is wait for an
843 // exception or detach...
844 if (listener.GetNextEvent(event_sp))
845 {
846 // We got an event, go through the loop again
847 event_type = event_sp->GetType();
848 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000849 }
850 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000851 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000852
Greg Claytone76f8c42012-09-21 16:31:20 +0000853 case eBroadcastBitAsyncThreadShouldExit:
854 if (log)
855 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
856 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000857 done = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000858 is_running = false;
859 break;
860
861 default:
862 if (log)
863 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
864 pid,
865 event_type);
866 done = true;
867 is_running = false;
868 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000869 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000870 } while (is_running);
Greg Clayton363be3f2011-07-15 03:27:12 +0000871 }
872 else
873 {
874 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000875 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
876 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000877 done = true;
878 }
879 }
880 }
881
882 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000883 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
884 arg,
885 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000886
887 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
888 return NULL;
889}
890
891