blob: c78aa42da206f18489ac2293b9d5da0cb0f92b78 [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
Andrew Kaylorba4e61d2013-05-07 18:35:34 +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),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +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 }
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000245 else
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000246 {
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000247 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
248 {
249 m_kernel_load_addr = kernel_load_addr;
250 }
Jason Molenda5e8534e2012-10-03 01:29:34 +0000251 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000252 }
253
Greg Clayton97d5cf02012-09-25 02:40:06 +0000254 // Set the thread ID
255 UpdateThreadListIfNeeded ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000256 SetID (1);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000257 GetThreadList ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000258 SetPrivateState (eStateStopped);
Greg Clayton07e66e32011-07-20 03:41:06 +0000259 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
260 if (async_strm_sp)
261 {
Greg Clayton5b882162011-07-21 01:12:01 +0000262 const char *cstr;
263 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton07e66e32011-07-20 03:41:06 +0000264 {
Greg Clayton5b882162011-07-21 01:12:01 +0000265 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton07e66e32011-07-20 03:41:06 +0000266 async_strm_sp->Flush();
267 }
Greg Clayton5b882162011-07-21 01:12:01 +0000268// if ((cstr = m_comm.GetImagePath ()) != NULL)
269// {
270// async_strm_sp->Printf ("Image Path: %s\n", cstr);
271// async_strm_sp->Flush();
272// }
Greg Clayton07e66e32011-07-20 03:41:06 +0000273 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000274 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000275 else
276 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000277 error.SetErrorString("KDP_REATTACH failed");
278 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000279 }
280 else
281 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000282 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000283 }
284 }
285 else
286 {
287 error.SetErrorString("invalid reply port from UDP connection");
288 }
289 }
290 else
291 {
292 if (error.Success())
293 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
294 }
295 if (error.Fail())
296 m_comm.Disconnect();
297
Greg Claytonf9765ac2011-07-15 03:27:12 +0000298 return error;
299}
300
301//----------------------------------------------------------------------
302// Process Control
303//----------------------------------------------------------------------
304Error
Greg Clayton982c9762011-11-03 21:22:33 +0000305ProcessKDP::DoLaunch (Module *exe_module,
306 const ProcessLaunchInfo &launch_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000307{
308 Error error;
309 error.SetErrorString ("launching not supported in kdp-remote plug-in");
310 return error;
311}
312
313
314Error
315ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
316{
317 Error error;
318 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
319 return error;
320}
321
Greg Claytonf9765ac2011-07-15 03:27:12 +0000322Error
Han Ming Ong84647042012-02-25 01:07:38 +0000323ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
324{
325 Error error;
326 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
327 return error;
328}
329
330Error
331ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000332{
333 Error error;
334 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
335 return error;
336}
337
338
339void
340ProcessKDP::DidAttach ()
341{
Greg Clayton5160ce52013-03-27 23:08:40 +0000342 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000343 if (log)
Johnny Chen54cb8f82011-10-11 21:17:10 +0000344 log->Printf ("ProcessKDP::DidAttach()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000345 if (GetID() != LLDB_INVALID_PROCESS_ID)
346 {
347 // TODO: figure out the register context that we will use
348 }
349}
350
Jason Molenda5e8534e2012-10-03 01:29:34 +0000351addr_t
352ProcessKDP::GetImageInfoAddress()
353{
354 return m_kernel_load_addr;
355}
356
357lldb_private::DynamicLoader *
358ProcessKDP::GetDynamicLoader ()
359{
360 if (m_dyld_ap.get() == NULL)
361 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
362 return m_dyld_ap.get();
363}
364
Greg Claytonf9765ac2011-07-15 03:27:12 +0000365Error
366ProcessKDP::WillResume ()
367{
368 return Error();
369}
370
371Error
372ProcessKDP::DoResume ()
373{
374 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000375 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000376 // Only start the async thread if we try to do any process control
377 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
378 StartAsyncThread ();
379
Greg Clayton97d5cf02012-09-25 02:40:06 +0000380 bool resume = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000381
Greg Clayton97d5cf02012-09-25 02:40:06 +0000382 // With KDP there is only one thread we can tell what to do
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000383 ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
384
Greg Clayton97d5cf02012-09-25 02:40:06 +0000385 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000386 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000387 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000388
389 if (log)
390 log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000391 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000392 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000393 case eStateSuspended:
394 // Nothing to do here when a thread will stay suspended
395 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000396 if (log)
397 log->Printf ("ProcessKDP::DoResume() = suspended???");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000398 break;
399
400 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000401 {
402 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
403
404 if (reg_ctx_sp)
405 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000406 if (log)
407 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
Greg Clayton1afa68e2013-04-02 20:32:37 +0000408 reg_ctx_sp->HardwareSingleStep (true);
409 resume = true;
410 }
411 else
412 {
413 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
414 }
415 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000416 break;
417
Greg Clayton7925fbb2012-09-21 16:31:20 +0000418 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000419 {
420 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
421
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000422 if (reg_ctx_sp)
423 {
424 if (log)
425 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
426 reg_ctx_sp->HardwareSingleStep (false);
427 resume = true;
428 }
429 else
430 {
431 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
432 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000433 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000434 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000435
Greg Clayton7925fbb2012-09-21 16:31:20 +0000436 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000437 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000438 assert (!"invalid thread resume state");
439 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000440 }
441 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000442
443 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000444 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000445 if (log)
446 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000447
Greg Clayton97d5cf02012-09-25 02:40:06 +0000448 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000449 {
450 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
451 SetPrivateState(eStateRunning);
452 }
453 else
454 error.SetErrorString ("KDP resume failed");
455 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000456 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000457 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000458 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000459 }
460
Greg Claytonf9765ac2011-07-15 03:27:12 +0000461 return error;
462}
463
Greg Clayton97d5cf02012-09-25 02:40:06 +0000464lldb::ThreadSP
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000465ProcessKDP::GetKernelThread()
Greg Clayton97d5cf02012-09-25 02:40:06 +0000466{
467 // KDP only tells us about one thread/core. Any other threads will usually
468 // be the ones that are read from memory by the OS plug-ins.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000469
470 ThreadSP thread_sp (m_kernel_thread_wp.lock());
Greg Clayton97d5cf02012-09-25 02:40:06 +0000471 if (!thread_sp)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000472 {
473 thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
474 m_kernel_thread_wp = thread_sp;
475 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000476 return thread_sp;
477}
478
479
480
481
Greg Clayton9fc13552012-04-10 00:18:59 +0000482bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000483ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000484{
485 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000486 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000487 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000488 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000489
Greg Clayton39da3ef2013-04-11 22:23:34 +0000490 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000491 // indivudually, there is really only one. Lets call this thread 1.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000492 ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
493 if (!thread_sp)
494 thread_sp = GetKernelThread ();
495 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000496
Greg Clayton9fc13552012-04-10 00:18:59 +0000497 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000498}
499
Greg Claytonf9765ac2011-07-15 03:27:12 +0000500void
501ProcessKDP::RefreshStateAfterStop ()
502{
503 // Let all threads recover from stopping and do any clean up based
504 // on the previous thread state (if any).
505 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000506}
507
508Error
509ProcessKDP::DoHalt (bool &caused_stop)
510{
511 Error error;
512
Greg Clayton97d5cf02012-09-25 02:40:06 +0000513 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000514 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000515 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000516 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000517 // If we are attemping to destroy, we need to not return an error to
518 // Halt or DoDestroy won't get called.
519 // We are also currently running, so send a process stopped event
520 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000521 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000522 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000523 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000524 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000525 }
526 }
527 return error;
528}
529
530Error
Jim Inghamacff8952013-05-02 00:27:30 +0000531ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000532{
533 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000534 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000535 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000536 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000537
Greg Clayton97d5cf02012-09-25 02:40:06 +0000538 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000539 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000540 // We are running and we can't interrupt a running kernel, so we need
541 // to just close the connection to the kernel and hope for the best
542 }
543 else
544 {
545 DisableAllBreakpointSites ();
546
547 m_thread_list.DiscardThreadPlans();
548
Jim Inghamacff8952013-05-02 00:27:30 +0000549 // If we are going to keep the target stopped, then don't send the disconnect message.
550 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000551 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000552 const bool success = m_comm.SendRequestDisconnect();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000553 if (log)
554 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000555 if (success)
556 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000557 else
Jim Ingham77e82d12013-05-09 00:05:35 +0000558 log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000559 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000560 m_comm.Disconnect ();
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000561 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000562 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000563 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000564 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000565
566 SetPrivateState (eStateDetached);
567 ResumePrivateStateThread();
568
569 //KillDebugserverProcess ();
570 return error;
571}
572
573Error
574ProcessKDP::DoDestroy ()
575{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000576 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000577 bool keep_stopped = false;
578 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000579}
580
581//------------------------------------------------------------------
582// Process Queries
583//------------------------------------------------------------------
584
585bool
586ProcessKDP::IsAlive ()
587{
588 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
589}
590
591//------------------------------------------------------------------
592// Process Memory
593//------------------------------------------------------------------
594size_t
595ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
596{
Greg Claytona63d08c2011-07-19 03:57:15 +0000597 if (m_comm.IsConnected())
598 return m_comm.SendRequestReadMemory (addr, buf, size, error);
599 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000600 return 0;
601}
602
603size_t
604ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
605{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000606 if (m_comm.IsConnected())
607 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
608 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000609 return 0;
610}
611
612lldb::addr_t
613ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
614{
615 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
616 return LLDB_INVALID_ADDRESS;
617}
618
619Error
620ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
621{
622 Error error;
623 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
624 return error;
625}
626
627Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000628ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000629{
Greg Clayton07e66e32011-07-20 03:41:06 +0000630 if (m_comm.LocalBreakpointsAreSupported ())
631 {
632 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000633 if (!bp_site->IsEnabled())
634 {
635 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
636 {
637 bp_site->SetEnabled(true);
638 bp_site->SetType (BreakpointSite::eExternal);
639 }
640 else
641 {
642 error.SetErrorString ("KDP set breakpoint failed");
643 }
644 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000645 return error;
646 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000647 return EnableSoftwareBreakpoint (bp_site);
648}
649
650Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000651ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000652{
Greg Clayton07e66e32011-07-20 03:41:06 +0000653 if (m_comm.LocalBreakpointsAreSupported ())
654 {
655 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000656 if (bp_site->IsEnabled())
657 {
658 BreakpointSite::Type bp_type = bp_site->GetType();
659 if (bp_type == BreakpointSite::eExternal)
660 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000661 if (m_destroy_in_process && m_comm.IsRunning())
662 {
663 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000664 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000665 }
Greg Clayton5b882162011-07-21 01:12:01 +0000666 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000667 {
668 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
669 bp_site->SetEnabled(false);
670 else
671 error.SetErrorString ("KDP remove breakpoint failed");
672 }
Greg Clayton5b882162011-07-21 01:12:01 +0000673 }
674 else
675 {
676 error = DisableSoftwareBreakpoint (bp_site);
677 }
678 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000679 return error;
680 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000681 return DisableSoftwareBreakpoint (bp_site);
682}
683
684Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000685ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000686{
687 Error error;
688 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
689 return error;
690}
691
692Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000693ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000694{
695 Error error;
696 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
697 return error;
698}
699
700void
701ProcessKDP::Clear()
702{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000703 m_thread_list.Clear();
704}
705
706Error
707ProcessKDP::DoSignal (int signo)
708{
709 Error error;
710 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
711 return error;
712}
713
714void
715ProcessKDP::Initialize()
716{
717 static bool g_initialized = false;
718
719 if (g_initialized == false)
720 {
721 g_initialized = true;
722 PluginManager::RegisterPlugin (GetPluginNameStatic(),
723 GetPluginDescriptionStatic(),
724 CreateInstance);
725
726 Log::Callbacks log_callbacks = {
727 ProcessKDPLog::DisableLog,
728 ProcessKDPLog::EnableLog,
729 ProcessKDPLog::ListLogCategories
730 };
731
732 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
733 }
734}
735
736bool
737ProcessKDP::StartAsyncThread ()
738{
Greg Clayton5160ce52013-03-27 23:08:40 +0000739 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000740
741 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000742 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000743
Greg Clayton7925fbb2012-09-21 16:31:20 +0000744 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
745 return true;
746
Greg Claytonf9765ac2011-07-15 03:27:12 +0000747 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
748 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
749}
750
751void
752ProcessKDP::StopAsyncThread ()
753{
Greg Clayton5160ce52013-03-27 23:08:40 +0000754 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000755
756 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000757 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000758
759 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
760
761 // Stop the stdio thread
762 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
763 {
764 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000765 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000766 }
767}
768
769
770void *
771ProcessKDP::AsyncThread (void *arg)
772{
773 ProcessKDP *process = (ProcessKDP*) arg;
774
Greg Clayton7925fbb2012-09-21 16:31:20 +0000775 const lldb::pid_t pid = process->GetID();
776
Greg Clayton5160ce52013-03-27 23:08:40 +0000777 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000778 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000779 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000780
781 Listener listener ("ProcessKDP::AsyncThread");
782 EventSP event_sp;
783 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
784 eBroadcastBitAsyncThreadShouldExit;
785
Greg Clayton7925fbb2012-09-21 16:31:20 +0000786
Greg Claytonf9765ac2011-07-15 03:27:12 +0000787 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
788 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000789 bool done = false;
790 while (!done)
791 {
792 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000793 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000794 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000795 if (listener.WaitForEvent (NULL, event_sp))
796 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000797 uint32_t event_type = event_sp->GetType();
798 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000799 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000800 pid,
801 event_type);
802
803 // When we are running, poll for 1 second to try and get an exception
804 // to indicate the process has stopped. If we don't get one, check to
805 // make sure no one asked us to exit
806 bool is_running = false;
807 DataExtractor exc_reply_packet;
808 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000809 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000810 switch (event_type)
811 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000812 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000813 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000814 is_running = true;
815 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000816 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000817 ThreadSP thread_sp (process->GetKernelThread());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000818 if (thread_sp)
819 {
820 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
821 if (reg_ctx_sp)
822 reg_ctx_sp->InvalidateAllRegisters();
823 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
824 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000825
Greg Clayton7925fbb2012-09-21 16:31:20 +0000826 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000827 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000828 process->SetPrivateState(eStateStopped);
829 }
830 else
831 {
832 // Check to see if we are supposed to exit. There is no way to
833 // interrupt a running kernel, so all we can do is wait for an
834 // exception or detach...
835 if (listener.GetNextEvent(event_sp))
836 {
837 // We got an event, go through the loop again
838 event_type = event_sp->GetType();
839 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000840 }
841 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000842 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000843
Greg Clayton7925fbb2012-09-21 16:31:20 +0000844 case eBroadcastBitAsyncThreadShouldExit:
845 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000846 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000847 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000848 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000849 is_running = false;
850 break;
851
852 default:
853 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000854 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000855 pid,
856 event_type);
857 done = true;
858 is_running = false;
859 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000860 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000861 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000862 }
863 else
864 {
865 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000866 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000867 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000868 done = true;
869 }
870 }
871 }
872
873 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000874 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000875 arg,
876 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000877
878 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
879 return NULL;
880}
881
882
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000883class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
884{
885private:
886
887 OptionGroupOptions m_option_group;
888 OptionGroupUInt64 m_command_byte;
889 OptionGroupString m_packet_data;
890
891 virtual Options *
892 GetOptions ()
893 {
894 return &m_option_group;
895 }
896
897
898public:
899 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
900 CommandObjectParsed (interpreter,
901 "process plugin packet send",
902 "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. ",
903 NULL),
904 m_option_group (interpreter),
905 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),
906 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)
907 {
908 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
909 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
910 m_option_group.Finalize();
911 }
912
913 ~CommandObjectProcessKDPPacketSend ()
914 {
915 }
916
917 bool
918 DoExecute (Args& command, CommandReturnObject &result)
919 {
920 const size_t argc = command.GetArgumentCount();
921 if (argc == 0)
922 {
923 if (!m_command_byte.GetOptionValue().OptionWasSet())
924 {
925 result.AppendError ("the --command option must be set to a valid command byte");
926 result.SetStatus (eReturnStatusFailed);
927 }
928 else
929 {
930 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
931 if (command_byte > 0 && command_byte <= UINT8_MAX)
932 {
933 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
934 if (process)
935 {
936 const StateType state = process->GetState();
937
938 if (StateIsStoppedState (state, true))
939 {
940 std::vector<uint8_t> payload_bytes;
941 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
942 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
943 {
944 StringExtractor extractor(ascii_hex_bytes_cstr);
945 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
946 if (ascii_hex_bytes_cstr_len & 1)
947 {
948 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
949 result.SetStatus (eReturnStatusFailed);
950 return false;
951 }
952 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
953 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
954 {
955 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
956 result.SetStatus (eReturnStatusFailed);
957 return false;
958 }
959 }
960 Error error;
961 DataExtractor reply;
962 process->GetCommunication().SendRawRequest (command_byte,
963 payload_bytes.empty() ? NULL : payload_bytes.data(),
964 payload_bytes.size(),
965 reply,
966 error);
967
968 if (error.Success())
969 {
970 // Copy the binary bytes into a hex ASCII string for the result
971 StreamString packet;
972 packet.PutBytesAsRawHex8(reply.GetDataStart(),
973 reply.GetByteSize(),
974 lldb::endian::InlHostByteOrder(),
975 lldb::endian::InlHostByteOrder());
976 result.AppendMessage(packet.GetString().c_str());
977 result.SetStatus (eReturnStatusSuccessFinishResult);
978 return true;
979 }
980 else
981 {
982 const char *error_cstr = error.AsCString();
983 if (error_cstr && error_cstr[0])
984 result.AppendError (error_cstr);
985 else
986 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
987 result.SetStatus (eReturnStatusFailed);
988 return false;
989 }
990 }
991 else
992 {
993 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
994 result.SetStatus (eReturnStatusFailed);
995 }
996 }
997 else
998 {
999 result.AppendError ("invalid process");
1000 result.SetStatus (eReturnStatusFailed);
1001 }
1002 }
1003 else
1004 {
Daniel Malead01b2952012-11-29 21:49:15 +00001005 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001006 result.SetStatus (eReturnStatusFailed);
1007 }
1008 }
1009 }
1010 else
1011 {
1012 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
1013 result.SetStatus (eReturnStatusFailed);
1014 }
1015 return false;
1016 }
1017};
1018
1019class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1020{
1021private:
1022
1023public:
1024 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1025 CommandObjectMultiword (interpreter,
1026 "process plugin packet",
1027 "Commands that deal with KDP remote packets.",
1028 NULL)
1029 {
1030 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1031 }
1032
1033 ~CommandObjectProcessKDPPacket ()
1034 {
1035 }
1036};
1037
1038class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1039{
1040public:
1041 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1042 CommandObjectMultiword (interpreter,
1043 "process plugin",
1044 "A set of commands for operating on a ProcessKDP process.",
1045 "process plugin <subcommand> [<subcommand-options>]")
1046 {
1047 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1048 }
1049
1050 ~CommandObjectMultiwordProcessKDP ()
1051 {
1052 }
1053};
1054
1055CommandObject *
1056ProcessKDP::GetPluginCommandObject()
1057{
1058 if (!m_command_sp)
1059 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1060 return m_command_sp.get();
1061}
1062