blob: d7de4138b4e05def82adda5a90ff89a7512ea1dd [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 Clayton307c7fd2012-10-19 22:22:57 +000025#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/CommandObject.h"
27#include "lldb/Interpreter/CommandObjectMultiword.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
29#include "lldb/Interpreter/OptionGroupString.h"
30#include "lldb/Interpreter/OptionGroupUInt64.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Claytone76f8c42012-09-21 16:31:20 +000032#include "lldb/Target/RegisterContext.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000033#include "lldb/Target/Target.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000034#include "lldb/Target/Thread.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000035
36// Project includes
37#include "ProcessKDP.h"
38#include "ProcessKDPLog.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000039#include "ThreadKDP.h"
Jason Molendab46937c2012-10-03 01:29:34 +000040#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
Greg Clayton307c7fd2012-10-19 22:22:57 +000041#include "Utility/StringExtractor.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000042
43using namespace lldb;
44using namespace lldb_private;
45
46const char *
47ProcessKDP::GetPluginNameStatic()
48{
49 return "kdp-remote";
50}
51
52const char *
53ProcessKDP::GetPluginDescriptionStatic()
54{
55 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
56}
57
58void
59ProcessKDP::Terminate()
60{
61 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
62}
63
64
Greg Clayton46c9a352012-02-09 06:16:32 +000065lldb::ProcessSP
66ProcessKDP::CreateInstance (Target &target,
67 Listener &listener,
68 const FileSpec *crash_file_path)
Greg Clayton363be3f2011-07-15 03:27:12 +000069{
Greg Clayton46c9a352012-02-09 06:16:32 +000070 lldb::ProcessSP process_sp;
71 if (crash_file_path == NULL)
72 process_sp.reset(new ProcessKDP (target, listener));
73 return process_sp;
Greg Clayton363be3f2011-07-15 03:27:12 +000074}
75
76bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000077ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Clayton363be3f2011-07-15 03:27:12 +000078{
Greg Clayton61ddf562011-10-21 21:41:45 +000079 if (plugin_specified_by_name)
80 return true;
81
Greg Clayton363be3f2011-07-15 03:27:12 +000082 // For now we are just making sure the file exists for a given module
Greg Clayton5beb99d2011-08-11 02:48:45 +000083 Module *exe_module = target.GetExecutableModulePointer();
84 if (exe_module)
Greg Clayton363be3f2011-07-15 03:27:12 +000085 {
86 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Claytonb170aee2012-05-08 01:45:38 +000087 switch (triple_ref.getOS())
Greg Clayton363be3f2011-07-15 03:27:12 +000088 {
Greg Claytonb170aee2012-05-08 01:45:38 +000089 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
90 case llvm::Triple::MacOSX: // For desktop targets
91 case llvm::Triple::IOS: // For arm targets
92 if (triple_ref.getVendor() == llvm::Triple::Apple)
93 {
94 ObjectFile *exe_objfile = exe_module->GetObjectFile();
95 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
96 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
97 return true;
98 }
99 break;
100
101 default:
102 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000103 }
104 }
Greg Clayton61ddf562011-10-21 21:41:45 +0000105 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +0000106}
107
108//----------------------------------------------------------------------
109// ProcessKDP constructor
110//----------------------------------------------------------------------
111ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
112 Process (target, listener),
113 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham5a15e692012-02-16 06:50:00 +0000114 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton3acaa922012-09-25 02:40:06 +0000115 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molendab46937c2012-10-03 01:29:34 +0000116 m_destroy_in_process (false),
117 m_dyld_plugin_name (),
Greg Clayton307c7fd2012-10-19 22:22:57 +0000118 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
119 m_command_sp()
Greg Clayton363be3f2011-07-15 03:27:12 +0000120{
Greg Claytone76f8c42012-09-21 16:31:20 +0000121 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
122 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton363be3f2011-07-15 03:27:12 +0000123}
124
125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128ProcessKDP::~ProcessKDP()
129{
130 Clear();
Greg Claytonffa43a62011-11-17 04:46:02 +0000131 // We need to call finalize on the process before destroying ourselves
132 // to make sure all of the broadcaster cleanup goes as planned. If we
133 // destruct this class, then Process::~Process() might have problems
134 // trying to fully destroy the broadcaster.
135 Finalize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000136}
137
138//----------------------------------------------------------------------
139// PluginInterface
140//----------------------------------------------------------------------
141const char *
142ProcessKDP::GetPluginName()
143{
144 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
145}
146
147const char *
148ProcessKDP::GetShortPluginName()
149{
150 return GetPluginNameStatic();
151}
152
153uint32_t
154ProcessKDP::GetPluginVersion()
155{
156 return 1;
157}
158
159Error
160ProcessKDP::WillLaunch (Module* module)
161{
162 Error error;
163 error.SetErrorString ("launching not supported in kdp-remote plug-in");
164 return error;
165}
166
167Error
168ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
169{
170 Error error;
171 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
172 return error;
173}
174
175Error
176ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
177{
178 Error error;
179 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
180 return error;
181}
182
183Error
Jason Molendafac2e622012-09-29 04:02:01 +0000184ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Clayton363be3f2011-07-15 03:27:12 +0000185{
Greg Clayton363be3f2011-07-15 03:27:12 +0000186 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000187
188 // Don't let any JIT happen when doing KDP as we can't allocate
189 // memory and we don't want to be mucking with threads that might
190 // already be handling exceptions
191 SetCanJIT(false);
192
Greg Clayton8d2ea282011-07-17 20:36:25 +0000193 if (remote_url == NULL || remote_url[0] == '\0')
Greg Claytone76f8c42012-09-21 16:31:20 +0000194 {
195 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
196 return error;
197 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000198
199 std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
200 if (conn_ap.get())
201 {
202 // Only try once for now.
203 // TODO: check if we should be retrying?
204 const uint32_t max_retry_count = 1;
205 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
206 {
207 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
208 break;
209 usleep (100000);
210 }
211 }
212
213 if (conn_ap->IsConnected())
214 {
215 const uint16_t reply_port = conn_ap->GetReadPort ();
216
217 if (reply_port != 0)
218 {
219 m_comm.SetConnection(conn_ap.release());
220
221 if (m_comm.SendRequestReattach(reply_port))
222 {
223 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
224 {
225 m_comm.GetVersion();
226 uint32_t cpu = m_comm.GetCPUType();
227 uint32_t sub = m_comm.GetCPUSubtype();
228 ArchSpec kernel_arch;
229 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
230 m_target.SetArchitecture(kernel_arch);
Jason Molendafac2e622012-09-29 04:02:01 +0000231
232 /* Get the kernel's UUID and load address via kdp-kernelversion packet. */
233
234 UUID kernel_uuid = m_comm.GetUUID ();
235 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molendafac2e622012-09-29 04:02:01 +0000236
Jason Molendab46937c2012-10-03 01:29:34 +0000237 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
Jason Molendafac2e622012-09-29 04:02:01 +0000238 {
Jason Molendab46937c2012-10-03 01:29:34 +0000239 m_kernel_load_addr = kernel_load_addr;
240 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molendafac2e622012-09-29 04:02:01 +0000241 }
242
Greg Clayton3acaa922012-09-25 02:40:06 +0000243 // Set the thread ID
244 UpdateThreadListIfNeeded ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000245 SetID (1);
Greg Clayton37f962e2011-08-22 02:49:39 +0000246 GetThreadList ();
Greg Clayton0fa51242011-07-19 03:57:15 +0000247 SetPrivateState (eStateStopped);
Greg Clayton234981a2011-07-20 03:41:06 +0000248 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
249 if (async_strm_sp)
250 {
Greg Clayton7b139222011-07-21 01:12:01 +0000251 const char *cstr;
252 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton234981a2011-07-20 03:41:06 +0000253 {
Greg Clayton7b139222011-07-21 01:12:01 +0000254 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton234981a2011-07-20 03:41:06 +0000255 async_strm_sp->Flush();
256 }
Greg Clayton7b139222011-07-21 01:12:01 +0000257// if ((cstr = m_comm.GetImagePath ()) != NULL)
258// {
259// async_strm_sp->Printf ("Image Path: %s\n", cstr);
260// async_strm_sp->Flush();
261// }
Greg Clayton234981a2011-07-20 03:41:06 +0000262 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000263 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000264 else
265 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000266 error.SetErrorString("KDP_REATTACH failed");
267 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000268 }
269 else
270 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000271 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton8d2ea282011-07-17 20:36:25 +0000272 }
273 }
274 else
275 {
276 error.SetErrorString("invalid reply port from UDP connection");
277 }
278 }
279 else
280 {
281 if (error.Success())
282 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
283 }
284 if (error.Fail())
285 m_comm.Disconnect();
286
Greg Clayton363be3f2011-07-15 03:27:12 +0000287 return error;
288}
289
290//----------------------------------------------------------------------
291// Process Control
292//----------------------------------------------------------------------
293Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000294ProcessKDP::DoLaunch (Module *exe_module,
295 const ProcessLaunchInfo &launch_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000296{
297 Error error;
298 error.SetErrorString ("launching not supported in kdp-remote plug-in");
299 return error;
300}
301
302
303Error
304ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
305{
306 Error error;
307 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
308 return error;
309}
310
Greg Clayton363be3f2011-07-15 03:27:12 +0000311Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000312ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
313{
314 Error error;
315 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
316 return error;
317}
318
319Error
320ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Clayton363be3f2011-07-15 03:27:12 +0000321{
322 Error error;
323 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
324 return error;
325}
326
327
328void
329ProcessKDP::DidAttach ()
330{
331 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
332 if (log)
Johnny Chen01df0572011-10-11 21:17:10 +0000333 log->Printf ("ProcessKDP::DidAttach()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000334 if (GetID() != LLDB_INVALID_PROCESS_ID)
335 {
336 // TODO: figure out the register context that we will use
337 }
338}
339
Jason Molendab46937c2012-10-03 01:29:34 +0000340addr_t
341ProcessKDP::GetImageInfoAddress()
342{
343 return m_kernel_load_addr;
344}
345
346lldb_private::DynamicLoader *
347ProcessKDP::GetDynamicLoader ()
348{
349 if (m_dyld_ap.get() == NULL)
350 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
351 return m_dyld_ap.get();
352}
353
Greg Clayton363be3f2011-07-15 03:27:12 +0000354Error
355ProcessKDP::WillResume ()
356{
357 return Error();
358}
359
360Error
361ProcessKDP::DoResume ()
362{
363 Error error;
Greg Claytone76f8c42012-09-21 16:31:20 +0000364 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
365 // Only start the async thread if we try to do any process control
366 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
367 StartAsyncThread ();
368
Greg Clayton3acaa922012-09-25 02:40:06 +0000369 bool resume = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000370
Greg Clayton3acaa922012-09-25 02:40:06 +0000371 // With KDP there is only one thread we can tell what to do
372 ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
373 if (kernel_thread_sp)
Greg Claytonea636012012-09-21 01:55:30 +0000374 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000375 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Claytone76f8c42012-09-21 16:31:20 +0000376 switch (thread_resume_state)
Greg Claytonea636012012-09-21 01:55:30 +0000377 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000378 case eStateSuspended:
379 // Nothing to do here when a thread will stay suspended
380 // we just leave the CPU mask bit set to zero for the thread
381 break;
382
383 case eStateStepping:
Greg Clayton3acaa922012-09-25 02:40:06 +0000384 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (true);
385 resume = true;
386 break;
387
Greg Claytone76f8c42012-09-21 16:31:20 +0000388 case eStateRunning:
Greg Clayton3acaa922012-09-25 02:40:06 +0000389 kernel_thread_sp->GetRegisterContext()->HardwareSingleStep (false);
390 resume = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000391 break;
Greg Clayton3acaa922012-09-25 02:40:06 +0000392
Greg Claytone76f8c42012-09-21 16:31:20 +0000393 default:
Greg Clayton3acaa922012-09-25 02:40:06 +0000394 // The only valid thread resume states are listed above
Greg Claytone76f8c42012-09-21 16:31:20 +0000395 assert (!"invalid thread resume state");
396 break;
Greg Claytonea636012012-09-21 01:55:30 +0000397 }
398 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000399
400 if (resume)
Greg Claytone76f8c42012-09-21 16:31:20 +0000401 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000402 if (log)
403 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Claytone76f8c42012-09-21 16:31:20 +0000404
Greg Clayton3acaa922012-09-25 02:40:06 +0000405 if (m_comm.SendRequestResume ())
Greg Claytone76f8c42012-09-21 16:31:20 +0000406 {
407 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
408 SetPrivateState(eStateRunning);
409 }
410 else
411 error.SetErrorString ("KDP resume failed");
412 }
Greg Claytonea636012012-09-21 01:55:30 +0000413 else
Greg Claytone76f8c42012-09-21 16:31:20 +0000414 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000415 error.SetErrorString ("kernel thread is suspended");
Greg Claytone76f8c42012-09-21 16:31:20 +0000416 }
417
Greg Clayton363be3f2011-07-15 03:27:12 +0000418 return error;
419}
420
Greg Clayton3acaa922012-09-25 02:40:06 +0000421lldb::ThreadSP
422ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
423{
424 // KDP only tells us about one thread/core. Any other threads will usually
425 // be the ones that are read from memory by the OS plug-ins.
426 const lldb::tid_t kernel_tid = 1;
427 ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
428 if (!thread_sp)
429 {
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000430 thread_sp.reset(new ThreadKDP (*this, kernel_tid));
Greg Clayton3acaa922012-09-25 02:40:06 +0000431 new_thread_list.AddThread(thread_sp);
432 }
433 return thread_sp;
434}
435
436
437
438
Greg Claytonae932352012-04-10 00:18:59 +0000439bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000440ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton363be3f2011-07-15 03:27:12 +0000441{
442 // locker will keep a mutex locked until it goes out of scope
443 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
444 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Greg Clayton444e35b2011-10-19 18:09:39 +0000445 log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID());
Greg Clayton363be3f2011-07-15 03:27:12 +0000446
Greg Clayton3acaa922012-09-25 02:40:06 +0000447 // Even though there is a CPU mask, it doesn't mean to can see each CPU
448 // indivudually, there is really only one. Lets call this thread 1.
449 GetKernelThread (old_thread_list, new_thread_list);
450
Greg Claytonae932352012-04-10 00:18:59 +0000451 return new_thread_list.GetSize(false) > 0;
Greg Clayton363be3f2011-07-15 03:27:12 +0000452}
453
Greg Clayton363be3f2011-07-15 03:27:12 +0000454void
455ProcessKDP::RefreshStateAfterStop ()
456{
457 // Let all threads recover from stopping and do any clean up based
458 // on the previous thread state (if any).
459 m_thread_list.RefreshStateAfterStop();
Greg Clayton363be3f2011-07-15 03:27:12 +0000460}
461
462Error
463ProcessKDP::DoHalt (bool &caused_stop)
464{
465 Error error;
466
Greg Clayton3acaa922012-09-25 02:40:06 +0000467 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000468 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000469 if (m_destroy_in_process)
Greg Clayton363be3f2011-07-15 03:27:12 +0000470 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000471 // If we are attemping to destroy, we need to not return an error to
472 // Halt or DoDestroy won't get called.
473 // We are also currently running, so send a process stopped event
474 SetPrivateState (eStateStopped);
Greg Clayton363be3f2011-07-15 03:27:12 +0000475 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000476 else
Greg Clayton363be3f2011-07-15 03:27:12 +0000477 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000478 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Clayton363be3f2011-07-15 03:27:12 +0000479 }
480 }
481 return error;
482}
483
484Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000485ProcessKDP::DoDetach()
486{
487 Error error;
488 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
489 if (log)
490 log->Printf ("ProcessKDP::DoDetach()");
491
Greg Clayton3acaa922012-09-25 02:40:06 +0000492 if (m_comm.IsRunning())
Greg Clayton363be3f2011-07-15 03:27:12 +0000493 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000494 // We are running and we can't interrupt a running kernel, so we need
495 // to just close the connection to the kernel and hope for the best
496 }
497 else
498 {
499 DisableAllBreakpointSites ();
500
501 m_thread_list.DiscardThreadPlans();
502
503 if (m_comm.IsConnected())
Greg Clayton8d2ea282011-07-17 20:36:25 +0000504 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000505
506 m_comm.SendRequestDisconnect();
507
508 size_t response_size = m_comm.Disconnect ();
509 if (log)
510 {
511 if (response_size)
512 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
513 else
514 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
515 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000516 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000517 }
Greg Clayton3acaa922012-09-25 02:40:06 +0000518 StopAsyncThread ();
Greg Claytondb9d6f42012-01-31 04:56:17 +0000519 m_comm.Clear();
Greg Clayton363be3f2011-07-15 03:27:12 +0000520
521 SetPrivateState (eStateDetached);
522 ResumePrivateStateThread();
523
524 //KillDebugserverProcess ();
525 return error;
526}
527
528Error
Greg Clayton3acaa922012-09-25 02:40:06 +0000529ProcessKDP::WillDestroy ()
530{
531 Error error;
532 m_destroy_in_process = true;
533 return error;
534}
535
536Error
Greg Clayton363be3f2011-07-15 03:27:12 +0000537ProcessKDP::DoDestroy ()
538{
Greg Claytone76f8c42012-09-21 16:31:20 +0000539 // For KDP there really is no difference between destroy and detach
540 return DoDetach();
Greg Clayton363be3f2011-07-15 03:27:12 +0000541}
542
543//------------------------------------------------------------------
544// Process Queries
545//------------------------------------------------------------------
546
547bool
548ProcessKDP::IsAlive ()
549{
550 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
551}
552
553//------------------------------------------------------------------
554// Process Memory
555//------------------------------------------------------------------
556size_t
557ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
558{
Greg Clayton0fa51242011-07-19 03:57:15 +0000559 if (m_comm.IsConnected())
560 return m_comm.SendRequestReadMemory (addr, buf, size, error);
561 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000562 return 0;
563}
564
565size_t
566ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
567{
Greg Claytone76f8c42012-09-21 16:31:20 +0000568 if (m_comm.IsConnected())
569 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
570 error.SetErrorString ("not connected");
Greg Clayton363be3f2011-07-15 03:27:12 +0000571 return 0;
572}
573
574lldb::addr_t
575ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
576{
577 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
578 return LLDB_INVALID_ADDRESS;
579}
580
581Error
582ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
583{
584 Error error;
585 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
586 return error;
587}
588
589Error
590ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site)
591{
Greg Clayton234981a2011-07-20 03:41:06 +0000592 if (m_comm.LocalBreakpointsAreSupported ())
593 {
594 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000595 if (!bp_site->IsEnabled())
596 {
597 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
598 {
599 bp_site->SetEnabled(true);
600 bp_site->SetType (BreakpointSite::eExternal);
601 }
602 else
603 {
604 error.SetErrorString ("KDP set breakpoint failed");
605 }
606 }
Greg Clayton234981a2011-07-20 03:41:06 +0000607 return error;
608 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000609 return EnableSoftwareBreakpoint (bp_site);
610}
611
612Error
613ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site)
614{
Greg Clayton234981a2011-07-20 03:41:06 +0000615 if (m_comm.LocalBreakpointsAreSupported ())
616 {
617 Error error;
Greg Clayton7b139222011-07-21 01:12:01 +0000618 if (bp_site->IsEnabled())
619 {
620 BreakpointSite::Type bp_type = bp_site->GetType();
621 if (bp_type == BreakpointSite::eExternal)
622 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000623 if (m_destroy_in_process && m_comm.IsRunning())
624 {
625 // We are trying to destroy our connection and we are running
Greg Clayton7b139222011-07-21 01:12:01 +0000626 bp_site->SetEnabled(false);
Greg Clayton3acaa922012-09-25 02:40:06 +0000627 }
Greg Clayton7b139222011-07-21 01:12:01 +0000628 else
Greg Clayton3acaa922012-09-25 02:40:06 +0000629 {
630 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
631 bp_site->SetEnabled(false);
632 else
633 error.SetErrorString ("KDP remove breakpoint failed");
634 }
Greg Clayton7b139222011-07-21 01:12:01 +0000635 }
636 else
637 {
638 error = DisableSoftwareBreakpoint (bp_site);
639 }
640 }
Greg Clayton234981a2011-07-20 03:41:06 +0000641 return error;
642 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000643 return DisableSoftwareBreakpoint (bp_site);
644}
645
646Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000647ProcessKDP::EnableWatchpoint (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
654Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000655ProcessKDP::DisableWatchpoint (Watchpoint *wp)
Greg Clayton363be3f2011-07-15 03:27:12 +0000656{
657 Error error;
658 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
659 return error;
660}
661
662void
663ProcessKDP::Clear()
664{
Greg Clayton363be3f2011-07-15 03:27:12 +0000665 m_thread_list.Clear();
666}
667
668Error
669ProcessKDP::DoSignal (int signo)
670{
671 Error error;
672 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
673 return error;
674}
675
676void
677ProcessKDP::Initialize()
678{
679 static bool g_initialized = false;
680
681 if (g_initialized == false)
682 {
683 g_initialized = true;
684 PluginManager::RegisterPlugin (GetPluginNameStatic(),
685 GetPluginDescriptionStatic(),
686 CreateInstance);
687
688 Log::Callbacks log_callbacks = {
689 ProcessKDPLog::DisableLog,
690 ProcessKDPLog::EnableLog,
691 ProcessKDPLog::ListLogCategories
692 };
693
694 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
695 }
696}
697
698bool
699ProcessKDP::StartAsyncThread ()
700{
701 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
702
703 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000704 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000705
Greg Claytone76f8c42012-09-21 16:31:20 +0000706 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
707 return true;
708
Greg Clayton363be3f2011-07-15 03:27:12 +0000709 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
710 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
711}
712
713void
714ProcessKDP::StopAsyncThread ()
715{
716 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
717
718 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000719 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Clayton363be3f2011-07-15 03:27:12 +0000720
721 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
722
723 // Stop the stdio thread
724 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
725 {
726 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Claytone76f8c42012-09-21 16:31:20 +0000727 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Clayton363be3f2011-07-15 03:27:12 +0000728 }
729}
730
731
732void *
733ProcessKDP::AsyncThread (void *arg)
734{
735 ProcessKDP *process = (ProcessKDP*) arg;
736
Greg Claytone76f8c42012-09-21 16:31:20 +0000737 const lldb::pid_t pid = process->GetID();
738
Greg Clayton363be3f2011-07-15 03:27:12 +0000739 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
740 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000741 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread starting...", arg, pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000742
743 Listener listener ("ProcessKDP::AsyncThread");
744 EventSP event_sp;
745 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
746 eBroadcastBitAsyncThreadShouldExit;
747
Greg Claytone76f8c42012-09-21 16:31:20 +0000748
Greg Clayton363be3f2011-07-15 03:27:12 +0000749 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
750 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000751 bool done = false;
752 while (!done)
753 {
754 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000755 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp)...",
756 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000757 if (listener.WaitForEvent (NULL, event_sp))
758 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000759 uint32_t event_type = event_sp->GetType();
760 if (log)
761 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) Got an event of type: %d...",
762 pid,
763 event_type);
764
765 // When we are running, poll for 1 second to try and get an exception
766 // to indicate the process has stopped. If we don't get one, check to
767 // make sure no one asked us to exit
768 bool is_running = false;
769 DataExtractor exc_reply_packet;
770 do
Greg Clayton363be3f2011-07-15 03:27:12 +0000771 {
Greg Clayton363be3f2011-07-15 03:27:12 +0000772 switch (event_type)
773 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000774 case eBroadcastBitAsyncContinue:
Greg Clayton363be3f2011-07-15 03:27:12 +0000775 {
Greg Claytone76f8c42012-09-21 16:31:20 +0000776 is_running = true;
777 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Clayton363be3f2011-07-15 03:27:12 +0000778 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000779 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
780 thread_sp->GetRegisterContext()->InvalidateAllRegisters();
781 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
782
Greg Claytone76f8c42012-09-21 16:31:20 +0000783 // TODO: parse the stop reply packet
Greg Clayton3acaa922012-09-25 02:40:06 +0000784 is_running = false;
Greg Claytone76f8c42012-09-21 16:31:20 +0000785 process->SetPrivateState(eStateStopped);
786 }
787 else
788 {
789 // Check to see if we are supposed to exit. There is no way to
790 // interrupt a running kernel, so all we can do is wait for an
791 // exception or detach...
792 if (listener.GetNextEvent(event_sp))
793 {
794 // We got an event, go through the loop again
795 event_type = event_sp->GetType();
796 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000797 }
798 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000799 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000800
Greg Claytone76f8c42012-09-21 16:31:20 +0000801 case eBroadcastBitAsyncThreadShouldExit:
802 if (log)
803 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got eBroadcastBitAsyncThreadShouldExit...",
804 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000805 done = true;
Greg Claytone76f8c42012-09-21 16:31:20 +0000806 is_running = false;
807 break;
808
809 default:
810 if (log)
811 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) got unknown event 0x%8.8x",
812 pid,
813 event_type);
814 done = true;
815 is_running = false;
816 break;
Greg Clayton363be3f2011-07-15 03:27:12 +0000817 }
Greg Claytone76f8c42012-09-21 16:31:20 +0000818 } while (is_running);
Greg Clayton363be3f2011-07-15 03:27:12 +0000819 }
820 else
821 {
822 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000823 log->Printf ("ProcessKDP::AsyncThread (pid = %llu) listener.WaitForEvent (NULL, event_sp) => false",
824 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000825 done = true;
826 }
827 }
828 }
829
830 if (log)
Greg Claytone76f8c42012-09-21 16:31:20 +0000831 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %llu) thread exiting...",
832 arg,
833 pid);
Greg Clayton363be3f2011-07-15 03:27:12 +0000834
835 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
836 return NULL;
837}
838
839
Greg Clayton307c7fd2012-10-19 22:22:57 +0000840class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
841{
842private:
843
844 OptionGroupOptions m_option_group;
845 OptionGroupUInt64 m_command_byte;
846 OptionGroupString m_packet_data;
847
848 virtual Options *
849 GetOptions ()
850 {
851 return &m_option_group;
852 }
853
854
855public:
856 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
857 CommandObjectParsed (interpreter,
858 "process plugin packet send",
859 "Send a custom packet through the KDP protocol by specifying the command byte and the packet payload data. A packet will be sent with a correct header and payload, and the raw result bytes will be displayed as a string value. ",
860 NULL),
861 m_option_group (interpreter),
862 m_command_byte(LLDB_OPT_SET_1, true , "command", 'c', 0, eArgTypeNone, "Specify the command byte to use when sending the KDP request packet.", 0),
863 m_packet_data (LLDB_OPT_SET_1, false, "payload", 'p', 0, eArgTypeNone, "Specify packet payload bytes as a hex ASCII string with no spaces or hex prefixes.", NULL)
864 {
865 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
866 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
867 m_option_group.Finalize();
868 }
869
870 ~CommandObjectProcessKDPPacketSend ()
871 {
872 }
873
874 bool
875 DoExecute (Args& command, CommandReturnObject &result)
876 {
877 const size_t argc = command.GetArgumentCount();
878 if (argc == 0)
879 {
880 if (!m_command_byte.GetOptionValue().OptionWasSet())
881 {
882 result.AppendError ("the --command option must be set to a valid command byte");
883 result.SetStatus (eReturnStatusFailed);
884 }
885 else
886 {
887 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
888 if (command_byte > 0 && command_byte <= UINT8_MAX)
889 {
890 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
891 if (process)
892 {
893 const StateType state = process->GetState();
894
895 if (StateIsStoppedState (state, true))
896 {
897 std::vector<uint8_t> payload_bytes;
898 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
899 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
900 {
901 StringExtractor extractor(ascii_hex_bytes_cstr);
902 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
903 if (ascii_hex_bytes_cstr_len & 1)
904 {
905 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
906 result.SetStatus (eReturnStatusFailed);
907 return false;
908 }
909 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
910 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
911 {
912 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
913 result.SetStatus (eReturnStatusFailed);
914 return false;
915 }
916 }
917 Error error;
918 DataExtractor reply;
919 process->GetCommunication().SendRawRequest (command_byte,
920 payload_bytes.empty() ? NULL : payload_bytes.data(),
921 payload_bytes.size(),
922 reply,
923 error);
924
925 if (error.Success())
926 {
927 // Copy the binary bytes into a hex ASCII string for the result
928 StreamString packet;
929 packet.PutBytesAsRawHex8(reply.GetDataStart(),
930 reply.GetByteSize(),
931 lldb::endian::InlHostByteOrder(),
932 lldb::endian::InlHostByteOrder());
933 result.AppendMessage(packet.GetString().c_str());
934 result.SetStatus (eReturnStatusSuccessFinishResult);
935 return true;
936 }
937 else
938 {
939 const char *error_cstr = error.AsCString();
940 if (error_cstr && error_cstr[0])
941 result.AppendError (error_cstr);
942 else
943 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
944 result.SetStatus (eReturnStatusFailed);
945 return false;
946 }
947 }
948 else
949 {
950 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
951 result.SetStatus (eReturnStatusFailed);
952 }
953 }
954 else
955 {
956 result.AppendError ("invalid process");
957 result.SetStatus (eReturnStatusFailed);
958 }
959 }
960 else
961 {
962 result.AppendErrorWithFormat ("invalid command byte 0x%llx, valid values are 1 - 255", command_byte);
963 result.SetStatus (eReturnStatusFailed);
964 }
965 }
966 }
967 else
968 {
969 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
970 result.SetStatus (eReturnStatusFailed);
971 }
972 return false;
973 }
974};
975
976class CommandObjectProcessKDPPacket : public CommandObjectMultiword
977{
978private:
979
980public:
981 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
982 CommandObjectMultiword (interpreter,
983 "process plugin packet",
984 "Commands that deal with KDP remote packets.",
985 NULL)
986 {
987 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
988 }
989
990 ~CommandObjectProcessKDPPacket ()
991 {
992 }
993};
994
995class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
996{
997public:
998 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
999 CommandObjectMultiword (interpreter,
1000 "process plugin",
1001 "A set of commands for operating on a ProcessKDP process.",
1002 "process plugin <subcommand> [<subcommand-options>]")
1003 {
1004 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1005 }
1006
1007 ~CommandObjectMultiwordProcessKDP ()
1008 {
1009 }
1010};
1011
1012CommandObject *
1013ProcessKDP::GetPluginCommandObject()
1014{
1015 if (!m_command_sp)
1016 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1017 return m_command_sp.get();
1018}
1019