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"
331f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
347925fbbaSGreg Clayton #include "lldb/Target/RegisterContext.h"
3557508026SGreg Clayton #include "lldb/Target/Target.h"
36a63d08c9SGreg Clayton #include "lldb/Target/Thread.h"
37f9765acdSGreg Clayton 
38510938e5SCharles Davis #define USEC_PER_SEC 1000000
39510938e5SCharles Davis 
40f9765acdSGreg Clayton // Project includes
41f9765acdSGreg Clayton #include "ProcessKDP.h"
42f9765acdSGreg Clayton #include "ProcessKDPLog.h"
43a63d08c9SGreg Clayton #include "ThreadKDP.h"
445e8534efSJason Molenda #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
45840f12cfSJason Molenda #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
461d19a2f2SGreg Clayton #include "Utility/StringExtractor.h"
47f9765acdSGreg Clayton 
48f9765acdSGreg Clayton using namespace lldb;
49f9765acdSGreg Clayton using namespace lldb_private;
50f9765acdSGreg Clayton 
517f98240dSGreg Clayton namespace {
527f98240dSGreg Clayton 
537f98240dSGreg Clayton     static PropertyDefinition
547f98240dSGreg Clayton     g_properties[] =
557f98240dSGreg Clayton     {
567f98240dSGreg Clayton         { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." },
577f98240dSGreg Clayton         {  NULL            , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL  }
587f98240dSGreg Clayton     };
597f98240dSGreg Clayton 
607f98240dSGreg Clayton     enum
617f98240dSGreg Clayton     {
627f98240dSGreg Clayton         ePropertyPacketTimeout
637f98240dSGreg Clayton     };
647f98240dSGreg Clayton 
657f98240dSGreg Clayton     class PluginProperties : public Properties
667f98240dSGreg Clayton     {
677f98240dSGreg Clayton     public:
687f98240dSGreg Clayton 
697f98240dSGreg Clayton         static ConstString
707f98240dSGreg Clayton         GetSettingName ()
717f98240dSGreg Clayton         {
727f98240dSGreg Clayton             return ProcessKDP::GetPluginNameStatic();
737f98240dSGreg Clayton         }
747f98240dSGreg Clayton 
757f98240dSGreg Clayton         PluginProperties() :
767f98240dSGreg Clayton             Properties ()
777f98240dSGreg Clayton         {
787f98240dSGreg Clayton             m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
797f98240dSGreg Clayton             m_collection_sp->Initialize(g_properties);
807f98240dSGreg Clayton         }
817f98240dSGreg Clayton 
827f98240dSGreg Clayton         virtual
837f98240dSGreg Clayton         ~PluginProperties()
847f98240dSGreg Clayton         {
857f98240dSGreg Clayton         }
867f98240dSGreg Clayton 
877f98240dSGreg Clayton         uint64_t
887f98240dSGreg Clayton         GetPacketTimeout()
897f98240dSGreg Clayton         {
907f98240dSGreg Clayton             const uint32_t idx = ePropertyPacketTimeout;
917f98240dSGreg Clayton             return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value);
927f98240dSGreg Clayton         }
937f98240dSGreg Clayton     };
947f98240dSGreg Clayton 
957f98240dSGreg Clayton     typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
967f98240dSGreg Clayton 
977f98240dSGreg Clayton     static const ProcessKDPPropertiesSP &
987f98240dSGreg Clayton     GetGlobalPluginProperties()
997f98240dSGreg Clayton     {
1007f98240dSGreg Clayton         static ProcessKDPPropertiesSP g_settings_sp;
1017f98240dSGreg Clayton         if (!g_settings_sp)
1027f98240dSGreg Clayton             g_settings_sp.reset (new PluginProperties ());
1037f98240dSGreg Clayton         return g_settings_sp;
1047f98240dSGreg Clayton     }
1057f98240dSGreg Clayton 
1067f98240dSGreg Clayton } // anonymous namespace end
1077f98240dSGreg Clayton 
108ba4e61d3SAndrew Kaylor static const lldb::tid_t g_kernel_tid = 1;
109ba4e61d3SAndrew Kaylor 
11057abc5d6SGreg Clayton ConstString
111f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic()
112f9765acdSGreg Clayton {
11357abc5d6SGreg Clayton     static ConstString g_name("kdp-remote");
11457abc5d6SGreg Clayton     return g_name;
115f9765acdSGreg Clayton }
116f9765acdSGreg Clayton 
117f9765acdSGreg Clayton const char *
118f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic()
119f9765acdSGreg Clayton {
120f9765acdSGreg Clayton     return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
121f9765acdSGreg Clayton }
122f9765acdSGreg Clayton 
123f9765acdSGreg Clayton void
124f9765acdSGreg Clayton ProcessKDP::Terminate()
125f9765acdSGreg Clayton {
126f9765acdSGreg Clayton     PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
127f9765acdSGreg Clayton }
128f9765acdSGreg Clayton 
129f9765acdSGreg Clayton 
130c3776bf2SGreg Clayton lldb::ProcessSP
131c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target,
132c3776bf2SGreg Clayton                             Listener &listener,
133c3776bf2SGreg Clayton                             const FileSpec *crash_file_path)
134f9765acdSGreg Clayton {
135c3776bf2SGreg Clayton     lldb::ProcessSP process_sp;
136c3776bf2SGreg Clayton     if (crash_file_path == NULL)
137c3776bf2SGreg Clayton         process_sp.reset(new ProcessKDP (target, listener));
138c3776bf2SGreg Clayton     return process_sp;
139f9765acdSGreg Clayton }
140f9765acdSGreg Clayton 
141f9765acdSGreg Clayton bool
1423a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
143f9765acdSGreg Clayton {
144596ed24eSGreg Clayton     if (plugin_specified_by_name)
145596ed24eSGreg Clayton         return true;
146596ed24eSGreg Clayton 
147f9765acdSGreg Clayton     // For now we are just making sure the file exists for a given module
148aa149cbdSGreg Clayton     Module *exe_module = target.GetExecutableModulePointer();
149aa149cbdSGreg Clayton     if (exe_module)
150f9765acdSGreg Clayton     {
151f9765acdSGreg Clayton         const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
15270512317SGreg Clayton         switch (triple_ref.getOS())
15370512317SGreg Clayton         {
15470512317SGreg Clayton             case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
15570512317SGreg Clayton             case llvm::Triple::MacOSX:  // For desktop targets
15670512317SGreg Clayton             case llvm::Triple::IOS:     // For arm targets
15770512317SGreg Clayton                 if (triple_ref.getVendor() == llvm::Triple::Apple)
158f9765acdSGreg Clayton                 {
159aa149cbdSGreg Clayton                     ObjectFile *exe_objfile = exe_module->GetObjectFile();
160f9765acdSGreg Clayton                     if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
161f9765acdSGreg Clayton                         exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
162f9765acdSGreg Clayton                         return true;
163f9765acdSGreg Clayton                 }
16470512317SGreg Clayton                 break;
16570512317SGreg Clayton 
16670512317SGreg Clayton             default:
16770512317SGreg Clayton                 break;
16870512317SGreg Clayton         }
169f9765acdSGreg Clayton     }
170596ed24eSGreg Clayton     return false;
1713a29bdbeSGreg Clayton }
172f9765acdSGreg Clayton 
173f9765acdSGreg Clayton //----------------------------------------------------------------------
174f9765acdSGreg Clayton // ProcessKDP constructor
175f9765acdSGreg Clayton //----------------------------------------------------------------------
176f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
177f9765acdSGreg Clayton     Process (target, listener),
178f9765acdSGreg Clayton     m_comm("lldb.process.kdp-remote.communication"),
1794bddaeb5SJim Ingham     m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
1805e8534efSJason Molenda     m_dyld_plugin_name (),
1811d19a2f2SGreg Clayton     m_kernel_load_addr (LLDB_INVALID_ADDRESS),
182ba4e61d3SAndrew Kaylor     m_command_sp(),
183ba4e61d3SAndrew Kaylor     m_kernel_thread_wp()
184f9765acdSGreg Clayton {
1857925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
1867925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
1877f98240dSGreg Clayton     const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout();
1887f98240dSGreg Clayton     if (timeout_seconds > 0)
1897f98240dSGreg Clayton         m_comm.SetPacketTimeout(timeout_seconds);
190f9765acdSGreg Clayton }
191f9765acdSGreg Clayton 
192f9765acdSGreg Clayton //----------------------------------------------------------------------
193f9765acdSGreg Clayton // Destructor
194f9765acdSGreg Clayton //----------------------------------------------------------------------
195f9765acdSGreg Clayton ProcessKDP::~ProcessKDP()
196f9765acdSGreg Clayton {
197f9765acdSGreg Clayton     Clear();
198e24c4acfSGreg Clayton     // We need to call finalize on the process before destroying ourselves
199e24c4acfSGreg Clayton     // to make sure all of the broadcaster cleanup goes as planned. If we
200e24c4acfSGreg Clayton     // destruct this class, then Process::~Process() might have problems
201e24c4acfSGreg Clayton     // trying to fully destroy the broadcaster.
202e24c4acfSGreg Clayton     Finalize();
203f9765acdSGreg Clayton }
204f9765acdSGreg Clayton 
205f9765acdSGreg Clayton //----------------------------------------------------------------------
206f9765acdSGreg Clayton // PluginInterface
207f9765acdSGreg Clayton //----------------------------------------------------------------------
20857abc5d6SGreg Clayton lldb_private::ConstString
209f9765acdSGreg Clayton ProcessKDP::GetPluginName()
210f9765acdSGreg Clayton {
211f9765acdSGreg Clayton     return GetPluginNameStatic();
212f9765acdSGreg Clayton }
213f9765acdSGreg Clayton 
214f9765acdSGreg Clayton uint32_t
215f9765acdSGreg Clayton ProcessKDP::GetPluginVersion()
216f9765acdSGreg Clayton {
217f9765acdSGreg Clayton     return 1;
218f9765acdSGreg Clayton }
219f9765acdSGreg Clayton 
220f9765acdSGreg Clayton Error
221f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module)
222f9765acdSGreg Clayton {
223f9765acdSGreg Clayton     Error error;
224f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
225f9765acdSGreg Clayton     return error;
226f9765acdSGreg Clayton }
227f9765acdSGreg Clayton 
228f9765acdSGreg Clayton Error
229f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
230f9765acdSGreg Clayton {
231f9765acdSGreg Clayton     Error error;
232f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
233f9765acdSGreg Clayton     return error;
234f9765acdSGreg Clayton }
235f9765acdSGreg Clayton 
236f9765acdSGreg Clayton Error
237f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
238f9765acdSGreg Clayton {
239f9765acdSGreg Clayton     Error error;
240f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
241f9765acdSGreg Clayton     return error;
242f9765acdSGreg Clayton }
243f9765acdSGreg Clayton 
244f9765acdSGreg Clayton Error
2454bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
246f9765acdSGreg Clayton {
247f9765acdSGreg Clayton     Error error;
2483a29bdbeSGreg Clayton 
2497925fbbaSGreg Clayton     // Don't let any JIT happen when doing KDP as we can't allocate
2507925fbbaSGreg Clayton     // memory and we don't want to be mucking with threads that might
2517925fbbaSGreg Clayton     // already be handling exceptions
2527925fbbaSGreg Clayton     SetCanJIT(false);
2537925fbbaSGreg Clayton 
2543a29bdbeSGreg Clayton     if (remote_url == NULL || remote_url[0] == '\0')
2557925fbbaSGreg Clayton     {
2567925fbbaSGreg Clayton         error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
2577925fbbaSGreg Clayton         return error;
2587925fbbaSGreg Clayton     }
2593a29bdbeSGreg Clayton 
2607b0992d9SGreg Clayton     std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
2613a29bdbeSGreg Clayton     if (conn_ap.get())
2623a29bdbeSGreg Clayton     {
2633a29bdbeSGreg Clayton         // Only try once for now.
2643a29bdbeSGreg Clayton         // TODO: check if we should be retrying?
2653a29bdbeSGreg Clayton         const uint32_t max_retry_count = 1;
2663a29bdbeSGreg Clayton         for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
2673a29bdbeSGreg Clayton         {
2683a29bdbeSGreg Clayton             if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
2693a29bdbeSGreg Clayton                 break;
2703a29bdbeSGreg Clayton             usleep (100000);
2713a29bdbeSGreg Clayton         }
2723a29bdbeSGreg Clayton     }
2733a29bdbeSGreg Clayton 
2743a29bdbeSGreg Clayton     if (conn_ap->IsConnected())
2753a29bdbeSGreg Clayton     {
2763d7162b1SKeno Fischer         const Socket& socket = static_cast<const Socket&>(*conn_ap->GetReadObject());
2773d7162b1SKeno Fischer         const uint16_t reply_port = socket.GetPortNumber();
2783a29bdbeSGreg Clayton 
2793a29bdbeSGreg Clayton         if (reply_port != 0)
2803a29bdbeSGreg Clayton         {
2813a29bdbeSGreg Clayton             m_comm.SetConnection(conn_ap.release());
2823a29bdbeSGreg Clayton 
2833a29bdbeSGreg Clayton             if (m_comm.SendRequestReattach(reply_port))
2843a29bdbeSGreg Clayton             {
2853a29bdbeSGreg Clayton                 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
2863a29bdbeSGreg Clayton                 {
2873a29bdbeSGreg Clayton                     m_comm.GetVersion();
2883a29bdbeSGreg Clayton                     uint32_t cpu = m_comm.GetCPUType();
2893a29bdbeSGreg Clayton                     uint32_t sub = m_comm.GetCPUSubtype();
2903a29bdbeSGreg Clayton                     ArchSpec kernel_arch;
2913a29bdbeSGreg Clayton                     kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
2923a29bdbeSGreg Clayton                     m_target.SetArchitecture(kernel_arch);
2934bd4e7e3SJason Molenda 
294840f12cfSJason Molenda                     /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet.  */
295840f12cfSJason Molenda                     /* An EFI kdp session has neither UUID nor load address. */
2964bd4e7e3SJason Molenda 
2974bd4e7e3SJason Molenda                     UUID kernel_uuid = m_comm.GetUUID ();
2984bd4e7e3SJason Molenda                     addr_t kernel_load_addr = m_comm.GetLoadAddress ();
2994bd4e7e3SJason Molenda 
300840f12cfSJason Molenda                     if (m_comm.RemoteIsEFI ())
301840f12cfSJason Molenda                     {
302a1bce2efSGreg Clayton                         // Select an invalid plugin name for the dynamic loader so one doesn't get used
303a1bce2efSGreg Clayton                         // since EFI does its own manual loading via python scripting
304a1bce2efSGreg Clayton                         static ConstString g_none_dynamic_loader("none");
305a1bce2efSGreg Clayton                         m_dyld_plugin_name = g_none_dynamic_loader;
306a1bce2efSGreg Clayton 
307a1bce2efSGreg Clayton                         if (kernel_uuid.IsValid()) {
308a1bce2efSGreg Clayton                             // If EFI passed in a UUID= try to lookup UUID
309a1bce2efSGreg Clayton                             // The slide will not be provided. But the UUID
310a1bce2efSGreg Clayton                             // lookup will be used to launch EFI debug scripts
311a1bce2efSGreg Clayton                             // from the dSYM, that can load all of the symbols.
312a1bce2efSGreg Clayton                             ModuleSpec module_spec;
313a1bce2efSGreg Clayton                             module_spec.GetUUID() = kernel_uuid;
314a1bce2efSGreg Clayton                             module_spec.GetArchitecture() = m_target.GetArchitecture();
315a1bce2efSGreg Clayton 
316a1bce2efSGreg Clayton                             // Lookup UUID locally, before attempting dsymForUUID like action
317a1bce2efSGreg Clayton                             module_spec.GetSymbolFileSpec() = Symbols::LocateExecutableSymbolFile(module_spec);
318a1bce2efSGreg Clayton                             if (module_spec.GetSymbolFileSpec())
319a1bce2efSGreg Clayton                                  module_spec.GetFileSpec() = Symbols::LocateExecutableObjectFile (module_spec);
320a1bce2efSGreg Clayton                             if (!module_spec.GetSymbolFileSpec() || !module_spec.GetSymbolFileSpec())
321a1bce2efSGreg Clayton                                  Symbols::DownloadObjectAndSymbolFile (module_spec, true);
322a1bce2efSGreg Clayton 
323a1bce2efSGreg Clayton                             if (module_spec.GetFileSpec().Exists())
324a1bce2efSGreg Clayton                             {
325a1bce2efSGreg Clayton                                 ModuleSP module_sp(new Module (module_spec.GetFileSpec(), m_target.GetArchitecture()));
326a1bce2efSGreg Clayton                                 if (module_sp.get() && module_sp->MatchesModuleSpec (module_spec))
327a1bce2efSGreg Clayton                                 {
328a1bce2efSGreg Clayton                                     // Get the current target executable
329a1bce2efSGreg Clayton                                     ModuleSP exe_module_sp (m_target.GetExecutableModule ());
330a1bce2efSGreg Clayton 
331a1bce2efSGreg Clayton                                     // Make sure you don't already have the right module loaded and they will be uniqued
332a1bce2efSGreg Clayton                                     if (exe_module_sp.get() != module_sp.get())
333a1bce2efSGreg Clayton                                         m_target.SetExecutableModule (module_sp, false);
334a1bce2efSGreg Clayton                                 }
335a1bce2efSGreg Clayton                             }
336a1bce2efSGreg Clayton                         }
337840f12cfSJason Molenda                     }
338ca2ffa7eSJason Molenda                     else if (m_comm.RemoteIsDarwinKernel ())
339a8ea4baeSJason Molenda                     {
340ca2ffa7eSJason Molenda                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
341a8ea4baeSJason Molenda                         if (kernel_load_addr != LLDB_INVALID_ADDRESS)
3424bd4e7e3SJason Molenda                         {
3435e8534efSJason Molenda                             m_kernel_load_addr = kernel_load_addr;
344a8ea4baeSJason Molenda                         }
3454bd4e7e3SJason Molenda                     }
3464bd4e7e3SJason Molenda 
34797d5cf05SGreg Clayton                     // Set the thread ID
34897d5cf05SGreg Clayton                     UpdateThreadListIfNeeded ();
349a63d08c9SGreg Clayton                     SetID (1);
35056d9a1b3SGreg Clayton                     GetThreadList ();
351a63d08c9SGreg Clayton                     SetPrivateState (eStateStopped);
35207e66e3eSGreg Clayton                     StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
35307e66e3eSGreg Clayton                     if (async_strm_sp)
35407e66e3eSGreg Clayton                     {
3555b88216dSGreg Clayton                         const char *cstr;
3565b88216dSGreg Clayton                         if ((cstr = m_comm.GetKernelVersion ()) != NULL)
35707e66e3eSGreg Clayton                         {
3585b88216dSGreg Clayton                             async_strm_sp->Printf ("Version: %s\n", cstr);
35907e66e3eSGreg Clayton                             async_strm_sp->Flush();
36007e66e3eSGreg Clayton                         }
3615b88216dSGreg Clayton //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
3625b88216dSGreg Clayton //                      {
3635b88216dSGreg Clayton //                          async_strm_sp->Printf ("Image Path: %s\n", cstr);
3645b88216dSGreg Clayton //                          async_strm_sp->Flush();
3655b88216dSGreg Clayton //                      }
36607e66e3eSGreg Clayton                     }
3673a29bdbeSGreg Clayton                 }
36897d5cf05SGreg Clayton                 else
36997d5cf05SGreg Clayton                 {
37097d5cf05SGreg Clayton                     error.SetErrorString("KDP_REATTACH failed");
37197d5cf05SGreg Clayton                 }
3723a29bdbeSGreg Clayton             }
3733a29bdbeSGreg Clayton             else
3743a29bdbeSGreg Clayton             {
37597d5cf05SGreg Clayton                 error.SetErrorString("KDP_REATTACH failed");
3763a29bdbeSGreg Clayton             }
3773a29bdbeSGreg Clayton         }
3783a29bdbeSGreg Clayton         else
3793a29bdbeSGreg Clayton         {
3803a29bdbeSGreg Clayton             error.SetErrorString("invalid reply port from UDP connection");
3813a29bdbeSGreg Clayton         }
3823a29bdbeSGreg Clayton     }
3833a29bdbeSGreg Clayton     else
3843a29bdbeSGreg Clayton     {
3853a29bdbeSGreg Clayton         if (error.Success())
3863a29bdbeSGreg Clayton             error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
3873a29bdbeSGreg Clayton     }
3883a29bdbeSGreg Clayton     if (error.Fail())
3893a29bdbeSGreg Clayton         m_comm.Disconnect();
3903a29bdbeSGreg Clayton 
391f9765acdSGreg Clayton     return error;
392f9765acdSGreg Clayton }
393f9765acdSGreg Clayton 
394f9765acdSGreg Clayton //----------------------------------------------------------------------
395f9765acdSGreg Clayton // Process Control
396f9765acdSGreg Clayton //----------------------------------------------------------------------
397f9765acdSGreg Clayton Error
398982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module,
3997782de92SJean-Daniel Dupas                       ProcessLaunchInfo &launch_info)
400f9765acdSGreg Clayton {
401f9765acdSGreg Clayton     Error error;
402f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
403f9765acdSGreg Clayton     return error;
404f9765acdSGreg Clayton }
405f9765acdSGreg Clayton 
406f9765acdSGreg Clayton 
407f9765acdSGreg Clayton Error
408f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
409f9765acdSGreg Clayton {
410f9765acdSGreg Clayton     Error error;
411f9765acdSGreg Clayton     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
412f9765acdSGreg Clayton     return error;
413f9765acdSGreg Clayton }
414f9765acdSGreg Clayton 
415f9765acdSGreg Clayton Error
41684647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
41784647048SHan Ming Ong {
41884647048SHan Ming Ong     Error error;
41984647048SHan Ming Ong     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
42084647048SHan Ming Ong     return error;
42184647048SHan Ming Ong }
42284647048SHan Ming Ong 
42384647048SHan Ming Ong Error
4249c517c0dSJean-Daniel Dupas ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info)
425f9765acdSGreg Clayton {
426f9765acdSGreg Clayton     Error error;
427f9765acdSGreg Clayton     error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
428f9765acdSGreg Clayton     return error;
429f9765acdSGreg Clayton }
430f9765acdSGreg Clayton 
431f9765acdSGreg Clayton 
432f9765acdSGreg Clayton void
433bb006ce2SJim Ingham ProcessKDP::DidAttach (ArchSpec &process_arch)
434f9765acdSGreg Clayton {
435bb006ce2SJim Ingham     Process::DidAttach(process_arch);
436bb006ce2SJim Ingham 
4375160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
438f9765acdSGreg Clayton     if (log)
43954cb8f83SJohnny Chen         log->Printf ("ProcessKDP::DidAttach()");
440f9765acdSGreg Clayton     if (GetID() != LLDB_INVALID_PROCESS_ID)
441f9765acdSGreg Clayton     {
442*c3eefa39SGreg Clayton         uint32_t cpu = m_comm.GetCPUType();
443*c3eefa39SGreg Clayton         if (cpu)
444*c3eefa39SGreg Clayton         {
445*c3eefa39SGreg Clayton             uint32_t sub = m_comm.GetCPUSubtype();
446*c3eefa39SGreg Clayton             process_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
447*c3eefa39SGreg Clayton         }
448f9765acdSGreg Clayton     }
449f9765acdSGreg Clayton }
450f9765acdSGreg Clayton 
4515e8534efSJason Molenda addr_t
4525e8534efSJason Molenda ProcessKDP::GetImageInfoAddress()
4535e8534efSJason Molenda {
4545e8534efSJason Molenda     return m_kernel_load_addr;
4555e8534efSJason Molenda }
4565e8534efSJason Molenda 
4575e8534efSJason Molenda lldb_private::DynamicLoader *
4585e8534efSJason Molenda ProcessKDP::GetDynamicLoader ()
4595e8534efSJason Molenda {
4605e8534efSJason Molenda     if (m_dyld_ap.get() == NULL)
4612e56a254SJason Molenda         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
4625e8534efSJason Molenda     return m_dyld_ap.get();
4635e8534efSJason Molenda }
4645e8534efSJason Molenda 
465f9765acdSGreg Clayton Error
466f9765acdSGreg Clayton ProcessKDP::WillResume ()
467f9765acdSGreg Clayton {
468f9765acdSGreg Clayton     return Error();
469f9765acdSGreg Clayton }
470f9765acdSGreg Clayton 
471f9765acdSGreg Clayton Error
472f9765acdSGreg Clayton ProcessKDP::DoResume ()
473f9765acdSGreg Clayton {
474f9765acdSGreg Clayton     Error error;
4755160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
4767925fbbaSGreg Clayton     // Only start the async thread if we try to do any process control
477acee96aeSZachary Turner     if (!m_async_thread.IsJoinable())
4787925fbbaSGreg Clayton         StartAsyncThread();
4797925fbbaSGreg Clayton 
48097d5cf05SGreg Clayton     bool resume = false;
4817925fbbaSGreg Clayton 
48297d5cf05SGreg Clayton     // With KDP there is only one thread we can tell what to do
483ba4e61d3SAndrew Kaylor     ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
484ba4e61d3SAndrew Kaylor 
48597d5cf05SGreg Clayton     if (kernel_thread_sp)
4864b1b8b3eSGreg Clayton     {
48797d5cf05SGreg Clayton         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
4886e0ff1a3SGreg Clayton 
4896e0ff1a3SGreg Clayton         if (log)
4906e0ff1a3SGreg Clayton             log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
4917925fbbaSGreg Clayton         switch (thread_resume_state)
4924b1b8b3eSGreg Clayton         {
4937925fbbaSGreg Clayton             case eStateSuspended:
4947925fbbaSGreg Clayton                 // Nothing to do here when a thread will stay suspended
4957925fbbaSGreg Clayton                 // we just leave the CPU mask bit set to zero for the thread
4966e0ff1a3SGreg Clayton                 if (log)
4976e0ff1a3SGreg Clayton                     log->Printf ("ProcessKDP::DoResume() = suspended???");
4987925fbbaSGreg Clayton                 break;
4997925fbbaSGreg Clayton 
5007925fbbaSGreg Clayton             case eStateStepping:
5011afa68edSGreg Clayton                 {
5021afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5031afa68edSGreg Clayton 
5041afa68edSGreg Clayton                     if (reg_ctx_sp)
5051afa68edSGreg Clayton                     {
5066e0ff1a3SGreg Clayton                         if (log)
5076e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
5081afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (true);
50997d5cf05SGreg Clayton                         resume = true;
5101afa68edSGreg Clayton                     }
5111afa68edSGreg Clayton                     else
5121afa68edSGreg Clayton                     {
5131afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5141afa68edSGreg Clayton                     }
5151afa68edSGreg Clayton                 }
5167925fbbaSGreg Clayton                 break;
5177925fbbaSGreg Clayton 
51897d5cf05SGreg Clayton             case eStateRunning:
5191afa68edSGreg Clayton                 {
5201afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
5211afa68edSGreg Clayton 
5221afa68edSGreg Clayton                     if (reg_ctx_sp)
5231afa68edSGreg Clayton                     {
5246e0ff1a3SGreg Clayton                         if (log)
5256e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
5261afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (false);
52797d5cf05SGreg Clayton                         resume = true;
5281afa68edSGreg Clayton                     }
5291afa68edSGreg Clayton                     else
5301afa68edSGreg Clayton                     {
5311afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
5321afa68edSGreg Clayton                     }
5331afa68edSGreg Clayton                 }
5347925fbbaSGreg Clayton                 break;
5357925fbbaSGreg Clayton 
5367925fbbaSGreg Clayton             default:
53797d5cf05SGreg Clayton                 // The only valid thread resume states are listed above
5387925fbbaSGreg Clayton                 assert (!"invalid thread resume state");
5397925fbbaSGreg Clayton                 break;
5404b1b8b3eSGreg Clayton         }
5414b1b8b3eSGreg Clayton     }
5427925fbbaSGreg Clayton 
54397d5cf05SGreg Clayton     if (resume)
54497d5cf05SGreg Clayton     {
54597d5cf05SGreg Clayton         if (log)
54697d5cf05SGreg Clayton             log->Printf ("ProcessKDP::DoResume () sending resume");
54797d5cf05SGreg Clayton 
54897d5cf05SGreg Clayton         if (m_comm.SendRequestResume ())
5497925fbbaSGreg Clayton         {
5507925fbbaSGreg Clayton             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
5517925fbbaSGreg Clayton             SetPrivateState(eStateRunning);
5527925fbbaSGreg Clayton         }
5534b1b8b3eSGreg Clayton         else
55407e66e3eSGreg Clayton             error.SetErrorString ("KDP resume failed");
5557925fbbaSGreg Clayton     }
5567925fbbaSGreg Clayton     else
5577925fbbaSGreg Clayton     {
55897d5cf05SGreg Clayton         error.SetErrorString ("kernel thread is suspended");
5597925fbbaSGreg Clayton     }
5607925fbbaSGreg Clayton 
561f9765acdSGreg Clayton     return error;
562f9765acdSGreg Clayton }
563f9765acdSGreg Clayton 
56497d5cf05SGreg Clayton lldb::ThreadSP
565ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread()
56697d5cf05SGreg Clayton {
56797d5cf05SGreg Clayton     // KDP only tells us about one thread/core. Any other threads will usually
56897d5cf05SGreg Clayton     // be the ones that are read from memory by the OS plug-ins.
569ba4e61d3SAndrew Kaylor 
570ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (m_kernel_thread_wp.lock());
57197d5cf05SGreg Clayton     if (!thread_sp)
572ba4e61d3SAndrew Kaylor     {
573ba4e61d3SAndrew Kaylor         thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
574ba4e61d3SAndrew Kaylor         m_kernel_thread_wp = thread_sp;
575ba4e61d3SAndrew Kaylor     }
57697d5cf05SGreg Clayton     return thread_sp;
57797d5cf05SGreg Clayton }
57897d5cf05SGreg Clayton 
57997d5cf05SGreg Clayton 
58097d5cf05SGreg Clayton 
58197d5cf05SGreg Clayton 
5829fc13556SGreg Clayton bool
58356d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
584f9765acdSGreg Clayton {
585f9765acdSGreg Clayton     // locker will keep a mutex locked until it goes out of scope
5865160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
587f9765acdSGreg Clayton     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
588d01b2953SDaniel Malea         log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
589f9765acdSGreg Clayton 
59039da3efdSGreg Clayton     // Even though there is a CPU mask, it doesn't mean we can see each CPU
59197d5cf05SGreg Clayton     // indivudually, there is really only one. Lets call this thread 1.
592ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
593ba4e61d3SAndrew Kaylor     if (!thread_sp)
594ba4e61d3SAndrew Kaylor         thread_sp = GetKernelThread ();
595ba4e61d3SAndrew Kaylor     new_thread_list.AddThread(thread_sp);
59697d5cf05SGreg Clayton 
5979fc13556SGreg Clayton     return new_thread_list.GetSize(false) > 0;
598f9765acdSGreg Clayton }
599f9765acdSGreg Clayton 
600f9765acdSGreg Clayton void
601f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop ()
602f9765acdSGreg Clayton {
603f9765acdSGreg Clayton     // Let all threads recover from stopping and do any clean up based
604f9765acdSGreg Clayton     // on the previous thread state (if any).
605f9765acdSGreg Clayton     m_thread_list.RefreshStateAfterStop();
606f9765acdSGreg Clayton }
607f9765acdSGreg Clayton 
608f9765acdSGreg Clayton Error
609f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop)
610f9765acdSGreg Clayton {
611f9765acdSGreg Clayton     Error error;
612f9765acdSGreg Clayton 
61397d5cf05SGreg Clayton     if (m_comm.IsRunning())
614f9765acdSGreg Clayton     {
61597d5cf05SGreg Clayton         if (m_destroy_in_process)
61697d5cf05SGreg Clayton         {
61797d5cf05SGreg Clayton             // If we are attemping to destroy, we need to not return an error to
61897d5cf05SGreg Clayton             // Halt or DoDestroy won't get called.
61997d5cf05SGreg Clayton             // We are also currently running, so send a process stopped event
62097d5cf05SGreg Clayton             SetPrivateState (eStateStopped);
621f9765acdSGreg Clayton         }
622f9765acdSGreg Clayton         else
623f9765acdSGreg Clayton         {
62497d5cf05SGreg Clayton             error.SetErrorString ("KDP cannot interrupt a running kernel");
625f9765acdSGreg Clayton         }
626f9765acdSGreg Clayton     }
627f9765acdSGreg Clayton     return error;
628f9765acdSGreg Clayton }
629f9765acdSGreg Clayton 
630f9765acdSGreg Clayton Error
631acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped)
632f9765acdSGreg Clayton {
633f9765acdSGreg Clayton     Error error;
6345160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
635f9765acdSGreg Clayton     if (log)
636acff8950SJim Ingham         log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
637f9765acdSGreg Clayton 
63897d5cf05SGreg Clayton     if (m_comm.IsRunning())
63997d5cf05SGreg Clayton     {
64097d5cf05SGreg Clayton         // We are running and we can't interrupt a running kernel, so we need
64197d5cf05SGreg Clayton         // to just close the connection to the kernel and hope for the best
64297d5cf05SGreg Clayton     }
64397d5cf05SGreg Clayton     else
64497d5cf05SGreg Clayton     {
645acff8950SJim Ingham         // If we are going to keep the target stopped, then don't send the disconnect message.
646acff8950SJim Ingham         if (!keep_stopped && m_comm.IsConnected())
6473a29bdbeSGreg Clayton         {
6486e0ff1a3SGreg Clayton             const bool success = m_comm.SendRequestDisconnect();
649f9765acdSGreg Clayton             if (log)
650f9765acdSGreg Clayton             {
6516e0ff1a3SGreg Clayton                 if (success)
6526e0ff1a3SGreg Clayton                     log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
653f9765acdSGreg Clayton                 else
65477e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
655f9765acdSGreg Clayton             }
6566e0ff1a3SGreg Clayton             m_comm.Disconnect ();
6573a29bdbeSGreg Clayton         }
65897d5cf05SGreg Clayton     }
659f9765acdSGreg Clayton     StopAsyncThread ();
66074d4193eSGreg Clayton     m_comm.Clear();
661f9765acdSGreg Clayton 
662f9765acdSGreg Clayton     SetPrivateState (eStateDetached);
663f9765acdSGreg Clayton     ResumePrivateStateThread();
664f9765acdSGreg Clayton 
665f9765acdSGreg Clayton     //KillDebugserverProcess ();
666f9765acdSGreg Clayton     return error;
667f9765acdSGreg Clayton }
668f9765acdSGreg Clayton 
669f9765acdSGreg Clayton Error
670f9765acdSGreg Clayton ProcessKDP::DoDestroy ()
671f9765acdSGreg Clayton {
6727925fbbaSGreg Clayton     // For KDP there really is no difference between destroy and detach
673acff8950SJim Ingham     bool keep_stopped = false;
674acff8950SJim Ingham     return DoDetach(keep_stopped);
675f9765acdSGreg Clayton }
676f9765acdSGreg Clayton 
677f9765acdSGreg Clayton //------------------------------------------------------------------
678f9765acdSGreg Clayton // Process Queries
679f9765acdSGreg Clayton //------------------------------------------------------------------
680f9765acdSGreg Clayton 
681f9765acdSGreg Clayton bool
682f9765acdSGreg Clayton ProcessKDP::IsAlive ()
683f9765acdSGreg Clayton {
684f9765acdSGreg Clayton     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
685f9765acdSGreg Clayton }
686f9765acdSGreg Clayton 
687f9765acdSGreg Clayton //------------------------------------------------------------------
688f9765acdSGreg Clayton // Process Memory
689f9765acdSGreg Clayton //------------------------------------------------------------------
690f9765acdSGreg Clayton size_t
691f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
692f9765acdSGreg Clayton {
6938eb32817SJason Molenda     uint8_t *data_buffer = (uint8_t *) buf;
694a63d08c9SGreg Clayton     if (m_comm.IsConnected())
6958eb32817SJason Molenda     {
6968eb32817SJason Molenda         const size_t max_read_size = 512;
6978eb32817SJason Molenda         size_t total_bytes_read = 0;
6988eb32817SJason Molenda 
6998eb32817SJason Molenda         // Read the requested amount of memory in 512 byte chunks
7008eb32817SJason Molenda         while (total_bytes_read < size)
7018eb32817SJason Molenda         {
7028eb32817SJason Molenda             size_t bytes_to_read_this_request = size - total_bytes_read;
7038eb32817SJason Molenda             if (bytes_to_read_this_request > max_read_size)
7048eb32817SJason Molenda             {
7058eb32817SJason Molenda                 bytes_to_read_this_request = max_read_size;
7068eb32817SJason Molenda             }
7078eb32817SJason Molenda             size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read,
7088eb32817SJason Molenda                                                               data_buffer + total_bytes_read,
7098eb32817SJason Molenda                                                               bytes_to_read_this_request, error);
7108eb32817SJason Molenda             total_bytes_read += bytes_read;
7118eb32817SJason Molenda             if (error.Fail() || bytes_read == 0)
7128eb32817SJason Molenda             {
7138eb32817SJason Molenda                 return total_bytes_read;
7148eb32817SJason Molenda             }
7158eb32817SJason Molenda         }
7168eb32817SJason Molenda 
7178eb32817SJason Molenda         return total_bytes_read;
7188eb32817SJason Molenda     }
719a63d08c9SGreg Clayton     error.SetErrorString ("not connected");
720f9765acdSGreg Clayton     return 0;
721f9765acdSGreg Clayton }
722f9765acdSGreg Clayton 
723f9765acdSGreg Clayton size_t
724f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
725f9765acdSGreg Clayton {
7267925fbbaSGreg Clayton     if (m_comm.IsConnected())
7277925fbbaSGreg Clayton         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
7287925fbbaSGreg Clayton     error.SetErrorString ("not connected");
729f9765acdSGreg Clayton     return 0;
730f9765acdSGreg Clayton }
731f9765acdSGreg Clayton 
732f9765acdSGreg Clayton lldb::addr_t
733f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
734f9765acdSGreg Clayton {
735f9765acdSGreg Clayton     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
736f9765acdSGreg Clayton     return LLDB_INVALID_ADDRESS;
737f9765acdSGreg Clayton }
738f9765acdSGreg Clayton 
739f9765acdSGreg Clayton Error
740f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
741f9765acdSGreg Clayton {
742f9765acdSGreg Clayton     Error error;
743f9765acdSGreg Clayton     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
744f9765acdSGreg Clayton     return error;
745f9765acdSGreg Clayton }
746f9765acdSGreg Clayton 
747f9765acdSGreg Clayton Error
748299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
749f9765acdSGreg Clayton {
75007e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
75107e66e3eSGreg Clayton     {
75207e66e3eSGreg Clayton         Error error;
7535b88216dSGreg Clayton         if (!bp_site->IsEnabled())
7545b88216dSGreg Clayton         {
7555b88216dSGreg Clayton             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
7565b88216dSGreg Clayton             {
7575b88216dSGreg Clayton                 bp_site->SetEnabled(true);
7585b88216dSGreg Clayton                 bp_site->SetType (BreakpointSite::eExternal);
7595b88216dSGreg Clayton             }
7605b88216dSGreg Clayton             else
7615b88216dSGreg Clayton             {
76207e66e3eSGreg Clayton                 error.SetErrorString ("KDP set breakpoint failed");
7635b88216dSGreg Clayton             }
7645b88216dSGreg Clayton         }
76507e66e3eSGreg Clayton         return error;
76607e66e3eSGreg Clayton     }
767f9765acdSGreg Clayton     return EnableSoftwareBreakpoint (bp_site);
768f9765acdSGreg Clayton }
769f9765acdSGreg Clayton 
770f9765acdSGreg Clayton Error
771299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
772f9765acdSGreg Clayton {
77307e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
77407e66e3eSGreg Clayton     {
77507e66e3eSGreg Clayton         Error error;
7765b88216dSGreg Clayton         if (bp_site->IsEnabled())
7775b88216dSGreg Clayton         {
7785b88216dSGreg Clayton             BreakpointSite::Type bp_type = bp_site->GetType();
7795b88216dSGreg Clayton             if (bp_type == BreakpointSite::eExternal)
7805b88216dSGreg Clayton             {
78197d5cf05SGreg Clayton                 if (m_destroy_in_process && m_comm.IsRunning())
78297d5cf05SGreg Clayton                 {
78397d5cf05SGreg Clayton                     // We are trying to destroy our connection and we are running
78497d5cf05SGreg Clayton                     bp_site->SetEnabled(false);
78597d5cf05SGreg Clayton                 }
78697d5cf05SGreg Clayton                 else
78797d5cf05SGreg Clayton                 {
7885b88216dSGreg Clayton                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
7895b88216dSGreg Clayton                         bp_site->SetEnabled(false);
7905b88216dSGreg Clayton                     else
79107e66e3eSGreg Clayton                         error.SetErrorString ("KDP remove breakpoint failed");
7925b88216dSGreg Clayton                 }
79397d5cf05SGreg Clayton             }
7945b88216dSGreg Clayton             else
7955b88216dSGreg Clayton             {
7965b88216dSGreg Clayton                 error = DisableSoftwareBreakpoint (bp_site);
7975b88216dSGreg Clayton             }
7985b88216dSGreg Clayton         }
79907e66e3eSGreg Clayton         return error;
80007e66e3eSGreg Clayton     }
801f9765acdSGreg Clayton     return DisableSoftwareBreakpoint (bp_site);
802f9765acdSGreg Clayton }
803f9765acdSGreg Clayton 
804f9765acdSGreg Clayton Error
8051b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
806f9765acdSGreg Clayton {
807f9765acdSGreg Clayton     Error error;
808f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
809f9765acdSGreg Clayton     return error;
810f9765acdSGreg Clayton }
811f9765acdSGreg Clayton 
812f9765acdSGreg Clayton Error
8131b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
814f9765acdSGreg Clayton {
815f9765acdSGreg Clayton     Error error;
816f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
817f9765acdSGreg Clayton     return error;
818f9765acdSGreg Clayton }
819f9765acdSGreg Clayton 
820f9765acdSGreg Clayton void
821f9765acdSGreg Clayton ProcessKDP::Clear()
822f9765acdSGreg Clayton {
823f9765acdSGreg Clayton     m_thread_list.Clear();
824f9765acdSGreg Clayton }
825f9765acdSGreg Clayton 
826f9765acdSGreg Clayton Error
827f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo)
828f9765acdSGreg Clayton {
829f9765acdSGreg Clayton     Error error;
830f9765acdSGreg Clayton     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
831f9765acdSGreg Clayton     return error;
832f9765acdSGreg Clayton }
833f9765acdSGreg Clayton 
834f9765acdSGreg Clayton void
835f9765acdSGreg Clayton ProcessKDP::Initialize()
836f9765acdSGreg Clayton {
837f9765acdSGreg Clayton     static bool g_initialized = false;
838f9765acdSGreg Clayton 
839f9765acdSGreg Clayton     if (g_initialized == false)
840f9765acdSGreg Clayton     {
841f9765acdSGreg Clayton         g_initialized = true;
842f9765acdSGreg Clayton         PluginManager::RegisterPlugin (GetPluginNameStatic(),
843f9765acdSGreg Clayton                                        GetPluginDescriptionStatic(),
8447f98240dSGreg Clayton                                        CreateInstance,
8457f98240dSGreg Clayton                                        DebuggerInitialize);
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);
854f9765acdSGreg Clayton     }
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