blob: 8bb853c085860ee965a621b7d88ec2fb8b3b4da9 [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
47const char *
48ProcessKDP::GetPluginNameStatic()
49{
50 return "kdp-remote";
51}
52
53const char *
54ProcessKDP::GetPluginDescriptionStatic()
55{
56 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
57}
58
59void
60ProcessKDP::Terminate()
61{
62 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
63}
64
65
Greg Claytonc3776bf2012-02-09 06:16:32 +000066lldb::ProcessSP
67ProcessKDP::CreateInstance (Target &target,
68 Listener &listener,
69 const FileSpec *crash_file_path)
Greg Claytonf9765ac2011-07-15 03:27:12 +000070{
Greg Claytonc3776bf2012-02-09 06:16:32 +000071 lldb::ProcessSP process_sp;
72 if (crash_file_path == NULL)
73 process_sp.reset(new ProcessKDP (target, listener));
74 return process_sp;
Greg Claytonf9765ac2011-07-15 03:27:12 +000075}
76
77bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000078ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Claytonf9765ac2011-07-15 03:27:12 +000079{
Greg Clayton596ed242011-10-21 21:41:45 +000080 if (plugin_specified_by_name)
81 return true;
82
Greg Claytonf9765ac2011-07-15 03:27:12 +000083 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +000084 Module *exe_module = target.GetExecutableModulePointer();
85 if (exe_module)
Greg Claytonf9765ac2011-07-15 03:27:12 +000086 {
87 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +000088 switch (triple_ref.getOS())
Greg Claytonf9765ac2011-07-15 03:27:12 +000089 {
Greg Clayton70512312012-05-08 01:45:38 +000090 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
91 case llvm::Triple::MacOSX: // For desktop targets
92 case llvm::Triple::IOS: // For arm targets
93 if (triple_ref.getVendor() == llvm::Triple::Apple)
94 {
95 ObjectFile *exe_objfile = exe_module->GetObjectFile();
96 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
97 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
98 return true;
99 }
100 break;
101
102 default:
103 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000104 }
105 }
Greg Clayton596ed242011-10-21 21:41:45 +0000106 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000107}
108
109//----------------------------------------------------------------------
110// ProcessKDP constructor
111//----------------------------------------------------------------------
112ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
113 Process (target, listener),
114 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000115 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton97d5cf02012-09-25 02:40:06 +0000116 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000117 m_dyld_plugin_name (),
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000118 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000119 m_command_sp()
Greg Claytonf9765ac2011-07-15 03:27:12 +0000120{
Greg Clayton7925fbb2012-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 Claytonf9765ac2011-07-15 03:27:12 +0000123}
124
125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128ProcessKDP::~ProcessKDP()
129{
130 Clear();
Greg Claytone24c4ac2011-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 Claytonf9765ac2011-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 Molenda4bd4e7e2012-09-29 04:02:01 +0000184ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000185{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000186 Error error;
Greg Clayton7925fbb2012-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 Clayton3a29bdb2011-07-17 20:36:25 +0000193 if (remote_url == NULL || remote_url[0] == '\0')
Greg Clayton7925fbb2012-09-21 16:31:20 +0000194 {
195 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
196 return error;
197 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000198
Greg Clayton7b0992d2013-04-18 22:45:39 +0000199 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000200 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 Molenda4bd4e7e2012-09-29 04:02:01 +0000231
Jason Molenda840f12c2012-10-25 00:25:13 +0000232 /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */
233 /* An EFI kdp session has neither UUID nor load address. */
234
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000235 UUID kernel_uuid = m_comm.GetUUID ();
236 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000237
Jason Molenda840f12c2012-10-25 00:25:13 +0000238 if (m_comm.RemoteIsEFI ())
239 {
240 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
241 }
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000242 else
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000243 {
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000244 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
245 {
246 m_kernel_load_addr = kernel_load_addr;
247 }
Jason Molenda5e8534e2012-10-03 01:29:34 +0000248 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
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000380 ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000381 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000382 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000383 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton7925fbb2012-09-21 16:31:20 +0000384 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000385 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000386 case eStateSuspended:
387 // Nothing to do here when a thread will stay suspended
388 // we just leave the CPU mask bit set to zero for the thread
389 break;
390
391 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000392 {
393 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
394
395 if (reg_ctx_sp)
396 {
397 reg_ctx_sp->HardwareSingleStep (true);
398 resume = true;
399 }
400 else
401 {
402 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
403 }
404 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000405 break;
406
Greg Clayton7925fbb2012-09-21 16:31:20 +0000407 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000408 {
409 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
410
411 if (reg_ctx_sp)
412 {
413 reg_ctx_sp->HardwareSingleStep (false);
414 resume = true;
415 }
416 else
417 {
418 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
419 }
420 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000421 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000422
Greg Clayton7925fbb2012-09-21 16:31:20 +0000423 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000424 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000425 assert (!"invalid thread resume state");
426 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000427 }
428 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000429
430 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000431 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000432 if (log)
433 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000434
Greg Clayton97d5cf02012-09-25 02:40:06 +0000435 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000436 {
437 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
438 SetPrivateState(eStateRunning);
439 }
440 else
441 error.SetErrorString ("KDP resume failed");
442 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000443 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000444 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000445 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000446 }
447
Greg Claytonf9765ac2011-07-15 03:27:12 +0000448 return error;
449}
450
Greg Clayton97d5cf02012-09-25 02:40:06 +0000451lldb::ThreadSP
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000452ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton97d5cf02012-09-25 02:40:06 +0000453{
454 // KDP only tells us about one thread/core. Any other threads will usually
455 // be the ones that are read from memory by the OS plug-ins.
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000456 const lldb::tid_t kernel_tid = 1;
457 ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000458 if (!thread_sp)
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000459 thread_sp.reset(new ThreadKDP (*this, kernel_tid));
460 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000461 return thread_sp;
462}
463
464
465
466
Greg Clayton9fc13552012-04-10 00:18:59 +0000467bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000468ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000469{
470 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000471 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000472 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000473 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000474
Greg Clayton39da3ef2013-04-11 22:23:34 +0000475 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000476 // indivudually, there is really only one. Lets call this thread 1.
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000477 GetKernelThread (old_thread_list, new_thread_list);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000478
Greg Clayton9fc13552012-04-10 00:18:59 +0000479 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000480}
481
Greg Claytonf9765ac2011-07-15 03:27:12 +0000482void
483ProcessKDP::RefreshStateAfterStop ()
484{
485 // Let all threads recover from stopping and do any clean up based
486 // on the previous thread state (if any).
487 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000488}
489
490Error
491ProcessKDP::DoHalt (bool &caused_stop)
492{
493 Error error;
494
Greg Clayton97d5cf02012-09-25 02:40:06 +0000495 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000496 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000497 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000498 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000499 // If we are attemping to destroy, we need to not return an error to
500 // Halt or DoDestroy won't get called.
501 // We are also currently running, so send a process stopped event
502 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000503 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000504 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000505 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000506 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000507 }
508 }
509 return error;
510}
511
512Error
Jim Inghamacff8952013-05-02 00:27:30 +0000513ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000514{
515 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000516 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000517 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000518 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000519
Greg Clayton97d5cf02012-09-25 02:40:06 +0000520 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000521 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000522 // We are running and we can't interrupt a running kernel, so we need
523 // to just close the connection to the kernel and hope for the best
524 }
525 else
526 {
527 DisableAllBreakpointSites ();
528
529 m_thread_list.DiscardThreadPlans();
530
Jim Inghamacff8952013-05-02 00:27:30 +0000531 // If we are going to keep the target stopped, then don't send the disconnect message.
532 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000533 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000534
535 m_comm.SendRequestDisconnect();
536
537 size_t response_size = m_comm.Disconnect ();
538 if (log)
539 {
540 if (response_size)
541 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
542 else
543 log->PutCString ("ProcessKDP::DoDetach() detach packet send failed");
544 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000545 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000546 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000547 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000548 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000549
550 SetPrivateState (eStateDetached);
551 ResumePrivateStateThread();
552
553 //KillDebugserverProcess ();
554 return error;
555}
556
557Error
558ProcessKDP::DoDestroy ()
559{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000560 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000561 bool keep_stopped = false;
562 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000563}
564
565//------------------------------------------------------------------
566// Process Queries
567//------------------------------------------------------------------
568
569bool
570ProcessKDP::IsAlive ()
571{
572 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
573}
574
575//------------------------------------------------------------------
576// Process Memory
577//------------------------------------------------------------------
578size_t
579ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
580{
Greg Claytona63d08c2011-07-19 03:57:15 +0000581 if (m_comm.IsConnected())
582 return m_comm.SendRequestReadMemory (addr, buf, size, error);
583 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000584 return 0;
585}
586
587size_t
588ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
589{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000590 if (m_comm.IsConnected())
591 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
592 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000593 return 0;
594}
595
596lldb::addr_t
597ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
598{
599 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
600 return LLDB_INVALID_ADDRESS;
601}
602
603Error
604ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
605{
606 Error error;
607 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
608 return error;
609}
610
611Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000612ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000613{
Greg Clayton07e66e32011-07-20 03:41:06 +0000614 if (m_comm.LocalBreakpointsAreSupported ())
615 {
616 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000617 if (!bp_site->IsEnabled())
618 {
619 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
620 {
621 bp_site->SetEnabled(true);
622 bp_site->SetType (BreakpointSite::eExternal);
623 }
624 else
625 {
626 error.SetErrorString ("KDP set breakpoint failed");
627 }
628 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000629 return error;
630 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000631 return EnableSoftwareBreakpoint (bp_site);
632}
633
634Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000635ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000636{
Greg Clayton07e66e32011-07-20 03:41:06 +0000637 if (m_comm.LocalBreakpointsAreSupported ())
638 {
639 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000640 if (bp_site->IsEnabled())
641 {
642 BreakpointSite::Type bp_type = bp_site->GetType();
643 if (bp_type == BreakpointSite::eExternal)
644 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000645 if (m_destroy_in_process && m_comm.IsRunning())
646 {
647 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000648 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000649 }
Greg Clayton5b882162011-07-21 01:12:01 +0000650 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000651 {
652 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
653 bp_site->SetEnabled(false);
654 else
655 error.SetErrorString ("KDP remove breakpoint failed");
656 }
Greg Clayton5b882162011-07-21 01:12:01 +0000657 }
658 else
659 {
660 error = DisableSoftwareBreakpoint (bp_site);
661 }
662 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000663 return error;
664 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000665 return DisableSoftwareBreakpoint (bp_site);
666}
667
668Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000669ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000670{
671 Error error;
672 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
673 return error;
674}
675
676Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000677ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000678{
679 Error error;
680 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
681 return error;
682}
683
684void
685ProcessKDP::Clear()
686{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000687 m_thread_list.Clear();
688}
689
690Error
691ProcessKDP::DoSignal (int signo)
692{
693 Error error;
694 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
695 return error;
696}
697
698void
699ProcessKDP::Initialize()
700{
701 static bool g_initialized = false;
702
703 if (g_initialized == false)
704 {
705 g_initialized = true;
706 PluginManager::RegisterPlugin (GetPluginNameStatic(),
707 GetPluginDescriptionStatic(),
708 CreateInstance);
709
710 Log::Callbacks log_callbacks = {
711 ProcessKDPLog::DisableLog,
712 ProcessKDPLog::EnableLog,
713 ProcessKDPLog::ListLogCategories
714 };
715
716 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
717 }
718}
719
720bool
721ProcessKDP::StartAsyncThread ()
722{
Greg Clayton5160ce52013-03-27 23:08:40 +0000723 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000724
725 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000726 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000727
Greg Clayton7925fbb2012-09-21 16:31:20 +0000728 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
729 return true;
730
Greg Claytonf9765ac2011-07-15 03:27:12 +0000731 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
732 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
733}
734
735void
736ProcessKDP::StopAsyncThread ()
737{
Greg Clayton5160ce52013-03-27 23:08:40 +0000738 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000739
740 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000741 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000742
743 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
744
745 // Stop the stdio thread
746 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
747 {
748 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000749 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000750 }
751}
752
753
754void *
755ProcessKDP::AsyncThread (void *arg)
756{
757 ProcessKDP *process = (ProcessKDP*) arg;
758
Greg Clayton7925fbb2012-09-21 16:31:20 +0000759 const lldb::pid_t pid = process->GetID();
760
Greg Clayton5160ce52013-03-27 23:08:40 +0000761 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000762 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000763 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000764
765 Listener listener ("ProcessKDP::AsyncThread");
766 EventSP event_sp;
767 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
768 eBroadcastBitAsyncThreadShouldExit;
769
Greg Clayton7925fbb2012-09-21 16:31:20 +0000770
Greg Claytonf9765ac2011-07-15 03:27:12 +0000771 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
772 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000773 bool done = false;
774 while (!done)
775 {
776 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000777 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000778 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000779 if (listener.WaitForEvent (NULL, event_sp))
780 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000781 uint32_t event_type = event_sp->GetType();
782 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000783 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000784 pid,
785 event_type);
786
787 // When we are running, poll for 1 second to try and get an exception
788 // to indicate the process has stopped. If we don't get one, check to
789 // make sure no one asked us to exit
790 bool is_running = false;
791 DataExtractor exc_reply_packet;
792 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000793 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000794 switch (event_type)
795 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000796 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000797 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000798 is_running = true;
799 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000800 {
Ashok Thirumurthif5b92402013-05-07 15:01:34 +0000801 ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
Greg Clayton1afa68e2013-04-02 20:32:37 +0000802 if (thread_sp)
803 {
804 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
805 if (reg_ctx_sp)
806 reg_ctx_sp->InvalidateAllRegisters();
807 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
808 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000809
Greg Clayton7925fbb2012-09-21 16:31:20 +0000810 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000811 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000812 process->SetPrivateState(eStateStopped);
813 }
814 else
815 {
816 // Check to see if we are supposed to exit. There is no way to
817 // interrupt a running kernel, so all we can do is wait for an
818 // exception or detach...
819 if (listener.GetNextEvent(event_sp))
820 {
821 // We got an event, go through the loop again
822 event_type = event_sp->GetType();
823 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000824 }
825 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000826 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000827
Greg Clayton7925fbb2012-09-21 16:31:20 +0000828 case eBroadcastBitAsyncThreadShouldExit:
829 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000830 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000831 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000832 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000833 is_running = false;
834 break;
835
836 default:
837 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000838 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000839 pid,
840 event_type);
841 done = true;
842 is_running = false;
843 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000844 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000845 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000846 }
847 else
848 {
849 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000850 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000851 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000852 done = true;
853 }
854 }
855 }
856
857 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000858 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000859 arg,
860 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000861
862 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
863 return NULL;
864}
865
866
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000867class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
868{
869private:
870
871 OptionGroupOptions m_option_group;
872 OptionGroupUInt64 m_command_byte;
873 OptionGroupString m_packet_data;
874
875 virtual Options *
876 GetOptions ()
877 {
878 return &m_option_group;
879 }
880
881
882public:
883 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
884 CommandObjectParsed (interpreter,
885 "process plugin packet send",
886 "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. ",
887 NULL),
888 m_option_group (interpreter),
889 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),
890 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)
891 {
892 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
893 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
894 m_option_group.Finalize();
895 }
896
897 ~CommandObjectProcessKDPPacketSend ()
898 {
899 }
900
901 bool
902 DoExecute (Args& command, CommandReturnObject &result)
903 {
904 const size_t argc = command.GetArgumentCount();
905 if (argc == 0)
906 {
907 if (!m_command_byte.GetOptionValue().OptionWasSet())
908 {
909 result.AppendError ("the --command option must be set to a valid command byte");
910 result.SetStatus (eReturnStatusFailed);
911 }
912 else
913 {
914 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
915 if (command_byte > 0 && command_byte <= UINT8_MAX)
916 {
917 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
918 if (process)
919 {
920 const StateType state = process->GetState();
921
922 if (StateIsStoppedState (state, true))
923 {
924 std::vector<uint8_t> payload_bytes;
925 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
926 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
927 {
928 StringExtractor extractor(ascii_hex_bytes_cstr);
929 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
930 if (ascii_hex_bytes_cstr_len & 1)
931 {
932 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
933 result.SetStatus (eReturnStatusFailed);
934 return false;
935 }
936 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
937 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
938 {
939 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
940 result.SetStatus (eReturnStatusFailed);
941 return false;
942 }
943 }
944 Error error;
945 DataExtractor reply;
946 process->GetCommunication().SendRawRequest (command_byte,
947 payload_bytes.empty() ? NULL : payload_bytes.data(),
948 payload_bytes.size(),
949 reply,
950 error);
951
952 if (error.Success())
953 {
954 // Copy the binary bytes into a hex ASCII string for the result
955 StreamString packet;
956 packet.PutBytesAsRawHex8(reply.GetDataStart(),
957 reply.GetByteSize(),
958 lldb::endian::InlHostByteOrder(),
959 lldb::endian::InlHostByteOrder());
960 result.AppendMessage(packet.GetString().c_str());
961 result.SetStatus (eReturnStatusSuccessFinishResult);
962 return true;
963 }
964 else
965 {
966 const char *error_cstr = error.AsCString();
967 if (error_cstr && error_cstr[0])
968 result.AppendError (error_cstr);
969 else
970 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
971 result.SetStatus (eReturnStatusFailed);
972 return false;
973 }
974 }
975 else
976 {
977 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
978 result.SetStatus (eReturnStatusFailed);
979 }
980 }
981 else
982 {
983 result.AppendError ("invalid process");
984 result.SetStatus (eReturnStatusFailed);
985 }
986 }
987 else
988 {
Daniel Malead01b2952012-11-29 21:49:15 +0000989 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000990 result.SetStatus (eReturnStatusFailed);
991 }
992 }
993 }
994 else
995 {
996 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
997 result.SetStatus (eReturnStatusFailed);
998 }
999 return false;
1000 }
1001};
1002
1003class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1004{
1005private:
1006
1007public:
1008 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1009 CommandObjectMultiword (interpreter,
1010 "process plugin packet",
1011 "Commands that deal with KDP remote packets.",
1012 NULL)
1013 {
1014 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1015 }
1016
1017 ~CommandObjectProcessKDPPacket ()
1018 {
1019 }
1020};
1021
1022class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1023{
1024public:
1025 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1026 CommandObjectMultiword (interpreter,
1027 "process plugin",
1028 "A set of commands for operating on a ProcessKDP process.",
1029 "process plugin <subcommand> [<subcommand-options>]")
1030 {
1031 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1032 }
1033
1034 ~CommandObjectMultiwordProcessKDP ()
1035 {
1036 }
1037};
1038
1039CommandObject *
1040ProcessKDP::GetPluginCommandObject()
1041{
1042 if (!m_command_sp)
1043 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1044 return m_command_sp.get();
1045}
1046