blob: ef97079dca38b39eaad1e70213a5b6cf6fcf8603 [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 Clayton57abc5d2013-05-10 21:47:16 +000049ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +000050ProcessKDP::GetPluginNameStatic()
51{
Greg Clayton57abc5d2013-05-10 21:47:16 +000052 static ConstString g_name("kdp-remote");
53 return g_name;
Greg Claytonf9765ac2011-07-15 03:27:12 +000054}
55
56const char *
57ProcessKDP::GetPluginDescriptionStatic()
58{
59 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
60}
61
62void
63ProcessKDP::Terminate()
64{
65 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
66}
67
68
Greg Claytonc3776bf2012-02-09 06:16:32 +000069lldb::ProcessSP
70ProcessKDP::CreateInstance (Target &target,
71 Listener &listener,
72 const FileSpec *crash_file_path)
Greg Claytonf9765ac2011-07-15 03:27:12 +000073{
Greg Claytonc3776bf2012-02-09 06:16:32 +000074 lldb::ProcessSP process_sp;
75 if (crash_file_path == NULL)
76 process_sp.reset(new ProcessKDP (target, listener));
77 return process_sp;
Greg Claytonf9765ac2011-07-15 03:27:12 +000078}
79
80bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000081ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Claytonf9765ac2011-07-15 03:27:12 +000082{
Greg Clayton596ed242011-10-21 21:41:45 +000083 if (plugin_specified_by_name)
84 return true;
85
Greg Claytonf9765ac2011-07-15 03:27:12 +000086 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +000087 Module *exe_module = target.GetExecutableModulePointer();
88 if (exe_module)
Greg Claytonf9765ac2011-07-15 03:27:12 +000089 {
90 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +000091 switch (triple_ref.getOS())
Greg Claytonf9765ac2011-07-15 03:27:12 +000092 {
Greg Clayton70512312012-05-08 01:45:38 +000093 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
94 case llvm::Triple::MacOSX: // For desktop targets
95 case llvm::Triple::IOS: // For arm targets
96 if (triple_ref.getVendor() == llvm::Triple::Apple)
97 {
98 ObjectFile *exe_objfile = exe_module->GetObjectFile();
99 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
100 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
101 return true;
102 }
103 break;
104
105 default:
106 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000107 }
108 }
Greg Clayton596ed242011-10-21 21:41:45 +0000109 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000110}
111
112//----------------------------------------------------------------------
113// ProcessKDP constructor
114//----------------------------------------------------------------------
115ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
116 Process (target, listener),
117 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000118 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton97d5cf02012-09-25 02:40:06 +0000119 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000120 m_dyld_plugin_name (),
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000121 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000122 m_command_sp(),
123 m_kernel_thread_wp()
Greg Claytonf9765ac2011-07-15 03:27:12 +0000124{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000125 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
126 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000127}
128
129//----------------------------------------------------------------------
130// Destructor
131//----------------------------------------------------------------------
132ProcessKDP::~ProcessKDP()
133{
134 Clear();
Greg Claytone24c4ac2011-11-17 04:46:02 +0000135 // We need to call finalize on the process before destroying ourselves
136 // to make sure all of the broadcaster cleanup goes as planned. If we
137 // destruct this class, then Process::~Process() might have problems
138 // trying to fully destroy the broadcaster.
139 Finalize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000140}
141
142//----------------------------------------------------------------------
143// PluginInterface
144//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000145lldb_private::ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +0000146ProcessKDP::GetPluginName()
147{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000148 return GetPluginNameStatic();
149}
150
151uint32_t
152ProcessKDP::GetPluginVersion()
153{
154 return 1;
155}
156
157Error
158ProcessKDP::WillLaunch (Module* module)
159{
160 Error error;
161 error.SetErrorString ("launching not supported in kdp-remote plug-in");
162 return error;
163}
164
165Error
166ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
167{
168 Error error;
169 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
170 return error;
171}
172
173Error
174ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
175{
176 Error error;
177 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
178 return error;
179}
180
181Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000182ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000183{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000184 Error error;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000185
186 // Don't let any JIT happen when doing KDP as we can't allocate
187 // memory and we don't want to be mucking with threads that might
188 // already be handling exceptions
189 SetCanJIT(false);
190
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000191 if (remote_url == NULL || remote_url[0] == '\0')
Greg Clayton7925fbb2012-09-21 16:31:20 +0000192 {
193 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
194 return error;
195 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000196
Greg Clayton7b0992d2013-04-18 22:45:39 +0000197 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000198 if (conn_ap.get())
199 {
200 // Only try once for now.
201 // TODO: check if we should be retrying?
202 const uint32_t max_retry_count = 1;
203 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
204 {
205 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
206 break;
207 usleep (100000);
208 }
209 }
210
211 if (conn_ap->IsConnected())
212 {
213 const uint16_t reply_port = conn_ap->GetReadPort ();
214
215 if (reply_port != 0)
216 {
217 m_comm.SetConnection(conn_ap.release());
218
219 if (m_comm.SendRequestReattach(reply_port))
220 {
221 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
222 {
223 m_comm.GetVersion();
224 uint32_t cpu = m_comm.GetCPUType();
225 uint32_t sub = m_comm.GetCPUSubtype();
226 ArchSpec kernel_arch;
227 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
228 m_target.SetArchitecture(kernel_arch);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000229
Jason Molenda840f12c2012-10-25 00:25:13 +0000230 /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */
231 /* An EFI kdp session has neither UUID nor load address. */
232
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000233 UUID kernel_uuid = m_comm.GetUUID ();
234 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000235
Jason Molenda840f12c2012-10-25 00:25:13 +0000236 if (m_comm.RemoteIsEFI ())
237 {
238 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
239 }
Jason Molendaca2ffa72013-05-09 23:52:21 +0000240 else if (m_comm.RemoteIsDarwinKernel ())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000241 {
Jason Molendaca2ffa72013-05-09 23:52:21 +0000242 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000243 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
244 {
245 m_kernel_load_addr = kernel_load_addr;
246 }
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000247 }
248
Greg Clayton97d5cf02012-09-25 02:40:06 +0000249 // Set the thread ID
250 UpdateThreadListIfNeeded ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000251 SetID (1);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000252 GetThreadList ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000253 SetPrivateState (eStateStopped);
Greg Clayton07e66e32011-07-20 03:41:06 +0000254 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
255 if (async_strm_sp)
256 {
Greg Clayton5b882162011-07-21 01:12:01 +0000257 const char *cstr;
258 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton07e66e32011-07-20 03:41:06 +0000259 {
Greg Clayton5b882162011-07-21 01:12:01 +0000260 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton07e66e32011-07-20 03:41:06 +0000261 async_strm_sp->Flush();
262 }
Greg Clayton5b882162011-07-21 01:12:01 +0000263// if ((cstr = m_comm.GetImagePath ()) != NULL)
264// {
265// async_strm_sp->Printf ("Image Path: %s\n", cstr);
266// async_strm_sp->Flush();
267// }
Greg Clayton07e66e32011-07-20 03:41:06 +0000268 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000269 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000270 else
271 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000272 error.SetErrorString("KDP_REATTACH failed");
273 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000274 }
275 else
276 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000277 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000278 }
279 }
280 else
281 {
282 error.SetErrorString("invalid reply port from UDP connection");
283 }
284 }
285 else
286 {
287 if (error.Success())
288 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
289 }
290 if (error.Fail())
291 m_comm.Disconnect();
292
Greg Claytonf9765ac2011-07-15 03:27:12 +0000293 return error;
294}
295
296//----------------------------------------------------------------------
297// Process Control
298//----------------------------------------------------------------------
299Error
Greg Clayton982c9762011-11-03 21:22:33 +0000300ProcessKDP::DoLaunch (Module *exe_module,
301 const ProcessLaunchInfo &launch_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000302{
303 Error error;
304 error.SetErrorString ("launching not supported in kdp-remote plug-in");
305 return error;
306}
307
308
309Error
310ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
311{
312 Error error;
313 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
314 return error;
315}
316
Greg Claytonf9765ac2011-07-15 03:27:12 +0000317Error
Han Ming Ong84647042012-02-25 01:07:38 +0000318ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
319{
320 Error error;
321 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
322 return error;
323}
324
325Error
326ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000327{
328 Error error;
329 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
330 return error;
331}
332
333
334void
335ProcessKDP::DidAttach ()
336{
Greg Clayton5160ce52013-03-27 23:08:40 +0000337 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000338 if (log)
Johnny Chen54cb8f82011-10-11 21:17:10 +0000339 log->Printf ("ProcessKDP::DidAttach()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000340 if (GetID() != LLDB_INVALID_PROCESS_ID)
341 {
342 // TODO: figure out the register context that we will use
343 }
344}
345
Jason Molenda5e8534e2012-10-03 01:29:34 +0000346addr_t
347ProcessKDP::GetImageInfoAddress()
348{
349 return m_kernel_load_addr;
350}
351
352lldb_private::DynamicLoader *
353ProcessKDP::GetDynamicLoader ()
354{
355 if (m_dyld_ap.get() == NULL)
356 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
357 return m_dyld_ap.get();
358}
359
Greg Claytonf9765ac2011-07-15 03:27:12 +0000360Error
361ProcessKDP::WillResume ()
362{
363 return Error();
364}
365
366Error
367ProcessKDP::DoResume ()
368{
369 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000370 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000371 // Only start the async thread if we try to do any process control
372 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
373 StartAsyncThread ();
374
Greg Clayton97d5cf02012-09-25 02:40:06 +0000375 bool resume = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000376
Greg Clayton97d5cf02012-09-25 02:40:06 +0000377 // With KDP there is only one thread we can tell what to do
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000378 ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
379
Greg Clayton97d5cf02012-09-25 02:40:06 +0000380 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000381 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000382 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000383
384 if (log)
385 log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000386 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000387 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000388 case eStateSuspended:
389 // Nothing to do here when a thread will stay suspended
390 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000391 if (log)
392 log->Printf ("ProcessKDP::DoResume() = suspended???");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000393 break;
394
395 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000396 {
397 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
398
399 if (reg_ctx_sp)
400 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000401 if (log)
402 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
Greg Clayton1afa68e2013-04-02 20:32:37 +0000403 reg_ctx_sp->HardwareSingleStep (true);
404 resume = true;
405 }
406 else
407 {
408 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
409 }
410 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000411 break;
412
Greg Clayton7925fbb2012-09-21 16:31:20 +0000413 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000414 {
415 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
416
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000417 if (reg_ctx_sp)
418 {
419 if (log)
420 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
421 reg_ctx_sp->HardwareSingleStep (false);
422 resume = true;
423 }
424 else
425 {
426 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
427 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000428 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000429 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000430
Greg Clayton7925fbb2012-09-21 16:31:20 +0000431 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000432 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000433 assert (!"invalid thread resume state");
434 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000435 }
436 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000437
438 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000439 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000440 if (log)
441 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000442
Greg Clayton97d5cf02012-09-25 02:40:06 +0000443 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000444 {
445 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
446 SetPrivateState(eStateRunning);
447 }
448 else
449 error.SetErrorString ("KDP resume failed");
450 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000451 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000452 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000453 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000454 }
455
Greg Claytonf9765ac2011-07-15 03:27:12 +0000456 return error;
457}
458
Greg Clayton97d5cf02012-09-25 02:40:06 +0000459lldb::ThreadSP
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000460ProcessKDP::GetKernelThread()
Greg Clayton97d5cf02012-09-25 02:40:06 +0000461{
462 // KDP only tells us about one thread/core. Any other threads will usually
463 // be the ones that are read from memory by the OS plug-ins.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000464
465 ThreadSP thread_sp (m_kernel_thread_wp.lock());
Greg Clayton97d5cf02012-09-25 02:40:06 +0000466 if (!thread_sp)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000467 {
468 thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
469 m_kernel_thread_wp = thread_sp;
470 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000471 return thread_sp;
472}
473
474
475
476
Greg Clayton9fc13552012-04-10 00:18:59 +0000477bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000478ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000479{
480 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000481 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000482 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000483 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000484
Greg Clayton39da3ef2013-04-11 22:23:34 +0000485 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000486 // indivudually, there is really only one. Lets call this thread 1.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000487 ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
488 if (!thread_sp)
489 thread_sp = GetKernelThread ();
490 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000491
Greg Clayton9fc13552012-04-10 00:18:59 +0000492 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000493}
494
Greg Claytonf9765ac2011-07-15 03:27:12 +0000495void
496ProcessKDP::RefreshStateAfterStop ()
497{
498 // Let all threads recover from stopping and do any clean up based
499 // on the previous thread state (if any).
500 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000501}
502
503Error
504ProcessKDP::DoHalt (bool &caused_stop)
505{
506 Error error;
507
Greg Clayton97d5cf02012-09-25 02:40:06 +0000508 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000509 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000510 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000511 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000512 // If we are attemping to destroy, we need to not return an error to
513 // Halt or DoDestroy won't get called.
514 // We are also currently running, so send a process stopped event
515 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000516 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000517 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000518 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000519 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000520 }
521 }
522 return error;
523}
524
525Error
Jim Inghamacff8952013-05-02 00:27:30 +0000526ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000527{
528 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000529 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000530 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000531 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000532
Greg Clayton97d5cf02012-09-25 02:40:06 +0000533 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000534 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000535 // We are running and we can't interrupt a running kernel, so we need
536 // to just close the connection to the kernel and hope for the best
537 }
538 else
539 {
540 DisableAllBreakpointSites ();
541
542 m_thread_list.DiscardThreadPlans();
543
Jim Inghamacff8952013-05-02 00:27:30 +0000544 // If we are going to keep the target stopped, then don't send the disconnect message.
545 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000546 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000547 const bool success = m_comm.SendRequestDisconnect();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000548 if (log)
549 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000550 if (success)
551 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000552 else
Jim Ingham77e82d12013-05-09 00:05:35 +0000553 log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000554 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000555 m_comm.Disconnect ();
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000556 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000557 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000558 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000559 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000560
561 SetPrivateState (eStateDetached);
562 ResumePrivateStateThread();
563
564 //KillDebugserverProcess ();
565 return error;
566}
567
568Error
569ProcessKDP::DoDestroy ()
570{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000571 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000572 bool keep_stopped = false;
573 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000574}
575
576//------------------------------------------------------------------
577// Process Queries
578//------------------------------------------------------------------
579
580bool
581ProcessKDP::IsAlive ()
582{
583 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
584}
585
586//------------------------------------------------------------------
587// Process Memory
588//------------------------------------------------------------------
589size_t
590ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
591{
Greg Claytona63d08c2011-07-19 03:57:15 +0000592 if (m_comm.IsConnected())
593 return m_comm.SendRequestReadMemory (addr, buf, size, error);
594 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000595 return 0;
596}
597
598size_t
599ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
600{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000601 if (m_comm.IsConnected())
602 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
603 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000604 return 0;
605}
606
607lldb::addr_t
608ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
609{
610 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
611 return LLDB_INVALID_ADDRESS;
612}
613
614Error
615ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
616{
617 Error error;
618 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
619 return error;
620}
621
622Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000623ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000624{
Greg Clayton07e66e32011-07-20 03:41:06 +0000625 if (m_comm.LocalBreakpointsAreSupported ())
626 {
627 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000628 if (!bp_site->IsEnabled())
629 {
630 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
631 {
632 bp_site->SetEnabled(true);
633 bp_site->SetType (BreakpointSite::eExternal);
634 }
635 else
636 {
637 error.SetErrorString ("KDP set breakpoint failed");
638 }
639 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000640 return error;
641 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000642 return EnableSoftwareBreakpoint (bp_site);
643}
644
645Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000646ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000647{
Greg Clayton07e66e32011-07-20 03:41:06 +0000648 if (m_comm.LocalBreakpointsAreSupported ())
649 {
650 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000651 if (bp_site->IsEnabled())
652 {
653 BreakpointSite::Type bp_type = bp_site->GetType();
654 if (bp_type == BreakpointSite::eExternal)
655 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000656 if (m_destroy_in_process && m_comm.IsRunning())
657 {
658 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000659 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000660 }
Greg Clayton5b882162011-07-21 01:12:01 +0000661 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000662 {
663 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
664 bp_site->SetEnabled(false);
665 else
666 error.SetErrorString ("KDP remove breakpoint failed");
667 }
Greg Clayton5b882162011-07-21 01:12:01 +0000668 }
669 else
670 {
671 error = DisableSoftwareBreakpoint (bp_site);
672 }
673 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000674 return error;
675 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000676 return DisableSoftwareBreakpoint (bp_site);
677}
678
679Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000680ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000681{
682 Error error;
683 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
684 return error;
685}
686
687Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000688ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000689{
690 Error error;
691 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
692 return error;
693}
694
695void
696ProcessKDP::Clear()
697{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000698 m_thread_list.Clear();
699}
700
701Error
702ProcessKDP::DoSignal (int signo)
703{
704 Error error;
705 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
706 return error;
707}
708
709void
710ProcessKDP::Initialize()
711{
712 static bool g_initialized = false;
713
714 if (g_initialized == false)
715 {
716 g_initialized = true;
717 PluginManager::RegisterPlugin (GetPluginNameStatic(),
718 GetPluginDescriptionStatic(),
719 CreateInstance);
720
721 Log::Callbacks log_callbacks = {
722 ProcessKDPLog::DisableLog,
723 ProcessKDPLog::EnableLog,
724 ProcessKDPLog::ListLogCategories
725 };
726
727 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
728 }
729}
730
731bool
732ProcessKDP::StartAsyncThread ()
733{
Greg Clayton5160ce52013-03-27 23:08:40 +0000734 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000735
736 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000737 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000738
Greg Clayton7925fbb2012-09-21 16:31:20 +0000739 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
740 return true;
741
Greg Claytonf9765ac2011-07-15 03:27:12 +0000742 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
743 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
744}
745
746void
747ProcessKDP::StopAsyncThread ()
748{
Greg Clayton5160ce52013-03-27 23:08:40 +0000749 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000750
751 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000752 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000753
754 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
755
756 // Stop the stdio thread
757 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
758 {
759 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000760 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000761 }
762}
763
764
765void *
766ProcessKDP::AsyncThread (void *arg)
767{
768 ProcessKDP *process = (ProcessKDP*) arg;
769
Greg Clayton7925fbb2012-09-21 16:31:20 +0000770 const lldb::pid_t pid = process->GetID();
771
Greg Clayton5160ce52013-03-27 23:08:40 +0000772 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000773 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000774 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000775
776 Listener listener ("ProcessKDP::AsyncThread");
777 EventSP event_sp;
778 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
779 eBroadcastBitAsyncThreadShouldExit;
780
Greg Clayton7925fbb2012-09-21 16:31:20 +0000781
Greg Claytonf9765ac2011-07-15 03:27:12 +0000782 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
783 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000784 bool done = false;
785 while (!done)
786 {
787 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000788 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000789 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000790 if (listener.WaitForEvent (NULL, event_sp))
791 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000792 uint32_t event_type = event_sp->GetType();
793 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000794 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000795 pid,
796 event_type);
797
798 // When we are running, poll for 1 second to try and get an exception
799 // to indicate the process has stopped. If we don't get one, check to
800 // make sure no one asked us to exit
801 bool is_running = false;
802 DataExtractor exc_reply_packet;
803 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000804 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000805 switch (event_type)
806 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000807 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000808 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000809 is_running = true;
810 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000811 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000812 ThreadSP thread_sp (process->GetKernelThread());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000813 if (thread_sp)
814 {
815 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
816 if (reg_ctx_sp)
817 reg_ctx_sp->InvalidateAllRegisters();
818 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
819 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000820
Greg Clayton7925fbb2012-09-21 16:31:20 +0000821 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000822 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000823 process->SetPrivateState(eStateStopped);
824 }
825 else
826 {
827 // Check to see if we are supposed to exit. There is no way to
828 // interrupt a running kernel, so all we can do is wait for an
829 // exception or detach...
830 if (listener.GetNextEvent(event_sp))
831 {
832 // We got an event, go through the loop again
833 event_type = event_sp->GetType();
834 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000835 }
836 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000837 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000838
Greg Clayton7925fbb2012-09-21 16:31:20 +0000839 case eBroadcastBitAsyncThreadShouldExit:
840 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000841 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000842 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000843 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000844 is_running = false;
845 break;
846
847 default:
848 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000849 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000850 pid,
851 event_type);
852 done = true;
853 is_running = false;
854 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000855 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000856 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000857 }
858 else
859 {
860 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000861 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000862 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000863 done = true;
864 }
865 }
866 }
867
868 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000869 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000870 arg,
871 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000872
873 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
874 return NULL;
875}
876
877
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000878class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
879{
880private:
881
882 OptionGroupOptions m_option_group;
883 OptionGroupUInt64 m_command_byte;
884 OptionGroupString m_packet_data;
885
886 virtual Options *
887 GetOptions ()
888 {
889 return &m_option_group;
890 }
891
892
893public:
894 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
895 CommandObjectParsed (interpreter,
896 "process plugin packet send",
897 "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. ",
898 NULL),
899 m_option_group (interpreter),
900 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),
901 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)
902 {
903 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
904 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
905 m_option_group.Finalize();
906 }
907
908 ~CommandObjectProcessKDPPacketSend ()
909 {
910 }
911
912 bool
913 DoExecute (Args& command, CommandReturnObject &result)
914 {
915 const size_t argc = command.GetArgumentCount();
916 if (argc == 0)
917 {
918 if (!m_command_byte.GetOptionValue().OptionWasSet())
919 {
920 result.AppendError ("the --command option must be set to a valid command byte");
921 result.SetStatus (eReturnStatusFailed);
922 }
923 else
924 {
925 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
926 if (command_byte > 0 && command_byte <= UINT8_MAX)
927 {
928 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
929 if (process)
930 {
931 const StateType state = process->GetState();
932
933 if (StateIsStoppedState (state, true))
934 {
935 std::vector<uint8_t> payload_bytes;
936 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
937 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
938 {
939 StringExtractor extractor(ascii_hex_bytes_cstr);
940 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
941 if (ascii_hex_bytes_cstr_len & 1)
942 {
943 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
944 result.SetStatus (eReturnStatusFailed);
945 return false;
946 }
947 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
948 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
949 {
950 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
951 result.SetStatus (eReturnStatusFailed);
952 return false;
953 }
954 }
955 Error error;
956 DataExtractor reply;
957 process->GetCommunication().SendRawRequest (command_byte,
958 payload_bytes.empty() ? NULL : payload_bytes.data(),
959 payload_bytes.size(),
960 reply,
961 error);
962
963 if (error.Success())
964 {
965 // Copy the binary bytes into a hex ASCII string for the result
966 StreamString packet;
967 packet.PutBytesAsRawHex8(reply.GetDataStart(),
968 reply.GetByteSize(),
969 lldb::endian::InlHostByteOrder(),
970 lldb::endian::InlHostByteOrder());
971 result.AppendMessage(packet.GetString().c_str());
972 result.SetStatus (eReturnStatusSuccessFinishResult);
973 return true;
974 }
975 else
976 {
977 const char *error_cstr = error.AsCString();
978 if (error_cstr && error_cstr[0])
979 result.AppendError (error_cstr);
980 else
981 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
982 result.SetStatus (eReturnStatusFailed);
983 return false;
984 }
985 }
986 else
987 {
988 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
989 result.SetStatus (eReturnStatusFailed);
990 }
991 }
992 else
993 {
994 result.AppendError ("invalid process");
995 result.SetStatus (eReturnStatusFailed);
996 }
997 }
998 else
999 {
Daniel Malead01b2952012-11-29 21:49:15 +00001000 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001001 result.SetStatus (eReturnStatusFailed);
1002 }
1003 }
1004 }
1005 else
1006 {
1007 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
1008 result.SetStatus (eReturnStatusFailed);
1009 }
1010 return false;
1011 }
1012};
1013
1014class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1015{
1016private:
1017
1018public:
1019 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1020 CommandObjectMultiword (interpreter,
1021 "process plugin packet",
1022 "Commands that deal with KDP remote packets.",
1023 NULL)
1024 {
1025 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1026 }
1027
1028 ~CommandObjectProcessKDPPacket ()
1029 {
1030 }
1031};
1032
1033class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1034{
1035public:
1036 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1037 CommandObjectMultiword (interpreter,
1038 "process plugin",
1039 "A set of commands for operating on a ProcessKDP process.",
1040 "process plugin <subcommand> [<subcommand-options>]")
1041 {
1042 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1043 }
1044
1045 ~CommandObjectMultiwordProcessKDP ()
1046 {
1047 }
1048};
1049
1050CommandObject *
1051ProcessKDP::GetPluginCommandObject()
1052{
1053 if (!m_command_sp)
1054 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1055 return m_command_sp.get();
1056}
1057