159ec512cSGreg Clayton //===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===//
2f9765acdSGreg Clayton //
3f9765acdSGreg Clayton //                     The LLVM Compiler Infrastructure
4f9765acdSGreg Clayton //
5f9765acdSGreg Clayton // This file is distributed under the University of Illinois Open Source
6f9765acdSGreg Clayton // License. See LICENSE.TXT for details.
7f9765acdSGreg Clayton //
8f9765acdSGreg Clayton //===----------------------------------------------------------------------===//
9f9765acdSGreg Clayton 
10f9765acdSGreg Clayton // C Includes
11f9765acdSGreg Clayton #include <errno.h>
12f9765acdSGreg Clayton #include <stdlib.h>
13f9765acdSGreg Clayton 
14f9765acdSGreg Clayton // C++ Includes
15*3f69fa6fSBenjamin Kramer #include <mutex>
16*3f69fa6fSBenjamin Kramer 
17f9765acdSGreg Clayton // Other libraries and framework includes
1807e66e3eSGreg Clayton #include "lldb/Core/Debugger.h"
19f9765acdSGreg Clayton #include "lldb/Core/PluginManager.h"
201f746071SGreg Clayton #include "lldb/Core/Module.h"
214bd4e7e3SJason Molenda #include "lldb/Core/ModuleSpec.h"
22f9765acdSGreg Clayton #include "lldb/Core/State.h"
234bd4e7e3SJason Molenda #include "lldb/Core/UUID.h"
2493a66fc1SZachary Turner #include "lldb/Host/ConnectionFileDescriptor.h"
25f9765acdSGreg Clayton #include "lldb/Host/Host.h"
264bd4e7e3SJason Molenda #include "lldb/Host/Symbols.h"
273d7162b1SKeno Fischer #include "lldb/Host/Socket.h"
2839de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h"
291d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
301d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObject.h"
311d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObjectMultiword.h"
321d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandReturnObject.h"
331d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupString.h"
341d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupUInt64.h"
3541204d09SIlia K #include "lldb/Interpreter/OptionValueProperties.h"
361f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
377925fbbaSGreg Clayton #include "lldb/Target/RegisterContext.h"
3857508026SGreg Clayton #include "lldb/Target/Target.h"
39a63d08c9SGreg Clayton #include "lldb/Target/Thread.h"
40f9765acdSGreg Clayton 
41510938e5SCharles Davis #define USEC_PER_SEC 1000000
42510938e5SCharles Davis 
43f9765acdSGreg Clayton // Project includes
44f9765acdSGreg Clayton #include "ProcessKDP.h"
45f9765acdSGreg Clayton #include "ProcessKDPLog.h"
46a63d08c9SGreg Clayton #include "ThreadKDP.h"
475e8534efSJason Molenda #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
48840f12cfSJason Molenda #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
491d19a2f2SGreg Clayton #include "Utility/StringExtractor.h"
50f9765acdSGreg Clayton 
51f9765acdSGreg Clayton using namespace lldb;
52f9765acdSGreg Clayton using namespace lldb_private;
53f9765acdSGreg Clayton 
547f98240dSGreg Clayton namespace {
557f98240dSGreg Clayton 
567f98240dSGreg Clayton     static PropertyDefinition
577f98240dSGreg Clayton     g_properties[] =
587f98240dSGreg Clayton     {
597f98240dSGreg Clayton         { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." },
607f98240dSGreg Clayton         {  NULL            , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL  }
617f98240dSGreg Clayton     };
627f98240dSGreg Clayton 
637f98240dSGreg Clayton     enum
647f98240dSGreg Clayton     {
657f98240dSGreg Clayton         ePropertyPacketTimeout
667f98240dSGreg Clayton     };
677f98240dSGreg Clayton 
687f98240dSGreg Clayton     class PluginProperties : public Properties
697f98240dSGreg Clayton     {
707f98240dSGreg Clayton     public:
717f98240dSGreg Clayton 
727f98240dSGreg Clayton         static ConstString
737f98240dSGreg Clayton         GetSettingName ()
747f98240dSGreg Clayton         {
757f98240dSGreg Clayton             return ProcessKDP::GetPluginNameStatic();
767f98240dSGreg Clayton         }
777f98240dSGreg Clayton 
787f98240dSGreg Clayton         PluginProperties() :
797f98240dSGreg Clayton             Properties ()
807f98240dSGreg Clayton         {
817f98240dSGreg Clayton             m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
827f98240dSGreg Clayton             m_collection_sp->Initialize(g_properties);
837f98240dSGreg Clayton         }
847f98240dSGreg Clayton 
857f98240dSGreg Clayton         virtual
867f98240dSGreg Clayton         ~PluginProperties()
877f98240dSGreg Clayton         {
887f98240dSGreg Clayton         }
897f98240dSGreg Clayton 
907f98240dSGreg Clayton         uint64_t
917f98240dSGreg Clayton         GetPacketTimeout()
927f98240dSGreg Clayton         {
937f98240dSGreg Clayton             const uint32_t idx = ePropertyPacketTimeout;
947f98240dSGreg Clayton             return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
957f98240dSGreg Clayton         }
967f98240dSGreg Clayton     };
977f98240dSGreg Clayton 
987f98240dSGreg Clayton     typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
997f98240dSGreg Clayton 
1007f98240dSGreg Clayton     static const ProcessKDPPropertiesSP &
1017f98240dSGreg Clayton     GetGlobalPluginProperties()
1027f98240dSGreg Clayton     {
1037f98240dSGreg Clayton         static ProcessKDPPropertiesSP g_settings_sp;
1047f98240dSGreg Clayton         if (!g_settings_sp)
1057f98240dSGreg Clayton             g_settings_sp.reset (new PluginProperties ());
1067f98240dSGreg Clayton         return g_settings_sp;
1077f98240dSGreg Clayton     }
1087f98240dSGreg Clayton 
1097f98240dSGreg Clayton } // anonymous namespace end
1107f98240dSGreg Clayton 
111ba4e61d3SAndrew Kaylor static const lldb::tid_t g_kernel_tid = 1;
112ba4e61d3SAndrew Kaylor 
11357abc5d6SGreg Clayton ConstString
114f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic()
115f9765acdSGreg Clayton {
11657abc5d6SGreg Clayton     static ConstString g_name("kdp-remote");
11757abc5d6SGreg Clayton     return g_name;
118f9765acdSGreg Clayton }
119f9765acdSGreg Clayton 
120f9765acdSGreg Clayton const char *
121f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic()
122f9765acdSGreg Clayton {
123f9765acdSGreg Clayton     return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
124f9765acdSGreg Clayton }
125f9765acdSGreg Clayton 
126f9765acdSGreg Clayton void
127f9765acdSGreg Clayton ProcessKDP::Terminate()
128f9765acdSGreg Clayton {
129f9765acdSGreg Clayton     PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
130f9765acdSGreg Clayton }
131f9765acdSGreg Clayton 
132f9765acdSGreg Clayton 
133c3776bf2SGreg Clayton lldb::ProcessSP
134c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target,
135c3776bf2SGreg Clayton                             Listener &listener,
136c3776bf2SGreg Clayton                             const FileSpec *crash_file_path)
137f9765acdSGreg Clayton {
138c3776bf2SGreg Clayton     lldb::ProcessSP process_sp;
139c3776bf2SGreg Clayton     if (crash_file_path == NULL)
140c3776bf2SGreg Clayton         process_sp.reset(new ProcessKDP (target, listener));
141c3776bf2SGreg Clayton     return process_sp;
142f9765acdSGreg Clayton }
143f9765acdSGreg Clayton 
144f9765acdSGreg Clayton bool
1453a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
146f9765acdSGreg Clayton {
147596ed24eSGreg Clayton     if (plugin_specified_by_name)
148596ed24eSGreg Clayton         return true;
149596ed24eSGreg Clayton 
150f9765acdSGreg Clayton     // For now we are just making sure the file exists for a given module
151aa149cbdSGreg Clayton     Module *exe_module = target.GetExecutableModulePointer();
152aa149cbdSGreg Clayton     if (exe_module)
153f9765acdSGreg Clayton     {
154f9765acdSGreg Clayton         const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
15570512317SGreg Clayton         switch (triple_ref.getOS())
15670512317SGreg Clayton         {
15770512317SGreg Clayton             case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
15870512317SGreg Clayton             case llvm::Triple::MacOSX:  // For desktop targets
15970512317SGreg Clayton             case llvm::Triple::IOS:     // For arm targets
16070512317SGreg Clayton                 if (triple_ref.getVendor() == llvm::Triple::Apple)
161f9765acdSGreg Clayton                 {
162aa149cbdSGreg Clayton                     ObjectFile *exe_objfile = exe_module->GetObjectFile();
163f9765acdSGreg Clayton                     if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
164f9765acdSGreg Clayton                         exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
165f9765acdSGreg Clayton                         return true;
166f9765acdSGreg Clayton                 }
16770512317SGreg Clayton                 break;
16870512317SGreg Clayton 
16970512317SGreg Clayton             default:
17070512317SGreg Clayton                 break;
17170512317SGreg Clayton         }
172f9765acdSGreg Clayton     }
173596ed24eSGreg Clayton     return false;
1743a29bdbeSGreg Clayton }
175f9765acdSGreg Clayton 
176f9765acdSGreg Clayton //----------------------------------------------------------------------
177f9765acdSGreg Clayton // ProcessKDP constructor
178f9765acdSGreg Clayton //----------------------------------------------------------------------
179f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
180f9765acdSGreg Clayton     Process (target, listener),
181f9765acdSGreg Clayton     m_comm("lldb.process.kdp-remote.communication"),
1824bddaeb5SJim Ingham     m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
1835e8534efSJason Molenda     m_dyld_plugin_name (),
1841d19a2f2SGreg Clayton     m_kernel_load_addr (LLDB_INVALID_ADDRESS),
185ba4e61d3SAndrew Kaylor     m_command_sp(),
186ba4e61d3SAndrew Kaylor     m_kernel_thread_wp()
187f9765acdSGreg Clayton {
1887925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
1897925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
1907f98240dSGreg Clayton     const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
1917f98240dSGreg Clayton     if (timeout_seconds > 0)
1927f98240dSGreg Clayton         m_comm.SetPacketTimeout(timeout_seconds);
193f9765acdSGreg Clayton }
194f9765acdSGreg Clayton 
195f9765acdSGreg Clayton //----------------------------------------------------------------------
196f9765acdSGreg Clayton // Destructor
197f9765acdSGreg Clayton //----------------------------------------------------------------------
198f9765acdSGreg Clayton ProcessKDP::~ProcessKDP()
199f9765acdSGreg Clayton {
200f9765acdSGreg Clayton     Clear();
201e24c4acfSGreg Clayton     // We need to call finalize on the process before destroying ourselves
202e24c4acfSGreg Clayton     // to make sure all of the broadcaster cleanup goes as planned. If we
203e24c4acfSGreg Clayton     // destruct this class, then Process::~Process() might have problems
204e24c4acfSGreg Clayton     // trying to fully destroy the broadcaster.
205e24c4acfSGreg Clayton     Finalize();
206f9765acdSGreg Clayton }
207f9765acdSGreg Clayton 
208f9765acdSGreg Clayton //----------------------------------------------------------------------
209f9765acdSGreg Clayton // PluginInterface
210f9765acdSGreg Clayton //----------------------------------------------------------------------
21157abc5d6SGreg Clayton lldb_private::ConstString
212f9765acdSGreg Clayton ProcessKDP::GetPluginName()
213f9765acdSGreg Clayton {
214f9765acdSGreg Clayton     return GetPluginNameStatic();
215f9765acdSGreg Clayton }
216f9765acdSGreg Clayton 
217f9765acdSGreg Clayton uint32_t
218f9765acdSGreg Clayton ProcessKDP::GetPluginVersion()
219f9765acdSGreg Clayton {
220f9765acdSGreg Clayton     return 1;
221f9765acdSGreg Clayton }
222f9765acdSGreg Clayton 
223f9765acdSGreg Clayton Error
224f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module)
225f9765acdSGreg Clayton {
226f9765acdSGreg Clayton     Error error;
227f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
228f9765acdSGreg Clayton     return error;
229f9765acdSGreg Clayton }
230f9765acdSGreg Clayton 
231f9765acdSGreg Clayton Error
232f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
233f9765acdSGreg Clayton {
234f9765acdSGreg Clayton     Error error;
235f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
236f9765acdSGreg Clayton     return error;
237f9765acdSGreg Clayton }
238f9765acdSGreg Clayton 
239f9765acdSGreg Clayton Error
240f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
241f9765acdSGreg Clayton {
242f9765acdSGreg Clayton     Error error;
243f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
244f9765acdSGreg Clayton     return error;
245f9765acdSGreg Clayton }
246f9765acdSGreg Clayton 
247f9765acdSGreg Clayton Error
2484bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
249f9765acdSGreg Clayton {
250f9765acdSGreg Clayton     Error error;
2513a29bdbeSGreg Clayton 
2527925fbbaSGreg Clayton     // Don't let any JIT happen when doing KDP as we can't allocate
2537925fbbaSGreg Clayton     // memory and we don't want to be mucking with threads that might
2547925fbbaSGreg Clayton     // already be handling exceptions
2557925fbbaSGreg Clayton     SetCanJIT(false);
2567925fbbaSGreg Clayton 
2573a29bdbeSGreg Clayton     if (remote_url == NULL || remote_url[0] == '\0')
2587925fbbaSGreg Clayton     {
2597925fbbaSGreg Clayton         error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
2607925fbbaSGreg Clayton         return error;
2617925fbbaSGreg Clayton     }
2623a29bdbeSGreg Clayton 
2637b0992d9SGreg Clayton     std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
2643a29bdbeSGreg Clayton     if (conn_ap.get())
2653a29bdbeSGreg Clayton     {
2663a29bdbeSGreg Clayton         // Only try once for now.
2673a29bdbeSGreg Clayton         // TODO: check if we should be retrying?
2683a29bdbeSGreg Clayton         const uint32_t max_retry_count = 1;
2693a29bdbeSGreg Clayton         for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
2703a29bdbeSGreg Clayton         {
2713a29bdbeSGreg Clayton             if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
2723a29bdbeSGreg Clayton                 break;
2733a29bdbeSGreg Clayton             usleep (100000);
2743a29bdbeSGreg Clayton         }
2753a29bdbeSGreg Clayton     }
2763a29bdbeSGreg Clayton 
2773a29bdbeSGreg Clayton     if (conn_ap->IsConnected())
2783a29bdbeSGreg Clayton     {
2793d7162b1SKeno Fischer         const Socket& socket = static_cast<const Socket&>(*conn_ap->GetReadObject());
280014bb7daSVince Harron         const uint16_t reply_port = socket.GetLocalPortNumber();
2813a29bdbeSGreg Clayton 
2823a29bdbeSGreg Clayton         if (reply_port != 0)
2833a29bdbeSGreg Clayton         {
2843a29bdbeSGreg Clayton             m_comm.SetConnection(conn_ap.release());
2853a29bdbeSGreg Clayton 
2863a29bdbeSGreg Clayton             if (m_comm.SendRequestReattach(reply_port))
2873a29bdbeSGreg Clayton             {
2883a29bdbeSGreg Clayton                 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
2893a29bdbeSGreg Clayton                 {
2903a29bdbeSGreg Clayton                     m_comm.GetVersion();
2913a29bdbeSGreg Clayton                     uint32_t cpu = m_comm.GetCPUType();
2923a29bdbeSGreg Clayton                     uint32_t sub = m_comm.GetCPUSubtype();
2933a29bdbeSGreg Clayton                     ArchSpec kernel_arch;
2943a29bdbeSGreg Clayton                     kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
2953a29bdbeSGreg Clayton                     m_target.SetArchitecture(kernel_arch);
2964bd4e7e3SJason Molenda 
297840f12cfSJason Molenda                     /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet.  */
298840f12cfSJason Molenda                     /* An EFI kdp session has neither UUID nor load address. */
2994bd4e7e3SJason Molenda 
3004bd4e7e3SJason Molenda                     UUID kernel_uuid = m_comm.GetUUID ();
3014bd4e7e3SJason Molenda                     addr_t kernel_load_addr = m_comm.GetLoadAddress ();
3024bd4e7e3SJason Molenda 
303840f12cfSJason Molenda                     if (m_comm.RemoteIsEFI ())
304840f12cfSJason Molenda                     {
305a1bce2efSGreg Clayton                         // Select an invalid plugin name for the dynamic loader so one doesn't get used
306a1bce2efSGreg Clayton                         // since EFI does its own manual loading via python scripting
307a1bce2efSGreg Clayton                         static ConstString g_none_dynamic_loader("none");
308a1bce2efSGreg Clayton                         m_dyld_plugin_name = g_none_dynamic_loader;
309a1bce2efSGreg Clayton 
310a1bce2efSGreg Clayton                         if (kernel_uuid.IsValid()) {
311a1bce2efSGreg Clayton                             // If EFI passed in a UUID= try to lookup UUID
312a1bce2efSGreg Clayton                             // The slide will not be provided. But the UUID
313a1bce2efSGreg Clayton                             // lookup will be used to launch EFI debug scripts
314a1bce2efSGreg Clayton                             // from the dSYM, that can load all of the symbols.
315a1bce2efSGreg Clayton                             ModuleSpec module_spec;
316a1bce2efSGreg Clayton                             module_spec.GetUUID() = kernel_uuid;
317a1bce2efSGreg Clayton                             module_spec.GetArchitecture() = m_target.GetArchitecture();
318a1bce2efSGreg Clayton 
319a1bce2efSGreg Clayton                             // Lookup UUID locally, before attempting dsymForUUID like action
320a1bce2efSGreg Clayton                             module_spec.GetSymbolFileSpec() = Symbols::LocateExecutableSymbolFile(module_spec);
321a1bce2efSGreg Clayton                             if (module_spec.GetSymbolFileSpec())
322a1bce2efSGreg Clayton                                  module_spec.GetFileSpec() = Symbols::LocateExecutableObjectFile (module_spec);
323a1bce2efSGreg Clayton                             if (!module_spec.GetSymbolFileSpec() || !module_spec.GetSymbolFileSpec())
324a1bce2efSGreg Clayton                                  Symbols::DownloadObjectAndSymbolFile (module_spec, true);
325a1bce2efSGreg Clayton 
326a1bce2efSGreg Clayton                             if (module_spec.GetFileSpec().Exists())
327a1bce2efSGreg Clayton                             {
328a1bce2efSGreg Clayton                                 ModuleSP module_sp(new Module (module_spec.GetFileSpec(), m_target.GetArchitecture()));
329a1bce2efSGreg Clayton                                 if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec))
330a1bce2efSGreg Clayton                                 {
331a1bce2efSGreg Clayton                                     // Get the current target executable
332a1bce2efSGreg Clayton                                     ModuleSP exe_module_sp (m_target.GetExecutableModule ());
333a1bce2efSGreg Clayton 
334a1bce2efSGreg Clayton                                     // Make sure you don't already have the right module loaded and they will be uniqued
335a1bce2efSGreg Clayton                                     if (exe_module_sp.get() != module_sp.get())
336a1bce2efSGreg Clayton                                         m_target.SetExecutableModule (module_sp, false);
337a1bce2efSGreg Clayton                                 }
338a1bce2efSGreg Clayton                             }
339a1bce2efSGreg Clayton                         }
340840f12cfSJason Molenda                     }
341ca2ffa7eSJason Molenda                     else if (m_comm.RemoteIsDarwinKernel ())
342a8ea4baeSJason Molenda                     {
343ca2ffa7eSJason Molenda                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
344a8ea4baeSJason Molenda                         if (kernel_load_addr != LLDB_INVALID_ADDRESS)
3454bd4e7e3SJason Molenda                         {
3465e8534efSJason Molenda                             m_kernel_load_addr = kernel_load_addr;
347a8ea4baeSJason Molenda                         }
3484bd4e7e3SJason Molenda                     }
3494bd4e7e3SJason Molenda 
35097d5cf05SGreg Clayton                     // Set the thread ID
35197d5cf05SGreg Clayton                     UpdateThreadListIfNeeded ();
352a63d08c9SGreg Clayton                     SetID (1);
35356d9a1b3SGreg Clayton                     GetThreadList ();
354a63d08c9SGreg Clayton                     SetPrivateState (eStateStopped);
35507e66e3eSGreg Clayton                     StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
35607e66e3eSGreg Clayton                     if (async_strm_sp)
35707e66e3eSGreg Clayton                     {
3585b88216dSGreg Clayton                         const char *cstr;
3595b88216dSGreg Clayton                         if ((cstr = m_comm.GetKernelVersion ()) != NULL)
36007e66e3eSGreg Clayton                         {
3615b88216dSGreg Clayton                             async_strm_sp->Printf ("Version: %s\n", cstr);
36207e66e3eSGreg Clayton                             async_strm_sp->Flush();
36307e66e3eSGreg Clayton                         }
3645b88216dSGreg Clayton //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
3655b88216dSGreg Clayton //                      {
3665b88216dSGreg Clayton //                          async_strm_sp->Printf ("Image Path: %s\n", cstr);
3675b88216dSGreg Clayton //                          async_strm_sp->Flush();
3685b88216dSGreg Clayton //                      }
36907e66e3eSGreg Clayton                     }
3703a29bdbeSGreg Clayton                 }
37197d5cf05SGreg Clayton                 else
37297d5cf05SGreg Clayton                 {
37397d5cf05SGreg Clayton                     error.SetErrorString("KDP_REATTACH failed");
37497d5cf05SGreg Clayton                 }
3753a29bdbeSGreg Clayton             }
3763a29bdbeSGreg Clayton             else
3773a29bdbeSGreg Clayton             {
37897d5cf05SGreg Clayton                 error.SetErrorString("KDP_REATTACH failed");
3793a29bdbeSGreg Clayton             }
3803a29bdbeSGreg Clayton         }
3813a29bdbeSGreg Clayton         else
3823a29bdbeSGreg Clayton         {
3833a29bdbeSGreg Clayton             error.SetErrorString("invalid reply port from UDP connection");
3843a29bdbeSGreg Clayton         }
3853a29bdbeSGreg Clayton     }
3863a29bdbeSGreg Clayton     else
3873a29bdbeSGreg Clayton     {
3883a29bdbeSGreg Clayton         if (error.Success())
3893a29bdbeSGreg Clayton             error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
3903a29bdbeSGreg Clayton     }
3913a29bdbeSGreg Clayton     if (error.Fail())
3923a29bdbeSGreg Clayton         m_comm.Disconnect();
3933a29bdbeSGreg Clayton 
394f9765acdSGreg Clayton     return error;
395f9765acdSGreg Clayton }
396f9765acdSGreg Clayton 
397f9765acdSGreg Clayton //----------------------------------------------------------------------
398f9765acdSGreg Clayton // Process Control
399f9765acdSGreg Clayton //----------------------------------------------------------------------
400f9765acdSGreg Clayton Error
401982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module,
4027782de92SJean-Daniel Dupas                       ProcessLaunchInfo &launch_info)
403f9765acdSGreg Clayton {
404f9765acdSGreg Clayton     Error error;
405f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
406f9765acdSGreg Clayton     return error;
407f9765acdSGreg Clayton }
408f9765acdSGreg Clayton 
409f9765acdSGreg Clayton 
410f9765acdSGreg Clayton Error
411f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
412f9765acdSGreg Clayton {
413f9765acdSGreg Clayton     Error error;
414f9765acdSGreg Clayton     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
415f9765acdSGreg Clayton     return error;
416f9765acdSGreg Clayton }
417f9765acdSGreg Clayton 
418f9765acdSGreg Clayton Error
41984647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
42084647048SHan Ming Ong {
42184647048SHan Ming Ong     Error error;
42284647048SHan Ming Ong     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
42384647048SHan Ming Ong     return error;
42484647048SHan Ming Ong }
42584647048SHan Ming Ong 
42684647048SHan Ming Ong Error
4279c517c0dSJean-Daniel Dupas ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
428f9765acdSGreg Clayton {
429f9765acdSGreg Clayton     Error error;
430f9765acdSGreg Clayton     error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
431f9765acdSGreg Clayton     return error;
432f9765acdSGreg Clayton }
433f9765acdSGreg Clayton 
434f9765acdSGreg Clayton 
435f9765acdSGreg Clayton void
436bb006ce2SJim Ingham ProcessKDP::DidAttach (ArchSpec &process_arch)
437f9765acdSGreg Clayton {
438bb006ce2SJim Ingham     Process::DidAttach(process_arch);
439bb006ce2SJim Ingham 
4405160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
441f9765acdSGreg Clayton     if (log)
44254cb8f83SJohnny Chen         log->Printf ("ProcessKDP::DidAttach()");
443f9765acdSGreg Clayton     if (GetID() != LLDB_INVALID_PROCESS_ID)
444f9765acdSGreg Clayton     {
445c3eefa39SGreg Clayton         uint32_t cpu = m_comm.GetCPUType();
446c3eefa39SGreg Clayton         if (cpu)
447c3eefa39SGreg Clayton         {
448c3eefa39SGreg Clayton             uint32_t sub = m_comm.GetCPUSubtype();
449c3eefa39SGreg Clayton             process_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
450c3eefa39SGreg Clayton         }
451f9765acdSGreg Clayton     }
452f9765acdSGreg Clayton }
453f9765acdSGreg Clayton 
4545e8534efSJason Molenda addr_t
4555e8534efSJason Molenda ProcessKDP::GetImageInfoAddress()
4565e8534efSJason Molenda {
4575e8534efSJason Molenda     return m_kernel_load_addr;
4585e8534efSJason Molenda }
4595e8534efSJason Molenda 
4605e8534efSJason Molenda lldb_private::DynamicLoader *
4615e8534efSJason Molenda ProcessKDP::GetDynamicLoader ()
4625e8534efSJason Molenda {
4635e8534efSJason Molenda     if (m_dyld_ap.get() == NULL)
4642e56a254SJason Molenda         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
4655e8534efSJason Molenda     return m_dyld_ap.get();
4665e8534efSJason Molenda }
4675e8534efSJason Molenda 
468f9765acdSGreg Clayton Error
469f9765acdSGreg Clayton ProcessKDP::WillResume ()
470f9765acdSGreg Clayton {
471f9765acdSGreg Clayton     return Error();
472f9765acdSGreg Clayton }
473f9765acdSGreg Clayton 
474f9765acdSGreg Clayton Error
475f9765acdSGreg Clayton ProcessKDP::DoResume ()
476f9765acdSGreg Clayton {
477f9765acdSGreg Clayton     Error error;
4785160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
4797925fbbaSGreg Clayton     // Only start the async thread if we try to do any process control
480acee96aeSZachary Turner     if (!m_async_thread.IsJoinable())
4817925fbbaSGreg Clayton         StartAsyncThread();
4827925fbbaSGreg Clayton 
48397d5cf05SGreg Clayton     bool resume = false;
4847925fbbaSGreg Clayton 
48597d5cf05SGreg Clayton     // With KDP there is only one thread we can tell what to do
486ba4e61d3SAndrew Kaylor     ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
487ba4e61d3SAndrew Kaylor 
48897d5cf05SGreg Clayton     if (kernel_thread_sp)
4894b1b8b3eSGreg Clayton     {
49097d5cf05SGreg Clayton         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
4916e0ff1a3SGreg Clayton 
4926e0ff1a3SGreg Clayton         if (log)
4936e0ff1a3SGreg Clayton             log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
4947925fbbaSGreg Clayton         switch (thread_resume_state)
4954b1b8b3eSGreg Clayton         {
4967925fbbaSGreg Clayton             case eStateSuspended:
4977925fbbaSGreg Clayton                 // Nothing to do here when a thread will stay suspended
4987925fbbaSGreg Clayton                 // we just leave the CPU mask bit set to zero for the thread
4996e0ff1a3SGreg Clayton                 if (log)
5006e0ff1a3SGreg Clayton                     log->Printf ("ProcessKDP::DoResume() = suspended???");
5017925fbbaSGreg Clayton                 break;
5027925fbbaSGreg Clayton 
5037925fbbaSGreg Clayton             case eStateStepping:
5041afa68edSGreg Clayton                 {
5051afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5061afa68edSGreg Clayton 
5071afa68edSGreg Clayton                     if (reg_ctx_sp)
5081afa68edSGreg Clayton                     {
5096e0ff1a3SGreg Clayton                         if (log)
5106e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
5111afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (true);
51297d5cf05SGreg Clayton                         resume = true;
5131afa68edSGreg Clayton                     }
5141afa68edSGreg Clayton                     else
5151afa68edSGreg Clayton                     {
5161afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5171afa68edSGreg Clayton                     }
5181afa68edSGreg Clayton                 }
5197925fbbaSGreg Clayton                 break;
5207925fbbaSGreg Clayton 
52197d5cf05SGreg Clayton             case eStateRunning:
5221afa68edSGreg Clayton                 {
5231afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5241afa68edSGreg Clayton 
5251afa68edSGreg Clayton                     if (reg_ctx_sp)
5261afa68edSGreg Clayton                     {
5276e0ff1a3SGreg Clayton                         if (log)
5286e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
5291afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (false);
53097d5cf05SGreg Clayton                         resume = true;
5311afa68edSGreg Clayton                     }
5321afa68edSGreg Clayton                     else
5331afa68edSGreg Clayton                     {
5341afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5351afa68edSGreg Clayton                     }
5361afa68edSGreg Clayton                 }
5377925fbbaSGreg Clayton                 break;
5387925fbbaSGreg Clayton 
5397925fbbaSGreg Clayton             default:
54097d5cf05SGreg Clayton                 // The only valid thread resume states are listed above
5417925fbbaSGreg Clayton                 assert (!"invalid thread resume state");
5427925fbbaSGreg Clayton                 break;
5434b1b8b3eSGreg Clayton         }
5444b1b8b3eSGreg Clayton     }
5457925fbbaSGreg Clayton 
54697d5cf05SGreg Clayton     if (resume)
54797d5cf05SGreg Clayton     {
54897d5cf05SGreg Clayton         if (log)
54997d5cf05SGreg Clayton             log->Printf ("ProcessKDP::DoResume () sending resume");
55097d5cf05SGreg Clayton 
55197d5cf05SGreg Clayton         if (m_comm.SendRequestResume ())
5527925fbbaSGreg Clayton         {
5537925fbbaSGreg Clayton             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
5547925fbbaSGreg Clayton             SetPrivateState(eStateRunning);
5557925fbbaSGreg Clayton         }
5564b1b8b3eSGreg Clayton         else
55707e66e3eSGreg Clayton             error.SetErrorString ("KDP resume failed");
5587925fbbaSGreg Clayton     }
5597925fbbaSGreg Clayton     else
5607925fbbaSGreg Clayton     {
56197d5cf05SGreg Clayton         error.SetErrorString ("kernel thread is suspended");
5627925fbbaSGreg Clayton     }
5637925fbbaSGreg Clayton 
564f9765acdSGreg Clayton     return error;
565f9765acdSGreg Clayton }
566f9765acdSGreg Clayton 
56797d5cf05SGreg Clayton lldb::ThreadSP
568ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread()
56997d5cf05SGreg Clayton {
57097d5cf05SGreg Clayton     // KDP only tells us about one thread/core. Any other threads will usually
57197d5cf05SGreg Clayton     // be the ones that are read from memory by the OS plug-ins.
572ba4e61d3SAndrew Kaylor 
573ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (m_kernel_thread_wp.lock());
57497d5cf05SGreg Clayton     if (!thread_sp)
575ba4e61d3SAndrew Kaylor     {
576ba4e61d3SAndrew Kaylor         thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
577ba4e61d3SAndrew Kaylor         m_kernel_thread_wp = thread_sp;
578ba4e61d3SAndrew Kaylor     }
57997d5cf05SGreg Clayton     return thread_sp;
58097d5cf05SGreg Clayton }
58197d5cf05SGreg Clayton 
58297d5cf05SGreg Clayton 
58397d5cf05SGreg Clayton 
58497d5cf05SGreg Clayton 
5859fc13556SGreg Clayton bool
58656d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
587f9765acdSGreg Clayton {
588f9765acdSGreg Clayton     // locker will keep a mutex locked until it goes out of scope
5895160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
590f9765acdSGreg Clayton     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
591d01b2953SDaniel Malea         log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
592f9765acdSGreg Clayton 
59339da3efdSGreg Clayton     // Even though there is a CPU mask, it doesn't mean we can see each CPU
59497d5cf05SGreg Clayton     // indivudually, there is really only one. Lets call this thread 1.
595ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
596ba4e61d3SAndrew Kaylor     if (!thread_sp)
597ba4e61d3SAndrew Kaylor         thread_sp = GetKernelThread ();
598ba4e61d3SAndrew Kaylor     new_thread_list.AddThread(thread_sp);
59997d5cf05SGreg Clayton 
6009fc13556SGreg Clayton     return new_thread_list.GetSize(false) > 0;
601f9765acdSGreg Clayton }
602f9765acdSGreg Clayton 
603f9765acdSGreg Clayton void
604f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop ()
605f9765acdSGreg Clayton {
606f9765acdSGreg Clayton     // Let all threads recover from stopping and do any clean up based
607f9765acdSGreg Clayton     // on the previous thread state (if any).
608f9765acdSGreg Clayton     m_thread_list.RefreshStateAfterStop();
609f9765acdSGreg Clayton }
610f9765acdSGreg Clayton 
611f9765acdSGreg Clayton Error
612f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop)
613f9765acdSGreg Clayton {
614f9765acdSGreg Clayton     Error error;
615f9765acdSGreg Clayton 
61697d5cf05SGreg Clayton     if (m_comm.IsRunning())
617f9765acdSGreg Clayton     {
61897d5cf05SGreg Clayton         if (m_destroy_in_process)
61997d5cf05SGreg Clayton         {
62097d5cf05SGreg Clayton             // If we are attemping to destroy, we need to not return an error to
62197d5cf05SGreg Clayton             // Halt or DoDestroy won't get called.
62297d5cf05SGreg Clayton             // We are also currently running, so send a process stopped event
62397d5cf05SGreg Clayton             SetPrivateState (eStateStopped);
624f9765acdSGreg Clayton         }
625f9765acdSGreg Clayton         else
626f9765acdSGreg Clayton         {
62797d5cf05SGreg Clayton             error.SetErrorString ("KDP cannot interrupt a running kernel");
628f9765acdSGreg Clayton         }
629f9765acdSGreg Clayton     }
630f9765acdSGreg Clayton     return error;
631f9765acdSGreg Clayton }
632f9765acdSGreg Clayton 
633f9765acdSGreg Clayton Error
634acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped)
635f9765acdSGreg Clayton {
636f9765acdSGreg Clayton     Error error;
6375160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
638f9765acdSGreg Clayton     if (log)
639acff8950SJim Ingham         log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
640f9765acdSGreg Clayton 
64197d5cf05SGreg Clayton     if (m_comm.IsRunning())
64297d5cf05SGreg Clayton     {
64397d5cf05SGreg Clayton         // We are running and we can't interrupt a running kernel, so we need
64497d5cf05SGreg Clayton         // to just close the connection to the kernel and hope for the best
64597d5cf05SGreg Clayton     }
64697d5cf05SGreg Clayton     else
64797d5cf05SGreg Clayton     {
648acff8950SJim Ingham         // If we are going to keep the target stopped, then don't send the disconnect message.
649acff8950SJim Ingham         if (!keep_stopped && m_comm.IsConnected())
6503a29bdbeSGreg Clayton         {
6516e0ff1a3SGreg Clayton             const bool success = m_comm.SendRequestDisconnect();
652f9765acdSGreg Clayton             if (log)
653f9765acdSGreg Clayton             {
6546e0ff1a3SGreg Clayton                 if (success)
6556e0ff1a3SGreg Clayton                     log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
656f9765acdSGreg Clayton                 else
65777e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
658f9765acdSGreg Clayton             }
6596e0ff1a3SGreg Clayton             m_comm.Disconnect ();
6603a29bdbeSGreg Clayton         }
66197d5cf05SGreg Clayton     }
662f9765acdSGreg Clayton     StopAsyncThread ();
66374d4193eSGreg Clayton     m_comm.Clear();
664f9765acdSGreg Clayton 
665f9765acdSGreg Clayton     SetPrivateState (eStateDetached);
666f9765acdSGreg Clayton     ResumePrivateStateThread();
667f9765acdSGreg Clayton 
668f9765acdSGreg Clayton     //KillDebugserverProcess ();
669f9765acdSGreg Clayton     return error;
670f9765acdSGreg Clayton }
671f9765acdSGreg Clayton 
672f9765acdSGreg Clayton Error
673f9765acdSGreg Clayton ProcessKDP::DoDestroy ()
674f9765acdSGreg Clayton {
6757925fbbaSGreg Clayton     // For KDP there really is no difference between destroy and detach
676acff8950SJim Ingham     bool keep_stopped = false;
677acff8950SJim Ingham     return DoDetach(keep_stopped);
678f9765acdSGreg Clayton }
679f9765acdSGreg Clayton 
680f9765acdSGreg Clayton //------------------------------------------------------------------
681f9765acdSGreg Clayton // Process Queries
682f9765acdSGreg Clayton //------------------------------------------------------------------
683f9765acdSGreg Clayton 
684f9765acdSGreg Clayton bool
685f9765acdSGreg Clayton ProcessKDP::IsAlive ()
686f9765acdSGreg Clayton {
687f9765acdSGreg Clayton     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
688f9765acdSGreg Clayton }
689f9765acdSGreg Clayton 
690f9765acdSGreg Clayton //------------------------------------------------------------------
691f9765acdSGreg Clayton // Process Memory
692f9765acdSGreg Clayton //------------------------------------------------------------------
693f9765acdSGreg Clayton size_t
694f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
695f9765acdSGreg Clayton {
6968eb32817SJason Molenda     uint8_t *data_buffer = (uint8_t *) buf;
697a63d08c9SGreg Clayton     if (m_comm.IsConnected())
6988eb32817SJason Molenda     {
6998eb32817SJason Molenda         const size_t max_read_size = 512;
7008eb32817SJason Molenda         size_t total_bytes_read = 0;
7018eb32817SJason Molenda 
7028eb32817SJason Molenda         // Read the requested amount of memory in 512 byte chunks
7038eb32817SJason Molenda         while (total_bytes_read < size)
7048eb32817SJason Molenda         {
7058eb32817SJason Molenda             size_t bytes_to_read_this_request = size - total_bytes_read;
7068eb32817SJason Molenda             if (bytes_to_read_this_request > max_read_size)
7078eb32817SJason Molenda             {
7088eb32817SJason Molenda                 bytes_to_read_this_request = max_read_size;
7098eb32817SJason Molenda             }
7108eb32817SJason Molenda             size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read,
7118eb32817SJason Molenda                                                               data_buffer + total_bytes_read,
7128eb32817SJason Molenda                                                               bytes_to_read_this_request, error);
7138eb32817SJason Molenda             total_bytes_read += bytes_read;
7148eb32817SJason Molenda             if (error.Fail() || bytes_read == 0)
7158eb32817SJason Molenda             {
7168eb32817SJason Molenda                 return total_bytes_read;
7178eb32817SJason Molenda             }
7188eb32817SJason Molenda         }
7198eb32817SJason Molenda 
7208eb32817SJason Molenda         return total_bytes_read;
7218eb32817SJason Molenda     }
722a63d08c9SGreg Clayton     error.SetErrorString ("not connected");
723f9765acdSGreg Clayton     return 0;
724f9765acdSGreg Clayton }
725f9765acdSGreg Clayton 
726f9765acdSGreg Clayton size_t
727f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
728f9765acdSGreg Clayton {
7297925fbbaSGreg Clayton     if (m_comm.IsConnected())
7307925fbbaSGreg Clayton         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
7317925fbbaSGreg Clayton     error.SetErrorString ("not connected");
732f9765acdSGreg Clayton     return 0;
733f9765acdSGreg Clayton }
734f9765acdSGreg Clayton 
735f9765acdSGreg Clayton lldb::addr_t
736f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
737f9765acdSGreg Clayton {
738f9765acdSGreg Clayton     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
739f9765acdSGreg Clayton     return LLDB_INVALID_ADDRESS;
740f9765acdSGreg Clayton }
741f9765acdSGreg Clayton 
742f9765acdSGreg Clayton Error
743f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
744f9765acdSGreg Clayton {
745f9765acdSGreg Clayton     Error error;
746f9765acdSGreg Clayton     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
747f9765acdSGreg Clayton     return error;
748f9765acdSGreg Clayton }
749f9765acdSGreg Clayton 
750f9765acdSGreg Clayton Error
751299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
752f9765acdSGreg Clayton {
75307e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
75407e66e3eSGreg Clayton     {
75507e66e3eSGreg Clayton         Error error;
7565b88216dSGreg Clayton         if (!bp_site->IsEnabled())
7575b88216dSGreg Clayton         {
7585b88216dSGreg Clayton             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
7595b88216dSGreg Clayton             {
7605b88216dSGreg Clayton                 bp_site->SetEnabled(true);
7615b88216dSGreg Clayton                 bp_site->SetType (BreakpointSite::eExternal);
7625b88216dSGreg Clayton             }
7635b88216dSGreg Clayton             else
7645b88216dSGreg Clayton             {
76507e66e3eSGreg Clayton                 error.SetErrorString ("KDP set breakpoint failed");
7665b88216dSGreg Clayton             }
7675b88216dSGreg Clayton         }
76807e66e3eSGreg Clayton         return error;
76907e66e3eSGreg Clayton     }
770f9765acdSGreg Clayton     return EnableSoftwareBreakpoint (bp_site);
771f9765acdSGreg Clayton }
772f9765acdSGreg Clayton 
773f9765acdSGreg Clayton Error
774299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
775f9765acdSGreg Clayton {
77607e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
77707e66e3eSGreg Clayton     {
77807e66e3eSGreg Clayton         Error error;
7795b88216dSGreg Clayton         if (bp_site->IsEnabled())
7805b88216dSGreg Clayton         {
7815b88216dSGreg Clayton             BreakpointSite::Type bp_type = bp_site->GetType();
7825b88216dSGreg Clayton             if (bp_type == BreakpointSite::eExternal)
7835b88216dSGreg Clayton             {
78497d5cf05SGreg Clayton                 if (m_destroy_in_process && m_comm.IsRunning())
78597d5cf05SGreg Clayton                 {
78697d5cf05SGreg Clayton                     // We are trying to destroy our connection and we are running
78797d5cf05SGreg Clayton                     bp_site->SetEnabled(false);
78897d5cf05SGreg Clayton                 }
78997d5cf05SGreg Clayton                 else
79097d5cf05SGreg Clayton                 {
7915b88216dSGreg Clayton                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
7925b88216dSGreg Clayton                         bp_site->SetEnabled(false);
7935b88216dSGreg Clayton                     else
79407e66e3eSGreg Clayton                         error.SetErrorString ("KDP remove breakpoint failed");
7955b88216dSGreg Clayton                 }
79697d5cf05SGreg Clayton             }
7975b88216dSGreg Clayton             else
7985b88216dSGreg Clayton             {
7995b88216dSGreg Clayton                 error = DisableSoftwareBreakpoint (bp_site);
8005b88216dSGreg Clayton             }
8015b88216dSGreg Clayton         }
80207e66e3eSGreg Clayton         return error;
80307e66e3eSGreg Clayton     }
804f9765acdSGreg Clayton     return DisableSoftwareBreakpoint (bp_site);
805f9765acdSGreg Clayton }
806f9765acdSGreg Clayton 
807f9765acdSGreg Clayton Error
8081b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
809f9765acdSGreg Clayton {
810f9765acdSGreg Clayton     Error error;
811f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
812f9765acdSGreg Clayton     return error;
813f9765acdSGreg Clayton }
814f9765acdSGreg Clayton 
815f9765acdSGreg Clayton Error
8161b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
817f9765acdSGreg Clayton {
818f9765acdSGreg Clayton     Error error;
819f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
820f9765acdSGreg Clayton     return error;
821f9765acdSGreg Clayton }
822f9765acdSGreg Clayton 
823f9765acdSGreg Clayton void
824f9765acdSGreg Clayton ProcessKDP::Clear()
825f9765acdSGreg Clayton {
826f9765acdSGreg Clayton     m_thread_list.Clear();
827f9765acdSGreg Clayton }
828f9765acdSGreg Clayton 
829f9765acdSGreg Clayton Error
830f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo)
831f9765acdSGreg Clayton {
832f9765acdSGreg Clayton     Error error;
833f9765acdSGreg Clayton     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
834f9765acdSGreg Clayton     return error;
835f9765acdSGreg Clayton }
836f9765acdSGreg Clayton 
837f9765acdSGreg Clayton void
838f9765acdSGreg Clayton ProcessKDP::Initialize()
839f9765acdSGreg Clayton {
840c8d69828SDavide Italiano     static std::once_flag g_once_flag;
841f9765acdSGreg Clayton 
842c8d69828SDavide Italiano     std::call_once(g_once_flag, []()
843f9765acdSGreg Clayton     {
844f9765acdSGreg Clayton         PluginManager::RegisterPlugin (GetPluginNameStatic(),
845f9765acdSGreg Clayton                                        GetPluginDescriptionStatic(),
8467f98240dSGreg Clayton                                        CreateInstance,
847488c89edSBruce Mitchener                                        DebuggerInitialize);
848f9765acdSGreg Clayton 
849f9765acdSGreg Clayton         Log::Callbacks log_callbacks = {
850f9765acdSGreg Clayton             ProcessKDPLog::DisableLog,
851f9765acdSGreg Clayton             ProcessKDPLog::EnableLog,
852f9765acdSGreg Clayton             ProcessKDPLog::ListLogCategories
853f9765acdSGreg Clayton         };
854f9765acdSGreg Clayton 
855f9765acdSGreg Clayton         Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
856c8d69828SDavide Italiano     });
857f9765acdSGreg Clayton }
858f9765acdSGreg Clayton 
8597f98240dSGreg Clayton void
8607f98240dSGreg Clayton ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger)
8617f98240dSGreg Clayton {
8627f98240dSGreg Clayton     if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
8637f98240dSGreg Clayton     {
8647f98240dSGreg Clayton         const bool is_global_setting = true;
8657f98240dSGreg Clayton         PluginManager::CreateSettingForProcessPlugin (debugger,
8667f98240dSGreg Clayton                                                       GetGlobalPluginProperties()->GetValueProperties(),
8677f98240dSGreg Clayton                                                       ConstString ("Properties for the kdp-remote process plug-in."),
8687f98240dSGreg Clayton                                                       is_global_setting);
8697f98240dSGreg Clayton     }
8707f98240dSGreg Clayton }
8717f98240dSGreg Clayton 
872f9765acdSGreg Clayton bool
873f9765acdSGreg Clayton ProcessKDP::StartAsyncThread ()
874f9765acdSGreg Clayton {
8755160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
876f9765acdSGreg Clayton 
877f9765acdSGreg Clayton     if (log)
8787925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StartAsyncThread ()");
879f9765acdSGreg Clayton 
880acee96aeSZachary Turner     if (m_async_thread.IsJoinable())
8817925fbbaSGreg Clayton         return true;
8827925fbbaSGreg Clayton 
88339de3110SZachary Turner     m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
884acee96aeSZachary Turner     return m_async_thread.IsJoinable();
885f9765acdSGreg Clayton }
886f9765acdSGreg Clayton 
887f9765acdSGreg Clayton void
888f9765acdSGreg Clayton ProcessKDP::StopAsyncThread ()
889f9765acdSGreg Clayton {
8905160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
891f9765acdSGreg Clayton 
892f9765acdSGreg Clayton     if (log)
8937925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StopAsyncThread ()");
894f9765acdSGreg Clayton 
895f9765acdSGreg Clayton     m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
896f9765acdSGreg Clayton 
897f9765acdSGreg Clayton     // Stop the stdio thread
898acee96aeSZachary Turner     if (m_async_thread.IsJoinable())
89939de3110SZachary Turner         m_async_thread.Join(nullptr);
900f9765acdSGreg Clayton }
901f9765acdSGreg Clayton 
902f9765acdSGreg Clayton 
903f9765acdSGreg Clayton void *
904f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg)
905f9765acdSGreg Clayton {
906f9765acdSGreg Clayton     ProcessKDP *process = (ProcessKDP*) arg;
907f9765acdSGreg Clayton 
9087925fbbaSGreg Clayton     const lldb::pid_t pid = process->GetID();
9097925fbbaSGreg Clayton 
9105160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
911f9765acdSGreg Clayton     if (log)
912d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
913f9765acdSGreg Clayton 
914f9765acdSGreg Clayton     Listener listener ("ProcessKDP::AsyncThread");
915f9765acdSGreg Clayton     EventSP event_sp;
916f9765acdSGreg Clayton     const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
917f9765acdSGreg Clayton                                         eBroadcastBitAsyncThreadShouldExit;
918f9765acdSGreg Clayton 
9197925fbbaSGreg Clayton 
920f9765acdSGreg Clayton     if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
921f9765acdSGreg Clayton     {
922f9765acdSGreg Clayton         bool done = false;
923f9765acdSGreg Clayton         while (!done)
924f9765acdSGreg Clayton         {
925f9765acdSGreg Clayton             if (log)
926d01b2953SDaniel Malea                 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
9277925fbbaSGreg Clayton                              pid);
928f9765acdSGreg Clayton             if (listener.WaitForEvent (NULL, event_sp))
929f9765acdSGreg Clayton             {
9307925fbbaSGreg Clayton                 uint32_t event_type = event_sp->GetType();
931f9765acdSGreg Clayton                 if (log)
932d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
9337925fbbaSGreg Clayton                                  pid,
9347925fbbaSGreg Clayton                                  event_type);
935f9765acdSGreg Clayton 
9367925fbbaSGreg Clayton                 // When we are running, poll for 1 second to try and get an exception
9377925fbbaSGreg Clayton                 // to indicate the process has stopped. If we don't get one, check to
9387925fbbaSGreg Clayton                 // make sure no one asked us to exit
9397925fbbaSGreg Clayton                 bool is_running = false;
9407925fbbaSGreg Clayton                 DataExtractor exc_reply_packet;
9417925fbbaSGreg Clayton                 do
9427925fbbaSGreg Clayton                 {
943f9765acdSGreg Clayton                     switch (event_type)
944f9765acdSGreg Clayton                     {
945f9765acdSGreg Clayton                     case eBroadcastBitAsyncContinue:
946f9765acdSGreg Clayton                         {
9477925fbbaSGreg Clayton                             is_running = true;
9487925fbbaSGreg Clayton                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
949f9765acdSGreg Clayton                             {
950ba4e61d3SAndrew Kaylor                                 ThreadSP thread_sp (process->GetKernelThread());
9511afa68edSGreg Clayton                                 if (thread_sp)
9521afa68edSGreg Clayton                                 {
9531afa68edSGreg Clayton                                     lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
9541afa68edSGreg Clayton                                     if (reg_ctx_sp)
9551afa68edSGreg Clayton                                         reg_ctx_sp->InvalidateAllRegisters();
95697d5cf05SGreg Clayton                                     static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
9571afa68edSGreg Clayton                                 }
95897d5cf05SGreg Clayton 
9597925fbbaSGreg Clayton                                 // TODO: parse the stop reply packet
9607925fbbaSGreg Clayton                                 is_running = false;
9617925fbbaSGreg Clayton                                 process->SetPrivateState(eStateStopped);
9627925fbbaSGreg Clayton                             }
9637925fbbaSGreg Clayton                             else
9647925fbbaSGreg Clayton                             {
9657925fbbaSGreg Clayton                                 // Check to see if we are supposed to exit. There is no way to
9667925fbbaSGreg Clayton                                 // interrupt a running kernel, so all we can do is wait for an
9677925fbbaSGreg Clayton                                 // exception or detach...
9687925fbbaSGreg Clayton                                 if (listener.GetNextEvent(event_sp))
9697925fbbaSGreg Clayton                                 {
9707925fbbaSGreg Clayton                                     // We got an event, go through the loop again
9717925fbbaSGreg Clayton                                     event_type = event_sp->GetType();
9727925fbbaSGreg Clayton                                 }
973f9765acdSGreg Clayton                             }
974f9765acdSGreg Clayton                         }
975f9765acdSGreg Clayton                         break;
976f9765acdSGreg Clayton 
977f9765acdSGreg Clayton                     case eBroadcastBitAsyncThreadShouldExit:
978f9765acdSGreg Clayton                         if (log)
979d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
9807925fbbaSGreg Clayton                                          pid);
981f9765acdSGreg Clayton                         done = true;
9827925fbbaSGreg Clayton                         is_running = false;
983f9765acdSGreg Clayton                         break;
984f9765acdSGreg Clayton 
985f9765acdSGreg Clayton                     default:
986f9765acdSGreg Clayton                         if (log)
987d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
9887925fbbaSGreg Clayton                                          pid,
9897925fbbaSGreg Clayton                                          event_type);
990f9765acdSGreg Clayton                         done = true;
9917925fbbaSGreg Clayton                         is_running = false;
992f9765acdSGreg Clayton                         break;
993f9765acdSGreg Clayton                     }
9947925fbbaSGreg Clayton                 } while (is_running);
995f9765acdSGreg Clayton             }
996f9765acdSGreg Clayton             else
997f9765acdSGreg Clayton             {
998f9765acdSGreg Clayton                 if (log)
999d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
10007925fbbaSGreg Clayton                                  pid);
1001f9765acdSGreg Clayton                 done = true;
1002f9765acdSGreg Clayton             }
1003f9765acdSGreg Clayton         }
1004f9765acdSGreg Clayton     }
1005f9765acdSGreg Clayton 
1006f9765acdSGreg Clayton     if (log)
1007d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
10087925fbbaSGreg Clayton                      arg,
10097925fbbaSGreg Clayton                      pid);
1010f9765acdSGreg Clayton 
101139de3110SZachary Turner     process->m_async_thread.Reset();
1012f9765acdSGreg Clayton     return NULL;
1013f9765acdSGreg Clayton }
1014f9765acdSGreg Clayton 
1015f9765acdSGreg Clayton 
10161d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
10171d19a2f2SGreg Clayton {
10181d19a2f2SGreg Clayton private:
10191d19a2f2SGreg Clayton 
10201d19a2f2SGreg Clayton     OptionGroupOptions m_option_group;
10211d19a2f2SGreg Clayton     OptionGroupUInt64 m_command_byte;
10221d19a2f2SGreg Clayton     OptionGroupString m_packet_data;
10231d19a2f2SGreg Clayton 
10241d19a2f2SGreg Clayton     virtual Options *
10251d19a2f2SGreg Clayton     GetOptions ()
10261d19a2f2SGreg Clayton     {
10271d19a2f2SGreg Clayton         return &m_option_group;
10281d19a2f2SGreg Clayton     }
10291d19a2f2SGreg Clayton 
10301d19a2f2SGreg Clayton 
10311d19a2f2SGreg Clayton public:
10321d19a2f2SGreg Clayton     CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
10331d19a2f2SGreg Clayton         CommandObjectParsed (interpreter,
10341d19a2f2SGreg Clayton                              "process plugin packet send",
10351d19a2f2SGreg Clayton                              "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. ",
10361d19a2f2SGreg Clayton                              NULL),
10371d19a2f2SGreg Clayton         m_option_group (interpreter),
10381d19a2f2SGreg Clayton         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),
10391d19a2f2SGreg Clayton         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)
10401d19a2f2SGreg Clayton     {
10411d19a2f2SGreg Clayton         m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
10421d19a2f2SGreg Clayton         m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
10431d19a2f2SGreg Clayton         m_option_group.Finalize();
10441d19a2f2SGreg Clayton     }
10451d19a2f2SGreg Clayton 
10461d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacketSend ()
10471d19a2f2SGreg Clayton     {
10481d19a2f2SGreg Clayton     }
10491d19a2f2SGreg Clayton 
10501d19a2f2SGreg Clayton     bool
10511d19a2f2SGreg Clayton     DoExecute (Args& command, CommandReturnObject &result)
10521d19a2f2SGreg Clayton     {
10531d19a2f2SGreg Clayton         const size_t argc = command.GetArgumentCount();
10541d19a2f2SGreg Clayton         if (argc == 0)
10551d19a2f2SGreg Clayton         {
10561d19a2f2SGreg Clayton             if (!m_command_byte.GetOptionValue().OptionWasSet())
10571d19a2f2SGreg Clayton             {
10581d19a2f2SGreg Clayton                 result.AppendError ("the --command option must be set to a valid command byte");
10591d19a2f2SGreg Clayton                 result.SetStatus (eReturnStatusFailed);
10601d19a2f2SGreg Clayton             }
10611d19a2f2SGreg Clayton             else
10621d19a2f2SGreg Clayton             {
10631d19a2f2SGreg Clayton                 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
10641d19a2f2SGreg Clayton                 if (command_byte > 0 && command_byte <= UINT8_MAX)
10651d19a2f2SGreg Clayton                 {
10661d19a2f2SGreg Clayton                     ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
10671d19a2f2SGreg Clayton                     if (process)
10681d19a2f2SGreg Clayton                     {
10691d19a2f2SGreg Clayton                         const StateType state = process->GetState();
10701d19a2f2SGreg Clayton 
10711d19a2f2SGreg Clayton                         if (StateIsStoppedState (state, true))
10721d19a2f2SGreg Clayton                         {
10731d19a2f2SGreg Clayton                             std::vector<uint8_t> payload_bytes;
10741d19a2f2SGreg Clayton                             const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
10751d19a2f2SGreg Clayton                             if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
10761d19a2f2SGreg Clayton                             {
10771d19a2f2SGreg Clayton                                 StringExtractor extractor(ascii_hex_bytes_cstr);
10781d19a2f2SGreg Clayton                                 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
10791d19a2f2SGreg Clayton                                 if (ascii_hex_bytes_cstr_len & 1)
10801d19a2f2SGreg Clayton                                 {
10811d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
10821d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
10831d19a2f2SGreg Clayton                                     return false;
10841d19a2f2SGreg Clayton                                 }
10851d19a2f2SGreg Clayton                                 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
10861d19a2f2SGreg Clayton                                 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
10871d19a2f2SGreg Clayton                                 {
10881d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
10891d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
10901d19a2f2SGreg Clayton                                     return false;
10911d19a2f2SGreg Clayton                                 }
10921d19a2f2SGreg Clayton                             }
10931d19a2f2SGreg Clayton                             Error error;
10941d19a2f2SGreg Clayton                             DataExtractor reply;
10951d19a2f2SGreg Clayton                             process->GetCommunication().SendRawRequest (command_byte,
10961d19a2f2SGreg Clayton                                                                         payload_bytes.empty() ? NULL : payload_bytes.data(),
10971d19a2f2SGreg Clayton                                                                         payload_bytes.size(),
10981d19a2f2SGreg Clayton                                                                         reply,
10991d19a2f2SGreg Clayton                                                                         error);
11001d19a2f2SGreg Clayton 
11011d19a2f2SGreg Clayton                             if (error.Success())
11021d19a2f2SGreg Clayton                             {
11031d19a2f2SGreg Clayton                                 // Copy the binary bytes into a hex ASCII string for the result
11041d19a2f2SGreg Clayton                                 StreamString packet;
11051d19a2f2SGreg Clayton                                 packet.PutBytesAsRawHex8(reply.GetDataStart(),
11061d19a2f2SGreg Clayton                                                          reply.GetByteSize(),
11071d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder(),
11081d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder());
11091d19a2f2SGreg Clayton                                 result.AppendMessage(packet.GetString().c_str());
11101d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusSuccessFinishResult);
11111d19a2f2SGreg Clayton                                 return true;
11121d19a2f2SGreg Clayton                             }
11131d19a2f2SGreg Clayton                             else
11141d19a2f2SGreg Clayton                             {
11151d19a2f2SGreg Clayton                                 const char *error_cstr = error.AsCString();
11161d19a2f2SGreg Clayton                                 if (error_cstr && error_cstr[0])
11171d19a2f2SGreg Clayton                                     result.AppendError (error_cstr);
11181d19a2f2SGreg Clayton                                 else
11191d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
11201d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusFailed);
11211d19a2f2SGreg Clayton                                 return false;
11221d19a2f2SGreg Clayton                             }
11231d19a2f2SGreg Clayton                         }
11241d19a2f2SGreg Clayton                         else
11251d19a2f2SGreg Clayton                         {
11261d19a2f2SGreg Clayton                             result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
11271d19a2f2SGreg Clayton                             result.SetStatus (eReturnStatusFailed);
11281d19a2f2SGreg Clayton                         }
11291d19a2f2SGreg Clayton                     }
11301d19a2f2SGreg Clayton                     else
11311d19a2f2SGreg Clayton                     {
11321d19a2f2SGreg Clayton                         result.AppendError ("invalid process");
11331d19a2f2SGreg Clayton                         result.SetStatus (eReturnStatusFailed);
11341d19a2f2SGreg Clayton                     }
11351d19a2f2SGreg Clayton                 }
11361d19a2f2SGreg Clayton                 else
11371d19a2f2SGreg Clayton                 {
1138d01b2953SDaniel Malea                     result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
11391d19a2f2SGreg Clayton                     result.SetStatus (eReturnStatusFailed);
11401d19a2f2SGreg Clayton                 }
11411d19a2f2SGreg Clayton             }
11421d19a2f2SGreg Clayton         }
11431d19a2f2SGreg Clayton         else
11441d19a2f2SGreg Clayton         {
11451d19a2f2SGreg Clayton             result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
11461d19a2f2SGreg Clayton             result.SetStatus (eReturnStatusFailed);
11471d19a2f2SGreg Clayton         }
11481d19a2f2SGreg Clayton         return false;
11491d19a2f2SGreg Clayton     }
11501d19a2f2SGreg Clayton };
11511d19a2f2SGreg Clayton 
11521d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword
11531d19a2f2SGreg Clayton {
11541d19a2f2SGreg Clayton private:
11551d19a2f2SGreg Clayton 
11561d19a2f2SGreg Clayton public:
11571d19a2f2SGreg Clayton     CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
11581d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
11591d19a2f2SGreg Clayton                             "process plugin packet",
11601d19a2f2SGreg Clayton                             "Commands that deal with KDP remote packets.",
11611d19a2f2SGreg Clayton                             NULL)
11621d19a2f2SGreg Clayton     {
11631d19a2f2SGreg Clayton         LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
11641d19a2f2SGreg Clayton     }
11651d19a2f2SGreg Clayton 
11661d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacket ()
11671d19a2f2SGreg Clayton     {
11681d19a2f2SGreg Clayton     }
11691d19a2f2SGreg Clayton };
11701d19a2f2SGreg Clayton 
11711d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
11721d19a2f2SGreg Clayton {
11731d19a2f2SGreg Clayton public:
11741d19a2f2SGreg Clayton     CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
11751d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
11761d19a2f2SGreg Clayton                             "process plugin",
11771d19a2f2SGreg Clayton                             "A set of commands for operating on a ProcessKDP process.",
11781d19a2f2SGreg Clayton                             "process plugin <subcommand> [<subcommand-options>]")
11791d19a2f2SGreg Clayton     {
11801d19a2f2SGreg Clayton         LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket    (interpreter)));
11811d19a2f2SGreg Clayton     }
11821d19a2f2SGreg Clayton 
11831d19a2f2SGreg Clayton     ~CommandObjectMultiwordProcessKDP ()
11841d19a2f2SGreg Clayton     {
11851d19a2f2SGreg Clayton     }
11861d19a2f2SGreg Clayton };
11871d19a2f2SGreg Clayton 
11881d19a2f2SGreg Clayton CommandObject *
11891d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject()
11901d19a2f2SGreg Clayton {
11911d19a2f2SGreg Clayton     if (!m_command_sp)
11921d19a2f2SGreg Clayton         m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
11931d19a2f2SGreg Clayton     return m_command_sp.get();
11941d19a2f2SGreg Clayton }
11951d19a2f2SGreg Clayton 
1196