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
15f9765acdSGreg Clayton // Other libraries and framework includes
1607e66e3eSGreg Clayton #include "lldb/Core/Debugger.h"
17f9765acdSGreg Clayton #include "lldb/Core/PluginManager.h"
181f746071SGreg Clayton #include "lldb/Core/Module.h"
194bd4e7e3SJason Molenda #include "lldb/Core/ModuleSpec.h"
20f9765acdSGreg Clayton #include "lldb/Core/State.h"
214bd4e7e3SJason Molenda #include "lldb/Core/UUID.h"
2293a66fc1SZachary Turner #include "lldb/Host/ConnectionFileDescriptor.h"
23f9765acdSGreg Clayton #include "lldb/Host/Host.h"
244bd4e7e3SJason Molenda #include "lldb/Host/Symbols.h"
253d7162b1SKeno Fischer #include "lldb/Host/Socket.h"
2639de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h"
271d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
281d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObject.h"
291d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObjectMultiword.h"
301d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandReturnObject.h"
311d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupString.h"
321d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupUInt64.h"
3341204d09SIlia K #include "lldb/Interpreter/OptionValueProperties.h"
341f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
357925fbbaSGreg Clayton #include "lldb/Target/RegisterContext.h"
3657508026SGreg Clayton #include "lldb/Target/Target.h"
37a63d08c9SGreg Clayton #include "lldb/Target/Thread.h"
38f9765acdSGreg Clayton 
39510938e5SCharles Davis #define USEC_PER_SEC 1000000
40510938e5SCharles Davis 
41f9765acdSGreg Clayton // Project includes
42f9765acdSGreg Clayton #include "ProcessKDP.h"
43f9765acdSGreg Clayton #include "ProcessKDPLog.h"
44a63d08c9SGreg Clayton #include "ThreadKDP.h"
455e8534efSJason Molenda #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
46840f12cfSJason Molenda #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
471d19a2f2SGreg Clayton #include "Utility/StringExtractor.h"
48f9765acdSGreg Clayton 
49f9765acdSGreg Clayton using namespace lldb;
50f9765acdSGreg Clayton using namespace lldb_private;
51f9765acdSGreg Clayton 
527f98240dSGreg Clayton namespace {
537f98240dSGreg Clayton 
547f98240dSGreg Clayton     static PropertyDefinition
557f98240dSGreg Clayton     g_properties[] =
567f98240dSGreg Clayton     {
577f98240dSGreg Clayton         { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." },
587f98240dSGreg Clayton         {  NULL            , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL  }
597f98240dSGreg Clayton     };
607f98240dSGreg Clayton 
617f98240dSGreg Clayton     enum
627f98240dSGreg Clayton     {
637f98240dSGreg Clayton         ePropertyPacketTimeout
647f98240dSGreg Clayton     };
657f98240dSGreg Clayton 
667f98240dSGreg Clayton     class PluginProperties : public Properties
677f98240dSGreg Clayton     {
687f98240dSGreg Clayton     public:
697f98240dSGreg Clayton 
707f98240dSGreg Clayton         static ConstString
717f98240dSGreg Clayton         GetSettingName ()
727f98240dSGreg Clayton         {
737f98240dSGreg Clayton             return ProcessKDP::GetPluginNameStatic();
747f98240dSGreg Clayton         }
757f98240dSGreg Clayton 
767f98240dSGreg Clayton         PluginProperties() :
777f98240dSGreg Clayton             Properties ()
787f98240dSGreg Clayton         {
797f98240dSGreg Clayton             m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
807f98240dSGreg Clayton             m_collection_sp->Initialize(g_properties);
817f98240dSGreg Clayton         }
827f98240dSGreg Clayton 
837f98240dSGreg Clayton         virtual
847f98240dSGreg Clayton         ~PluginProperties()
857f98240dSGreg Clayton         {
867f98240dSGreg Clayton         }
877f98240dSGreg Clayton 
887f98240dSGreg Clayton         uint64_t
897f98240dSGreg Clayton         GetPacketTimeout()
907f98240dSGreg Clayton         {
917f98240dSGreg Clayton             const uint32_t idx = ePropertyPacketTimeout;
927f98240dSGreg Clayton             return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
937f98240dSGreg Clayton         }
947f98240dSGreg Clayton     };
957f98240dSGreg Clayton 
967f98240dSGreg Clayton     typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
977f98240dSGreg Clayton 
987f98240dSGreg Clayton     static const ProcessKDPPropertiesSP &
997f98240dSGreg Clayton     GetGlobalPluginProperties()
1007f98240dSGreg Clayton     {
1017f98240dSGreg Clayton         static ProcessKDPPropertiesSP g_settings_sp;
1027f98240dSGreg Clayton         if (!g_settings_sp)
1037f98240dSGreg Clayton             g_settings_sp.reset (new PluginProperties ());
1047f98240dSGreg Clayton         return g_settings_sp;
1057f98240dSGreg Clayton     }
1067f98240dSGreg Clayton 
1077f98240dSGreg Clayton } // anonymous namespace end
1087f98240dSGreg Clayton 
109ba4e61d3SAndrew Kaylor static const lldb::tid_t g_kernel_tid = 1;
110ba4e61d3SAndrew Kaylor 
11157abc5d6SGreg Clayton ConstString
112f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic()
113f9765acdSGreg Clayton {
11457abc5d6SGreg Clayton     static ConstString g_name("kdp-remote");
11557abc5d6SGreg Clayton     return g_name;
116f9765acdSGreg Clayton }
117f9765acdSGreg Clayton 
118f9765acdSGreg Clayton const char *
119f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic()
120f9765acdSGreg Clayton {
121f9765acdSGreg Clayton     return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
122f9765acdSGreg Clayton }
123f9765acdSGreg Clayton 
124f9765acdSGreg Clayton void
125f9765acdSGreg Clayton ProcessKDP::Terminate()
126f9765acdSGreg Clayton {
127f9765acdSGreg Clayton     PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
128f9765acdSGreg Clayton }
129f9765acdSGreg Clayton 
130f9765acdSGreg Clayton 
131c3776bf2SGreg Clayton lldb::ProcessSP
132c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target,
133c3776bf2SGreg Clayton                             Listener &listener,
134c3776bf2SGreg Clayton                             const FileSpec *crash_file_path)
135f9765acdSGreg Clayton {
136c3776bf2SGreg Clayton     lldb::ProcessSP process_sp;
137c3776bf2SGreg Clayton     if (crash_file_path == NULL)
138c3776bf2SGreg Clayton         process_sp.reset(new ProcessKDP (target, listener));
139c3776bf2SGreg Clayton     return process_sp;
140f9765acdSGreg Clayton }
141f9765acdSGreg Clayton 
142f9765acdSGreg Clayton bool
1433a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
144f9765acdSGreg Clayton {
145596ed24eSGreg Clayton     if (plugin_specified_by_name)
146596ed24eSGreg Clayton         return true;
147596ed24eSGreg Clayton 
148f9765acdSGreg Clayton     // For now we are just making sure the file exists for a given module
149aa149cbdSGreg Clayton     Module *exe_module = target.GetExecutableModulePointer();
150aa149cbdSGreg Clayton     if (exe_module)
151f9765acdSGreg Clayton     {
152f9765acdSGreg Clayton         const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
15370512317SGreg Clayton         switch (triple_ref.getOS())
15470512317SGreg Clayton         {
15570512317SGreg Clayton             case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
15670512317SGreg Clayton             case llvm::Triple::MacOSX:  // For desktop targets
15770512317SGreg Clayton             case llvm::Triple::IOS:     // For arm targets
15870512317SGreg Clayton                 if (triple_ref.getVendor() == llvm::Triple::Apple)
159f9765acdSGreg Clayton                 {
160aa149cbdSGreg Clayton                     ObjectFile *exe_objfile = exe_module->GetObjectFile();
161f9765acdSGreg Clayton                     if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
162f9765acdSGreg Clayton                         exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
163f9765acdSGreg Clayton                         return true;
164f9765acdSGreg Clayton                 }
16570512317SGreg Clayton                 break;
16670512317SGreg Clayton 
16770512317SGreg Clayton             default:
16870512317SGreg Clayton                 break;
16970512317SGreg Clayton         }
170f9765acdSGreg Clayton     }
171596ed24eSGreg Clayton     return false;
1723a29bdbeSGreg Clayton }
173f9765acdSGreg Clayton 
174f9765acdSGreg Clayton //----------------------------------------------------------------------
175f9765acdSGreg Clayton // ProcessKDP constructor
176f9765acdSGreg Clayton //----------------------------------------------------------------------
177f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
178f9765acdSGreg Clayton     Process (target, listener),
179f9765acdSGreg Clayton     m_comm("lldb.process.kdp-remote.communication"),
1804bddaeb5SJim Ingham     m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
1815e8534efSJason Molenda     m_dyld_plugin_name (),
1821d19a2f2SGreg Clayton     m_kernel_load_addr (LLDB_INVALID_ADDRESS),
183ba4e61d3SAndrew Kaylor     m_command_sp(),
184ba4e61d3SAndrew Kaylor     m_kernel_thread_wp()
185f9765acdSGreg Clayton {
1867925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
1877925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
1887f98240dSGreg Clayton     const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
1897f98240dSGreg Clayton     if (timeout_seconds > 0)
1907f98240dSGreg Clayton         m_comm.SetPacketTimeout(timeout_seconds);
191f9765acdSGreg Clayton }
192f9765acdSGreg Clayton 
193f9765acdSGreg Clayton //----------------------------------------------------------------------
194f9765acdSGreg Clayton // Destructor
195f9765acdSGreg Clayton //----------------------------------------------------------------------
196f9765acdSGreg Clayton ProcessKDP::~ProcessKDP()
197f9765acdSGreg Clayton {
198f9765acdSGreg Clayton     Clear();
199e24c4acfSGreg Clayton     // We need to call finalize on the process before destroying ourselves
200e24c4acfSGreg Clayton     // to make sure all of the broadcaster cleanup goes as planned. If we
201e24c4acfSGreg Clayton     // destruct this class, then Process::~Process() might have problems
202e24c4acfSGreg Clayton     // trying to fully destroy the broadcaster.
203e24c4acfSGreg Clayton     Finalize();
204f9765acdSGreg Clayton }
205f9765acdSGreg Clayton 
206f9765acdSGreg Clayton //----------------------------------------------------------------------
207f9765acdSGreg Clayton // PluginInterface
208f9765acdSGreg Clayton //----------------------------------------------------------------------
20957abc5d6SGreg Clayton lldb_private::ConstString
210f9765acdSGreg Clayton ProcessKDP::GetPluginName()
211f9765acdSGreg Clayton {
212f9765acdSGreg Clayton     return GetPluginNameStatic();
213f9765acdSGreg Clayton }
214f9765acdSGreg Clayton 
215f9765acdSGreg Clayton uint32_t
216f9765acdSGreg Clayton ProcessKDP::GetPluginVersion()
217f9765acdSGreg Clayton {
218f9765acdSGreg Clayton     return 1;
219f9765acdSGreg Clayton }
220f9765acdSGreg Clayton 
221f9765acdSGreg Clayton Error
222f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module)
223f9765acdSGreg Clayton {
224f9765acdSGreg Clayton     Error error;
225f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
226f9765acdSGreg Clayton     return error;
227f9765acdSGreg Clayton }
228f9765acdSGreg Clayton 
229f9765acdSGreg Clayton Error
230f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
231f9765acdSGreg Clayton {
232f9765acdSGreg Clayton     Error error;
233f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
234f9765acdSGreg Clayton     return error;
235f9765acdSGreg Clayton }
236f9765acdSGreg Clayton 
237f9765acdSGreg Clayton Error
238f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
239f9765acdSGreg Clayton {
240f9765acdSGreg Clayton     Error error;
241f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
242f9765acdSGreg Clayton     return error;
243f9765acdSGreg Clayton }
244f9765acdSGreg Clayton 
245f9765acdSGreg Clayton Error
2464bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
247f9765acdSGreg Clayton {
248f9765acdSGreg Clayton     Error error;
2493a29bdbeSGreg Clayton 
2507925fbbaSGreg Clayton     // Don't let any JIT happen when doing KDP as we can't allocate
2517925fbbaSGreg Clayton     // memory and we don't want to be mucking with threads that might
2527925fbbaSGreg Clayton     // already be handling exceptions
2537925fbbaSGreg Clayton     SetCanJIT(false);
2547925fbbaSGreg Clayton 
2553a29bdbeSGreg Clayton     if (remote_url == NULL || remote_url[0] == '\0')
2567925fbbaSGreg Clayton     {
2577925fbbaSGreg Clayton         error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
2587925fbbaSGreg Clayton         return error;
2597925fbbaSGreg Clayton     }
2603a29bdbeSGreg Clayton 
2617b0992d9SGreg Clayton     std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
2623a29bdbeSGreg Clayton     if (conn_ap.get())
2633a29bdbeSGreg Clayton     {
2643a29bdbeSGreg Clayton         // Only try once for now.
2653a29bdbeSGreg Clayton         // TODO: check if we should be retrying?
2663a29bdbeSGreg Clayton         const uint32_t max_retry_count = 1;
2673a29bdbeSGreg Clayton         for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
2683a29bdbeSGreg Clayton         {
2693a29bdbeSGreg Clayton             if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
2703a29bdbeSGreg Clayton                 break;
2713a29bdbeSGreg Clayton             usleep (100000);
2723a29bdbeSGreg Clayton         }
2733a29bdbeSGreg Clayton     }
2743a29bdbeSGreg Clayton 
2753a29bdbeSGreg Clayton     if (conn_ap->IsConnected())
2763a29bdbeSGreg Clayton     {
2773d7162b1SKeno Fischer         const Socket& socket = static_cast<const Socket&>(*conn_ap->GetReadObject());
278014bb7daSVince Harron         const uint16_t reply_port = socket.GetLocalPortNumber();
2793a29bdbeSGreg Clayton 
2803a29bdbeSGreg Clayton         if (reply_port != 0)
2813a29bdbeSGreg Clayton         {
2823a29bdbeSGreg Clayton             m_comm.SetConnection(conn_ap.release());
2833a29bdbeSGreg Clayton 
2843a29bdbeSGreg Clayton             if (m_comm.SendRequestReattach(reply_port))
2853a29bdbeSGreg Clayton             {
2863a29bdbeSGreg Clayton                 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
2873a29bdbeSGreg Clayton                 {
2883a29bdbeSGreg Clayton                     m_comm.GetVersion();
2893a29bdbeSGreg Clayton                     uint32_t cpu = m_comm.GetCPUType();
2903a29bdbeSGreg Clayton                     uint32_t sub = m_comm.GetCPUSubtype();
2913a29bdbeSGreg Clayton                     ArchSpec kernel_arch;
2923a29bdbeSGreg Clayton                     kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
2933a29bdbeSGreg Clayton                     m_target.SetArchitecture(kernel_arch);
2944bd4e7e3SJason Molenda 
295840f12cfSJason Molenda                     /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet.  */
296840f12cfSJason Molenda                     /* An EFI kdp session has neither UUID nor load address. */
2974bd4e7e3SJason Molenda 
2984bd4e7e3SJason Molenda                     UUID kernel_uuid = m_comm.GetUUID ();
2994bd4e7e3SJason Molenda                     addr_t kernel_load_addr = m_comm.GetLoadAddress ();
3004bd4e7e3SJason Molenda 
301840f12cfSJason Molenda                     if (m_comm.RemoteIsEFI ())
302840f12cfSJason Molenda                     {
303a1bce2efSGreg Clayton                         // Select an invalid plugin name for the dynamic loader so one doesn't get used
304a1bce2efSGreg Clayton                         // since EFI does its own manual loading via python scripting
305a1bce2efSGreg Clayton                         static ConstString g_none_dynamic_loader("none");
306a1bce2efSGreg Clayton                         m_dyld_plugin_name = g_none_dynamic_loader;
307a1bce2efSGreg Clayton 
308a1bce2efSGreg Clayton                         if (kernel_uuid.IsValid()) {
309a1bce2efSGreg Clayton                             // If EFI passed in a UUID= try to lookup UUID
310a1bce2efSGreg Clayton                             // The slide will not be provided. But the UUID
311a1bce2efSGreg Clayton                             // lookup will be used to launch EFI debug scripts
312a1bce2efSGreg Clayton                             // from the dSYM, that can load all of the symbols.
313a1bce2efSGreg Clayton                             ModuleSpec module_spec;
314a1bce2efSGreg Clayton                             module_spec.GetUUID() = kernel_uuid;
315a1bce2efSGreg Clayton                             module_spec.GetArchitecture() = m_target.GetArchitecture();
316a1bce2efSGreg Clayton 
317a1bce2efSGreg Clayton                             // Lookup UUID locally, before attempting dsymForUUID like action
318a1bce2efSGreg Clayton                             module_spec.GetSymbolFileSpec() = Symbols::LocateExecutableSymbolFile(module_spec);
319a1bce2efSGreg Clayton                             if (module_spec.GetSymbolFileSpec())
320a1bce2efSGreg Clayton                                  module_spec.GetFileSpec() = Symbols::LocateExecutableObjectFile (module_spec);
321a1bce2efSGreg Clayton                             if (!module_spec.GetSymbolFileSpec() || !module_spec.GetSymbolFileSpec())
322a1bce2efSGreg Clayton                                  Symbols::DownloadObjectAndSymbolFile (module_spec, true);
323a1bce2efSGreg Clayton 
324a1bce2efSGreg Clayton                             if (module_spec.GetFileSpec().Exists())
325a1bce2efSGreg Clayton                             {
326a1bce2efSGreg Clayton                                 ModuleSP module_sp(new Module (module_spec.GetFileSpec(), m_target.GetArchitecture()));
327a1bce2efSGreg Clayton                                 if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec))
328a1bce2efSGreg Clayton                                 {
329a1bce2efSGreg Clayton                                     // Get the current target executable
330a1bce2efSGreg Clayton                                     ModuleSP exe_module_sp (m_target.GetExecutableModule ());
331a1bce2efSGreg Clayton 
332a1bce2efSGreg Clayton                                     // Make sure you don't already have the right module loaded and they will be uniqued
333a1bce2efSGreg Clayton                                     if (exe_module_sp.get() != module_sp.get())
334a1bce2efSGreg Clayton                                         m_target.SetExecutableModule (module_sp, false);
335a1bce2efSGreg Clayton                                 }
336a1bce2efSGreg Clayton                             }
337a1bce2efSGreg Clayton                         }
338840f12cfSJason Molenda                     }
339ca2ffa7eSJason Molenda                     else if (m_comm.RemoteIsDarwinKernel ())
340a8ea4baeSJason Molenda                     {
341ca2ffa7eSJason Molenda                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
342a8ea4baeSJason Molenda                         if (kernel_load_addr != LLDB_INVALID_ADDRESS)
3434bd4e7e3SJason Molenda                         {
3445e8534efSJason Molenda                             m_kernel_load_addr = kernel_load_addr;
345a8ea4baeSJason Molenda                         }
3464bd4e7e3SJason Molenda                     }
3474bd4e7e3SJason Molenda 
34897d5cf05SGreg Clayton                     // Set the thread ID
34997d5cf05SGreg Clayton                     UpdateThreadListIfNeeded ();
350a63d08c9SGreg Clayton                     SetID (1);
35156d9a1b3SGreg Clayton                     GetThreadList ();
352a63d08c9SGreg Clayton                     SetPrivateState (eStateStopped);
35307e66e3eSGreg Clayton                     StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
35407e66e3eSGreg Clayton                     if (async_strm_sp)
35507e66e3eSGreg Clayton                     {
3565b88216dSGreg Clayton                         const char *cstr;
3575b88216dSGreg Clayton                         if ((cstr = m_comm.GetKernelVersion ()) != NULL)
35807e66e3eSGreg Clayton                         {
3595b88216dSGreg Clayton                             async_strm_sp->Printf ("Version: %s\n", cstr);
36007e66e3eSGreg Clayton                             async_strm_sp->Flush();
36107e66e3eSGreg Clayton                         }
3625b88216dSGreg Clayton //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
3635b88216dSGreg Clayton //                      {
3645b88216dSGreg Clayton //                          async_strm_sp->Printf ("Image Path: %s\n", cstr);
3655b88216dSGreg Clayton //                          async_strm_sp->Flush();
3665b88216dSGreg Clayton //                      }
36707e66e3eSGreg Clayton                     }
3683a29bdbeSGreg Clayton                 }
36997d5cf05SGreg Clayton                 else
37097d5cf05SGreg Clayton                 {
37197d5cf05SGreg Clayton                     error.SetErrorString("KDP_REATTACH failed");
37297d5cf05SGreg Clayton                 }
3733a29bdbeSGreg Clayton             }
3743a29bdbeSGreg Clayton             else
3753a29bdbeSGreg Clayton             {
37697d5cf05SGreg Clayton                 error.SetErrorString("KDP_REATTACH failed");
3773a29bdbeSGreg Clayton             }
3783a29bdbeSGreg Clayton         }
3793a29bdbeSGreg Clayton         else
3803a29bdbeSGreg Clayton         {
3813a29bdbeSGreg Clayton             error.SetErrorString("invalid reply port from UDP connection");
3823a29bdbeSGreg Clayton         }
3833a29bdbeSGreg Clayton     }
3843a29bdbeSGreg Clayton     else
3853a29bdbeSGreg Clayton     {
3863a29bdbeSGreg Clayton         if (error.Success())
3873a29bdbeSGreg Clayton             error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
3883a29bdbeSGreg Clayton     }
3893a29bdbeSGreg Clayton     if (error.Fail())
3903a29bdbeSGreg Clayton         m_comm.Disconnect();
3913a29bdbeSGreg Clayton 
392f9765acdSGreg Clayton     return error;
393f9765acdSGreg Clayton }
394f9765acdSGreg Clayton 
395f9765acdSGreg Clayton //----------------------------------------------------------------------
396f9765acdSGreg Clayton // Process Control
397f9765acdSGreg Clayton //----------------------------------------------------------------------
398f9765acdSGreg Clayton Error
399982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module,
4007782de92SJean-Daniel Dupas                       ProcessLaunchInfo &launch_info)
401f9765acdSGreg Clayton {
402f9765acdSGreg Clayton     Error error;
403f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
404f9765acdSGreg Clayton     return error;
405f9765acdSGreg Clayton }
406f9765acdSGreg Clayton 
407f9765acdSGreg Clayton 
408f9765acdSGreg Clayton Error
409f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
410f9765acdSGreg Clayton {
411f9765acdSGreg Clayton     Error error;
412f9765acdSGreg Clayton     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
413f9765acdSGreg Clayton     return error;
414f9765acdSGreg Clayton }
415f9765acdSGreg Clayton 
416f9765acdSGreg Clayton Error
41784647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
41884647048SHan Ming Ong {
41984647048SHan Ming Ong     Error error;
42084647048SHan Ming Ong     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
42184647048SHan Ming Ong     return error;
42284647048SHan Ming Ong }
42384647048SHan Ming Ong 
42484647048SHan Ming Ong Error
4259c517c0dSJean-Daniel Dupas ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
426f9765acdSGreg Clayton {
427f9765acdSGreg Clayton     Error error;
428f9765acdSGreg Clayton     error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
429f9765acdSGreg Clayton     return error;
430f9765acdSGreg Clayton }
431f9765acdSGreg Clayton 
432f9765acdSGreg Clayton 
433f9765acdSGreg Clayton void
434bb006ce2SJim Ingham ProcessKDP::DidAttach (ArchSpec &process_arch)
435f9765acdSGreg Clayton {
436bb006ce2SJim Ingham     Process::DidAttach(process_arch);
437bb006ce2SJim Ingham 
4385160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
439f9765acdSGreg Clayton     if (log)
44054cb8f83SJohnny Chen         log->Printf ("ProcessKDP::DidAttach()");
441f9765acdSGreg Clayton     if (GetID() != LLDB_INVALID_PROCESS_ID)
442f9765acdSGreg Clayton     {
443c3eefa39SGreg Clayton         uint32_t cpu = m_comm.GetCPUType();
444c3eefa39SGreg Clayton         if (cpu)
445c3eefa39SGreg Clayton         {
446c3eefa39SGreg Clayton             uint32_t sub = m_comm.GetCPUSubtype();
447c3eefa39SGreg Clayton             process_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
448c3eefa39SGreg Clayton         }
449f9765acdSGreg Clayton     }
450f9765acdSGreg Clayton }
451f9765acdSGreg Clayton 
4525e8534efSJason Molenda addr_t
4535e8534efSJason Molenda ProcessKDP::GetImageInfoAddress()
4545e8534efSJason Molenda {
4555e8534efSJason Molenda     return m_kernel_load_addr;
4565e8534efSJason Molenda }
4575e8534efSJason Molenda 
4585e8534efSJason Molenda lldb_private::DynamicLoader *
4595e8534efSJason Molenda ProcessKDP::GetDynamicLoader ()
4605e8534efSJason Molenda {
4615e8534efSJason Molenda     if (m_dyld_ap.get() == NULL)
4622e56a254SJason Molenda         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
4635e8534efSJason Molenda     return m_dyld_ap.get();
4645e8534efSJason Molenda }
4655e8534efSJason Molenda 
466f9765acdSGreg Clayton Error
467f9765acdSGreg Clayton ProcessKDP::WillResume ()
468f9765acdSGreg Clayton {
469f9765acdSGreg Clayton     return Error();
470f9765acdSGreg Clayton }
471f9765acdSGreg Clayton 
472f9765acdSGreg Clayton Error
473f9765acdSGreg Clayton ProcessKDP::DoResume ()
474f9765acdSGreg Clayton {
475f9765acdSGreg Clayton     Error error;
4765160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
4777925fbbaSGreg Clayton     // Only start the async thread if we try to do any process control
478acee96aeSZachary Turner     if (!m_async_thread.IsJoinable())
4797925fbbaSGreg Clayton         StartAsyncThread();
4807925fbbaSGreg Clayton 
48197d5cf05SGreg Clayton     bool resume = false;
4827925fbbaSGreg Clayton 
48397d5cf05SGreg Clayton     // With KDP there is only one thread we can tell what to do
484ba4e61d3SAndrew Kaylor     ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
485ba4e61d3SAndrew Kaylor 
48697d5cf05SGreg Clayton     if (kernel_thread_sp)
4874b1b8b3eSGreg Clayton     {
48897d5cf05SGreg Clayton         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
4896e0ff1a3SGreg Clayton 
4906e0ff1a3SGreg Clayton         if (log)
4916e0ff1a3SGreg Clayton             log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
4927925fbbaSGreg Clayton         switch (thread_resume_state)
4934b1b8b3eSGreg Clayton         {
4947925fbbaSGreg Clayton             case eStateSuspended:
4957925fbbaSGreg Clayton                 // Nothing to do here when a thread will stay suspended
4967925fbbaSGreg Clayton                 // we just leave the CPU mask bit set to zero for the thread
4976e0ff1a3SGreg Clayton                 if (log)
4986e0ff1a3SGreg Clayton                     log->Printf ("ProcessKDP::DoResume() = suspended???");
4997925fbbaSGreg Clayton                 break;
5007925fbbaSGreg Clayton 
5017925fbbaSGreg Clayton             case eStateStepping:
5021afa68edSGreg Clayton                 {
5031afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5041afa68edSGreg Clayton 
5051afa68edSGreg Clayton                     if (reg_ctx_sp)
5061afa68edSGreg Clayton                     {
5076e0ff1a3SGreg Clayton                         if (log)
5086e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
5091afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (true);
51097d5cf05SGreg Clayton                         resume = true;
5111afa68edSGreg Clayton                     }
5121afa68edSGreg Clayton                     else
5131afa68edSGreg Clayton                     {
5141afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5151afa68edSGreg Clayton                     }
5161afa68edSGreg Clayton                 }
5177925fbbaSGreg Clayton                 break;
5187925fbbaSGreg Clayton 
51997d5cf05SGreg Clayton             case eStateRunning:
5201afa68edSGreg Clayton                 {
5211afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5221afa68edSGreg Clayton 
5231afa68edSGreg Clayton                     if (reg_ctx_sp)
5241afa68edSGreg Clayton                     {
5256e0ff1a3SGreg Clayton                         if (log)
5266e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
5271afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (false);
52897d5cf05SGreg Clayton                         resume = true;
5291afa68edSGreg Clayton                     }
5301afa68edSGreg Clayton                     else
5311afa68edSGreg Clayton                     {
5321afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5331afa68edSGreg Clayton                     }
5341afa68edSGreg Clayton                 }
5357925fbbaSGreg Clayton                 break;
5367925fbbaSGreg Clayton 
5377925fbbaSGreg Clayton             default:
53897d5cf05SGreg Clayton                 // The only valid thread resume states are listed above
5397925fbbaSGreg Clayton                 assert (!"invalid thread resume state");
5407925fbbaSGreg Clayton                 break;
5414b1b8b3eSGreg Clayton         }
5424b1b8b3eSGreg Clayton     }
5437925fbbaSGreg Clayton 
54497d5cf05SGreg Clayton     if (resume)
54597d5cf05SGreg Clayton     {
54697d5cf05SGreg Clayton         if (log)
54797d5cf05SGreg Clayton             log->Printf ("ProcessKDP::DoResume () sending resume");
54897d5cf05SGreg Clayton 
54997d5cf05SGreg Clayton         if (m_comm.SendRequestResume ())
5507925fbbaSGreg Clayton         {
5517925fbbaSGreg Clayton             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
5527925fbbaSGreg Clayton             SetPrivateState(eStateRunning);
5537925fbbaSGreg Clayton         }
5544b1b8b3eSGreg Clayton         else
55507e66e3eSGreg Clayton             error.SetErrorString ("KDP resume failed");
5567925fbbaSGreg Clayton     }
5577925fbbaSGreg Clayton     else
5587925fbbaSGreg Clayton     {
55997d5cf05SGreg Clayton         error.SetErrorString ("kernel thread is suspended");
5607925fbbaSGreg Clayton     }
5617925fbbaSGreg Clayton 
562f9765acdSGreg Clayton     return error;
563f9765acdSGreg Clayton }
564f9765acdSGreg Clayton 
56597d5cf05SGreg Clayton lldb::ThreadSP
566ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread()
56797d5cf05SGreg Clayton {
56897d5cf05SGreg Clayton     // KDP only tells us about one thread/core. Any other threads will usually
56997d5cf05SGreg Clayton     // be the ones that are read from memory by the OS plug-ins.
570ba4e61d3SAndrew Kaylor 
571ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (m_kernel_thread_wp.lock());
57297d5cf05SGreg Clayton     if (!thread_sp)
573ba4e61d3SAndrew Kaylor     {
574ba4e61d3SAndrew Kaylor         thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
575ba4e61d3SAndrew Kaylor         m_kernel_thread_wp = thread_sp;
576ba4e61d3SAndrew Kaylor     }
57797d5cf05SGreg Clayton     return thread_sp;
57897d5cf05SGreg Clayton }
57997d5cf05SGreg Clayton 
58097d5cf05SGreg Clayton 
58197d5cf05SGreg Clayton 
58297d5cf05SGreg Clayton 
5839fc13556SGreg Clayton bool
58456d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
585f9765acdSGreg Clayton {
586f9765acdSGreg Clayton     // locker will keep a mutex locked until it goes out of scope
5875160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
588f9765acdSGreg Clayton     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
589d01b2953SDaniel Malea         log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
590f9765acdSGreg Clayton 
59139da3efdSGreg Clayton     // Even though there is a CPU mask, it doesn't mean we can see each CPU
59297d5cf05SGreg Clayton     // indivudually, there is really only one. Lets call this thread 1.
593ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
594ba4e61d3SAndrew Kaylor     if (!thread_sp)
595ba4e61d3SAndrew Kaylor         thread_sp = GetKernelThread ();
596ba4e61d3SAndrew Kaylor     new_thread_list.AddThread(thread_sp);
59797d5cf05SGreg Clayton 
5989fc13556SGreg Clayton     return new_thread_list.GetSize(false) > 0;
599f9765acdSGreg Clayton }
600f9765acdSGreg Clayton 
601f9765acdSGreg Clayton void
602f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop ()
603f9765acdSGreg Clayton {
604f9765acdSGreg Clayton     // Let all threads recover from stopping and do any clean up based
605f9765acdSGreg Clayton     // on the previous thread state (if any).
606f9765acdSGreg Clayton     m_thread_list.RefreshStateAfterStop();
607f9765acdSGreg Clayton }
608f9765acdSGreg Clayton 
609f9765acdSGreg Clayton Error
610f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop)
611f9765acdSGreg Clayton {
612f9765acdSGreg Clayton     Error error;
613f9765acdSGreg Clayton 
61497d5cf05SGreg Clayton     if (m_comm.IsRunning())
615f9765acdSGreg Clayton     {
61697d5cf05SGreg Clayton         if (m_destroy_in_process)
61797d5cf05SGreg Clayton         {
61897d5cf05SGreg Clayton             // If we are attemping to destroy, we need to not return an error to
61997d5cf05SGreg Clayton             // Halt or DoDestroy won't get called.
62097d5cf05SGreg Clayton             // We are also currently running, so send a process stopped event
62197d5cf05SGreg Clayton             SetPrivateState (eStateStopped);
622f9765acdSGreg Clayton         }
623f9765acdSGreg Clayton         else
624f9765acdSGreg Clayton         {
62597d5cf05SGreg Clayton             error.SetErrorString ("KDP cannot interrupt a running kernel");
626f9765acdSGreg Clayton         }
627f9765acdSGreg Clayton     }
628f9765acdSGreg Clayton     return error;
629f9765acdSGreg Clayton }
630f9765acdSGreg Clayton 
631f9765acdSGreg Clayton Error
632acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped)
633f9765acdSGreg Clayton {
634f9765acdSGreg Clayton     Error error;
6355160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
636f9765acdSGreg Clayton     if (log)
637acff8950SJim Ingham         log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
638f9765acdSGreg Clayton 
63997d5cf05SGreg Clayton     if (m_comm.IsRunning())
64097d5cf05SGreg Clayton     {
64197d5cf05SGreg Clayton         // We are running and we can't interrupt a running kernel, so we need
64297d5cf05SGreg Clayton         // to just close the connection to the kernel and hope for the best
64397d5cf05SGreg Clayton     }
64497d5cf05SGreg Clayton     else
64597d5cf05SGreg Clayton     {
646acff8950SJim Ingham         // If we are going to keep the target stopped, then don't send the disconnect message.
647acff8950SJim Ingham         if (!keep_stopped && m_comm.IsConnected())
6483a29bdbeSGreg Clayton         {
6496e0ff1a3SGreg Clayton             const bool success = m_comm.SendRequestDisconnect();
650f9765acdSGreg Clayton             if (log)
651f9765acdSGreg Clayton             {
6526e0ff1a3SGreg Clayton                 if (success)
6536e0ff1a3SGreg Clayton                     log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
654f9765acdSGreg Clayton                 else
65577e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
656f9765acdSGreg Clayton             }
6576e0ff1a3SGreg Clayton             m_comm.Disconnect ();
6583a29bdbeSGreg Clayton         }
65997d5cf05SGreg Clayton     }
660f9765acdSGreg Clayton     StopAsyncThread ();
66174d4193eSGreg Clayton     m_comm.Clear();
662f9765acdSGreg Clayton 
663f9765acdSGreg Clayton     SetPrivateState (eStateDetached);
664f9765acdSGreg Clayton     ResumePrivateStateThread();
665f9765acdSGreg Clayton 
666f9765acdSGreg Clayton     //KillDebugserverProcess ();
667f9765acdSGreg Clayton     return error;
668f9765acdSGreg Clayton }
669f9765acdSGreg Clayton 
670f9765acdSGreg Clayton Error
671f9765acdSGreg Clayton ProcessKDP::DoDestroy ()
672f9765acdSGreg Clayton {
6737925fbbaSGreg Clayton     // For KDP there really is no difference between destroy and detach
674acff8950SJim Ingham     bool keep_stopped = false;
675acff8950SJim Ingham     return DoDetach(keep_stopped);
676f9765acdSGreg Clayton }
677f9765acdSGreg Clayton 
678f9765acdSGreg Clayton //------------------------------------------------------------------
679f9765acdSGreg Clayton // Process Queries
680f9765acdSGreg Clayton //------------------------------------------------------------------
681f9765acdSGreg Clayton 
682f9765acdSGreg Clayton bool
683f9765acdSGreg Clayton ProcessKDP::IsAlive ()
684f9765acdSGreg Clayton {
685f9765acdSGreg Clayton     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
686f9765acdSGreg Clayton }
687f9765acdSGreg Clayton 
688f9765acdSGreg Clayton //------------------------------------------------------------------
689f9765acdSGreg Clayton // Process Memory
690f9765acdSGreg Clayton //------------------------------------------------------------------
691f9765acdSGreg Clayton size_t
692f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
693f9765acdSGreg Clayton {
6948eb32817SJason Molenda     uint8_t *data_buffer = (uint8_t *) buf;
695a63d08c9SGreg Clayton     if (m_comm.IsConnected())
6968eb32817SJason Molenda     {
6978eb32817SJason Molenda         const size_t max_read_size = 512;
6988eb32817SJason Molenda         size_t total_bytes_read = 0;
6998eb32817SJason Molenda 
7008eb32817SJason Molenda         // Read the requested amount of memory in 512 byte chunks
7018eb32817SJason Molenda         while (total_bytes_read < size)
7028eb32817SJason Molenda         {
7038eb32817SJason Molenda             size_t bytes_to_read_this_request = size - total_bytes_read;
7048eb32817SJason Molenda             if (bytes_to_read_this_request > max_read_size)
7058eb32817SJason Molenda             {
7068eb32817SJason Molenda                 bytes_to_read_this_request = max_read_size;
7078eb32817SJason Molenda             }
7088eb32817SJason Molenda             size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read,
7098eb32817SJason Molenda                                                               data_buffer + total_bytes_read,
7108eb32817SJason Molenda                                                               bytes_to_read_this_request, error);
7118eb32817SJason Molenda             total_bytes_read += bytes_read;
7128eb32817SJason Molenda             if (error.Fail() || bytes_read == 0)
7138eb32817SJason Molenda             {
7148eb32817SJason Molenda                 return total_bytes_read;
7158eb32817SJason Molenda             }
7168eb32817SJason Molenda         }
7178eb32817SJason Molenda 
7188eb32817SJason Molenda         return total_bytes_read;
7198eb32817SJason Molenda     }
720a63d08c9SGreg Clayton     error.SetErrorString ("not connected");
721f9765acdSGreg Clayton     return 0;
722f9765acdSGreg Clayton }
723f9765acdSGreg Clayton 
724f9765acdSGreg Clayton size_t
725f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
726f9765acdSGreg Clayton {
7277925fbbaSGreg Clayton     if (m_comm.IsConnected())
7287925fbbaSGreg Clayton         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
7297925fbbaSGreg Clayton     error.SetErrorString ("not connected");
730f9765acdSGreg Clayton     return 0;
731f9765acdSGreg Clayton }
732f9765acdSGreg Clayton 
733f9765acdSGreg Clayton lldb::addr_t
734f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
735f9765acdSGreg Clayton {
736f9765acdSGreg Clayton     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
737f9765acdSGreg Clayton     return LLDB_INVALID_ADDRESS;
738f9765acdSGreg Clayton }
739f9765acdSGreg Clayton 
740f9765acdSGreg Clayton Error
741f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
742f9765acdSGreg Clayton {
743f9765acdSGreg Clayton     Error error;
744f9765acdSGreg Clayton     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
745f9765acdSGreg Clayton     return error;
746f9765acdSGreg Clayton }
747f9765acdSGreg Clayton 
748f9765acdSGreg Clayton Error
749299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
750f9765acdSGreg Clayton {
75107e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
75207e66e3eSGreg Clayton     {
75307e66e3eSGreg Clayton         Error error;
7545b88216dSGreg Clayton         if (!bp_site->IsEnabled())
7555b88216dSGreg Clayton         {
7565b88216dSGreg Clayton             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
7575b88216dSGreg Clayton             {
7585b88216dSGreg Clayton                 bp_site->SetEnabled(true);
7595b88216dSGreg Clayton                 bp_site->SetType (BreakpointSite::eExternal);
7605b88216dSGreg Clayton             }
7615b88216dSGreg Clayton             else
7625b88216dSGreg Clayton             {
76307e66e3eSGreg Clayton                 error.SetErrorString ("KDP set breakpoint failed");
7645b88216dSGreg Clayton             }
7655b88216dSGreg Clayton         }
76607e66e3eSGreg Clayton         return error;
76707e66e3eSGreg Clayton     }
768f9765acdSGreg Clayton     return EnableSoftwareBreakpoint (bp_site);
769f9765acdSGreg Clayton }
770f9765acdSGreg Clayton 
771f9765acdSGreg Clayton Error
772299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
773f9765acdSGreg Clayton {
77407e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
77507e66e3eSGreg Clayton     {
77607e66e3eSGreg Clayton         Error error;
7775b88216dSGreg Clayton         if (bp_site->IsEnabled())
7785b88216dSGreg Clayton         {
7795b88216dSGreg Clayton             BreakpointSite::Type bp_type = bp_site->GetType();
7805b88216dSGreg Clayton             if (bp_type == BreakpointSite::eExternal)
7815b88216dSGreg Clayton             {
78297d5cf05SGreg Clayton                 if (m_destroy_in_process && m_comm.IsRunning())
78397d5cf05SGreg Clayton                 {
78497d5cf05SGreg Clayton                     // We are trying to destroy our connection and we are running
78597d5cf05SGreg Clayton                     bp_site->SetEnabled(false);
78697d5cf05SGreg Clayton                 }
78797d5cf05SGreg Clayton                 else
78897d5cf05SGreg Clayton                 {
7895b88216dSGreg Clayton                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
7905b88216dSGreg Clayton                         bp_site->SetEnabled(false);
7915b88216dSGreg Clayton                     else
79207e66e3eSGreg Clayton                         error.SetErrorString ("KDP remove breakpoint failed");
7935b88216dSGreg Clayton                 }
79497d5cf05SGreg Clayton             }
7955b88216dSGreg Clayton             else
7965b88216dSGreg Clayton             {
7975b88216dSGreg Clayton                 error = DisableSoftwareBreakpoint (bp_site);
7985b88216dSGreg Clayton             }
7995b88216dSGreg Clayton         }
80007e66e3eSGreg Clayton         return error;
80107e66e3eSGreg Clayton     }
802f9765acdSGreg Clayton     return DisableSoftwareBreakpoint (bp_site);
803f9765acdSGreg Clayton }
804f9765acdSGreg Clayton 
805f9765acdSGreg Clayton Error
8061b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
807f9765acdSGreg Clayton {
808f9765acdSGreg Clayton     Error error;
809f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
810f9765acdSGreg Clayton     return error;
811f9765acdSGreg Clayton }
812f9765acdSGreg Clayton 
813f9765acdSGreg Clayton Error
8141b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
815f9765acdSGreg Clayton {
816f9765acdSGreg Clayton     Error error;
817f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
818f9765acdSGreg Clayton     return error;
819f9765acdSGreg Clayton }
820f9765acdSGreg Clayton 
821f9765acdSGreg Clayton void
822f9765acdSGreg Clayton ProcessKDP::Clear()
823f9765acdSGreg Clayton {
824f9765acdSGreg Clayton     m_thread_list.Clear();
825f9765acdSGreg Clayton }
826f9765acdSGreg Clayton 
827f9765acdSGreg Clayton Error
828f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo)
829f9765acdSGreg Clayton {
830f9765acdSGreg Clayton     Error error;
831f9765acdSGreg Clayton     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
832f9765acdSGreg Clayton     return error;
833f9765acdSGreg Clayton }
834f9765acdSGreg Clayton 
835f9765acdSGreg Clayton void
836f9765acdSGreg Clayton ProcessKDP::Initialize()
837f9765acdSGreg Clayton {
838*c8d69828SDavide Italiano     static std::once_flag g_once_flag;
839f9765acdSGreg Clayton 
840*c8d69828SDavide Italiano     std::call_once(g_once_flag, []()
841f9765acdSGreg Clayton     {
842f9765acdSGreg Clayton         PluginManager::RegisterPlugin (GetPluginNameStatic(),
843f9765acdSGreg Clayton                                        GetPluginDescriptionStatic(),
8447f98240dSGreg Clayton                                        CreateInstance,
845*c8d69828SDavide Italiano                                        DebuggerInitialize);a
846f9765acdSGreg Clayton 
847f9765acdSGreg Clayton         Log::Callbacks log_callbacks = {
848f9765acdSGreg Clayton             ProcessKDPLog::DisableLog,
849f9765acdSGreg Clayton             ProcessKDPLog::EnableLog,
850f9765acdSGreg Clayton             ProcessKDPLog::ListLogCategories
851f9765acdSGreg Clayton         };
852f9765acdSGreg Clayton 
853f9765acdSGreg Clayton         Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
854*c8d69828SDavide Italiano     });
855f9765acdSGreg Clayton }
856f9765acdSGreg Clayton 
8577f98240dSGreg Clayton void
8587f98240dSGreg Clayton ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger)
8597f98240dSGreg Clayton {
8607f98240dSGreg Clayton     if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName()))
8617f98240dSGreg Clayton     {
8627f98240dSGreg Clayton         const bool is_global_setting = true;
8637f98240dSGreg Clayton         PluginManager::CreateSettingForProcessPlugin (debugger,
8647f98240dSGreg Clayton                                                       GetGlobalPluginProperties()->GetValueProperties(),
8657f98240dSGreg Clayton                                                       ConstString ("Properties for the kdp-remote process plug-in."),
8667f98240dSGreg Clayton                                                       is_global_setting);
8677f98240dSGreg Clayton     }
8687f98240dSGreg Clayton }
8697f98240dSGreg Clayton 
870f9765acdSGreg Clayton bool
871f9765acdSGreg Clayton ProcessKDP::StartAsyncThread ()
872f9765acdSGreg Clayton {
8735160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
874f9765acdSGreg Clayton 
875f9765acdSGreg Clayton     if (log)
8767925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StartAsyncThread ()");
877f9765acdSGreg Clayton 
878acee96aeSZachary Turner     if (m_async_thread.IsJoinable())
8797925fbbaSGreg Clayton         return true;
8807925fbbaSGreg Clayton 
88139de3110SZachary Turner     m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
882acee96aeSZachary Turner     return m_async_thread.IsJoinable();
883f9765acdSGreg Clayton }
884f9765acdSGreg Clayton 
885f9765acdSGreg Clayton void
886f9765acdSGreg Clayton ProcessKDP::StopAsyncThread ()
887f9765acdSGreg Clayton {
8885160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
889f9765acdSGreg Clayton 
890f9765acdSGreg Clayton     if (log)
8917925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StopAsyncThread ()");
892f9765acdSGreg Clayton 
893f9765acdSGreg Clayton     m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
894f9765acdSGreg Clayton 
895f9765acdSGreg Clayton     // Stop the stdio thread
896acee96aeSZachary Turner     if (m_async_thread.IsJoinable())
89739de3110SZachary Turner         m_async_thread.Join(nullptr);
898f9765acdSGreg Clayton }
899f9765acdSGreg Clayton 
900f9765acdSGreg Clayton 
901f9765acdSGreg Clayton void *
902f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg)
903f9765acdSGreg Clayton {
904f9765acdSGreg Clayton     ProcessKDP *process = (ProcessKDP*) arg;
905f9765acdSGreg Clayton 
9067925fbbaSGreg Clayton     const lldb::pid_t pid = process->GetID();
9077925fbbaSGreg Clayton 
9085160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
909f9765acdSGreg Clayton     if (log)
910d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
911f9765acdSGreg Clayton 
912f9765acdSGreg Clayton     Listener listener ("ProcessKDP::AsyncThread");
913f9765acdSGreg Clayton     EventSP event_sp;
914f9765acdSGreg Clayton     const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
915f9765acdSGreg Clayton                                         eBroadcastBitAsyncThreadShouldExit;
916f9765acdSGreg Clayton 
9177925fbbaSGreg Clayton 
918f9765acdSGreg Clayton     if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
919f9765acdSGreg Clayton     {
920f9765acdSGreg Clayton         bool done = false;
921f9765acdSGreg Clayton         while (!done)
922f9765acdSGreg Clayton         {
923f9765acdSGreg Clayton             if (log)
924d01b2953SDaniel Malea                 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
9257925fbbaSGreg Clayton                              pid);
926f9765acdSGreg Clayton             if (listener.WaitForEvent (NULL, event_sp))
927f9765acdSGreg Clayton             {
9287925fbbaSGreg Clayton                 uint32_t event_type = event_sp->GetType();
929f9765acdSGreg Clayton                 if (log)
930d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
9317925fbbaSGreg Clayton                                  pid,
9327925fbbaSGreg Clayton                                  event_type);
933f9765acdSGreg Clayton 
9347925fbbaSGreg Clayton                 // When we are running, poll for 1 second to try and get an exception
9357925fbbaSGreg Clayton                 // to indicate the process has stopped. If we don't get one, check to
9367925fbbaSGreg Clayton                 // make sure no one asked us to exit
9377925fbbaSGreg Clayton                 bool is_running = false;
9387925fbbaSGreg Clayton                 DataExtractor exc_reply_packet;
9397925fbbaSGreg Clayton                 do
9407925fbbaSGreg Clayton                 {
941f9765acdSGreg Clayton                     switch (event_type)
942f9765acdSGreg Clayton                     {
943f9765acdSGreg Clayton                     case eBroadcastBitAsyncContinue:
944f9765acdSGreg Clayton                         {
9457925fbbaSGreg Clayton                             is_running = true;
9467925fbbaSGreg Clayton                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
947f9765acdSGreg Clayton                             {
948ba4e61d3SAndrew Kaylor                                 ThreadSP thread_sp (process->GetKernelThread());
9491afa68edSGreg Clayton                                 if (thread_sp)
9501afa68edSGreg Clayton                                 {
9511afa68edSGreg Clayton                                     lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
9521afa68edSGreg Clayton                                     if (reg_ctx_sp)
9531afa68edSGreg Clayton                                         reg_ctx_sp->InvalidateAllRegisters();
95497d5cf05SGreg Clayton                                     static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
9551afa68edSGreg Clayton                                 }
95697d5cf05SGreg Clayton 
9577925fbbaSGreg Clayton                                 // TODO: parse the stop reply packet
9587925fbbaSGreg Clayton                                 is_running = false;
9597925fbbaSGreg Clayton                                 process->SetPrivateState(eStateStopped);
9607925fbbaSGreg Clayton                             }
9617925fbbaSGreg Clayton                             else
9627925fbbaSGreg Clayton                             {
9637925fbbaSGreg Clayton                                 // Check to see if we are supposed to exit. There is no way to
9647925fbbaSGreg Clayton                                 // interrupt a running kernel, so all we can do is wait for an
9657925fbbaSGreg Clayton                                 // exception or detach...
9667925fbbaSGreg Clayton                                 if (listener.GetNextEvent(event_sp))
9677925fbbaSGreg Clayton                                 {
9687925fbbaSGreg Clayton                                     // We got an event, go through the loop again
9697925fbbaSGreg Clayton                                     event_type = event_sp->GetType();
9707925fbbaSGreg Clayton                                 }
971f9765acdSGreg Clayton                             }
972f9765acdSGreg Clayton                         }
973f9765acdSGreg Clayton                         break;
974f9765acdSGreg Clayton 
975f9765acdSGreg Clayton                     case eBroadcastBitAsyncThreadShouldExit:
976f9765acdSGreg Clayton                         if (log)
977d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
9787925fbbaSGreg Clayton                                          pid);
979f9765acdSGreg Clayton                         done = true;
9807925fbbaSGreg Clayton                         is_running = false;
981f9765acdSGreg Clayton                         break;
982f9765acdSGreg Clayton 
983f9765acdSGreg Clayton                     default:
984f9765acdSGreg Clayton                         if (log)
985d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
9867925fbbaSGreg Clayton                                          pid,
9877925fbbaSGreg Clayton                                          event_type);
988f9765acdSGreg Clayton                         done = true;
9897925fbbaSGreg Clayton                         is_running = false;
990f9765acdSGreg Clayton                         break;
991f9765acdSGreg Clayton                     }
9927925fbbaSGreg Clayton                 } while (is_running);
993f9765acdSGreg Clayton             }
994f9765acdSGreg Clayton             else
995f9765acdSGreg Clayton             {
996f9765acdSGreg Clayton                 if (log)
997d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
9987925fbbaSGreg Clayton                                  pid);
999f9765acdSGreg Clayton                 done = true;
1000f9765acdSGreg Clayton             }
1001f9765acdSGreg Clayton         }
1002f9765acdSGreg Clayton     }
1003f9765acdSGreg Clayton 
1004f9765acdSGreg Clayton     if (log)
1005d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
10067925fbbaSGreg Clayton                      arg,
10077925fbbaSGreg Clayton                      pid);
1008f9765acdSGreg Clayton 
100939de3110SZachary Turner     process->m_async_thread.Reset();
1010f9765acdSGreg Clayton     return NULL;
1011f9765acdSGreg Clayton }
1012f9765acdSGreg Clayton 
1013f9765acdSGreg Clayton 
10141d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
10151d19a2f2SGreg Clayton {
10161d19a2f2SGreg Clayton private:
10171d19a2f2SGreg Clayton 
10181d19a2f2SGreg Clayton     OptionGroupOptions m_option_group;
10191d19a2f2SGreg Clayton     OptionGroupUInt64 m_command_byte;
10201d19a2f2SGreg Clayton     OptionGroupString m_packet_data;
10211d19a2f2SGreg Clayton 
10221d19a2f2SGreg Clayton     virtual Options *
10231d19a2f2SGreg Clayton     GetOptions ()
10241d19a2f2SGreg Clayton     {
10251d19a2f2SGreg Clayton         return &m_option_group;
10261d19a2f2SGreg Clayton     }
10271d19a2f2SGreg Clayton 
10281d19a2f2SGreg Clayton 
10291d19a2f2SGreg Clayton public:
10301d19a2f2SGreg Clayton     CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
10311d19a2f2SGreg Clayton         CommandObjectParsed (interpreter,
10321d19a2f2SGreg Clayton                              "process plugin packet send",
10331d19a2f2SGreg 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. ",
10341d19a2f2SGreg Clayton                              NULL),
10351d19a2f2SGreg Clayton         m_option_group (interpreter),
10361d19a2f2SGreg 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),
10371d19a2f2SGreg 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)
10381d19a2f2SGreg Clayton     {
10391d19a2f2SGreg Clayton         m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
10401d19a2f2SGreg Clayton         m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
10411d19a2f2SGreg Clayton         m_option_group.Finalize();
10421d19a2f2SGreg Clayton     }
10431d19a2f2SGreg Clayton 
10441d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacketSend ()
10451d19a2f2SGreg Clayton     {
10461d19a2f2SGreg Clayton     }
10471d19a2f2SGreg Clayton 
10481d19a2f2SGreg Clayton     bool
10491d19a2f2SGreg Clayton     DoExecute (Args& command, CommandReturnObject &result)
10501d19a2f2SGreg Clayton     {
10511d19a2f2SGreg Clayton         const size_t argc = command.GetArgumentCount();
10521d19a2f2SGreg Clayton         if (argc == 0)
10531d19a2f2SGreg Clayton         {
10541d19a2f2SGreg Clayton             if (!m_command_byte.GetOptionValue().OptionWasSet())
10551d19a2f2SGreg Clayton             {
10561d19a2f2SGreg Clayton                 result.AppendError ("the --command option must be set to a valid command byte");
10571d19a2f2SGreg Clayton                 result.SetStatus (eReturnStatusFailed);
10581d19a2f2SGreg Clayton             }
10591d19a2f2SGreg Clayton             else
10601d19a2f2SGreg Clayton             {
10611d19a2f2SGreg Clayton                 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
10621d19a2f2SGreg Clayton                 if (command_byte > 0 && command_byte <= UINT8_MAX)
10631d19a2f2SGreg Clayton                 {
10641d19a2f2SGreg Clayton                     ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
10651d19a2f2SGreg Clayton                     if (process)
10661d19a2f2SGreg Clayton                     {
10671d19a2f2SGreg Clayton                         const StateType state = process->GetState();
10681d19a2f2SGreg Clayton 
10691d19a2f2SGreg Clayton                         if (StateIsStoppedState (state, true))
10701d19a2f2SGreg Clayton                         {
10711d19a2f2SGreg Clayton                             std::vector<uint8_t> payload_bytes;
10721d19a2f2SGreg Clayton                             const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
10731d19a2f2SGreg Clayton                             if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
10741d19a2f2SGreg Clayton                             {
10751d19a2f2SGreg Clayton                                 StringExtractor extractor(ascii_hex_bytes_cstr);
10761d19a2f2SGreg Clayton                                 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
10771d19a2f2SGreg Clayton                                 if (ascii_hex_bytes_cstr_len & 1)
10781d19a2f2SGreg Clayton                                 {
10791d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
10801d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
10811d19a2f2SGreg Clayton                                     return false;
10821d19a2f2SGreg Clayton                                 }
10831d19a2f2SGreg Clayton                                 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
10841d19a2f2SGreg Clayton                                 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
10851d19a2f2SGreg Clayton                                 {
10861d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
10871d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
10881d19a2f2SGreg Clayton                                     return false;
10891d19a2f2SGreg Clayton                                 }
10901d19a2f2SGreg Clayton                             }
10911d19a2f2SGreg Clayton                             Error error;
10921d19a2f2SGreg Clayton                             DataExtractor reply;
10931d19a2f2SGreg Clayton                             process->GetCommunication().SendRawRequest (command_byte,
10941d19a2f2SGreg Clayton                                                                         payload_bytes.empty() ? NULL : payload_bytes.data(),
10951d19a2f2SGreg Clayton                                                                         payload_bytes.size(),
10961d19a2f2SGreg Clayton                                                                         reply,
10971d19a2f2SGreg Clayton                                                                         error);
10981d19a2f2SGreg Clayton 
10991d19a2f2SGreg Clayton                             if (error.Success())
11001d19a2f2SGreg Clayton                             {
11011d19a2f2SGreg Clayton                                 // Copy the binary bytes into a hex ASCII string for the result
11021d19a2f2SGreg Clayton                                 StreamString packet;
11031d19a2f2SGreg Clayton                                 packet.PutBytesAsRawHex8(reply.GetDataStart(),
11041d19a2f2SGreg Clayton                                                          reply.GetByteSize(),
11051d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder(),
11061d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder());
11071d19a2f2SGreg Clayton                                 result.AppendMessage(packet.GetString().c_str());
11081d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusSuccessFinishResult);
11091d19a2f2SGreg Clayton                                 return true;
11101d19a2f2SGreg Clayton                             }
11111d19a2f2SGreg Clayton                             else
11121d19a2f2SGreg Clayton                             {
11131d19a2f2SGreg Clayton                                 const char *error_cstr = error.AsCString();
11141d19a2f2SGreg Clayton                                 if (error_cstr && error_cstr[0])
11151d19a2f2SGreg Clayton                                     result.AppendError (error_cstr);
11161d19a2f2SGreg Clayton                                 else
11171d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
11181d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusFailed);
11191d19a2f2SGreg Clayton                                 return false;
11201d19a2f2SGreg Clayton                             }
11211d19a2f2SGreg Clayton                         }
11221d19a2f2SGreg Clayton                         else
11231d19a2f2SGreg Clayton                         {
11241d19a2f2SGreg Clayton                             result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
11251d19a2f2SGreg Clayton                             result.SetStatus (eReturnStatusFailed);
11261d19a2f2SGreg Clayton                         }
11271d19a2f2SGreg Clayton                     }
11281d19a2f2SGreg Clayton                     else
11291d19a2f2SGreg Clayton                     {
11301d19a2f2SGreg Clayton                         result.AppendError ("invalid process");
11311d19a2f2SGreg Clayton                         result.SetStatus (eReturnStatusFailed);
11321d19a2f2SGreg Clayton                     }
11331d19a2f2SGreg Clayton                 }
11341d19a2f2SGreg Clayton                 else
11351d19a2f2SGreg Clayton                 {
1136d01b2953SDaniel Malea                     result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
11371d19a2f2SGreg Clayton                     result.SetStatus (eReturnStatusFailed);
11381d19a2f2SGreg Clayton                 }
11391d19a2f2SGreg Clayton             }
11401d19a2f2SGreg Clayton         }
11411d19a2f2SGreg Clayton         else
11421d19a2f2SGreg Clayton         {
11431d19a2f2SGreg Clayton             result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
11441d19a2f2SGreg Clayton             result.SetStatus (eReturnStatusFailed);
11451d19a2f2SGreg Clayton         }
11461d19a2f2SGreg Clayton         return false;
11471d19a2f2SGreg Clayton     }
11481d19a2f2SGreg Clayton };
11491d19a2f2SGreg Clayton 
11501d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword
11511d19a2f2SGreg Clayton {
11521d19a2f2SGreg Clayton private:
11531d19a2f2SGreg Clayton 
11541d19a2f2SGreg Clayton public:
11551d19a2f2SGreg Clayton     CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
11561d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
11571d19a2f2SGreg Clayton                             "process plugin packet",
11581d19a2f2SGreg Clayton                             "Commands that deal with KDP remote packets.",
11591d19a2f2SGreg Clayton                             NULL)
11601d19a2f2SGreg Clayton     {
11611d19a2f2SGreg Clayton         LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
11621d19a2f2SGreg Clayton     }
11631d19a2f2SGreg Clayton 
11641d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacket ()
11651d19a2f2SGreg Clayton     {
11661d19a2f2SGreg Clayton     }
11671d19a2f2SGreg Clayton };
11681d19a2f2SGreg Clayton 
11691d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
11701d19a2f2SGreg Clayton {
11711d19a2f2SGreg Clayton public:
11721d19a2f2SGreg Clayton     CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
11731d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
11741d19a2f2SGreg Clayton                             "process plugin",
11751d19a2f2SGreg Clayton                             "A set of commands for operating on a ProcessKDP process.",
11761d19a2f2SGreg Clayton                             "process plugin <subcommand> [<subcommand-options>]")
11771d19a2f2SGreg Clayton     {
11781d19a2f2SGreg Clayton         LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket    (interpreter)));
11791d19a2f2SGreg Clayton     }
11801d19a2f2SGreg Clayton 
11811d19a2f2SGreg Clayton     ~CommandObjectMultiwordProcessKDP ()
11821d19a2f2SGreg Clayton     {
11831d19a2f2SGreg Clayton     }
11841d19a2f2SGreg Clayton };
11851d19a2f2SGreg Clayton 
11861d19a2f2SGreg Clayton CommandObject *
11871d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject()
11881d19a2f2SGreg Clayton {
11891d19a2f2SGreg Clayton     if (!m_command_sp)
11901d19a2f2SGreg Clayton         m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
11911d19a2f2SGreg Clayton     return m_command_sp.get();
11921d19a2f2SGreg Clayton }
11931d19a2f2SGreg Clayton 
1194