blob: 3eb20175bdddc872c8fa43bb0f58085245707cdb [file] [log] [blame]
Greg Clayton59ec5122011-07-15 18:02:58 +00001//===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===//
Greg Claytonf9765ac2011-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 Clayton3a29bdb2011-07-17 20:36:25 +000016#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000017#include "lldb/Core/Debugger.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000018#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000020#include "lldb/Core/ModuleSpec.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000021#include "lldb/Core/State.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000022#include "lldb/Core/UUID.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000023#include "lldb/Host/Host.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000024#include "lldb/Host/Symbols.h"
Greg Clayton1d19a2f2012-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 Clayton1f746072012-08-29 21:13:06 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7925fbb2012-09-21 16:31:20 +000032#include "lldb/Target/RegisterContext.h"
Greg Clayton57508022011-07-15 16:31:38 +000033#include "lldb/Target/Target.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000034#include "lldb/Target/Thread.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000035
36// Project includes
37#include "ProcessKDP.h"
38#include "ProcessKDPLog.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000039#include "ThreadKDP.h"
Jason Molenda5e8534e2012-10-03 01:29:34 +000040#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
Jason Molenda840f12c2012-10-25 00:25:13 +000041#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000042#include "Utility/StringExtractor.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton1b7746e2013-05-04 01:38:48 +000047static const lldb::tid_t g_kernel_tid = 1;
48
Greg Claytonf9765ac2011-07-15 03:27:12 +000049const char *
50ProcessKDP::GetPluginNameStatic()
51{
52 return "kdp-remote";
53}
54
55const char *
56ProcessKDP::GetPluginDescriptionStatic()
57{
58 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
59}
60
61void
62ProcessKDP::Terminate()
63{
64 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
65}
66
67
Greg Claytonc3776bf2012-02-09 06:16:32 +000068lldb::ProcessSP
69ProcessKDP::CreateInstance (Target &target,
70 Listener &listener,
71 const FileSpec *crash_file_path)
Greg Claytonf9765ac2011-07-15 03:27:12 +000072{
Greg Claytonc3776bf2012-02-09 06:16:32 +000073 lldb::ProcessSP process_sp;
74 if (crash_file_path == NULL)
75 process_sp.reset(new ProcessKDP (target, listener));
76 return process_sp;
Greg Claytonf9765ac2011-07-15 03:27:12 +000077}
78
79bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000080ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Claytonf9765ac2011-07-15 03:27:12 +000081{
Greg Clayton596ed242011-10-21 21:41:45 +000082 if (plugin_specified_by_name)
83 return true;
84
Greg Claytonf9765ac2011-07-15 03:27:12 +000085 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +000086 Module *exe_module = target.GetExecutableModulePointer();
87 if (exe_module)
Greg Claytonf9765ac2011-07-15 03:27:12 +000088 {
89 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +000090 switch (triple_ref.getOS())
Greg Claytonf9765ac2011-07-15 03:27:12 +000091 {
Greg Clayton70512312012-05-08 01:45:38 +000092 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
93 case llvm::Triple::MacOSX: // For desktop targets
94 case llvm::Triple::IOS: // For arm targets
95 if (triple_ref.getVendor() == llvm::Triple::Apple)
96 {
97 ObjectFile *exe_objfile = exe_module->GetObjectFile();
98 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
99 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
100 return true;
101 }
102 break;
103
104 default:
105 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000106 }
107 }
Greg Clayton596ed242011-10-21 21:41:45 +0000108 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000109}
110
111//----------------------------------------------------------------------
112// ProcessKDP constructor
113//----------------------------------------------------------------------
114ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
115 Process (target, listener),
116 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000117 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton97d5cf02012-09-25 02:40:06 +0000118 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000119 m_dyld_plugin_name (),
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000120 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
Greg Clayton1b7746e2013-05-04 01:38:48 +0000121 m_command_sp(),
122 m_kernel_thread_wp()
Greg Claytonf9765ac2011-07-15 03:27:12 +0000123{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000124 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
125 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000126}
127
128//----------------------------------------------------------------------
129// Destructor
130//----------------------------------------------------------------------
131ProcessKDP::~ProcessKDP()
132{
133 Clear();
Greg Claytone24c4ac2011-11-17 04:46:02 +0000134 // We need to call finalize on the process before destroying ourselves
135 // to make sure all of the broadcaster cleanup goes as planned. If we
136 // destruct this class, then Process::~Process() might have problems
137 // trying to fully destroy the broadcaster.
138 Finalize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000139}
140
141//----------------------------------------------------------------------
142// PluginInterface
143//----------------------------------------------------------------------
144const char *
145ProcessKDP::GetPluginName()
146{
147 return "Process debugging plug-in that uses the Darwin KDP remote protocol";
148}
149
150const char *
151ProcessKDP::GetShortPluginName()
152{
153 return GetPluginNameStatic();
154}
155
156uint32_t
157ProcessKDP::GetPluginVersion()
158{
159 return 1;
160}
161
162Error
163ProcessKDP::WillLaunch (Module* module)
164{
165 Error error;
166 error.SetErrorString ("launching not supported in kdp-remote plug-in");
167 return error;
168}
169
170Error
171ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
172{
173 Error error;
174 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
175 return error;
176}
177
178Error
179ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
180{
181 Error error;
182 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
183 return error;
184}
185
186Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000187ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000188{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000189 Error error;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000190
191 // Don't let any JIT happen when doing KDP as we can't allocate
192 // memory and we don't want to be mucking with threads that might
193 // already be handling exceptions
194 SetCanJIT(false);
195
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000196 if (remote_url == NULL || remote_url[0] == '\0')
Greg Clayton7925fbb2012-09-21 16:31:20 +0000197 {
198 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
199 return error;
200 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000201
Greg Clayton7b0992d2013-04-18 22:45:39 +0000202 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000203 if (conn_ap.get())
204 {
205 // Only try once for now.
206 // TODO: check if we should be retrying?
207 const uint32_t max_retry_count = 1;
208 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
209 {
210 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
211 break;
212 usleep (100000);
213 }
214 }
215
216 if (conn_ap->IsConnected())
217 {
218 const uint16_t reply_port = conn_ap->GetReadPort ();
219
220 if (reply_port != 0)
221 {
222 m_comm.SetConnection(conn_ap.release());
223
224 if (m_comm.SendRequestReattach(reply_port))
225 {
226 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
227 {
228 m_comm.GetVersion();
229 uint32_t cpu = m_comm.GetCPUType();
230 uint32_t sub = m_comm.GetCPUSubtype();
231 ArchSpec kernel_arch;
232 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
233 m_target.SetArchitecture(kernel_arch);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000234
Jason Molenda840f12c2012-10-25 00:25:13 +0000235 /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */
236 /* An EFI kdp session has neither UUID nor load address. */
237
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000238 UUID kernel_uuid = m_comm.GetUUID ();
239 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000240
Jason Molenda840f12c2012-10-25 00:25:13 +0000241 if (m_comm.RemoteIsEFI ())
242 {
243 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
244 }
245 else if (kernel_load_addr != LLDB_INVALID_ADDRESS)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000246 {
Jason Molenda5e8534e2012-10-03 01:29:34 +0000247 m_kernel_load_addr = kernel_load_addr;
248 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000249 }
250
Greg Clayton97d5cf02012-09-25 02:40:06 +0000251 // Set the thread ID
252 UpdateThreadListIfNeeded ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000253 SetID (1);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000254 GetThreadList ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000255 SetPrivateState (eStateStopped);
Greg Clayton07e66e32011-07-20 03:41:06 +0000256 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
257 if (async_strm_sp)
258 {
Greg Clayton5b882162011-07-21 01:12:01 +0000259 const char *cstr;
260 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton07e66e32011-07-20 03:41:06 +0000261 {
Greg Clayton5b882162011-07-21 01:12:01 +0000262 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton07e66e32011-07-20 03:41:06 +0000263 async_strm_sp->Flush();
264 }
Greg Clayton5b882162011-07-21 01:12:01 +0000265// if ((cstr = m_comm.GetImagePath ()) != NULL)
266// {
267// async_strm_sp->Printf ("Image Path: %s\n", cstr);
268// async_strm_sp->Flush();
269// }
Greg Clayton07e66e32011-07-20 03:41:06 +0000270 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000271 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000272 else
273 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000274 error.SetErrorString("KDP_REATTACH failed");
275 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000276 }
277 else
278 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000279 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000280 }
281 }
282 else
283 {
284 error.SetErrorString("invalid reply port from UDP connection");
285 }
286 }
287 else
288 {
289 if (error.Success())
290 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
291 }
292 if (error.Fail())
293 m_comm.Disconnect();
294
Greg Claytonf9765ac2011-07-15 03:27:12 +0000295 return error;
296}
297
298//----------------------------------------------------------------------
299// Process Control
300//----------------------------------------------------------------------
301Error
Greg Clayton982c9762011-11-03 21:22:33 +0000302ProcessKDP::DoLaunch (Module *exe_module,
303 const ProcessLaunchInfo &launch_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000304{
305 Error error;
306 error.SetErrorString ("launching not supported in kdp-remote plug-in");
307 return error;
308}
309
310
311Error
312ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
313{
314 Error error;
315 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
316 return error;
317}
318
Greg Claytonf9765ac2011-07-15 03:27:12 +0000319Error
Han Ming Ong84647042012-02-25 01:07:38 +0000320ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
321{
322 Error error;
323 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
324 return error;
325}
326
327Error
328ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000329{
330 Error error;
331 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
332 return error;
333}
334
335
336void
337ProcessKDP::DidAttach ()
338{
Greg Clayton5160ce52013-03-27 23:08:40 +0000339 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000340 if (log)
Johnny Chen54cb8f82011-10-11 21:17:10 +0000341 log->Printf ("ProcessKDP::DidAttach()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000342 if (GetID() != LLDB_INVALID_PROCESS_ID)
343 {
344 // TODO: figure out the register context that we will use
345 }
346}
347
Jason Molenda5e8534e2012-10-03 01:29:34 +0000348addr_t
349ProcessKDP::GetImageInfoAddress()
350{
351 return m_kernel_load_addr;
352}
353
354lldb_private::DynamicLoader *
355ProcessKDP::GetDynamicLoader ()
356{
357 if (m_dyld_ap.get() == NULL)
358 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
359 return m_dyld_ap.get();
360}
361
Greg Claytonf9765ac2011-07-15 03:27:12 +0000362Error
363ProcessKDP::WillResume ()
364{
365 return Error();
366}
367
368Error
369ProcessKDP::DoResume ()
370{
371 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000372 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000373 // Only start the async thread if we try to do any process control
374 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
375 StartAsyncThread ();
376
Greg Clayton97d5cf02012-09-25 02:40:06 +0000377 bool resume = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000378
Greg Clayton97d5cf02012-09-25 02:40:06 +0000379 // With KDP there is only one thread we can tell what to do
Greg Clayton1b7746e2013-05-04 01:38:48 +0000380 ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
381
Greg Clayton97d5cf02012-09-25 02:40:06 +0000382 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000383 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000384 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton7925fbb2012-09-21 16:31:20 +0000385 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000386 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000387 case eStateSuspended:
388 // Nothing to do here when a thread will stay suspended
389 // we just leave the CPU mask bit set to zero for the thread
390 break;
391
392 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000393 {
394 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
395
396 if (reg_ctx_sp)
397 {
398 reg_ctx_sp->HardwareSingleStep (true);
399 resume = true;
400 }
401 else
402 {
403 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
404 }
405 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000406 break;
407
Greg Clayton7925fbb2012-09-21 16:31:20 +0000408 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000409 {
410 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
411
412 if (reg_ctx_sp)
413 {
414 reg_ctx_sp->HardwareSingleStep (false);
415 resume = true;
416 }
417 else
418 {
419 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
420 }
421 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000422 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000423
Greg Clayton7925fbb2012-09-21 16:31:20 +0000424 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000425 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000426 assert (!"invalid thread resume state");
427 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000428 }
429 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000430
431 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000432 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000433 if (log)
434 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000435
Greg Clayton97d5cf02012-09-25 02:40:06 +0000436 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000437 {
438 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
439 SetPrivateState(eStateRunning);
440 }
441 else
442 error.SetErrorString ("KDP resume failed");
443 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000444 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000445 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000446 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000447 }
448
Greg Claytonf9765ac2011-07-15 03:27:12 +0000449 return error;
450}
451
Greg Clayton97d5cf02012-09-25 02:40:06 +0000452lldb::ThreadSP
Greg Clayton1b7746e2013-05-04 01:38:48 +0000453ProcessKDP::GetKernelThread()
Greg Clayton97d5cf02012-09-25 02:40:06 +0000454{
455 // KDP only tells us about one thread/core. Any other threads will usually
456 // be the ones that are read from memory by the OS plug-ins.
Greg Clayton1b7746e2013-05-04 01:38:48 +0000457
458 ThreadSP thread_sp (m_kernel_thread_wp.lock());
Greg Clayton97d5cf02012-09-25 02:40:06 +0000459 if (!thread_sp)
Greg Clayton1b7746e2013-05-04 01:38:48 +0000460 {
461 thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
462 m_kernel_thread_wp = thread_sp;
463 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000464 return thread_sp;
465}
466
467
468
469
Greg Clayton9fc13552012-04-10 00:18:59 +0000470bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000471ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000472{
473 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000474 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000475 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000476 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000477
Greg Clayton39da3ef2013-04-11 22:23:34 +0000478 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000479 // indivudually, there is really only one. Lets call this thread 1.
Jason Molenda513db4d2013-05-04 05:51:02 +0000480 ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
Greg Clayton1b7746e2013-05-04 01:38:48 +0000481 if (!thread_sp)
482 thread_sp = GetKernelThread ();
483 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000484
Greg Clayton9fc13552012-04-10 00:18:59 +0000485 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000486}
487
Greg Claytonf9765ac2011-07-15 03:27:12 +0000488void
489ProcessKDP::RefreshStateAfterStop ()
490{
491 // Let all threads recover from stopping and do any clean up based
492 // on the previous thread state (if any).
493 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000494}
495
496Error
497ProcessKDP::DoHalt (bool &caused_stop)
498{
499 Error error;
500
Greg Clayton97d5cf02012-09-25 02:40:06 +0000501 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000502 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000503 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000504 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000505 // If we are attemping to destroy, we need to not return an error to
506 // Halt or DoDestroy won't get called.
507 // We are also currently running, so send a process stopped event
508 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000509 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000510 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000511 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000512 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000513 }
514 }
515 return error;
516}
517
518Error
Jim Inghamacff8952013-05-02 00:27:30 +0000519ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000520{
521 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000522 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000523 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000524 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000525
Greg Clayton97d5cf02012-09-25 02:40:06 +0000526 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000527 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000528 // We are running and we can't interrupt a running kernel, so we need
529 // to just close the connection to the kernel and hope for the best
530 }
531 else
532 {
533 DisableAllBreakpointSites ();
534
535 m_thread_list.DiscardThreadPlans();
536
Jim Inghamacff8952013-05-02 00:27:30 +0000537 // If we are going to keep the target stopped, then don't send the disconnect message.
538 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000539 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000540
541 m_comm.SendRequestDisconnect();
542
543 size_t response_size = m_comm.Disconnect ();
544 if (log)
545 {
546 if (response_size)
547 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
548 else
549 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
550 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000551 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000552 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000553 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000554 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000555
556 SetPrivateState (eStateDetached);
557 ResumePrivateStateThread();
558
559 //KillDebugserverProcess ();
560 return error;
561}
562
563Error
564ProcessKDP::DoDestroy ()
565{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000566 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000567 bool keep_stopped = false;
568 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000569}
570
571//------------------------------------------------------------------
572// Process Queries
573//------------------------------------------------------------------
574
575bool
576ProcessKDP::IsAlive ()
577{
578 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
579}
580
581//------------------------------------------------------------------
582// Process Memory
583//------------------------------------------------------------------
584size_t
585ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
586{
Greg Claytona63d08c2011-07-19 03:57:15 +0000587 if (m_comm.IsConnected())
588 return m_comm.SendRequestReadMemory (addr, buf, size, error);
589 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000590 return 0;
591}
592
593size_t
594ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
595{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000596 if (m_comm.IsConnected())
597 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
598 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000599 return 0;
600}
601
602lldb::addr_t
603ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
604{
605 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
606 return LLDB_INVALID_ADDRESS;
607}
608
609Error
610ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
611{
612 Error error;
613 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
614 return error;
615}
616
617Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000618ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000619{
Greg Clayton07e66e32011-07-20 03:41:06 +0000620 if (m_comm.LocalBreakpointsAreSupported ())
621 {
622 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000623 if (!bp_site->IsEnabled())
624 {
625 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
626 {
627 bp_site->SetEnabled(true);
628 bp_site->SetType (BreakpointSite::eExternal);
629 }
630 else
631 {
632 error.SetErrorString ("KDP set breakpoint failed");
633 }
634 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000635 return error;
636 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000637 return EnableSoftwareBreakpoint (bp_site);
638}
639
640Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000641ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000642{
Greg Clayton07e66e32011-07-20 03:41:06 +0000643 if (m_comm.LocalBreakpointsAreSupported ())
644 {
645 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000646 if (bp_site->IsEnabled())
647 {
648 BreakpointSite::Type bp_type = bp_site->GetType();
649 if (bp_type == BreakpointSite::eExternal)
650 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000651 if (m_destroy_in_process && m_comm.IsRunning())
652 {
653 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000654 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000655 }
Greg Clayton5b882162011-07-21 01:12:01 +0000656 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000657 {
658 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
659 bp_site->SetEnabled(false);
660 else
661 error.SetErrorString ("KDP remove breakpoint failed");
662 }
Greg Clayton5b882162011-07-21 01:12:01 +0000663 }
664 else
665 {
666 error = DisableSoftwareBreakpoint (bp_site);
667 }
668 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000669 return error;
670 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000671 return DisableSoftwareBreakpoint (bp_site);
672}
673
674Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000675ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000676{
677 Error error;
678 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
679 return error;
680}
681
682Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000683ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000684{
685 Error error;
686 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
687 return error;
688}
689
690void
691ProcessKDP::Clear()
692{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000693 m_thread_list.Clear();
694}
695
696Error
697ProcessKDP::DoSignal (int signo)
698{
699 Error error;
700 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
701 return error;
702}
703
704void
705ProcessKDP::Initialize()
706{
707 static bool g_initialized = false;
708
709 if (g_initialized == false)
710 {
711 g_initialized = true;
712 PluginManager::RegisterPlugin (GetPluginNameStatic(),
713 GetPluginDescriptionStatic(),
714 CreateInstance);
715
716 Log::Callbacks log_callbacks = {
717 ProcessKDPLog::DisableLog,
718 ProcessKDPLog::EnableLog,
719 ProcessKDPLog::ListLogCategories
720 };
721
722 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
723 }
724}
725
726bool
727ProcessKDP::StartAsyncThread ()
728{
Greg Clayton5160ce52013-03-27 23:08:40 +0000729 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000730
731 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000732 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000733
Greg Clayton7925fbb2012-09-21 16:31:20 +0000734 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
735 return true;
736
Greg Claytonf9765ac2011-07-15 03:27:12 +0000737 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
738 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
739}
740
741void
742ProcessKDP::StopAsyncThread ()
743{
Greg Clayton5160ce52013-03-27 23:08:40 +0000744 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000745
746 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000747 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000748
749 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
750
751 // Stop the stdio thread
752 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
753 {
754 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000755 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000756 }
757}
758
759
760void *
761ProcessKDP::AsyncThread (void *arg)
762{
763 ProcessKDP *process = (ProcessKDP*) arg;
764
Greg Clayton7925fbb2012-09-21 16:31:20 +0000765 const lldb::pid_t pid = process->GetID();
766
Greg Clayton5160ce52013-03-27 23:08:40 +0000767 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000768 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000769 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000770
771 Listener listener ("ProcessKDP::AsyncThread");
772 EventSP event_sp;
773 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
774 eBroadcastBitAsyncThreadShouldExit;
775
Greg Clayton7925fbb2012-09-21 16:31:20 +0000776
Greg Claytonf9765ac2011-07-15 03:27:12 +0000777 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
778 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000779 bool done = false;
780 while (!done)
781 {
782 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000783 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000784 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000785 if (listener.WaitForEvent (NULL, event_sp))
786 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000787 uint32_t event_type = event_sp->GetType();
788 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000789 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000790 pid,
791 event_type);
792
793 // When we are running, poll for 1 second to try and get an exception
794 // to indicate the process has stopped. If we don't get one, check to
795 // make sure no one asked us to exit
796 bool is_running = false;
797 DataExtractor exc_reply_packet;
798 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000799 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000800 switch (event_type)
801 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000802 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000803 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000804 is_running = true;
805 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000806 {
Greg Clayton1b7746e2013-05-04 01:38:48 +0000807 ThreadSP thread_sp (process->GetKernelThread());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000808 if (thread_sp)
809 {
810 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
811 if (reg_ctx_sp)
812 reg_ctx_sp->InvalidateAllRegisters();
813 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
814 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000815
Greg Clayton7925fbb2012-09-21 16:31:20 +0000816 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000817 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000818 process->SetPrivateState(eStateStopped);
819 }
820 else
821 {
822 // Check to see if we are supposed to exit. There is no way to
823 // interrupt a running kernel, so all we can do is wait for an
824 // exception or detach...
825 if (listener.GetNextEvent(event_sp))
826 {
827 // We got an event, go through the loop again
828 event_type = event_sp->GetType();
829 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000830 }
831 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000832 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000833
Greg Clayton7925fbb2012-09-21 16:31:20 +0000834 case eBroadcastBitAsyncThreadShouldExit:
835 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000836 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000837 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000838 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000839 is_running = false;
840 break;
841
842 default:
843 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000844 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000845 pid,
846 event_type);
847 done = true;
848 is_running = false;
849 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000850 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000851 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000852 }
853 else
854 {
855 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000856 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000857 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000858 done = true;
859 }
860 }
861 }
862
863 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000864 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000865 arg,
866 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000867
868 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
869 return NULL;
870}
871
872
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000873class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
874{
875private:
876
877 OptionGroupOptions m_option_group;
878 OptionGroupUInt64 m_command_byte;
879 OptionGroupString m_packet_data;
880
881 virtual Options *
882 GetOptions ()
883 {
884 return &m_option_group;
885 }
886
887
888public:
889 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
890 CommandObjectParsed (interpreter,
891 "process plugin packet send",
892 "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. ",
893 NULL),
894 m_option_group (interpreter),
895 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),
896 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)
897 {
898 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
899 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
900 m_option_group.Finalize();
901 }
902
903 ~CommandObjectProcessKDPPacketSend ()
904 {
905 }
906
907 bool
908 DoExecute (Args& command, CommandReturnObject &result)
909 {
910 const size_t argc = command.GetArgumentCount();
911 if (argc == 0)
912 {
913 if (!m_command_byte.GetOptionValue().OptionWasSet())
914 {
915 result.AppendError ("the --command option must be set to a valid command byte");
916 result.SetStatus (eReturnStatusFailed);
917 }
918 else
919 {
920 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
921 if (command_byte > 0 && command_byte <= UINT8_MAX)
922 {
923 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
924 if (process)
925 {
926 const StateType state = process->GetState();
927
928 if (StateIsStoppedState (state, true))
929 {
930 std::vector<uint8_t> payload_bytes;
931 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
932 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
933 {
934 StringExtractor extractor(ascii_hex_bytes_cstr);
935 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
936 if (ascii_hex_bytes_cstr_len & 1)
937 {
938 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
939 result.SetStatus (eReturnStatusFailed);
940 return false;
941 }
942 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
943 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
944 {
945 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
946 result.SetStatus (eReturnStatusFailed);
947 return false;
948 }
949 }
950 Error error;
951 DataExtractor reply;
952 process->GetCommunication().SendRawRequest (command_byte,
953 payload_bytes.empty() ? NULL : payload_bytes.data(),
954 payload_bytes.size(),
955 reply,
956 error);
957
958 if (error.Success())
959 {
960 // Copy the binary bytes into a hex ASCII string for the result
961 StreamString packet;
962 packet.PutBytesAsRawHex8(reply.GetDataStart(),
963 reply.GetByteSize(),
964 lldb::endian::InlHostByteOrder(),
965 lldb::endian::InlHostByteOrder());
966 result.AppendMessage(packet.GetString().c_str());
967 result.SetStatus (eReturnStatusSuccessFinishResult);
968 return true;
969 }
970 else
971 {
972 const char *error_cstr = error.AsCString();
973 if (error_cstr && error_cstr[0])
974 result.AppendError (error_cstr);
975 else
976 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
977 result.SetStatus (eReturnStatusFailed);
978 return false;
979 }
980 }
981 else
982 {
983 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
984 result.SetStatus (eReturnStatusFailed);
985 }
986 }
987 else
988 {
989 result.AppendError ("invalid process");
990 result.SetStatus (eReturnStatusFailed);
991 }
992 }
993 else
994 {
Daniel Malead01b2952012-11-29 21:49:15 +0000995 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000996 result.SetStatus (eReturnStatusFailed);
997 }
998 }
999 }
1000 else
1001 {
1002 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
1003 result.SetStatus (eReturnStatusFailed);
1004 }
1005 return false;
1006 }
1007};
1008
1009class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1010{
1011private:
1012
1013public:
1014 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1015 CommandObjectMultiword (interpreter,
1016 "process plugin packet",
1017 "Commands that deal with KDP remote packets.",
1018 NULL)
1019 {
1020 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1021 }
1022
1023 ~CommandObjectProcessKDPPacket ()
1024 {
1025 }
1026};
1027
1028class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1029{
1030public:
1031 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1032 CommandObjectMultiword (interpreter,
1033 "process plugin",
1034 "A set of commands for operating on a ProcessKDP process.",
1035 "process plugin <subcommand> [<subcommand-options>]")
1036 {
1037 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1038 }
1039
1040 ~CommandObjectMultiwordProcessKDP ()
1041 {
1042 }
1043};
1044
1045CommandObject *
1046ProcessKDP::GetPluginCommandObject()
1047{
1048 if (!m_command_sp)
1049 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1050 return m_command_sp.get();
1051}
1052