blob: a30b7fffef95beff6dde9af44ea5821dfece973d [file] [log] [blame]
Greg Clayton59ec5122011-07-15 18:02:58 +00001//===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===//
Greg Claytonf9765ac2011-07-15 03:27:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <errno.h>
12#include <stdlib.h>
13
14// C++ Includes
15// Other libraries and framework includes
Greg Clayton3a29bdb2011-07-17 20:36:25 +000016#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000017#include "lldb/Core/Debugger.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000018#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000020#include "lldb/Core/ModuleSpec.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000021#include "lldb/Core/State.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000022#include "lldb/Core/UUID.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000023#include "lldb/Host/Host.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000024#include "lldb/Host/Symbols.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000025#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/CommandObject.h"
27#include "lldb/Interpreter/CommandObjectMultiword.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
29#include "lldb/Interpreter/OptionGroupString.h"
30#include "lldb/Interpreter/OptionGroupUInt64.h"
Greg Clayton1f746072012-08-29 21:13:06 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7925fbb2012-09-21 16:31:20 +000032#include "lldb/Target/RegisterContext.h"
Greg Clayton57508022011-07-15 16:31:38 +000033#include "lldb/Target/Target.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000034#include "lldb/Target/Thread.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000035
36// Project includes
37#include "ProcessKDP.h"
38#include "ProcessKDPLog.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000039#include "ThreadKDP.h"
Jason Molenda5e8534e2012-10-03 01:29:34 +000040#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
Jason Molenda840f12c2012-10-25 00:25:13 +000041#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000042#include "Utility/StringExtractor.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton7f982402013-07-15 22:54:20 +000047namespace {
48
49 static PropertyDefinition
50 g_properties[] =
51 {
52 { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." },
53 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
54 };
55
56 enum
57 {
58 ePropertyPacketTimeout
59 };
60
61 class PluginProperties : public Properties
62 {
63 public:
64
65 static ConstString
66 GetSettingName ()
67 {
68 return ProcessKDP::GetPluginNameStatic();
69 }
70
71 PluginProperties() :
72 Properties ()
73 {
74 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
75 m_collection_sp->Initialize(g_properties);
76 }
77
78 virtual
79 ~PluginProperties()
80 {
81 }
82
83 uint64_t
84 GetPacketTimeout()
85 {
86 const uint32_t idx = ePropertyPacketTimeout;
87 return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
88 }
89 };
90
91 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
92
93 static const ProcessKDPPropertiesSP &
94 GetGlobalPluginProperties()
95 {
96 static ProcessKDPPropertiesSP g_settings_sp;
97 if (!g_settings_sp)
98 g_settings_sp.reset (new PluginProperties ());
99 return g_settings_sp;
100 }
101
102} // anonymous namespace end
103
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000104static const lldb::tid_t g_kernel_tid = 1;
105
Greg Clayton57abc5d2013-05-10 21:47:16 +0000106ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +0000107ProcessKDP::GetPluginNameStatic()
108{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000109 static ConstString g_name("kdp-remote");
110 return g_name;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000111}
112
113const char *
114ProcessKDP::GetPluginDescriptionStatic()
115{
116 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
117}
118
119void
120ProcessKDP::Terminate()
121{
122 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
123}
124
125
Greg Claytonc3776bf2012-02-09 06:16:32 +0000126lldb::ProcessSP
127ProcessKDP::CreateInstance (Target &target,
128 Listener &listener,
129 const FileSpec *crash_file_path)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000130{
Greg Claytonc3776bf2012-02-09 06:16:32 +0000131 lldb::ProcessSP process_sp;
132 if (crash_file_path == NULL)
133 process_sp.reset(new ProcessKDP (target, listener));
134 return process_sp;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000135}
136
137bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000138ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000139{
Greg Clayton596ed242011-10-21 21:41:45 +0000140 if (plugin_specified_by_name)
141 return true;
142
Greg Claytonf9765ac2011-07-15 03:27:12 +0000143 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +0000144 Module *exe_module = target.GetExecutableModulePointer();
145 if (exe_module)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000146 {
147 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +0000148 switch (triple_ref.getOS())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000149 {
Greg Clayton70512312012-05-08 01:45:38 +0000150 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
151 case llvm::Triple::MacOSX: // For desktop targets
152 case llvm::Triple::IOS: // For arm targets
153 if (triple_ref.getVendor() == llvm::Triple::Apple)
154 {
155 ObjectFile *exe_objfile = exe_module->GetObjectFile();
156 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
157 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
158 return true;
159 }
160 break;
161
162 default:
163 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000164 }
165 }
Greg Clayton596ed242011-10-21 21:41:45 +0000166 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000167}
168
169//----------------------------------------------------------------------
170// ProcessKDP constructor
171//----------------------------------------------------------------------
172ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
173 Process (target, listener),
174 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000175 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton97d5cf02012-09-25 02:40:06 +0000176 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000177 m_dyld_plugin_name (),
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000178 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000179 m_command_sp(),
180 m_kernel_thread_wp()
Greg Claytonf9765ac2011-07-15 03:27:12 +0000181{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000182 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
183 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton7f982402013-07-15 22:54:20 +0000184 const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
185 if (timeout_seconds > 0)
186 m_comm.SetPacketTimeout(timeout_seconds);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000187}
188
189//----------------------------------------------------------------------
190// Destructor
191//----------------------------------------------------------------------
192ProcessKDP::~ProcessKDP()
193{
194 Clear();
Greg Claytone24c4ac2011-11-17 04:46:02 +0000195 // We need to call finalize on the process before destroying ourselves
196 // to make sure all of the broadcaster cleanup goes as planned. If we
197 // destruct this class, then Process::~Process() might have problems
198 // trying to fully destroy the broadcaster.
199 Finalize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000200}
201
202//----------------------------------------------------------------------
203// PluginInterface
204//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000205lldb_private::ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +0000206ProcessKDP::GetPluginName()
207{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000208 return GetPluginNameStatic();
209}
210
211uint32_t
212ProcessKDP::GetPluginVersion()
213{
214 return 1;
215}
216
217Error
218ProcessKDP::WillLaunch (Module* module)
219{
220 Error error;
221 error.SetErrorString ("launching not supported in kdp-remote plug-in");
222 return error;
223}
224
225Error
226ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
227{
228 Error error;
229 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
230 return error;
231}
232
233Error
234ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
235{
236 Error error;
237 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
238 return error;
239}
240
241Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000242ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000243{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000244 Error error;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000245
246 // Don't let any JIT happen when doing KDP as we can't allocate
247 // memory and we don't want to be mucking with threads that might
248 // already be handling exceptions
249 SetCanJIT(false);
250
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000251 if (remote_url == NULL || remote_url[0] == '\0')
Greg Clayton7925fbb2012-09-21 16:31:20 +0000252 {
253 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
254 return error;
255 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000256
Greg Clayton7b0992d2013-04-18 22:45:39 +0000257 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000258 if (conn_ap.get())
259 {
260 // Only try once for now.
261 // TODO: check if we should be retrying?
262 const uint32_t max_retry_count = 1;
263 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
264 {
265 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
266 break;
267 usleep (100000);
268 }
269 }
270
271 if (conn_ap->IsConnected())
272 {
273 const uint16_t reply_port = conn_ap->GetReadPort ();
274
275 if (reply_port != 0)
276 {
277 m_comm.SetConnection(conn_ap.release());
278
279 if (m_comm.SendRequestReattach(reply_port))
280 {
281 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
282 {
283 m_comm.GetVersion();
284 uint32_t cpu = m_comm.GetCPUType();
285 uint32_t sub = m_comm.GetCPUSubtype();
286 ArchSpec kernel_arch;
287 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
288 m_target.SetArchitecture(kernel_arch);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000289
Jason Molenda840f12c2012-10-25 00:25:13 +0000290 /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */
291 /* An EFI kdp session has neither UUID nor load address. */
292
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000293 UUID kernel_uuid = m_comm.GetUUID ();
294 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000295
Jason Molenda840f12c2012-10-25 00:25:13 +0000296 if (m_comm.RemoteIsEFI ())
297 {
298 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
299 }
Jason Molendaca2ffa72013-05-09 23:52:21 +0000300 else if (m_comm.RemoteIsDarwinKernel ())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000301 {
Jason Molendaca2ffa72013-05-09 23:52:21 +0000302 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000303 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
304 {
305 m_kernel_load_addr = kernel_load_addr;
306 }
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000307 }
308
Greg Clayton97d5cf02012-09-25 02:40:06 +0000309 // Set the thread ID
310 UpdateThreadListIfNeeded ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000311 SetID (1);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000312 GetThreadList ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000313 SetPrivateState (eStateStopped);
Greg Clayton07e66e32011-07-20 03:41:06 +0000314 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
315 if (async_strm_sp)
316 {
Greg Clayton5b882162011-07-21 01:12:01 +0000317 const char *cstr;
318 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton07e66e32011-07-20 03:41:06 +0000319 {
Greg Clayton5b882162011-07-21 01:12:01 +0000320 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton07e66e32011-07-20 03:41:06 +0000321 async_strm_sp->Flush();
322 }
Greg Clayton5b882162011-07-21 01:12:01 +0000323// if ((cstr = m_comm.GetImagePath ()) != NULL)
324// {
325// async_strm_sp->Printf ("Image Path: %s\n", cstr);
326// async_strm_sp->Flush();
327// }
Greg Clayton07e66e32011-07-20 03:41:06 +0000328 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000329 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000330 else
331 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000332 error.SetErrorString("KDP_REATTACH failed");
333 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000334 }
335 else
336 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000337 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000338 }
339 }
340 else
341 {
342 error.SetErrorString("invalid reply port from UDP connection");
343 }
344 }
345 else
346 {
347 if (error.Success())
348 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
349 }
350 if (error.Fail())
351 m_comm.Disconnect();
352
Greg Claytonf9765ac2011-07-15 03:27:12 +0000353 return error;
354}
355
356//----------------------------------------------------------------------
357// Process Control
358//----------------------------------------------------------------------
359Error
Greg Clayton982c9762011-11-03 21:22:33 +0000360ProcessKDP::DoLaunch (Module *exe_module,
361 const ProcessLaunchInfo &launch_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000362{
363 Error error;
364 error.SetErrorString ("launching not supported in kdp-remote plug-in");
365 return error;
366}
367
368
369Error
370ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
371{
372 Error error;
373 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
374 return error;
375}
376
Greg Claytonf9765ac2011-07-15 03:27:12 +0000377Error
Han Ming Ong84647042012-02-25 01:07:38 +0000378ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
379{
380 Error error;
381 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
382 return error;
383}
384
385Error
386ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000387{
388 Error error;
389 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
390 return error;
391}
392
393
394void
395ProcessKDP::DidAttach ()
396{
Greg Clayton5160ce52013-03-27 23:08:40 +0000397 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000398 if (log)
Johnny Chen54cb8f82011-10-11 21:17:10 +0000399 log->Printf ("ProcessKDP::DidAttach()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000400 if (GetID() != LLDB_INVALID_PROCESS_ID)
401 {
402 // TODO: figure out the register context that we will use
403 }
404}
405
Jason Molenda5e8534e2012-10-03 01:29:34 +0000406addr_t
407ProcessKDP::GetImageInfoAddress()
408{
409 return m_kernel_load_addr;
410}
411
412lldb_private::DynamicLoader *
413ProcessKDP::GetDynamicLoader ()
414{
415 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +0000416 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
Jason Molenda5e8534e2012-10-03 01:29:34 +0000417 return m_dyld_ap.get();
418}
419
Greg Claytonf9765ac2011-07-15 03:27:12 +0000420Error
421ProcessKDP::WillResume ()
422{
423 return Error();
424}
425
426Error
427ProcessKDP::DoResume ()
428{
429 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000430 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000431 // Only start the async thread if we try to do any process control
432 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
433 StartAsyncThread ();
434
Greg Clayton97d5cf02012-09-25 02:40:06 +0000435 bool resume = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000436
Greg Clayton97d5cf02012-09-25 02:40:06 +0000437 // With KDP there is only one thread we can tell what to do
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000438 ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
439
Greg Clayton97d5cf02012-09-25 02:40:06 +0000440 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000441 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000442 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000443
444 if (log)
445 log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000446 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000447 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000448 case eStateSuspended:
449 // Nothing to do here when a thread will stay suspended
450 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000451 if (log)
452 log->Printf ("ProcessKDP::DoResume() = suspended???");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000453 break;
454
455 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000456 {
457 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
458
459 if (reg_ctx_sp)
460 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000461 if (log)
462 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
Greg Clayton1afa68e2013-04-02 20:32:37 +0000463 reg_ctx_sp->HardwareSingleStep (true);
464 resume = true;
465 }
466 else
467 {
468 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
469 }
470 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000471 break;
472
Greg Clayton7925fbb2012-09-21 16:31:20 +0000473 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000474 {
475 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
476
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000477 if (reg_ctx_sp)
478 {
479 if (log)
480 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
481 reg_ctx_sp->HardwareSingleStep (false);
482 resume = true;
483 }
484 else
485 {
486 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
487 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000488 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000489 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000490
Greg Clayton7925fbb2012-09-21 16:31:20 +0000491 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000492 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000493 assert (!"invalid thread resume state");
494 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000495 }
496 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000497
498 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000499 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000500 if (log)
501 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000502
Greg Clayton97d5cf02012-09-25 02:40:06 +0000503 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000504 {
505 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
506 SetPrivateState(eStateRunning);
507 }
508 else
509 error.SetErrorString ("KDP resume failed");
510 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000511 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000512 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000513 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000514 }
515
Greg Claytonf9765ac2011-07-15 03:27:12 +0000516 return error;
517}
518
Greg Clayton97d5cf02012-09-25 02:40:06 +0000519lldb::ThreadSP
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000520ProcessKDP::GetKernelThread()
Greg Clayton97d5cf02012-09-25 02:40:06 +0000521{
522 // KDP only tells us about one thread/core. Any other threads will usually
523 // be the ones that are read from memory by the OS plug-ins.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000524
525 ThreadSP thread_sp (m_kernel_thread_wp.lock());
Greg Clayton97d5cf02012-09-25 02:40:06 +0000526 if (!thread_sp)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000527 {
528 thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
529 m_kernel_thread_wp = thread_sp;
530 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000531 return thread_sp;
532}
533
534
535
536
Greg Clayton9fc13552012-04-10 00:18:59 +0000537bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000538ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000539{
540 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000541 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000542 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000543 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000544
Greg Clayton39da3ef2013-04-11 22:23:34 +0000545 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000546 // indivudually, there is really only one. Lets call this thread 1.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000547 ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
548 if (!thread_sp)
549 thread_sp = GetKernelThread ();
550 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000551
Greg Clayton9fc13552012-04-10 00:18:59 +0000552 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000553}
554
Greg Claytonf9765ac2011-07-15 03:27:12 +0000555void
556ProcessKDP::RefreshStateAfterStop ()
557{
558 // Let all threads recover from stopping and do any clean up based
559 // on the previous thread state (if any).
560 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000561}
562
563Error
564ProcessKDP::DoHalt (bool &caused_stop)
565{
566 Error error;
567
Greg Clayton97d5cf02012-09-25 02:40:06 +0000568 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000569 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000570 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000571 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000572 // If we are attemping to destroy, we need to not return an error to
573 // Halt or DoDestroy won't get called.
574 // We are also currently running, so send a process stopped event
575 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000576 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000577 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000578 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000579 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000580 }
581 }
582 return error;
583}
584
585Error
Jim Inghamacff8952013-05-02 00:27:30 +0000586ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000587{
588 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000589 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000590 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000591 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000592
Greg Clayton97d5cf02012-09-25 02:40:06 +0000593 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000594 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000595 // We are running and we can't interrupt a running kernel, so we need
596 // to just close the connection to the kernel and hope for the best
597 }
598 else
599 {
600 DisableAllBreakpointSites ();
601
602 m_thread_list.DiscardThreadPlans();
603
Jim Inghamacff8952013-05-02 00:27:30 +0000604 // If we are going to keep the target stopped, then don't send the disconnect message.
605 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000606 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000607 const bool success = m_comm.SendRequestDisconnect();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000608 if (log)
609 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000610 if (success)
611 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000612 else
Jim Ingham77e82d12013-05-09 00:05:35 +0000613 log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000614 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000615 m_comm.Disconnect ();
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000616 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000617 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000618 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000619 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000620
621 SetPrivateState (eStateDetached);
622 ResumePrivateStateThread();
623
624 //KillDebugserverProcess ();
625 return error;
626}
627
628Error
629ProcessKDP::DoDestroy ()
630{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000631 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000632 bool keep_stopped = false;
633 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000634}
635
636//------------------------------------------------------------------
637// Process Queries
638//------------------------------------------------------------------
639
640bool
641ProcessKDP::IsAlive ()
642{
643 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
644}
645
646//------------------------------------------------------------------
647// Process Memory
648//------------------------------------------------------------------
649size_t
650ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
651{
Greg Claytona63d08c2011-07-19 03:57:15 +0000652 if (m_comm.IsConnected())
653 return m_comm.SendRequestReadMemory (addr, buf, size, error);
654 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000655 return 0;
656}
657
658size_t
659ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
660{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000661 if (m_comm.IsConnected())
662 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
663 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000664 return 0;
665}
666
667lldb::addr_t
668ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
669{
670 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
671 return LLDB_INVALID_ADDRESS;
672}
673
674Error
675ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
676{
677 Error error;
678 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
679 return error;
680}
681
682Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000683ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000684{
Greg Clayton07e66e32011-07-20 03:41:06 +0000685 if (m_comm.LocalBreakpointsAreSupported ())
686 {
687 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000688 if (!bp_site->IsEnabled())
689 {
690 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
691 {
692 bp_site->SetEnabled(true);
693 bp_site->SetType (BreakpointSite::eExternal);
694 }
695 else
696 {
697 error.SetErrorString ("KDP set breakpoint failed");
698 }
699 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000700 return error;
701 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000702 return EnableSoftwareBreakpoint (bp_site);
703}
704
705Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000706ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000707{
Greg Clayton07e66e32011-07-20 03:41:06 +0000708 if (m_comm.LocalBreakpointsAreSupported ())
709 {
710 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000711 if (bp_site->IsEnabled())
712 {
713 BreakpointSite::Type bp_type = bp_site->GetType();
714 if (bp_type == BreakpointSite::eExternal)
715 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000716 if (m_destroy_in_process && m_comm.IsRunning())
717 {
718 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000719 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000720 }
Greg Clayton5b882162011-07-21 01:12:01 +0000721 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000722 {
723 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
724 bp_site->SetEnabled(false);
725 else
726 error.SetErrorString ("KDP remove breakpoint failed");
727 }
Greg Clayton5b882162011-07-21 01:12:01 +0000728 }
729 else
730 {
731 error = DisableSoftwareBreakpoint (bp_site);
732 }
733 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000734 return error;
735 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000736 return DisableSoftwareBreakpoint (bp_site);
737}
738
739Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000740ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000741{
742 Error error;
743 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
744 return error;
745}
746
747Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000748ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000749{
750 Error error;
751 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
752 return error;
753}
754
755void
756ProcessKDP::Clear()
757{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000758 m_thread_list.Clear();
759}
760
761Error
762ProcessKDP::DoSignal (int signo)
763{
764 Error error;
765 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
766 return error;
767}
768
769void
770ProcessKDP::Initialize()
771{
772 static bool g_initialized = false;
773
774 if (g_initialized == false)
775 {
776 g_initialized = true;
777 PluginManager::RegisterPlugin (GetPluginNameStatic(),
778 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +0000779 CreateInstance,
780 DebuggerInitialize);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000781
782 Log::Callbacks log_callbacks = {
783 ProcessKDPLog::DisableLog,
784 ProcessKDPLog::EnableLog,
785 ProcessKDPLog::ListLogCategories
786 };
787
788 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
789 }
790}
791
Greg Clayton7f982402013-07-15 22:54:20 +0000792void
793ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger)
794{
795 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
796 {
797 const bool is_global_setting = true;
798 PluginManager::CreateSettingForProcessPlugin (debugger,
799 GetGlobalPluginProperties()->GetValueProperties(),
800 ConstString ("Properties for the kdp-remote process plug-in."),
801 is_global_setting);
802 }
803}
804
Greg Claytonf9765ac2011-07-15 03:27:12 +0000805bool
806ProcessKDP::StartAsyncThread ()
807{
Greg Clayton5160ce52013-03-27 23:08:40 +0000808 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000809
810 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000811 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000812
Greg Clayton7925fbb2012-09-21 16:31:20 +0000813 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
814 return true;
815
Greg Claytonf9765ac2011-07-15 03:27:12 +0000816 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
817 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
818}
819
820void
821ProcessKDP::StopAsyncThread ()
822{
Greg Clayton5160ce52013-03-27 23:08:40 +0000823 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000824
825 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000826 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000827
828 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
829
830 // Stop the stdio thread
831 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
832 {
833 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000834 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000835 }
836}
837
838
839void *
840ProcessKDP::AsyncThread (void *arg)
841{
842 ProcessKDP *process = (ProcessKDP*) arg;
843
Greg Clayton7925fbb2012-09-21 16:31:20 +0000844 const lldb::pid_t pid = process->GetID();
845
Greg Clayton5160ce52013-03-27 23:08:40 +0000846 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000847 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000848 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000849
850 Listener listener ("ProcessKDP::AsyncThread");
851 EventSP event_sp;
852 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
853 eBroadcastBitAsyncThreadShouldExit;
854
Greg Clayton7925fbb2012-09-21 16:31:20 +0000855
Greg Claytonf9765ac2011-07-15 03:27:12 +0000856 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
857 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000858 bool done = false;
859 while (!done)
860 {
861 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000862 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000863 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000864 if (listener.WaitForEvent (NULL, event_sp))
865 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000866 uint32_t event_type = event_sp->GetType();
867 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000868 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000869 pid,
870 event_type);
871
872 // When we are running, poll for 1 second to try and get an exception
873 // to indicate the process has stopped. If we don't get one, check to
874 // make sure no one asked us to exit
875 bool is_running = false;
876 DataExtractor exc_reply_packet;
877 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000878 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000879 switch (event_type)
880 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000881 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000882 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000883 is_running = true;
884 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000885 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000886 ThreadSP thread_sp (process->GetKernelThread());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000887 if (thread_sp)
888 {
889 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
890 if (reg_ctx_sp)
891 reg_ctx_sp->InvalidateAllRegisters();
892 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
893 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000894
Greg Clayton7925fbb2012-09-21 16:31:20 +0000895 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000896 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000897 process->SetPrivateState(eStateStopped);
898 }
899 else
900 {
901 // Check to see if we are supposed to exit. There is no way to
902 // interrupt a running kernel, so all we can do is wait for an
903 // exception or detach...
904 if (listener.GetNextEvent(event_sp))
905 {
906 // We got an event, go through the loop again
907 event_type = event_sp->GetType();
908 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000909 }
910 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000911 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000912
Greg Clayton7925fbb2012-09-21 16:31:20 +0000913 case eBroadcastBitAsyncThreadShouldExit:
914 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000915 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000916 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000917 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000918 is_running = false;
919 break;
920
921 default:
922 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000923 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000924 pid,
925 event_type);
926 done = true;
927 is_running = false;
928 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000929 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000930 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000931 }
932 else
933 {
934 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000935 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000936 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000937 done = true;
938 }
939 }
940 }
941
942 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000943 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000944 arg,
945 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000946
947 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
948 return NULL;
949}
950
951
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000952class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
953{
954private:
955
956 OptionGroupOptions m_option_group;
957 OptionGroupUInt64 m_command_byte;
958 OptionGroupString m_packet_data;
959
960 virtual Options *
961 GetOptions ()
962 {
963 return &m_option_group;
964 }
965
966
967public:
968 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
969 CommandObjectParsed (interpreter,
970 "process plugin packet send",
971 "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. ",
972 NULL),
973 m_option_group (interpreter),
974 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),
975 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)
976 {
977 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
978 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
979 m_option_group.Finalize();
980 }
981
982 ~CommandObjectProcessKDPPacketSend ()
983 {
984 }
985
986 bool
987 DoExecute (Args& command, CommandReturnObject &result)
988 {
989 const size_t argc = command.GetArgumentCount();
990 if (argc == 0)
991 {
992 if (!m_command_byte.GetOptionValue().OptionWasSet())
993 {
994 result.AppendError ("the --command option must be set to a valid command byte");
995 result.SetStatus (eReturnStatusFailed);
996 }
997 else
998 {
999 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
1000 if (command_byte > 0 && command_byte <= UINT8_MAX)
1001 {
1002 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
1003 if (process)
1004 {
1005 const StateType state = process->GetState();
1006
1007 if (StateIsStoppedState (state, true))
1008 {
1009 std::vector<uint8_t> payload_bytes;
1010 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
1011 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
1012 {
1013 StringExtractor extractor(ascii_hex_bytes_cstr);
1014 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
1015 if (ascii_hex_bytes_cstr_len & 1)
1016 {
1017 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
1018 result.SetStatus (eReturnStatusFailed);
1019 return false;
1020 }
1021 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
1022 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
1023 {
1024 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
1025 result.SetStatus (eReturnStatusFailed);
1026 return false;
1027 }
1028 }
1029 Error error;
1030 DataExtractor reply;
1031 process->GetCommunication().SendRawRequest (command_byte,
1032 payload_bytes.empty() ? NULL : payload_bytes.data(),
1033 payload_bytes.size(),
1034 reply,
1035 error);
1036
1037 if (error.Success())
1038 {
1039 // Copy the binary bytes into a hex ASCII string for the result
1040 StreamString packet;
1041 packet.PutBytesAsRawHex8(reply.GetDataStart(),
1042 reply.GetByteSize(),
1043 lldb::endian::InlHostByteOrder(),
1044 lldb::endian::InlHostByteOrder());
1045 result.AppendMessage(packet.GetString().c_str());
1046 result.SetStatus (eReturnStatusSuccessFinishResult);
1047 return true;
1048 }
1049 else
1050 {
1051 const char *error_cstr = error.AsCString();
1052 if (error_cstr && error_cstr[0])
1053 result.AppendError (error_cstr);
1054 else
1055 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
1056 result.SetStatus (eReturnStatusFailed);
1057 return false;
1058 }
1059 }
1060 else
1061 {
1062 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
1063 result.SetStatus (eReturnStatusFailed);
1064 }
1065 }
1066 else
1067 {
1068 result.AppendError ("invalid process");
1069 result.SetStatus (eReturnStatusFailed);
1070 }
1071 }
1072 else
1073 {
Daniel Malead01b2952012-11-29 21:49:15 +00001074 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001075 result.SetStatus (eReturnStatusFailed);
1076 }
1077 }
1078 }
1079 else
1080 {
1081 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
1082 result.SetStatus (eReturnStatusFailed);
1083 }
1084 return false;
1085 }
1086};
1087
1088class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1089{
1090private:
1091
1092public:
1093 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1094 CommandObjectMultiword (interpreter,
1095 "process plugin packet",
1096 "Commands that deal with KDP remote packets.",
1097 NULL)
1098 {
1099 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1100 }
1101
1102 ~CommandObjectProcessKDPPacket ()
1103 {
1104 }
1105};
1106
1107class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1108{
1109public:
1110 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1111 CommandObjectMultiword (interpreter,
1112 "process plugin",
1113 "A set of commands for operating on a ProcessKDP process.",
1114 "process plugin <subcommand> [<subcommand-options>]")
1115 {
1116 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1117 }
1118
1119 ~CommandObjectMultiwordProcessKDP ()
1120 {
1121 }
1122};
1123
1124CommandObject *
1125ProcessKDP::GetPluginCommandObject()
1126{
1127 if (!m_command_sp)
1128 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1129 return m_command_sp.get();
1130}
1131