blob: c2ae2b962e50db9484744c8eda4ed1ede1920e3c [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
Charles Davis510938e2013-08-27 05:04:57 +000036#define USEC_PER_SEC 1000000
37
Greg Claytonf9765ac2011-07-15 03:27:12 +000038// Project includes
39#include "ProcessKDP.h"
40#include "ProcessKDPLog.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000041#include "ThreadKDP.h"
Jason Molenda5e8534e2012-10-03 01:29:34 +000042#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
Jason Molenda840f12c2012-10-25 00:25:13 +000043#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
Greg Clayton1d19a2f2012-10-19 22:22:57 +000044#include "Utility/StringExtractor.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Greg Clayton7f982402013-07-15 22:54:20 +000049namespace {
50
51 static PropertyDefinition
52 g_properties[] =
53 {
54 { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." },
55 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
56 };
57
58 enum
59 {
60 ePropertyPacketTimeout
61 };
62
63 class PluginProperties : public Properties
64 {
65 public:
66
67 static ConstString
68 GetSettingName ()
69 {
70 return ProcessKDP::GetPluginNameStatic();
71 }
72
73 PluginProperties() :
74 Properties ()
75 {
76 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
77 m_collection_sp->Initialize(g_properties);
78 }
79
80 virtual
81 ~PluginProperties()
82 {
83 }
84
85 uint64_t
86 GetPacketTimeout()
87 {
88 const uint32_t idx = ePropertyPacketTimeout;
89 return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
90 }
91 };
92
93 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
94
95 static const ProcessKDPPropertiesSP &
96 GetGlobalPluginProperties()
97 {
98 static ProcessKDPPropertiesSP g_settings_sp;
99 if (!g_settings_sp)
100 g_settings_sp.reset (new PluginProperties ());
101 return g_settings_sp;
102 }
103
104} // anonymous namespace end
105
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000106static const lldb::tid_t g_kernel_tid = 1;
107
Greg Clayton57abc5d2013-05-10 21:47:16 +0000108ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +0000109ProcessKDP::GetPluginNameStatic()
110{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000111 static ConstString g_name("kdp-remote");
112 return g_name;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000113}
114
115const char *
116ProcessKDP::GetPluginDescriptionStatic()
117{
118 return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
119}
120
121void
122ProcessKDP::Terminate()
123{
124 PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
125}
126
127
Greg Claytonc3776bf2012-02-09 06:16:32 +0000128lldb::ProcessSP
129ProcessKDP::CreateInstance (Target &target,
130 Listener &listener,
131 const FileSpec *crash_file_path)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000132{
Greg Claytonc3776bf2012-02-09 06:16:32 +0000133 lldb::ProcessSP process_sp;
134 if (crash_file_path == NULL)
135 process_sp.reset(new ProcessKDP (target, listener));
136 return process_sp;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000137}
138
139bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000140ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000141{
Greg Clayton596ed242011-10-21 21:41:45 +0000142 if (plugin_specified_by_name)
143 return true;
144
Greg Claytonf9765ac2011-07-15 03:27:12 +0000145 // For now we are just making sure the file exists for a given module
Greg Claytonaa149cb2011-08-11 02:48:45 +0000146 Module *exe_module = target.GetExecutableModulePointer();
147 if (exe_module)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000148 {
149 const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
Greg Clayton70512312012-05-08 01:45:38 +0000150 switch (triple_ref.getOS())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000151 {
Greg Clayton70512312012-05-08 01:45:38 +0000152 case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
153 case llvm::Triple::MacOSX: // For desktop targets
154 case llvm::Triple::IOS: // For arm targets
155 if (triple_ref.getVendor() == llvm::Triple::Apple)
156 {
157 ObjectFile *exe_objfile = exe_module->GetObjectFile();
158 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
159 exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
160 return true;
161 }
162 break;
163
164 default:
165 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000166 }
167 }
Greg Clayton596ed242011-10-21 21:41:45 +0000168 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000169}
170
171//----------------------------------------------------------------------
172// ProcessKDP constructor
173//----------------------------------------------------------------------
174ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
175 Process (target, listener),
176 m_comm("lldb.process.kdp-remote.communication"),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000177 m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
Greg Clayton97d5cf02012-09-25 02:40:06 +0000178 m_async_thread (LLDB_INVALID_HOST_THREAD),
Jason Molenda5e8534e2012-10-03 01:29:34 +0000179 m_dyld_plugin_name (),
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000180 m_kernel_load_addr (LLDB_INVALID_ADDRESS),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000181 m_command_sp(),
182 m_kernel_thread_wp()
Greg Claytonf9765ac2011-07-15 03:27:12 +0000183{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000184 m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit");
185 m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue");
Greg Clayton7f982402013-07-15 22:54:20 +0000186 const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
187 if (timeout_seconds > 0)
188 m_comm.SetPacketTimeout(timeout_seconds);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000189}
190
191//----------------------------------------------------------------------
192// Destructor
193//----------------------------------------------------------------------
194ProcessKDP::~ProcessKDP()
195{
196 Clear();
Greg Claytone24c4ac2011-11-17 04:46:02 +0000197 // We need to call finalize on the process before destroying ourselves
198 // to make sure all of the broadcaster cleanup goes as planned. If we
199 // destruct this class, then Process::~Process() might have problems
200 // trying to fully destroy the broadcaster.
201 Finalize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000202}
203
204//----------------------------------------------------------------------
205// PluginInterface
206//----------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000207lldb_private::ConstString
Greg Claytonf9765ac2011-07-15 03:27:12 +0000208ProcessKDP::GetPluginName()
209{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000210 return GetPluginNameStatic();
211}
212
213uint32_t
214ProcessKDP::GetPluginVersion()
215{
216 return 1;
217}
218
219Error
220ProcessKDP::WillLaunch (Module* module)
221{
222 Error error;
223 error.SetErrorString ("launching not supported in kdp-remote plug-in");
224 return error;
225}
226
227Error
228ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
229{
230 Error error;
231 error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
232 return error;
233}
234
235Error
236ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
237{
238 Error error;
239 error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
240 return error;
241}
242
243Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000244ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000245{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000246 Error error;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000247
248 // Don't let any JIT happen when doing KDP as we can't allocate
249 // memory and we don't want to be mucking with threads that might
250 // already be handling exceptions
251 SetCanJIT(false);
252
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000253 if (remote_url == NULL || remote_url[0] == '\0')
Greg Clayton7925fbb2012-09-21 16:31:20 +0000254 {
255 error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
256 return error;
257 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000258
Greg Clayton7b0992d2013-04-18 22:45:39 +0000259 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000260 if (conn_ap.get())
261 {
262 // Only try once for now.
263 // TODO: check if we should be retrying?
264 const uint32_t max_retry_count = 1;
265 for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
266 {
267 if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
268 break;
269 usleep (100000);
270 }
271 }
272
273 if (conn_ap->IsConnected())
274 {
275 const uint16_t reply_port = conn_ap->GetReadPort ();
276
277 if (reply_port != 0)
278 {
279 m_comm.SetConnection(conn_ap.release());
280
281 if (m_comm.SendRequestReattach(reply_port))
282 {
283 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
284 {
285 m_comm.GetVersion();
286 uint32_t cpu = m_comm.GetCPUType();
287 uint32_t sub = m_comm.GetCPUSubtype();
288 ArchSpec kernel_arch;
289 kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
290 m_target.SetArchitecture(kernel_arch);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000291
Jason Molenda840f12c2012-10-25 00:25:13 +0000292 /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */
293 /* An EFI kdp session has neither UUID nor load address. */
294
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000295 UUID kernel_uuid = m_comm.GetUUID ();
296 addr_t kernel_load_addr = m_comm.GetLoadAddress ();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000297
Jason Molenda840f12c2012-10-25 00:25:13 +0000298 if (m_comm.RemoteIsEFI ())
299 {
300 m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
301 }
Jason Molendaca2ffa72013-05-09 23:52:21 +0000302 else if (m_comm.RemoteIsDarwinKernel ())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000303 {
Jason Molendaca2ffa72013-05-09 23:52:21 +0000304 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
Jason Molendaa8ea4ba2013-05-06 23:02:03 +0000305 if (kernel_load_addr != LLDB_INVALID_ADDRESS)
306 {
307 m_kernel_load_addr = kernel_load_addr;
308 }
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000309 }
310
Greg Clayton97d5cf02012-09-25 02:40:06 +0000311 // Set the thread ID
312 UpdateThreadListIfNeeded ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000313 SetID (1);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000314 GetThreadList ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000315 SetPrivateState (eStateStopped);
Greg Clayton07e66e32011-07-20 03:41:06 +0000316 StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
317 if (async_strm_sp)
318 {
Greg Clayton5b882162011-07-21 01:12:01 +0000319 const char *cstr;
320 if ((cstr = m_comm.GetKernelVersion ()) != NULL)
Greg Clayton07e66e32011-07-20 03:41:06 +0000321 {
Greg Clayton5b882162011-07-21 01:12:01 +0000322 async_strm_sp->Printf ("Version: %s\n", cstr);
Greg Clayton07e66e32011-07-20 03:41:06 +0000323 async_strm_sp->Flush();
324 }
Greg Clayton5b882162011-07-21 01:12:01 +0000325// if ((cstr = m_comm.GetImagePath ()) != NULL)
326// {
327// async_strm_sp->Printf ("Image Path: %s\n", cstr);
328// async_strm_sp->Flush();
329// }
Greg Clayton07e66e32011-07-20 03:41:06 +0000330 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000331 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000332 else
333 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000334 error.SetErrorString("KDP_REATTACH failed");
335 }
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000336 }
337 else
338 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000339 error.SetErrorString("KDP_REATTACH failed");
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000340 }
341 }
342 else
343 {
344 error.SetErrorString("invalid reply port from UDP connection");
345 }
346 }
347 else
348 {
349 if (error.Success())
350 error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
351 }
352 if (error.Fail())
353 m_comm.Disconnect();
354
Greg Claytonf9765ac2011-07-15 03:27:12 +0000355 return error;
356}
357
358//----------------------------------------------------------------------
359// Process Control
360//----------------------------------------------------------------------
361Error
Greg Clayton982c9762011-11-03 21:22:33 +0000362ProcessKDP::DoLaunch (Module *exe_module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000363 ProcessLaunchInfo &launch_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000364{
365 Error error;
366 error.SetErrorString ("launching not supported in kdp-remote plug-in");
367 return error;
368}
369
370
371Error
372ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
373{
374 Error error;
375 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
376 return error;
377}
378
Greg Claytonf9765ac2011-07-15 03:27:12 +0000379Error
Han Ming Ong84647042012-02-25 01:07:38 +0000380ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
381{
382 Error error;
383 error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
384 return error;
385}
386
387Error
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +0000388ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000389{
390 Error error;
391 error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
392 return error;
393}
394
395
396void
397ProcessKDP::DidAttach ()
398{
Greg Clayton5160ce52013-03-27 23:08:40 +0000399 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000400 if (log)
Johnny Chen54cb8f82011-10-11 21:17:10 +0000401 log->Printf ("ProcessKDP::DidAttach()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000402 if (GetID() != LLDB_INVALID_PROCESS_ID)
403 {
404 // TODO: figure out the register context that we will use
405 }
406}
407
Jason Molenda5e8534e2012-10-03 01:29:34 +0000408addr_t
409ProcessKDP::GetImageInfoAddress()
410{
411 return m_kernel_load_addr;
412}
413
414lldb_private::DynamicLoader *
415ProcessKDP::GetDynamicLoader ()
416{
417 if (m_dyld_ap.get() == NULL)
Jason Molenda2e56a252013-05-11 03:09:05 +0000418 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 +0000419 return m_dyld_ap.get();
420}
421
Greg Claytonf9765ac2011-07-15 03:27:12 +0000422Error
423ProcessKDP::WillResume ()
424{
425 return Error();
426}
427
428Error
429ProcessKDP::DoResume ()
430{
431 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000432 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000433 // Only start the async thread if we try to do any process control
434 if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
435 StartAsyncThread ();
436
Greg Clayton97d5cf02012-09-25 02:40:06 +0000437 bool resume = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000438
Greg Clayton97d5cf02012-09-25 02:40:06 +0000439 // With KDP there is only one thread we can tell what to do
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000440 ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
441
Greg Clayton97d5cf02012-09-25 02:40:06 +0000442 if (kernel_thread_sp)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000443 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000444 const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000445
446 if (log)
447 log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
Greg Clayton7925fbb2012-09-21 16:31:20 +0000448 switch (thread_resume_state)
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000449 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000450 case eStateSuspended:
451 // Nothing to do here when a thread will stay suspended
452 // we just leave the CPU mask bit set to zero for the thread
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000453 if (log)
454 log->Printf ("ProcessKDP::DoResume() = suspended???");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000455 break;
456
457 case eStateStepping:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000458 {
459 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
460
461 if (reg_ctx_sp)
462 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000463 if (log)
464 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
Greg Clayton1afa68e2013-04-02 20:32:37 +0000465 reg_ctx_sp->HardwareSingleStep (true);
466 resume = true;
467 }
468 else
469 {
470 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
471 }
472 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000473 break;
474
Greg Clayton7925fbb2012-09-21 16:31:20 +0000475 case eStateRunning:
Greg Clayton1afa68e2013-04-02 20:32:37 +0000476 {
477 lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
478
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000479 if (reg_ctx_sp)
480 {
481 if (log)
482 log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
483 reg_ctx_sp->HardwareSingleStep (false);
484 resume = true;
485 }
486 else
487 {
488 error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
489 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000490 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000491 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000492
Greg Clayton7925fbb2012-09-21 16:31:20 +0000493 default:
Greg Clayton97d5cf02012-09-25 02:40:06 +0000494 // The only valid thread resume states are listed above
Greg Clayton7925fbb2012-09-21 16:31:20 +0000495 assert (!"invalid thread resume state");
496 break;
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000497 }
498 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000499
500 if (resume)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000501 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000502 if (log)
503 log->Printf ("ProcessKDP::DoResume () sending resume");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000504
Greg Clayton97d5cf02012-09-25 02:40:06 +0000505 if (m_comm.SendRequestResume ())
Greg Clayton7925fbb2012-09-21 16:31:20 +0000506 {
507 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
508 SetPrivateState(eStateRunning);
509 }
510 else
511 error.SetErrorString ("KDP resume failed");
512 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000513 else
Greg Clayton7925fbb2012-09-21 16:31:20 +0000514 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000515 error.SetErrorString ("kernel thread is suspended");
Greg Clayton7925fbb2012-09-21 16:31:20 +0000516 }
517
Greg Claytonf9765ac2011-07-15 03:27:12 +0000518 return error;
519}
520
Greg Clayton97d5cf02012-09-25 02:40:06 +0000521lldb::ThreadSP
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000522ProcessKDP::GetKernelThread()
Greg Clayton97d5cf02012-09-25 02:40:06 +0000523{
524 // KDP only tells us about one thread/core. Any other threads will usually
525 // be the ones that are read from memory by the OS plug-ins.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000526
527 ThreadSP thread_sp (m_kernel_thread_wp.lock());
Greg Clayton97d5cf02012-09-25 02:40:06 +0000528 if (!thread_sp)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000529 {
530 thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
531 m_kernel_thread_wp = thread_sp;
532 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000533 return thread_sp;
534}
535
536
537
538
Greg Clayton9fc13552012-04-10 00:18:59 +0000539bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000540ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000541{
542 // locker will keep a mutex locked until it goes out of scope
Greg Clayton5160ce52013-03-27 23:08:40 +0000543 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000544 if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000545 log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000546
Greg Clayton39da3ef2013-04-11 22:23:34 +0000547 // Even though there is a CPU mask, it doesn't mean we can see each CPU
Greg Clayton97d5cf02012-09-25 02:40:06 +0000548 // indivudually, there is really only one. Lets call this thread 1.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000549 ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
550 if (!thread_sp)
551 thread_sp = GetKernelThread ();
552 new_thread_list.AddThread(thread_sp);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000553
Greg Clayton9fc13552012-04-10 00:18:59 +0000554 return new_thread_list.GetSize(false) > 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000555}
556
Greg Claytonf9765ac2011-07-15 03:27:12 +0000557void
558ProcessKDP::RefreshStateAfterStop ()
559{
560 // Let all threads recover from stopping and do any clean up based
561 // on the previous thread state (if any).
562 m_thread_list.RefreshStateAfterStop();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000563}
564
565Error
566ProcessKDP::DoHalt (bool &caused_stop)
567{
568 Error error;
569
Greg Clayton97d5cf02012-09-25 02:40:06 +0000570 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000571 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000572 if (m_destroy_in_process)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000573 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000574 // If we are attemping to destroy, we need to not return an error to
575 // Halt or DoDestroy won't get called.
576 // We are also currently running, so send a process stopped event
577 SetPrivateState (eStateStopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000578 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000579 else
Greg Claytonf9765ac2011-07-15 03:27:12 +0000580 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000581 error.SetErrorString ("KDP cannot interrupt a running kernel");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000582 }
583 }
584 return error;
585}
586
587Error
Jim Inghamacff8952013-05-02 00:27:30 +0000588ProcessKDP::DoDetach(bool keep_stopped)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000589{
590 Error error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000591 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000592 if (log)
Jim Inghamacff8952013-05-02 00:27:30 +0000593 log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000594
Greg Clayton97d5cf02012-09-25 02:40:06 +0000595 if (m_comm.IsRunning())
Greg Claytonf9765ac2011-07-15 03:27:12 +0000596 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000597 // We are running and we can't interrupt a running kernel, so we need
598 // to just close the connection to the kernel and hope for the best
599 }
600 else
601 {
Jim Inghamacff8952013-05-02 00:27:30 +0000602 // If we are going to keep the target stopped, then don't send the disconnect message.
603 if (!keep_stopped && m_comm.IsConnected())
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000604 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000605 const bool success = m_comm.SendRequestDisconnect();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000606 if (log)
607 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000608 if (success)
609 log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000610 else
Jim Ingham77e82d12013-05-09 00:05:35 +0000611 log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
Greg Clayton97d5cf02012-09-25 02:40:06 +0000612 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000613 m_comm.Disconnect ();
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000614 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000615 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000616 StopAsyncThread ();
Greg Clayton74d41932012-01-31 04:56:17 +0000617 m_comm.Clear();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000618
619 SetPrivateState (eStateDetached);
620 ResumePrivateStateThread();
621
622 //KillDebugserverProcess ();
623 return error;
624}
625
626Error
627ProcessKDP::DoDestroy ()
628{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000629 // For KDP there really is no difference between destroy and detach
Jim Inghamacff8952013-05-02 00:27:30 +0000630 bool keep_stopped = false;
631 return DoDetach(keep_stopped);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000632}
633
634//------------------------------------------------------------------
635// Process Queries
636//------------------------------------------------------------------
637
638bool
639ProcessKDP::IsAlive ()
640{
641 return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
642}
643
644//------------------------------------------------------------------
645// Process Memory
646//------------------------------------------------------------------
647size_t
648ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
649{
Jason Molenda8eb32812014-05-21 23:44:02 +0000650 uint8_t *data_buffer = (uint8_t *) buf;
Greg Claytona63d08c2011-07-19 03:57:15 +0000651 if (m_comm.IsConnected())
Jason Molenda8eb32812014-05-21 23:44:02 +0000652 {
653 const size_t max_read_size = 512;
654 size_t total_bytes_read = 0;
655
656 // Read the requested amount of memory in 512 byte chunks
657 while (total_bytes_read < size)
658 {
659 size_t bytes_to_read_this_request = size - total_bytes_read;
660 if (bytes_to_read_this_request > max_read_size)
661 {
662 bytes_to_read_this_request = max_read_size;
663 }
664 size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read,
665 data_buffer + total_bytes_read,
666 bytes_to_read_this_request, error);
667 total_bytes_read += bytes_read;
668 if (error.Fail() || bytes_read == 0)
669 {
670 return total_bytes_read;
671 }
672 }
673
674 return total_bytes_read;
675 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000676 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000677 return 0;
678}
679
680size_t
681ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
682{
Greg Clayton7925fbb2012-09-21 16:31:20 +0000683 if (m_comm.IsConnected())
684 return m_comm.SendRequestWriteMemory (addr, buf, size, error);
685 error.SetErrorString ("not connected");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000686 return 0;
687}
688
689lldb::addr_t
690ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
691{
692 error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
693 return LLDB_INVALID_ADDRESS;
694}
695
696Error
697ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
698{
699 Error error;
700 error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
701 return error;
702}
703
704Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000705ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000706{
Greg Clayton07e66e32011-07-20 03:41:06 +0000707 if (m_comm.LocalBreakpointsAreSupported ())
708 {
709 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000710 if (!bp_site->IsEnabled())
711 {
712 if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
713 {
714 bp_site->SetEnabled(true);
715 bp_site->SetType (BreakpointSite::eExternal);
716 }
717 else
718 {
719 error.SetErrorString ("KDP set breakpoint failed");
720 }
721 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000722 return error;
723 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000724 return EnableSoftwareBreakpoint (bp_site);
725}
726
727Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000728ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000729{
Greg Clayton07e66e32011-07-20 03:41:06 +0000730 if (m_comm.LocalBreakpointsAreSupported ())
731 {
732 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000733 if (bp_site->IsEnabled())
734 {
735 BreakpointSite::Type bp_type = bp_site->GetType();
736 if (bp_type == BreakpointSite::eExternal)
737 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000738 if (m_destroy_in_process && m_comm.IsRunning())
739 {
740 // We are trying to destroy our connection and we are running
Greg Clayton5b882162011-07-21 01:12:01 +0000741 bp_site->SetEnabled(false);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000742 }
Greg Clayton5b882162011-07-21 01:12:01 +0000743 else
Greg Clayton97d5cf02012-09-25 02:40:06 +0000744 {
745 if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
746 bp_site->SetEnabled(false);
747 else
748 error.SetErrorString ("KDP remove breakpoint failed");
749 }
Greg Clayton5b882162011-07-21 01:12:01 +0000750 }
751 else
752 {
753 error = DisableSoftwareBreakpoint (bp_site);
754 }
755 }
Greg Clayton07e66e32011-07-20 03:41:06 +0000756 return error;
757 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000758 return DisableSoftwareBreakpoint (bp_site);
759}
760
761Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000762ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000763{
764 Error error;
765 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
766 return error;
767}
768
769Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000770ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000771{
772 Error error;
773 error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
774 return error;
775}
776
777void
778ProcessKDP::Clear()
779{
Greg Claytonf9765ac2011-07-15 03:27:12 +0000780 m_thread_list.Clear();
781}
782
783Error
784ProcessKDP::DoSignal (int signo)
785{
786 Error error;
787 error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
788 return error;
789}
790
791void
792ProcessKDP::Initialize()
793{
794 static bool g_initialized = false;
795
796 if (g_initialized == false)
797 {
798 g_initialized = true;
799 PluginManager::RegisterPlugin (GetPluginNameStatic(),
800 GetPluginDescriptionStatic(),
Greg Clayton7f982402013-07-15 22:54:20 +0000801 CreateInstance,
802 DebuggerInitialize);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000803
804 Log::Callbacks log_callbacks = {
805 ProcessKDPLog::DisableLog,
806 ProcessKDPLog::EnableLog,
807 ProcessKDPLog::ListLogCategories
808 };
809
810 Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
811 }
812}
813
Greg Clayton7f982402013-07-15 22:54:20 +0000814void
815ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger)
816{
817 if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
818 {
819 const bool is_global_setting = true;
820 PluginManager::CreateSettingForProcessPlugin (debugger,
821 GetGlobalPluginProperties()->GetValueProperties(),
822 ConstString ("Properties for the kdp-remote process plug-in."),
823 is_global_setting);
824 }
825}
826
Greg Claytonf9765ac2011-07-15 03:27:12 +0000827bool
828ProcessKDP::StartAsyncThread ()
829{
Greg Clayton5160ce52013-03-27 23:08:40 +0000830 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000831
832 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000833 log->Printf ("ProcessKDP::StartAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000834
Greg Clayton7925fbb2012-09-21 16:31:20 +0000835 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
836 return true;
837
Greg Claytonf9765ac2011-07-15 03:27:12 +0000838 m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
839 return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
840}
841
842void
843ProcessKDP::StopAsyncThread ()
844{
Greg Clayton5160ce52013-03-27 23:08:40 +0000845 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000846
847 if (log)
Greg Clayton7925fbb2012-09-21 16:31:20 +0000848 log->Printf ("ProcessKDP::StopAsyncThread ()");
Greg Claytonf9765ac2011-07-15 03:27:12 +0000849
850 m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
851
852 // Stop the stdio thread
853 if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
854 {
855 Host::ThreadJoin (m_async_thread, NULL, NULL);
Greg Clayton7925fbb2012-09-21 16:31:20 +0000856 m_async_thread = LLDB_INVALID_HOST_THREAD;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000857 }
858}
859
860
861void *
862ProcessKDP::AsyncThread (void *arg)
863{
864 ProcessKDP *process = (ProcessKDP*) arg;
865
Greg Clayton7925fbb2012-09-21 16:31:20 +0000866 const lldb::pid_t pid = process->GetID();
867
Greg Clayton5160ce52013-03-27 23:08:40 +0000868 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000869 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000870 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000871
872 Listener listener ("ProcessKDP::AsyncThread");
873 EventSP event_sp;
874 const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
875 eBroadcastBitAsyncThreadShouldExit;
876
Greg Clayton7925fbb2012-09-21 16:31:20 +0000877
Greg Claytonf9765ac2011-07-15 03:27:12 +0000878 if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
879 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000880 bool done = false;
881 while (!done)
882 {
883 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000884 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000885 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000886 if (listener.WaitForEvent (NULL, event_sp))
887 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000888 uint32_t event_type = event_sp->GetType();
889 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000890 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000891 pid,
892 event_type);
893
894 // When we are running, poll for 1 second to try and get an exception
895 // to indicate the process has stopped. If we don't get one, check to
896 // make sure no one asked us to exit
897 bool is_running = false;
898 DataExtractor exc_reply_packet;
899 do
Greg Claytonf9765ac2011-07-15 03:27:12 +0000900 {
Greg Claytonf9765ac2011-07-15 03:27:12 +0000901 switch (event_type)
902 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000903 case eBroadcastBitAsyncContinue:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000904 {
Greg Clayton7925fbb2012-09-21 16:31:20 +0000905 is_running = true;
906 if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
Greg Claytonf9765ac2011-07-15 03:27:12 +0000907 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000908 ThreadSP thread_sp (process->GetKernelThread());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000909 if (thread_sp)
910 {
911 lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
912 if (reg_ctx_sp)
913 reg_ctx_sp->InvalidateAllRegisters();
914 static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
915 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000916
Greg Clayton7925fbb2012-09-21 16:31:20 +0000917 // TODO: parse the stop reply packet
Greg Clayton97d5cf02012-09-25 02:40:06 +0000918 is_running = false;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000919 process->SetPrivateState(eStateStopped);
920 }
921 else
922 {
923 // Check to see if we are supposed to exit. There is no way to
924 // interrupt a running kernel, so all we can do is wait for an
925 // exception or detach...
926 if (listener.GetNextEvent(event_sp))
927 {
928 // We got an event, go through the loop again
929 event_type = event_sp->GetType();
930 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000931 }
932 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000933 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000934
Greg Clayton7925fbb2012-09-21 16:31:20 +0000935 case eBroadcastBitAsyncThreadShouldExit:
936 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000937 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000938 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000939 done = true;
Greg Clayton7925fbb2012-09-21 16:31:20 +0000940 is_running = false;
941 break;
942
943 default:
944 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000945 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000946 pid,
947 event_type);
948 done = true;
949 is_running = false;
950 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000951 }
Greg Clayton7925fbb2012-09-21 16:31:20 +0000952 } while (is_running);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000953 }
954 else
955 {
956 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000957 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000958 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000959 done = true;
960 }
961 }
962 }
963
964 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000965 log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
Greg Clayton7925fbb2012-09-21 16:31:20 +0000966 arg,
967 pid);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000968
969 process->m_async_thread = LLDB_INVALID_HOST_THREAD;
970 return NULL;
971}
972
973
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000974class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
975{
976private:
977
978 OptionGroupOptions m_option_group;
979 OptionGroupUInt64 m_command_byte;
980 OptionGroupString m_packet_data;
981
982 virtual Options *
983 GetOptions ()
984 {
985 return &m_option_group;
986 }
987
988
989public:
990 CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
991 CommandObjectParsed (interpreter,
992 "process plugin packet send",
993 "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. ",
994 NULL),
995 m_option_group (interpreter),
996 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),
997 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)
998 {
999 m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
1000 m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
1001 m_option_group.Finalize();
1002 }
1003
1004 ~CommandObjectProcessKDPPacketSend ()
1005 {
1006 }
1007
1008 bool
1009 DoExecute (Args& command, CommandReturnObject &result)
1010 {
1011 const size_t argc = command.GetArgumentCount();
1012 if (argc == 0)
1013 {
1014 if (!m_command_byte.GetOptionValue().OptionWasSet())
1015 {
1016 result.AppendError ("the --command option must be set to a valid command byte");
1017 result.SetStatus (eReturnStatusFailed);
1018 }
1019 else
1020 {
1021 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
1022 if (command_byte > 0 && command_byte <= UINT8_MAX)
1023 {
1024 ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
1025 if (process)
1026 {
1027 const StateType state = process->GetState();
1028
1029 if (StateIsStoppedState (state, true))
1030 {
1031 std::vector<uint8_t> payload_bytes;
1032 const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
1033 if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
1034 {
1035 StringExtractor extractor(ascii_hex_bytes_cstr);
1036 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
1037 if (ascii_hex_bytes_cstr_len & 1)
1038 {
1039 result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
1040 result.SetStatus (eReturnStatusFailed);
1041 return false;
1042 }
1043 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
1044 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
1045 {
1046 result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
1047 result.SetStatus (eReturnStatusFailed);
1048 return false;
1049 }
1050 }
1051 Error error;
1052 DataExtractor reply;
1053 process->GetCommunication().SendRawRequest (command_byte,
1054 payload_bytes.empty() ? NULL : payload_bytes.data(),
1055 payload_bytes.size(),
1056 reply,
1057 error);
1058
1059 if (error.Success())
1060 {
1061 // Copy the binary bytes into a hex ASCII string for the result
1062 StreamString packet;
1063 packet.PutBytesAsRawHex8(reply.GetDataStart(),
1064 reply.GetByteSize(),
1065 lldb::endian::InlHostByteOrder(),
1066 lldb::endian::InlHostByteOrder());
1067 result.AppendMessage(packet.GetString().c_str());
1068 result.SetStatus (eReturnStatusSuccessFinishResult);
1069 return true;
1070 }
1071 else
1072 {
1073 const char *error_cstr = error.AsCString();
1074 if (error_cstr && error_cstr[0])
1075 result.AppendError (error_cstr);
1076 else
1077 result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
1078 result.SetStatus (eReturnStatusFailed);
1079 return false;
1080 }
1081 }
1082 else
1083 {
1084 result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
1085 result.SetStatus (eReturnStatusFailed);
1086 }
1087 }
1088 else
1089 {
1090 result.AppendError ("invalid process");
1091 result.SetStatus (eReturnStatusFailed);
1092 }
1093 }
1094 else
1095 {
Daniel Malead01b2952012-11-29 21:49:15 +00001096 result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001097 result.SetStatus (eReturnStatusFailed);
1098 }
1099 }
1100 }
1101 else
1102 {
1103 result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
1104 result.SetStatus (eReturnStatusFailed);
1105 }
1106 return false;
1107 }
1108};
1109
1110class CommandObjectProcessKDPPacket : public CommandObjectMultiword
1111{
1112private:
1113
1114public:
1115 CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
1116 CommandObjectMultiword (interpreter,
1117 "process plugin packet",
1118 "Commands that deal with KDP remote packets.",
1119 NULL)
1120 {
1121 LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
1122 }
1123
1124 ~CommandObjectProcessKDPPacket ()
1125 {
1126 }
1127};
1128
1129class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
1130{
1131public:
1132 CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
1133 CommandObjectMultiword (interpreter,
1134 "process plugin",
1135 "A set of commands for operating on a ProcessKDP process.",
1136 "process plugin <subcommand> [<subcommand-options>]")
1137 {
1138 LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter)));
1139 }
1140
1141 ~CommandObjectMultiwordProcessKDP ()
1142 {
1143 }
1144};
1145
1146CommandObject *
1147ProcessKDP::GetPluginCommandObject()
1148{
1149 if (!m_command_sp)
1150 m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
1151 return m_command_sp.get();
1152}
1153