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
163a29bdbeSGreg Clayton #include "lldb/Core/ConnectionFileDescriptor.h"
1707e66e3eSGreg Clayton #include "lldb/Core/Debugger.h"
18f9765acdSGreg Clayton #include "lldb/Core/PluginManager.h"
191f746071SGreg Clayton #include "lldb/Core/Module.h"
204bd4e7e3SJason Molenda #include "lldb/Core/ModuleSpec.h"
21f9765acdSGreg Clayton #include "lldb/Core/State.h"
224bd4e7e3SJason Molenda #include "lldb/Core/UUID.h"
23f9765acdSGreg Clayton #include "lldb/Host/Host.h"
244bd4e7e3SJason Molenda #include "lldb/Host/Symbols.h"
251d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
261d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObject.h"
271d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandObjectMultiword.h"
281d19a2f2SGreg Clayton #include "lldb/Interpreter/CommandReturnObject.h"
291d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupString.h"
301d19a2f2SGreg Clayton #include "lldb/Interpreter/OptionGroupUInt64.h"
311f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
327925fbbaSGreg Clayton #include "lldb/Target/RegisterContext.h"
3357508026SGreg Clayton #include "lldb/Target/Target.h"
34a63d08c9SGreg Clayton #include "lldb/Target/Thread.h"
35f9765acdSGreg Clayton 
36f9765acdSGreg Clayton // Project includes
37f9765acdSGreg Clayton #include "ProcessKDP.h"
38f9765acdSGreg Clayton #include "ProcessKDPLog.h"
39a63d08c9SGreg Clayton #include "ThreadKDP.h"
405e8534efSJason Molenda #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
41840f12cfSJason Molenda #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
421d19a2f2SGreg Clayton #include "Utility/StringExtractor.h"
43f9765acdSGreg Clayton 
44f9765acdSGreg Clayton using namespace lldb;
45f9765acdSGreg Clayton using namespace lldb_private;
46f9765acdSGreg Clayton 
47ba4e61d3SAndrew Kaylor static const lldb::tid_t g_kernel_tid = 1;
48ba4e61d3SAndrew Kaylor 
49f9765acdSGreg Clayton const char *
50f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic()
51f9765acdSGreg Clayton {
52f9765acdSGreg Clayton     return "kdp-remote";
53f9765acdSGreg Clayton }
54f9765acdSGreg Clayton 
55f9765acdSGreg Clayton const char *
56f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic()
57f9765acdSGreg Clayton {
58f9765acdSGreg Clayton     return "KDP Remote protocol based debugging plug-in for darwin kernel debugging.";
59f9765acdSGreg Clayton }
60f9765acdSGreg Clayton 
61f9765acdSGreg Clayton void
62f9765acdSGreg Clayton ProcessKDP::Terminate()
63f9765acdSGreg Clayton {
64f9765acdSGreg Clayton     PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance);
65f9765acdSGreg Clayton }
66f9765acdSGreg Clayton 
67f9765acdSGreg Clayton 
68c3776bf2SGreg Clayton lldb::ProcessSP
69c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target,
70c3776bf2SGreg Clayton                             Listener &listener,
71c3776bf2SGreg Clayton                             const FileSpec *crash_file_path)
72f9765acdSGreg Clayton {
73c3776bf2SGreg Clayton     lldb::ProcessSP process_sp;
74c3776bf2SGreg Clayton     if (crash_file_path == NULL)
75c3776bf2SGreg Clayton         process_sp.reset(new ProcessKDP (target, listener));
76c3776bf2SGreg Clayton     return process_sp;
77f9765acdSGreg Clayton }
78f9765acdSGreg Clayton 
79f9765acdSGreg Clayton bool
803a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name)
81f9765acdSGreg Clayton {
82596ed24eSGreg Clayton     if (plugin_specified_by_name)
83596ed24eSGreg Clayton         return true;
84596ed24eSGreg Clayton 
85f9765acdSGreg Clayton     // For now we are just making sure the file exists for a given module
86aa149cbdSGreg Clayton     Module *exe_module = target.GetExecutableModulePointer();
87aa149cbdSGreg Clayton     if (exe_module)
88f9765acdSGreg Clayton     {
89f9765acdSGreg Clayton         const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple();
9070512317SGreg Clayton         switch (triple_ref.getOS())
9170512317SGreg Clayton         {
9270512317SGreg Clayton             case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case
9370512317SGreg Clayton             case llvm::Triple::MacOSX:  // For desktop targets
9470512317SGreg Clayton             case llvm::Triple::IOS:     // For arm targets
9570512317SGreg Clayton                 if (triple_ref.getVendor() == llvm::Triple::Apple)
96f9765acdSGreg Clayton                 {
97aa149cbdSGreg Clayton                     ObjectFile *exe_objfile = exe_module->GetObjectFile();
98f9765acdSGreg Clayton                     if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
99f9765acdSGreg Clayton                         exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
100f9765acdSGreg Clayton                         return true;
101f9765acdSGreg Clayton                 }
10270512317SGreg Clayton                 break;
10370512317SGreg Clayton 
10470512317SGreg Clayton             default:
10570512317SGreg Clayton                 break;
10670512317SGreg Clayton         }
107f9765acdSGreg Clayton     }
108596ed24eSGreg Clayton     return false;
1093a29bdbeSGreg Clayton }
110f9765acdSGreg Clayton 
111f9765acdSGreg Clayton //----------------------------------------------------------------------
112f9765acdSGreg Clayton // ProcessKDP constructor
113f9765acdSGreg Clayton //----------------------------------------------------------------------
114f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
115f9765acdSGreg Clayton     Process (target, listener),
116f9765acdSGreg Clayton     m_comm("lldb.process.kdp-remote.communication"),
1174bddaeb5SJim Ingham     m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
11897d5cf05SGreg Clayton     m_async_thread (LLDB_INVALID_HOST_THREAD),
1195e8534efSJason Molenda     m_dyld_plugin_name (),
1201d19a2f2SGreg Clayton     m_kernel_load_addr (LLDB_INVALID_ADDRESS),
121ba4e61d3SAndrew Kaylor     m_command_sp(),
122ba4e61d3SAndrew Kaylor     m_kernel_thread_wp()
123f9765acdSGreg Clayton {
1247925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
1257925fbbaSGreg Clayton     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
126f9765acdSGreg Clayton }
127f9765acdSGreg Clayton 
128f9765acdSGreg Clayton //----------------------------------------------------------------------
129f9765acdSGreg Clayton // Destructor
130f9765acdSGreg Clayton //----------------------------------------------------------------------
131f9765acdSGreg Clayton ProcessKDP::~ProcessKDP()
132f9765acdSGreg Clayton {
133f9765acdSGreg Clayton     Clear();
134e24c4acfSGreg Clayton     // We need to call finalize on the process before destroying ourselves
135e24c4acfSGreg Clayton     // to make sure all of the broadcaster cleanup goes as planned. If we
136e24c4acfSGreg Clayton     // destruct this class, then Process::~Process() might have problems
137e24c4acfSGreg Clayton     // trying to fully destroy the broadcaster.
138e24c4acfSGreg Clayton     Finalize();
139f9765acdSGreg Clayton }
140f9765acdSGreg Clayton 
141f9765acdSGreg Clayton //----------------------------------------------------------------------
142f9765acdSGreg Clayton // PluginInterface
143f9765acdSGreg Clayton //----------------------------------------------------------------------
144f9765acdSGreg Clayton const char *
145f9765acdSGreg Clayton ProcessKDP::GetPluginName()
146f9765acdSGreg Clayton {
147f9765acdSGreg Clayton     return "Process debugging plug-in that uses the Darwin KDP remote protocol";
148f9765acdSGreg Clayton }
149f9765acdSGreg Clayton 
150f9765acdSGreg Clayton const char *
151f9765acdSGreg Clayton ProcessKDP::GetShortPluginName()
152f9765acdSGreg Clayton {
153f9765acdSGreg Clayton     return GetPluginNameStatic();
154f9765acdSGreg Clayton }
155f9765acdSGreg Clayton 
156f9765acdSGreg Clayton uint32_t
157f9765acdSGreg Clayton ProcessKDP::GetPluginVersion()
158f9765acdSGreg Clayton {
159f9765acdSGreg Clayton     return 1;
160f9765acdSGreg Clayton }
161f9765acdSGreg Clayton 
162f9765acdSGreg Clayton Error
163f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module)
164f9765acdSGreg Clayton {
165f9765acdSGreg Clayton     Error error;
166f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
167f9765acdSGreg Clayton     return error;
168f9765acdSGreg Clayton }
169f9765acdSGreg Clayton 
170f9765acdSGreg Clayton Error
171f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid)
172f9765acdSGreg Clayton {
173f9765acdSGreg Clayton     Error error;
174f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in");
175f9765acdSGreg Clayton     return error;
176f9765acdSGreg Clayton }
177f9765acdSGreg Clayton 
178f9765acdSGreg Clayton Error
179f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch)
180f9765acdSGreg Clayton {
181f9765acdSGreg Clayton     Error error;
182f9765acdSGreg Clayton     error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in");
183f9765acdSGreg Clayton     return error;
184f9765acdSGreg Clayton }
185f9765acdSGreg Clayton 
186f9765acdSGreg Clayton Error
1874bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url)
188f9765acdSGreg Clayton {
189f9765acdSGreg Clayton     Error error;
1903a29bdbeSGreg Clayton 
1917925fbbaSGreg Clayton     // Don't let any JIT happen when doing KDP as we can't allocate
1927925fbbaSGreg Clayton     // memory and we don't want to be mucking with threads that might
1937925fbbaSGreg Clayton     // already be handling exceptions
1947925fbbaSGreg Clayton     SetCanJIT(false);
1957925fbbaSGreg Clayton 
1963a29bdbeSGreg Clayton     if (remote_url == NULL || remote_url[0] == '\0')
1977925fbbaSGreg Clayton     {
1987925fbbaSGreg Clayton         error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url);
1997925fbbaSGreg Clayton         return error;
2007925fbbaSGreg Clayton     }
2013a29bdbeSGreg Clayton 
2027b0992d9SGreg Clayton     std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
2033a29bdbeSGreg Clayton     if (conn_ap.get())
2043a29bdbeSGreg Clayton     {
2053a29bdbeSGreg Clayton         // Only try once for now.
2063a29bdbeSGreg Clayton         // TODO: check if we should be retrying?
2073a29bdbeSGreg Clayton         const uint32_t max_retry_count = 1;
2083a29bdbeSGreg Clayton         for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count)
2093a29bdbeSGreg Clayton         {
2103a29bdbeSGreg Clayton             if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
2113a29bdbeSGreg Clayton                 break;
2123a29bdbeSGreg Clayton             usleep (100000);
2133a29bdbeSGreg Clayton         }
2143a29bdbeSGreg Clayton     }
2153a29bdbeSGreg Clayton 
2163a29bdbeSGreg Clayton     if (conn_ap->IsConnected())
2173a29bdbeSGreg Clayton     {
2183a29bdbeSGreg Clayton         const uint16_t reply_port = conn_ap->GetReadPort ();
2193a29bdbeSGreg Clayton 
2203a29bdbeSGreg Clayton         if (reply_port != 0)
2213a29bdbeSGreg Clayton         {
2223a29bdbeSGreg Clayton             m_comm.SetConnection(conn_ap.release());
2233a29bdbeSGreg Clayton 
2243a29bdbeSGreg Clayton             if (m_comm.SendRequestReattach(reply_port))
2253a29bdbeSGreg Clayton             {
2263a29bdbeSGreg Clayton                 if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB..."))
2273a29bdbeSGreg Clayton                 {
2283a29bdbeSGreg Clayton                     m_comm.GetVersion();
2293a29bdbeSGreg Clayton                     uint32_t cpu = m_comm.GetCPUType();
2303a29bdbeSGreg Clayton                     uint32_t sub = m_comm.GetCPUSubtype();
2313a29bdbeSGreg Clayton                     ArchSpec kernel_arch;
2323a29bdbeSGreg Clayton                     kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
2333a29bdbeSGreg Clayton                     m_target.SetArchitecture(kernel_arch);
2344bd4e7e3SJason Molenda 
235840f12cfSJason Molenda                     /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet.  */
236840f12cfSJason Molenda                     /* An EFI kdp session has neither UUID nor load address. */
2374bd4e7e3SJason Molenda 
2384bd4e7e3SJason Molenda                     UUID kernel_uuid = m_comm.GetUUID ();
2394bd4e7e3SJason Molenda                     addr_t kernel_load_addr = m_comm.GetLoadAddress ();
2404bd4e7e3SJason Molenda 
241840f12cfSJason Molenda                     if (m_comm.RemoteIsEFI ())
242840f12cfSJason Molenda                     {
243840f12cfSJason Molenda                         m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
244840f12cfSJason Molenda                     }
245*ca2ffa7eSJason Molenda                     else if (m_comm.RemoteIsDarwinKernel ())
246a8ea4baeSJason Molenda                     {
247*ca2ffa7eSJason Molenda                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
248a8ea4baeSJason Molenda                         if (kernel_load_addr != LLDB_INVALID_ADDRESS)
2494bd4e7e3SJason Molenda                         {
2505e8534efSJason Molenda                             m_kernel_load_addr = kernel_load_addr;
251a8ea4baeSJason Molenda                         }
2524bd4e7e3SJason Molenda                     }
2534bd4e7e3SJason Molenda 
25497d5cf05SGreg Clayton                     // Set the thread ID
25597d5cf05SGreg Clayton                     UpdateThreadListIfNeeded ();
256a63d08c9SGreg Clayton                     SetID (1);
25756d9a1b3SGreg Clayton                     GetThreadList ();
258a63d08c9SGreg Clayton                     SetPrivateState (eStateStopped);
25907e66e3eSGreg Clayton                     StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream());
26007e66e3eSGreg Clayton                     if (async_strm_sp)
26107e66e3eSGreg Clayton                     {
2625b88216dSGreg Clayton                         const char *cstr;
2635b88216dSGreg Clayton                         if ((cstr = m_comm.GetKernelVersion ()) != NULL)
26407e66e3eSGreg Clayton                         {
2655b88216dSGreg Clayton                             async_strm_sp->Printf ("Version: %s\n", cstr);
26607e66e3eSGreg Clayton                             async_strm_sp->Flush();
26707e66e3eSGreg Clayton                         }
2685b88216dSGreg Clayton //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
2695b88216dSGreg Clayton //                      {
2705b88216dSGreg Clayton //                          async_strm_sp->Printf ("Image Path: %s\n", cstr);
2715b88216dSGreg Clayton //                          async_strm_sp->Flush();
2725b88216dSGreg Clayton //                      }
27307e66e3eSGreg Clayton                     }
2743a29bdbeSGreg Clayton                 }
27597d5cf05SGreg Clayton                 else
27697d5cf05SGreg Clayton                 {
27797d5cf05SGreg Clayton                     error.SetErrorString("KDP_REATTACH failed");
27897d5cf05SGreg Clayton                 }
2793a29bdbeSGreg Clayton             }
2803a29bdbeSGreg Clayton             else
2813a29bdbeSGreg Clayton             {
28297d5cf05SGreg Clayton                 error.SetErrorString("KDP_REATTACH failed");
2833a29bdbeSGreg Clayton             }
2843a29bdbeSGreg Clayton         }
2853a29bdbeSGreg Clayton         else
2863a29bdbeSGreg Clayton         {
2873a29bdbeSGreg Clayton             error.SetErrorString("invalid reply port from UDP connection");
2883a29bdbeSGreg Clayton         }
2893a29bdbeSGreg Clayton     }
2903a29bdbeSGreg Clayton     else
2913a29bdbeSGreg Clayton     {
2923a29bdbeSGreg Clayton         if (error.Success())
2933a29bdbeSGreg Clayton             error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url);
2943a29bdbeSGreg Clayton     }
2953a29bdbeSGreg Clayton     if (error.Fail())
2963a29bdbeSGreg Clayton         m_comm.Disconnect();
2973a29bdbeSGreg Clayton 
298f9765acdSGreg Clayton     return error;
299f9765acdSGreg Clayton }
300f9765acdSGreg Clayton 
301f9765acdSGreg Clayton //----------------------------------------------------------------------
302f9765acdSGreg Clayton // Process Control
303f9765acdSGreg Clayton //----------------------------------------------------------------------
304f9765acdSGreg Clayton Error
305982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module,
306982c9762SGreg Clayton                       const ProcessLaunchInfo &launch_info)
307f9765acdSGreg Clayton {
308f9765acdSGreg Clayton     Error error;
309f9765acdSGreg Clayton     error.SetErrorString ("launching not supported in kdp-remote plug-in");
310f9765acdSGreg Clayton     return error;
311f9765acdSGreg Clayton }
312f9765acdSGreg Clayton 
313f9765acdSGreg Clayton 
314f9765acdSGreg Clayton Error
315f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid)
316f9765acdSGreg Clayton {
317f9765acdSGreg Clayton     Error error;
318f9765acdSGreg Clayton     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
319f9765acdSGreg Clayton     return error;
320f9765acdSGreg Clayton }
321f9765acdSGreg Clayton 
322f9765acdSGreg Clayton Error
32384647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info)
32484647048SHan Ming Ong {
32584647048SHan Ming Ong     Error error;
32684647048SHan Ming Ong     error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging");
32784647048SHan Ming Ong     return error;
32884647048SHan Ming Ong }
32984647048SHan Ming Ong 
33084647048SHan Ming Ong Error
33184647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info)
332f9765acdSGreg Clayton {
333f9765acdSGreg Clayton     Error error;
334f9765acdSGreg Clayton     error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging");
335f9765acdSGreg Clayton     return error;
336f9765acdSGreg Clayton }
337f9765acdSGreg Clayton 
338f9765acdSGreg Clayton 
339f9765acdSGreg Clayton void
340f9765acdSGreg Clayton ProcessKDP::DidAttach ()
341f9765acdSGreg Clayton {
3425160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
343f9765acdSGreg Clayton     if (log)
34454cb8f83SJohnny Chen         log->Printf ("ProcessKDP::DidAttach()");
345f9765acdSGreg Clayton     if (GetID() != LLDB_INVALID_PROCESS_ID)
346f9765acdSGreg Clayton     {
347f9765acdSGreg Clayton         // TODO: figure out the register context that we will use
348f9765acdSGreg Clayton     }
349f9765acdSGreg Clayton }
350f9765acdSGreg Clayton 
3515e8534efSJason Molenda addr_t
3525e8534efSJason Molenda ProcessKDP::GetImageInfoAddress()
3535e8534efSJason Molenda {
3545e8534efSJason Molenda     return m_kernel_load_addr;
3555e8534efSJason Molenda }
3565e8534efSJason Molenda 
3575e8534efSJason Molenda lldb_private::DynamicLoader *
3585e8534efSJason Molenda ProcessKDP::GetDynamicLoader ()
3595e8534efSJason Molenda {
3605e8534efSJason Molenda     if (m_dyld_ap.get() == NULL)
3615e8534efSJason Molenda         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.empty() ? NULL : m_dyld_plugin_name.c_str()));
3625e8534efSJason Molenda     return m_dyld_ap.get();
3635e8534efSJason Molenda }
3645e8534efSJason Molenda 
365f9765acdSGreg Clayton Error
366f9765acdSGreg Clayton ProcessKDP::WillResume ()
367f9765acdSGreg Clayton {
368f9765acdSGreg Clayton     return Error();
369f9765acdSGreg Clayton }
370f9765acdSGreg Clayton 
371f9765acdSGreg Clayton Error
372f9765acdSGreg Clayton ProcessKDP::DoResume ()
373f9765acdSGreg Clayton {
374f9765acdSGreg Clayton     Error error;
3755160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
3767925fbbaSGreg Clayton     // Only start the async thread if we try to do any process control
3777925fbbaSGreg Clayton     if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
3787925fbbaSGreg Clayton         StartAsyncThread ();
3797925fbbaSGreg Clayton 
38097d5cf05SGreg Clayton     bool resume = false;
3817925fbbaSGreg Clayton 
38297d5cf05SGreg Clayton     // With KDP there is only one thread we can tell what to do
383ba4e61d3SAndrew Kaylor     ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
384ba4e61d3SAndrew Kaylor 
38597d5cf05SGreg Clayton     if (kernel_thread_sp)
3864b1b8b3eSGreg Clayton     {
38797d5cf05SGreg Clayton         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
3886e0ff1a3SGreg Clayton 
3896e0ff1a3SGreg Clayton         if (log)
3906e0ff1a3SGreg Clayton             log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state));
3917925fbbaSGreg Clayton         switch (thread_resume_state)
3924b1b8b3eSGreg Clayton         {
3937925fbbaSGreg Clayton             case eStateSuspended:
3947925fbbaSGreg Clayton                 // Nothing to do here when a thread will stay suspended
3957925fbbaSGreg Clayton                 // we just leave the CPU mask bit set to zero for the thread
3966e0ff1a3SGreg Clayton                 if (log)
3976e0ff1a3SGreg Clayton                     log->Printf ("ProcessKDP::DoResume() = suspended???");
3987925fbbaSGreg Clayton                 break;
3997925fbbaSGreg Clayton 
4007925fbbaSGreg Clayton             case eStateStepping:
4011afa68edSGreg Clayton                 {
4021afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
4031afa68edSGreg Clayton 
4041afa68edSGreg Clayton                     if (reg_ctx_sp)
4051afa68edSGreg Clayton                     {
4066e0ff1a3SGreg Clayton                         if (log)
4076e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
4081afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (true);
40997d5cf05SGreg Clayton                         resume = true;
4101afa68edSGreg Clayton                     }
4111afa68edSGreg Clayton                     else
4121afa68edSGreg Clayton                     {
4131afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
4141afa68edSGreg Clayton                     }
4151afa68edSGreg Clayton                 }
4167925fbbaSGreg Clayton                 break;
4177925fbbaSGreg Clayton 
41897d5cf05SGreg Clayton             case eStateRunning:
4191afa68edSGreg Clayton                 {
4201afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
4211afa68edSGreg Clayton 
4221afa68edSGreg Clayton                     if (reg_ctx_sp)
4231afa68edSGreg Clayton                     {
4246e0ff1a3SGreg Clayton                         if (log)
4256e0ff1a3SGreg Clayton                             log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);");
4261afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (false);
42797d5cf05SGreg Clayton                         resume = true;
4281afa68edSGreg Clayton                     }
4291afa68edSGreg Clayton                     else
4301afa68edSGreg Clayton                     {
4311afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
4321afa68edSGreg Clayton                     }
4331afa68edSGreg Clayton                 }
4347925fbbaSGreg Clayton                 break;
4357925fbbaSGreg Clayton 
4367925fbbaSGreg Clayton             default:
43797d5cf05SGreg Clayton                 // The only valid thread resume states are listed above
4387925fbbaSGreg Clayton                 assert (!"invalid thread resume state");
4397925fbbaSGreg Clayton                 break;
4404b1b8b3eSGreg Clayton         }
4414b1b8b3eSGreg Clayton     }
4427925fbbaSGreg Clayton 
44397d5cf05SGreg Clayton     if (resume)
44497d5cf05SGreg Clayton     {
44597d5cf05SGreg Clayton         if (log)
44697d5cf05SGreg Clayton             log->Printf ("ProcessKDP::DoResume () sending resume");
44797d5cf05SGreg Clayton 
44897d5cf05SGreg Clayton         if (m_comm.SendRequestResume ())
4497925fbbaSGreg Clayton         {
4507925fbbaSGreg Clayton             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
4517925fbbaSGreg Clayton             SetPrivateState(eStateRunning);
4527925fbbaSGreg Clayton         }
4534b1b8b3eSGreg Clayton         else
45407e66e3eSGreg Clayton             error.SetErrorString ("KDP resume failed");
4557925fbbaSGreg Clayton     }
4567925fbbaSGreg Clayton     else
4577925fbbaSGreg Clayton     {
45897d5cf05SGreg Clayton         error.SetErrorString ("kernel thread is suspended");
4597925fbbaSGreg Clayton     }
4607925fbbaSGreg Clayton 
461f9765acdSGreg Clayton     return error;
462f9765acdSGreg Clayton }
463f9765acdSGreg Clayton 
46497d5cf05SGreg Clayton lldb::ThreadSP
465ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread()
46697d5cf05SGreg Clayton {
46797d5cf05SGreg Clayton     // KDP only tells us about one thread/core. Any other threads will usually
46897d5cf05SGreg Clayton     // be the ones that are read from memory by the OS plug-ins.
469ba4e61d3SAndrew Kaylor 
470ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (m_kernel_thread_wp.lock());
47197d5cf05SGreg Clayton     if (!thread_sp)
472ba4e61d3SAndrew Kaylor     {
473ba4e61d3SAndrew Kaylor         thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
474ba4e61d3SAndrew Kaylor         m_kernel_thread_wp = thread_sp;
475ba4e61d3SAndrew Kaylor     }
47697d5cf05SGreg Clayton     return thread_sp;
47797d5cf05SGreg Clayton }
47897d5cf05SGreg Clayton 
47997d5cf05SGreg Clayton 
48097d5cf05SGreg Clayton 
48197d5cf05SGreg Clayton 
4829fc13556SGreg Clayton bool
48356d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
484f9765acdSGreg Clayton {
485f9765acdSGreg Clayton     // locker will keep a mutex locked until it goes out of scope
4865160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
487f9765acdSGreg Clayton     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
488d01b2953SDaniel Malea         log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
489f9765acdSGreg Clayton 
49039da3efdSGreg Clayton     // Even though there is a CPU mask, it doesn't mean we can see each CPU
49197d5cf05SGreg Clayton     // indivudually, there is really only one. Lets call this thread 1.
492ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
493ba4e61d3SAndrew Kaylor     if (!thread_sp)
494ba4e61d3SAndrew Kaylor         thread_sp = GetKernelThread ();
495ba4e61d3SAndrew Kaylor     new_thread_list.AddThread(thread_sp);
49697d5cf05SGreg Clayton 
4979fc13556SGreg Clayton     return new_thread_list.GetSize(false) > 0;
498f9765acdSGreg Clayton }
499f9765acdSGreg Clayton 
500f9765acdSGreg Clayton void
501f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop ()
502f9765acdSGreg Clayton {
503f9765acdSGreg Clayton     // Let all threads recover from stopping and do any clean up based
504f9765acdSGreg Clayton     // on the previous thread state (if any).
505f9765acdSGreg Clayton     m_thread_list.RefreshStateAfterStop();
506f9765acdSGreg Clayton }
507f9765acdSGreg Clayton 
508f9765acdSGreg Clayton Error
509f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop)
510f9765acdSGreg Clayton {
511f9765acdSGreg Clayton     Error error;
512f9765acdSGreg Clayton 
51397d5cf05SGreg Clayton     if (m_comm.IsRunning())
514f9765acdSGreg Clayton     {
51597d5cf05SGreg Clayton         if (m_destroy_in_process)
51697d5cf05SGreg Clayton         {
51797d5cf05SGreg Clayton             // If we are attemping to destroy, we need to not return an error to
51897d5cf05SGreg Clayton             // Halt or DoDestroy won't get called.
51997d5cf05SGreg Clayton             // We are also currently running, so send a process stopped event
52097d5cf05SGreg Clayton             SetPrivateState (eStateStopped);
521f9765acdSGreg Clayton         }
522f9765acdSGreg Clayton         else
523f9765acdSGreg Clayton         {
52497d5cf05SGreg Clayton             error.SetErrorString ("KDP cannot interrupt a running kernel");
525f9765acdSGreg Clayton         }
526f9765acdSGreg Clayton     }
527f9765acdSGreg Clayton     return error;
528f9765acdSGreg Clayton }
529f9765acdSGreg Clayton 
530f9765acdSGreg Clayton Error
531acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped)
532f9765acdSGreg Clayton {
533f9765acdSGreg Clayton     Error error;
5345160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
535f9765acdSGreg Clayton     if (log)
536acff8950SJim Ingham         log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
537f9765acdSGreg Clayton 
53897d5cf05SGreg Clayton     if (m_comm.IsRunning())
53997d5cf05SGreg Clayton     {
54097d5cf05SGreg Clayton         // We are running and we can't interrupt a running kernel, so we need
54197d5cf05SGreg Clayton         // to just close the connection to the kernel and hope for the best
54297d5cf05SGreg Clayton     }
54397d5cf05SGreg Clayton     else
54497d5cf05SGreg Clayton     {
545f9765acdSGreg Clayton         DisableAllBreakpointSites ();
546f9765acdSGreg Clayton 
547f9765acdSGreg Clayton         m_thread_list.DiscardThreadPlans();
548f9765acdSGreg Clayton 
549acff8950SJim Ingham         // If we are going to keep the target stopped, then don't send the disconnect message.
550acff8950SJim Ingham         if (!keep_stopped && m_comm.IsConnected())
5513a29bdbeSGreg Clayton         {
5526e0ff1a3SGreg Clayton             const bool success = m_comm.SendRequestDisconnect();
553f9765acdSGreg Clayton             if (log)
554f9765acdSGreg Clayton             {
5556e0ff1a3SGreg Clayton                 if (success)
5566e0ff1a3SGreg Clayton                     log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully");
557f9765acdSGreg Clayton                 else
55877e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
559f9765acdSGreg Clayton             }
5606e0ff1a3SGreg Clayton             m_comm.Disconnect ();
5613a29bdbeSGreg Clayton         }
56297d5cf05SGreg Clayton     }
563f9765acdSGreg Clayton     StopAsyncThread ();
56474d4193eSGreg Clayton     m_comm.Clear();
565f9765acdSGreg Clayton 
566f9765acdSGreg Clayton     SetPrivateState (eStateDetached);
567f9765acdSGreg Clayton     ResumePrivateStateThread();
568f9765acdSGreg Clayton 
569f9765acdSGreg Clayton     //KillDebugserverProcess ();
570f9765acdSGreg Clayton     return error;
571f9765acdSGreg Clayton }
572f9765acdSGreg Clayton 
573f9765acdSGreg Clayton Error
574f9765acdSGreg Clayton ProcessKDP::DoDestroy ()
575f9765acdSGreg Clayton {
5767925fbbaSGreg Clayton     // For KDP there really is no difference between destroy and detach
577acff8950SJim Ingham     bool keep_stopped = false;
578acff8950SJim Ingham     return DoDetach(keep_stopped);
579f9765acdSGreg Clayton }
580f9765acdSGreg Clayton 
581f9765acdSGreg Clayton //------------------------------------------------------------------
582f9765acdSGreg Clayton // Process Queries
583f9765acdSGreg Clayton //------------------------------------------------------------------
584f9765acdSGreg Clayton 
585f9765acdSGreg Clayton bool
586f9765acdSGreg Clayton ProcessKDP::IsAlive ()
587f9765acdSGreg Clayton {
588f9765acdSGreg Clayton     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
589f9765acdSGreg Clayton }
590f9765acdSGreg Clayton 
591f9765acdSGreg Clayton //------------------------------------------------------------------
592f9765acdSGreg Clayton // Process Memory
593f9765acdSGreg Clayton //------------------------------------------------------------------
594f9765acdSGreg Clayton size_t
595f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
596f9765acdSGreg Clayton {
597a63d08c9SGreg Clayton     if (m_comm.IsConnected())
598a63d08c9SGreg Clayton         return m_comm.SendRequestReadMemory (addr, buf, size, error);
599a63d08c9SGreg Clayton     error.SetErrorString ("not connected");
600f9765acdSGreg Clayton     return 0;
601f9765acdSGreg Clayton }
602f9765acdSGreg Clayton 
603f9765acdSGreg Clayton size_t
604f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
605f9765acdSGreg Clayton {
6067925fbbaSGreg Clayton     if (m_comm.IsConnected())
6077925fbbaSGreg Clayton         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
6087925fbbaSGreg Clayton     error.SetErrorString ("not connected");
609f9765acdSGreg Clayton     return 0;
610f9765acdSGreg Clayton }
611f9765acdSGreg Clayton 
612f9765acdSGreg Clayton lldb::addr_t
613f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
614f9765acdSGreg Clayton {
615f9765acdSGreg Clayton     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
616f9765acdSGreg Clayton     return LLDB_INVALID_ADDRESS;
617f9765acdSGreg Clayton }
618f9765acdSGreg Clayton 
619f9765acdSGreg Clayton Error
620f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
621f9765acdSGreg Clayton {
622f9765acdSGreg Clayton     Error error;
623f9765acdSGreg Clayton     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
624f9765acdSGreg Clayton     return error;
625f9765acdSGreg Clayton }
626f9765acdSGreg Clayton 
627f9765acdSGreg Clayton Error
628299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
629f9765acdSGreg Clayton {
63007e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
63107e66e3eSGreg Clayton     {
63207e66e3eSGreg Clayton         Error error;
6335b88216dSGreg Clayton         if (!bp_site->IsEnabled())
6345b88216dSGreg Clayton         {
6355b88216dSGreg Clayton             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
6365b88216dSGreg Clayton             {
6375b88216dSGreg Clayton                 bp_site->SetEnabled(true);
6385b88216dSGreg Clayton                 bp_site->SetType (BreakpointSite::eExternal);
6395b88216dSGreg Clayton             }
6405b88216dSGreg Clayton             else
6415b88216dSGreg Clayton             {
64207e66e3eSGreg Clayton                 error.SetErrorString ("KDP set breakpoint failed");
6435b88216dSGreg Clayton             }
6445b88216dSGreg Clayton         }
64507e66e3eSGreg Clayton         return error;
64607e66e3eSGreg Clayton     }
647f9765acdSGreg Clayton     return EnableSoftwareBreakpoint (bp_site);
648f9765acdSGreg Clayton }
649f9765acdSGreg Clayton 
650f9765acdSGreg Clayton Error
651299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
652f9765acdSGreg Clayton {
65307e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
65407e66e3eSGreg Clayton     {
65507e66e3eSGreg Clayton         Error error;
6565b88216dSGreg Clayton         if (bp_site->IsEnabled())
6575b88216dSGreg Clayton         {
6585b88216dSGreg Clayton             BreakpointSite::Type bp_type = bp_site->GetType();
6595b88216dSGreg Clayton             if (bp_type == BreakpointSite::eExternal)
6605b88216dSGreg Clayton             {
66197d5cf05SGreg Clayton                 if (m_destroy_in_process && m_comm.IsRunning())
66297d5cf05SGreg Clayton                 {
66397d5cf05SGreg Clayton                     // We are trying to destroy our connection and we are running
66497d5cf05SGreg Clayton                     bp_site->SetEnabled(false);
66597d5cf05SGreg Clayton                 }
66697d5cf05SGreg Clayton                 else
66797d5cf05SGreg Clayton                 {
6685b88216dSGreg Clayton                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
6695b88216dSGreg Clayton                         bp_site->SetEnabled(false);
6705b88216dSGreg Clayton                     else
67107e66e3eSGreg Clayton                         error.SetErrorString ("KDP remove breakpoint failed");
6725b88216dSGreg Clayton                 }
67397d5cf05SGreg Clayton             }
6745b88216dSGreg Clayton             else
6755b88216dSGreg Clayton             {
6765b88216dSGreg Clayton                 error = DisableSoftwareBreakpoint (bp_site);
6775b88216dSGreg Clayton             }
6785b88216dSGreg Clayton         }
67907e66e3eSGreg Clayton         return error;
68007e66e3eSGreg Clayton     }
681f9765acdSGreg Clayton     return DisableSoftwareBreakpoint (bp_site);
682f9765acdSGreg Clayton }
683f9765acdSGreg Clayton 
684f9765acdSGreg Clayton Error
6851b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
686f9765acdSGreg Clayton {
687f9765acdSGreg Clayton     Error error;
688f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
689f9765acdSGreg Clayton     return error;
690f9765acdSGreg Clayton }
691f9765acdSGreg Clayton 
692f9765acdSGreg Clayton Error
6931b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
694f9765acdSGreg Clayton {
695f9765acdSGreg Clayton     Error error;
696f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
697f9765acdSGreg Clayton     return error;
698f9765acdSGreg Clayton }
699f9765acdSGreg Clayton 
700f9765acdSGreg Clayton void
701f9765acdSGreg Clayton ProcessKDP::Clear()
702f9765acdSGreg Clayton {
703f9765acdSGreg Clayton     m_thread_list.Clear();
704f9765acdSGreg Clayton }
705f9765acdSGreg Clayton 
706f9765acdSGreg Clayton Error
707f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo)
708f9765acdSGreg Clayton {
709f9765acdSGreg Clayton     Error error;
710f9765acdSGreg Clayton     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
711f9765acdSGreg Clayton     return error;
712f9765acdSGreg Clayton }
713f9765acdSGreg Clayton 
714f9765acdSGreg Clayton void
715f9765acdSGreg Clayton ProcessKDP::Initialize()
716f9765acdSGreg Clayton {
717f9765acdSGreg Clayton     static bool g_initialized = false;
718f9765acdSGreg Clayton 
719f9765acdSGreg Clayton     if (g_initialized == false)
720f9765acdSGreg Clayton     {
721f9765acdSGreg Clayton         g_initialized = true;
722f9765acdSGreg Clayton         PluginManager::RegisterPlugin (GetPluginNameStatic(),
723f9765acdSGreg Clayton                                        GetPluginDescriptionStatic(),
724f9765acdSGreg Clayton                                        CreateInstance);
725f9765acdSGreg Clayton 
726f9765acdSGreg Clayton         Log::Callbacks log_callbacks = {
727f9765acdSGreg Clayton             ProcessKDPLog::DisableLog,
728f9765acdSGreg Clayton             ProcessKDPLog::EnableLog,
729f9765acdSGreg Clayton             ProcessKDPLog::ListLogCategories
730f9765acdSGreg Clayton         };
731f9765acdSGreg Clayton 
732f9765acdSGreg Clayton         Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
733f9765acdSGreg Clayton     }
734f9765acdSGreg Clayton }
735f9765acdSGreg Clayton 
736f9765acdSGreg Clayton bool
737f9765acdSGreg Clayton ProcessKDP::StartAsyncThread ()
738f9765acdSGreg Clayton {
7395160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
740f9765acdSGreg Clayton 
741f9765acdSGreg Clayton     if (log)
7427925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StartAsyncThread ()");
743f9765acdSGreg Clayton 
7447925fbbaSGreg Clayton     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
7457925fbbaSGreg Clayton         return true;
7467925fbbaSGreg Clayton 
747f9765acdSGreg Clayton     m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
748f9765acdSGreg Clayton     return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
749f9765acdSGreg Clayton }
750f9765acdSGreg Clayton 
751f9765acdSGreg Clayton void
752f9765acdSGreg Clayton ProcessKDP::StopAsyncThread ()
753f9765acdSGreg Clayton {
7545160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
755f9765acdSGreg Clayton 
756f9765acdSGreg Clayton     if (log)
7577925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StopAsyncThread ()");
758f9765acdSGreg Clayton 
759f9765acdSGreg Clayton     m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
760f9765acdSGreg Clayton 
761f9765acdSGreg Clayton     // Stop the stdio thread
762f9765acdSGreg Clayton     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
763f9765acdSGreg Clayton     {
764f9765acdSGreg Clayton         Host::ThreadJoin (m_async_thread, NULL, NULL);
7657925fbbaSGreg Clayton         m_async_thread = LLDB_INVALID_HOST_THREAD;
766f9765acdSGreg Clayton     }
767f9765acdSGreg Clayton }
768f9765acdSGreg Clayton 
769f9765acdSGreg Clayton 
770f9765acdSGreg Clayton void *
771f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg)
772f9765acdSGreg Clayton {
773f9765acdSGreg Clayton     ProcessKDP *process = (ProcessKDP*) arg;
774f9765acdSGreg Clayton 
7757925fbbaSGreg Clayton     const lldb::pid_t pid = process->GetID();
7767925fbbaSGreg Clayton 
7775160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
778f9765acdSGreg Clayton     if (log)
779d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
780f9765acdSGreg Clayton 
781f9765acdSGreg Clayton     Listener listener ("ProcessKDP::AsyncThread");
782f9765acdSGreg Clayton     EventSP event_sp;
783f9765acdSGreg Clayton     const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
784f9765acdSGreg Clayton                                         eBroadcastBitAsyncThreadShouldExit;
785f9765acdSGreg Clayton 
7867925fbbaSGreg Clayton 
787f9765acdSGreg Clayton     if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
788f9765acdSGreg Clayton     {
789f9765acdSGreg Clayton         bool done = false;
790f9765acdSGreg Clayton         while (!done)
791f9765acdSGreg Clayton         {
792f9765acdSGreg Clayton             if (log)
793d01b2953SDaniel Malea                 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
7947925fbbaSGreg Clayton                              pid);
795f9765acdSGreg Clayton             if (listener.WaitForEvent (NULL, event_sp))
796f9765acdSGreg Clayton             {
7977925fbbaSGreg Clayton                 uint32_t event_type = event_sp->GetType();
798f9765acdSGreg Clayton                 if (log)
799d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
8007925fbbaSGreg Clayton                                  pid,
8017925fbbaSGreg Clayton                                  event_type);
802f9765acdSGreg Clayton 
8037925fbbaSGreg Clayton                 // When we are running, poll for 1 second to try and get an exception
8047925fbbaSGreg Clayton                 // to indicate the process has stopped. If we don't get one, check to
8057925fbbaSGreg Clayton                 // make sure no one asked us to exit
8067925fbbaSGreg Clayton                 bool is_running = false;
8077925fbbaSGreg Clayton                 DataExtractor exc_reply_packet;
8087925fbbaSGreg Clayton                 do
8097925fbbaSGreg Clayton                 {
810f9765acdSGreg Clayton                     switch (event_type)
811f9765acdSGreg Clayton                     {
812f9765acdSGreg Clayton                     case eBroadcastBitAsyncContinue:
813f9765acdSGreg Clayton                         {
8147925fbbaSGreg Clayton                             is_running = true;
8157925fbbaSGreg Clayton                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
816f9765acdSGreg Clayton                             {
817ba4e61d3SAndrew Kaylor                                 ThreadSP thread_sp (process->GetKernelThread());
8181afa68edSGreg Clayton                                 if (thread_sp)
8191afa68edSGreg Clayton                                 {
8201afa68edSGreg Clayton                                     lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
8211afa68edSGreg Clayton                                     if (reg_ctx_sp)
8221afa68edSGreg Clayton                                         reg_ctx_sp->InvalidateAllRegisters();
82397d5cf05SGreg Clayton                                     static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
8241afa68edSGreg Clayton                                 }
82597d5cf05SGreg Clayton 
8267925fbbaSGreg Clayton                                 // TODO: parse the stop reply packet
8277925fbbaSGreg Clayton                                 is_running = false;
8287925fbbaSGreg Clayton                                 process->SetPrivateState(eStateStopped);
8297925fbbaSGreg Clayton                             }
8307925fbbaSGreg Clayton                             else
8317925fbbaSGreg Clayton                             {
8327925fbbaSGreg Clayton                                 // Check to see if we are supposed to exit. There is no way to
8337925fbbaSGreg Clayton                                 // interrupt a running kernel, so all we can do is wait for an
8347925fbbaSGreg Clayton                                 // exception or detach...
8357925fbbaSGreg Clayton                                 if (listener.GetNextEvent(event_sp))
8367925fbbaSGreg Clayton                                 {
8377925fbbaSGreg Clayton                                     // We got an event, go through the loop again
8387925fbbaSGreg Clayton                                     event_type = event_sp->GetType();
8397925fbbaSGreg Clayton                                 }
840f9765acdSGreg Clayton                             }
841f9765acdSGreg Clayton                         }
842f9765acdSGreg Clayton                         break;
843f9765acdSGreg Clayton 
844f9765acdSGreg Clayton                     case eBroadcastBitAsyncThreadShouldExit:
845f9765acdSGreg Clayton                         if (log)
846d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
8477925fbbaSGreg Clayton                                          pid);
848f9765acdSGreg Clayton                         done = true;
8497925fbbaSGreg Clayton                         is_running = false;
850f9765acdSGreg Clayton                         break;
851f9765acdSGreg Clayton 
852f9765acdSGreg Clayton                     default:
853f9765acdSGreg Clayton                         if (log)
854d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
8557925fbbaSGreg Clayton                                          pid,
8567925fbbaSGreg Clayton                                          event_type);
857f9765acdSGreg Clayton                         done = true;
8587925fbbaSGreg Clayton                         is_running = false;
859f9765acdSGreg Clayton                         break;
860f9765acdSGreg Clayton                     }
8617925fbbaSGreg Clayton                 } while (is_running);
862f9765acdSGreg Clayton             }
863f9765acdSGreg Clayton             else
864f9765acdSGreg Clayton             {
865f9765acdSGreg Clayton                 if (log)
866d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
8677925fbbaSGreg Clayton                                  pid);
868f9765acdSGreg Clayton                 done = true;
869f9765acdSGreg Clayton             }
870f9765acdSGreg Clayton         }
871f9765acdSGreg Clayton     }
872f9765acdSGreg Clayton 
873f9765acdSGreg Clayton     if (log)
874d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
8757925fbbaSGreg Clayton                      arg,
8767925fbbaSGreg Clayton                      pid);
877f9765acdSGreg Clayton 
878f9765acdSGreg Clayton     process->m_async_thread = LLDB_INVALID_HOST_THREAD;
879f9765acdSGreg Clayton     return NULL;
880f9765acdSGreg Clayton }
881f9765acdSGreg Clayton 
882f9765acdSGreg Clayton 
8831d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
8841d19a2f2SGreg Clayton {
8851d19a2f2SGreg Clayton private:
8861d19a2f2SGreg Clayton 
8871d19a2f2SGreg Clayton     OptionGroupOptions m_option_group;
8881d19a2f2SGreg Clayton     OptionGroupUInt64 m_command_byte;
8891d19a2f2SGreg Clayton     OptionGroupString m_packet_data;
8901d19a2f2SGreg Clayton 
8911d19a2f2SGreg Clayton     virtual Options *
8921d19a2f2SGreg Clayton     GetOptions ()
8931d19a2f2SGreg Clayton     {
8941d19a2f2SGreg Clayton         return &m_option_group;
8951d19a2f2SGreg Clayton     }
8961d19a2f2SGreg Clayton 
8971d19a2f2SGreg Clayton 
8981d19a2f2SGreg Clayton public:
8991d19a2f2SGreg Clayton     CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
9001d19a2f2SGreg Clayton         CommandObjectParsed (interpreter,
9011d19a2f2SGreg Clayton                              "process plugin packet send",
9021d19a2f2SGreg 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. ",
9031d19a2f2SGreg Clayton                              NULL),
9041d19a2f2SGreg Clayton         m_option_group (interpreter),
9051d19a2f2SGreg 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),
9061d19a2f2SGreg 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)
9071d19a2f2SGreg Clayton     {
9081d19a2f2SGreg Clayton         m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
9091d19a2f2SGreg Clayton         m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
9101d19a2f2SGreg Clayton         m_option_group.Finalize();
9111d19a2f2SGreg Clayton     }
9121d19a2f2SGreg Clayton 
9131d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacketSend ()
9141d19a2f2SGreg Clayton     {
9151d19a2f2SGreg Clayton     }
9161d19a2f2SGreg Clayton 
9171d19a2f2SGreg Clayton     bool
9181d19a2f2SGreg Clayton     DoExecute (Args& command, CommandReturnObject &result)
9191d19a2f2SGreg Clayton     {
9201d19a2f2SGreg Clayton         const size_t argc = command.GetArgumentCount();
9211d19a2f2SGreg Clayton         if (argc == 0)
9221d19a2f2SGreg Clayton         {
9231d19a2f2SGreg Clayton             if (!m_command_byte.GetOptionValue().OptionWasSet())
9241d19a2f2SGreg Clayton             {
9251d19a2f2SGreg Clayton                 result.AppendError ("the --command option must be set to a valid command byte");
9261d19a2f2SGreg Clayton                 result.SetStatus (eReturnStatusFailed);
9271d19a2f2SGreg Clayton             }
9281d19a2f2SGreg Clayton             else
9291d19a2f2SGreg Clayton             {
9301d19a2f2SGreg Clayton                 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
9311d19a2f2SGreg Clayton                 if (command_byte > 0 && command_byte <= UINT8_MAX)
9321d19a2f2SGreg Clayton                 {
9331d19a2f2SGreg Clayton                     ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
9341d19a2f2SGreg Clayton                     if (process)
9351d19a2f2SGreg Clayton                     {
9361d19a2f2SGreg Clayton                         const StateType state = process->GetState();
9371d19a2f2SGreg Clayton 
9381d19a2f2SGreg Clayton                         if (StateIsStoppedState (state, true))
9391d19a2f2SGreg Clayton                         {
9401d19a2f2SGreg Clayton                             std::vector<uint8_t> payload_bytes;
9411d19a2f2SGreg Clayton                             const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
9421d19a2f2SGreg Clayton                             if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
9431d19a2f2SGreg Clayton                             {
9441d19a2f2SGreg Clayton                                 StringExtractor extractor(ascii_hex_bytes_cstr);
9451d19a2f2SGreg Clayton                                 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
9461d19a2f2SGreg Clayton                                 if (ascii_hex_bytes_cstr_len & 1)
9471d19a2f2SGreg Clayton                                 {
9481d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
9491d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
9501d19a2f2SGreg Clayton                                     return false;
9511d19a2f2SGreg Clayton                                 }
9521d19a2f2SGreg Clayton                                 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
9531d19a2f2SGreg Clayton                                 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
9541d19a2f2SGreg Clayton                                 {
9551d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
9561d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
9571d19a2f2SGreg Clayton                                     return false;
9581d19a2f2SGreg Clayton                                 }
9591d19a2f2SGreg Clayton                             }
9601d19a2f2SGreg Clayton                             Error error;
9611d19a2f2SGreg Clayton                             DataExtractor reply;
9621d19a2f2SGreg Clayton                             process->GetCommunication().SendRawRequest (command_byte,
9631d19a2f2SGreg Clayton                                                                         payload_bytes.empty() ? NULL : payload_bytes.data(),
9641d19a2f2SGreg Clayton                                                                         payload_bytes.size(),
9651d19a2f2SGreg Clayton                                                                         reply,
9661d19a2f2SGreg Clayton                                                                         error);
9671d19a2f2SGreg Clayton 
9681d19a2f2SGreg Clayton                             if (error.Success())
9691d19a2f2SGreg Clayton                             {
9701d19a2f2SGreg Clayton                                 // Copy the binary bytes into a hex ASCII string for the result
9711d19a2f2SGreg Clayton                                 StreamString packet;
9721d19a2f2SGreg Clayton                                 packet.PutBytesAsRawHex8(reply.GetDataStart(),
9731d19a2f2SGreg Clayton                                                          reply.GetByteSize(),
9741d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder(),
9751d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder());
9761d19a2f2SGreg Clayton                                 result.AppendMessage(packet.GetString().c_str());
9771d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusSuccessFinishResult);
9781d19a2f2SGreg Clayton                                 return true;
9791d19a2f2SGreg Clayton                             }
9801d19a2f2SGreg Clayton                             else
9811d19a2f2SGreg Clayton                             {
9821d19a2f2SGreg Clayton                                 const char *error_cstr = error.AsCString();
9831d19a2f2SGreg Clayton                                 if (error_cstr && error_cstr[0])
9841d19a2f2SGreg Clayton                                     result.AppendError (error_cstr);
9851d19a2f2SGreg Clayton                                 else
9861d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
9871d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusFailed);
9881d19a2f2SGreg Clayton                                 return false;
9891d19a2f2SGreg Clayton                             }
9901d19a2f2SGreg Clayton                         }
9911d19a2f2SGreg Clayton                         else
9921d19a2f2SGreg Clayton                         {
9931d19a2f2SGreg Clayton                             result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
9941d19a2f2SGreg Clayton                             result.SetStatus (eReturnStatusFailed);
9951d19a2f2SGreg Clayton                         }
9961d19a2f2SGreg Clayton                     }
9971d19a2f2SGreg Clayton                     else
9981d19a2f2SGreg Clayton                     {
9991d19a2f2SGreg Clayton                         result.AppendError ("invalid process");
10001d19a2f2SGreg Clayton                         result.SetStatus (eReturnStatusFailed);
10011d19a2f2SGreg Clayton                     }
10021d19a2f2SGreg Clayton                 }
10031d19a2f2SGreg Clayton                 else
10041d19a2f2SGreg Clayton                 {
1005d01b2953SDaniel Malea                     result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
10061d19a2f2SGreg Clayton                     result.SetStatus (eReturnStatusFailed);
10071d19a2f2SGreg Clayton                 }
10081d19a2f2SGreg Clayton             }
10091d19a2f2SGreg Clayton         }
10101d19a2f2SGreg Clayton         else
10111d19a2f2SGreg Clayton         {
10121d19a2f2SGreg Clayton             result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
10131d19a2f2SGreg Clayton             result.SetStatus (eReturnStatusFailed);
10141d19a2f2SGreg Clayton         }
10151d19a2f2SGreg Clayton         return false;
10161d19a2f2SGreg Clayton     }
10171d19a2f2SGreg Clayton };
10181d19a2f2SGreg Clayton 
10191d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword
10201d19a2f2SGreg Clayton {
10211d19a2f2SGreg Clayton private:
10221d19a2f2SGreg Clayton 
10231d19a2f2SGreg Clayton public:
10241d19a2f2SGreg Clayton     CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
10251d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
10261d19a2f2SGreg Clayton                             "process plugin packet",
10271d19a2f2SGreg Clayton                             "Commands that deal with KDP remote packets.",
10281d19a2f2SGreg Clayton                             NULL)
10291d19a2f2SGreg Clayton     {
10301d19a2f2SGreg Clayton         LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
10311d19a2f2SGreg Clayton     }
10321d19a2f2SGreg Clayton 
10331d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacket ()
10341d19a2f2SGreg Clayton     {
10351d19a2f2SGreg Clayton     }
10361d19a2f2SGreg Clayton };
10371d19a2f2SGreg Clayton 
10381d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
10391d19a2f2SGreg Clayton {
10401d19a2f2SGreg Clayton public:
10411d19a2f2SGreg Clayton     CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
10421d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
10431d19a2f2SGreg Clayton                             "process plugin",
10441d19a2f2SGreg Clayton                             "A set of commands for operating on a ProcessKDP process.",
10451d19a2f2SGreg Clayton                             "process plugin <subcommand> [<subcommand-options>]")
10461d19a2f2SGreg Clayton     {
10471d19a2f2SGreg Clayton         LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket    (interpreter)));
10481d19a2f2SGreg Clayton     }
10491d19a2f2SGreg Clayton 
10501d19a2f2SGreg Clayton     ~CommandObjectMultiwordProcessKDP ()
10511d19a2f2SGreg Clayton     {
10521d19a2f2SGreg Clayton     }
10531d19a2f2SGreg Clayton };
10541d19a2f2SGreg Clayton 
10551d19a2f2SGreg Clayton CommandObject *
10561d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject()
10571d19a2f2SGreg Clayton {
10581d19a2f2SGreg Clayton     if (!m_command_sp)
10591d19a2f2SGreg Clayton         m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
10601d19a2f2SGreg Clayton     return m_command_sp.get();
10611d19a2f2SGreg Clayton }
10621d19a2f2SGreg Clayton 
1063