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                     }
245a8ea4baeSJason Molenda                     else
246a8ea4baeSJason Molenda                     {
247a8ea4baeSJason Molenda                         if (kernel_load_addr != LLDB_INVALID_ADDRESS)
2484bd4e7e3SJason Molenda                         {
2495e8534efSJason Molenda                             m_kernel_load_addr = kernel_load_addr;
250a8ea4baeSJason Molenda                         }
2515e8534efSJason Molenda                         m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
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();
3887925fbbaSGreg Clayton         switch (thread_resume_state)
3894b1b8b3eSGreg Clayton         {
3907925fbbaSGreg Clayton             case eStateSuspended:
3917925fbbaSGreg Clayton                 // Nothing to do here when a thread will stay suspended
3927925fbbaSGreg Clayton                 // we just leave the CPU mask bit set to zero for the thread
3937925fbbaSGreg Clayton                 break;
3947925fbbaSGreg Clayton 
3957925fbbaSGreg Clayton             case eStateStepping:
3961afa68edSGreg Clayton                 {
3971afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
3981afa68edSGreg Clayton 
3991afa68edSGreg Clayton                     if (reg_ctx_sp)
4001afa68edSGreg Clayton                     {
4011afa68edSGreg Clayton                         reg_ctx_sp->HardwareSingleStep (true);
40297d5cf05SGreg Clayton                         resume = true;
4031afa68edSGreg Clayton                     }
4041afa68edSGreg Clayton                     else
4051afa68edSGreg Clayton                     {
4061afa68edSGreg Clayton                         error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
4071afa68edSGreg Clayton                     }
4081afa68edSGreg Clayton                 }
4097925fbbaSGreg Clayton                 break;
4107925fbbaSGreg Clayton 
41197d5cf05SGreg Clayton             case eStateRunning:
4121afa68edSGreg Clayton                 {
4131afa68edSGreg Clayton                     lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext());
4141afa68edSGreg Clayton 
4151afa68edSGreg Clayton                         if (reg_ctx_sp)
4161afa68edSGreg Clayton                         {
4171afa68edSGreg Clayton                             reg_ctx_sp->HardwareSingleStep (false);
41897d5cf05SGreg Clayton                             resume = true;
4191afa68edSGreg Clayton                         }
4201afa68edSGreg Clayton                         else
4211afa68edSGreg Clayton                         {
4221afa68edSGreg Clayton                             error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID());
4231afa68edSGreg Clayton                         }
4241afa68edSGreg Clayton                 }
4257925fbbaSGreg Clayton                 break;
4267925fbbaSGreg Clayton 
4277925fbbaSGreg Clayton             default:
42897d5cf05SGreg Clayton                 // The only valid thread resume states are listed above
4297925fbbaSGreg Clayton                 assert (!"invalid thread resume state");
4307925fbbaSGreg Clayton                 break;
4314b1b8b3eSGreg Clayton         }
4324b1b8b3eSGreg Clayton     }
4337925fbbaSGreg Clayton 
43497d5cf05SGreg Clayton     if (resume)
43597d5cf05SGreg Clayton     {
43697d5cf05SGreg Clayton         if (log)
43797d5cf05SGreg Clayton             log->Printf ("ProcessKDP::DoResume () sending resume");
43897d5cf05SGreg Clayton 
43997d5cf05SGreg Clayton         if (m_comm.SendRequestResume ())
4407925fbbaSGreg Clayton         {
4417925fbbaSGreg Clayton             m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue);
4427925fbbaSGreg Clayton             SetPrivateState(eStateRunning);
4437925fbbaSGreg Clayton         }
4444b1b8b3eSGreg Clayton         else
44507e66e3eSGreg Clayton             error.SetErrorString ("KDP resume failed");
4467925fbbaSGreg Clayton     }
4477925fbbaSGreg Clayton     else
4487925fbbaSGreg Clayton     {
44997d5cf05SGreg Clayton         error.SetErrorString ("kernel thread is suspended");
4507925fbbaSGreg Clayton     }
4517925fbbaSGreg Clayton 
452f9765acdSGreg Clayton     return error;
453f9765acdSGreg Clayton }
454f9765acdSGreg Clayton 
45597d5cf05SGreg Clayton lldb::ThreadSP
456ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread()
45797d5cf05SGreg Clayton {
45897d5cf05SGreg Clayton     // KDP only tells us about one thread/core. Any other threads will usually
45997d5cf05SGreg Clayton     // be the ones that are read from memory by the OS plug-ins.
460ba4e61d3SAndrew Kaylor 
461ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (m_kernel_thread_wp.lock());
46297d5cf05SGreg Clayton     if (!thread_sp)
463ba4e61d3SAndrew Kaylor     {
464ba4e61d3SAndrew Kaylor         thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
465ba4e61d3SAndrew Kaylor         m_kernel_thread_wp = thread_sp;
466ba4e61d3SAndrew Kaylor     }
46797d5cf05SGreg Clayton     return thread_sp;
46897d5cf05SGreg Clayton }
46997d5cf05SGreg Clayton 
47097d5cf05SGreg Clayton 
47197d5cf05SGreg Clayton 
47297d5cf05SGreg Clayton 
4739fc13556SGreg Clayton bool
47456d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
475f9765acdSGreg Clayton {
476f9765acdSGreg Clayton     // locker will keep a mutex locked until it goes out of scope
4775160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD));
478f9765acdSGreg Clayton     if (log && log->GetMask().Test(KDP_LOG_VERBOSE))
479d01b2953SDaniel Malea         log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID());
480f9765acdSGreg Clayton 
48139da3efdSGreg Clayton     // Even though there is a CPU mask, it doesn't mean we can see each CPU
48297d5cf05SGreg Clayton     // indivudually, there is really only one. Lets call this thread 1.
483ba4e61d3SAndrew Kaylor     ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
484ba4e61d3SAndrew Kaylor     if (!thread_sp)
485ba4e61d3SAndrew Kaylor         thread_sp = GetKernelThread ();
486ba4e61d3SAndrew Kaylor     new_thread_list.AddThread(thread_sp);
48797d5cf05SGreg Clayton 
4889fc13556SGreg Clayton     return new_thread_list.GetSize(false) > 0;
489f9765acdSGreg Clayton }
490f9765acdSGreg Clayton 
491f9765acdSGreg Clayton void
492f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop ()
493f9765acdSGreg Clayton {
494f9765acdSGreg Clayton     // Let all threads recover from stopping and do any clean up based
495f9765acdSGreg Clayton     // on the previous thread state (if any).
496f9765acdSGreg Clayton     m_thread_list.RefreshStateAfterStop();
497f9765acdSGreg Clayton }
498f9765acdSGreg Clayton 
499f9765acdSGreg Clayton Error
500f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop)
501f9765acdSGreg Clayton {
502f9765acdSGreg Clayton     Error error;
503f9765acdSGreg Clayton 
50497d5cf05SGreg Clayton     if (m_comm.IsRunning())
505f9765acdSGreg Clayton     {
50697d5cf05SGreg Clayton         if (m_destroy_in_process)
50797d5cf05SGreg Clayton         {
50897d5cf05SGreg Clayton             // If we are attemping to destroy, we need to not return an error to
50997d5cf05SGreg Clayton             // Halt or DoDestroy won't get called.
51097d5cf05SGreg Clayton             // We are also currently running, so send a process stopped event
51197d5cf05SGreg Clayton             SetPrivateState (eStateStopped);
512f9765acdSGreg Clayton         }
513f9765acdSGreg Clayton         else
514f9765acdSGreg Clayton         {
51597d5cf05SGreg Clayton             error.SetErrorString ("KDP cannot interrupt a running kernel");
516f9765acdSGreg Clayton         }
517f9765acdSGreg Clayton     }
518f9765acdSGreg Clayton     return error;
519f9765acdSGreg Clayton }
520f9765acdSGreg Clayton 
521f9765acdSGreg Clayton Error
522acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped)
523f9765acdSGreg Clayton {
524f9765acdSGreg Clayton     Error error;
5255160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
526f9765acdSGreg Clayton     if (log)
527acff8950SJim Ingham         log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
528f9765acdSGreg Clayton 
52997d5cf05SGreg Clayton     if (m_comm.IsRunning())
53097d5cf05SGreg Clayton     {
53197d5cf05SGreg Clayton         // We are running and we can't interrupt a running kernel, so we need
53297d5cf05SGreg Clayton         // to just close the connection to the kernel and hope for the best
53397d5cf05SGreg Clayton     }
53497d5cf05SGreg Clayton     else
53597d5cf05SGreg Clayton     {
536f9765acdSGreg Clayton         DisableAllBreakpointSites ();
537f9765acdSGreg Clayton 
538f9765acdSGreg Clayton         m_thread_list.DiscardThreadPlans();
539f9765acdSGreg Clayton 
540acff8950SJim Ingham         // If we are going to keep the target stopped, then don't send the disconnect message.
541acff8950SJim Ingham         if (!keep_stopped && m_comm.IsConnected())
5423a29bdbeSGreg Clayton         {
5433a29bdbeSGreg Clayton 
544*77e82d1eSJim Ingham             bool disconnect_success = m_comm.SendRequestDisconnect();
545*77e82d1eSJim Ingham             if (!disconnect_success)
546*77e82d1eSJim Ingham             {
547*77e82d1eSJim Ingham                 if (log)
548*77e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach(): send disconnect request failed");
549*77e82d1eSJim Ingham             }
5503a29bdbeSGreg Clayton 
551*77e82d1eSJim Ingham             ConnectionStatus comm_disconnect_result = m_comm.Disconnect ();
552f9765acdSGreg Clayton             if (log)
553f9765acdSGreg Clayton             {
554*77e82d1eSJim Ingham                 if (comm_disconnect_result == eConnectionStatusSuccess)
555*77e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() conncection channel shutdown successfully");
556f9765acdSGreg Clayton                 else
557*77e82d1eSJim Ingham                     log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed");
558f9765acdSGreg Clayton             }
5593a29bdbeSGreg Clayton         }
56097d5cf05SGreg Clayton     }
561f9765acdSGreg Clayton     StopAsyncThread ();
56274d4193eSGreg Clayton     m_comm.Clear();
563f9765acdSGreg Clayton 
564f9765acdSGreg Clayton     SetPrivateState (eStateDetached);
565f9765acdSGreg Clayton     ResumePrivateStateThread();
566f9765acdSGreg Clayton 
567f9765acdSGreg Clayton     //KillDebugserverProcess ();
568f9765acdSGreg Clayton     return error;
569f9765acdSGreg Clayton }
570f9765acdSGreg Clayton 
571f9765acdSGreg Clayton Error
572f9765acdSGreg Clayton ProcessKDP::DoDestroy ()
573f9765acdSGreg Clayton {
5747925fbbaSGreg Clayton     // For KDP there really is no difference between destroy and detach
575acff8950SJim Ingham     bool keep_stopped = false;
576acff8950SJim Ingham     return DoDetach(keep_stopped);
577f9765acdSGreg Clayton }
578f9765acdSGreg Clayton 
579f9765acdSGreg Clayton //------------------------------------------------------------------
580f9765acdSGreg Clayton // Process Queries
581f9765acdSGreg Clayton //------------------------------------------------------------------
582f9765acdSGreg Clayton 
583f9765acdSGreg Clayton bool
584f9765acdSGreg Clayton ProcessKDP::IsAlive ()
585f9765acdSGreg Clayton {
586f9765acdSGreg Clayton     return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited;
587f9765acdSGreg Clayton }
588f9765acdSGreg Clayton 
589f9765acdSGreg Clayton //------------------------------------------------------------------
590f9765acdSGreg Clayton // Process Memory
591f9765acdSGreg Clayton //------------------------------------------------------------------
592f9765acdSGreg Clayton size_t
593f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
594f9765acdSGreg Clayton {
595a63d08c9SGreg Clayton     if (m_comm.IsConnected())
596a63d08c9SGreg Clayton         return m_comm.SendRequestReadMemory (addr, buf, size, error);
597a63d08c9SGreg Clayton     error.SetErrorString ("not connected");
598f9765acdSGreg Clayton     return 0;
599f9765acdSGreg Clayton }
600f9765acdSGreg Clayton 
601f9765acdSGreg Clayton size_t
602f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
603f9765acdSGreg Clayton {
6047925fbbaSGreg Clayton     if (m_comm.IsConnected())
6057925fbbaSGreg Clayton         return m_comm.SendRequestWriteMemory (addr, buf, size, error);
6067925fbbaSGreg Clayton     error.SetErrorString ("not connected");
607f9765acdSGreg Clayton     return 0;
608f9765acdSGreg Clayton }
609f9765acdSGreg Clayton 
610f9765acdSGreg Clayton lldb::addr_t
611f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error)
612f9765acdSGreg Clayton {
613f9765acdSGreg Clayton     error.SetErrorString ("memory allocation not suppported in kdp remote debugging");
614f9765acdSGreg Clayton     return LLDB_INVALID_ADDRESS;
615f9765acdSGreg Clayton }
616f9765acdSGreg Clayton 
617f9765acdSGreg Clayton Error
618f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr)
619f9765acdSGreg Clayton {
620f9765acdSGreg Clayton     Error error;
621f9765acdSGreg Clayton     error.SetErrorString ("memory deallocation not suppported in kdp remote debugging");
622f9765acdSGreg Clayton     return error;
623f9765acdSGreg Clayton }
624f9765acdSGreg Clayton 
625f9765acdSGreg Clayton Error
626299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site)
627f9765acdSGreg Clayton {
62807e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
62907e66e3eSGreg Clayton     {
63007e66e3eSGreg Clayton         Error error;
6315b88216dSGreg Clayton         if (!bp_site->IsEnabled())
6325b88216dSGreg Clayton         {
6335b88216dSGreg Clayton             if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress()))
6345b88216dSGreg Clayton             {
6355b88216dSGreg Clayton                 bp_site->SetEnabled(true);
6365b88216dSGreg Clayton                 bp_site->SetType (BreakpointSite::eExternal);
6375b88216dSGreg Clayton             }
6385b88216dSGreg Clayton             else
6395b88216dSGreg Clayton             {
64007e66e3eSGreg Clayton                 error.SetErrorString ("KDP set breakpoint failed");
6415b88216dSGreg Clayton             }
6425b88216dSGreg Clayton         }
64307e66e3eSGreg Clayton         return error;
64407e66e3eSGreg Clayton     }
645f9765acdSGreg Clayton     return EnableSoftwareBreakpoint (bp_site);
646f9765acdSGreg Clayton }
647f9765acdSGreg Clayton 
648f9765acdSGreg Clayton Error
649299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site)
650f9765acdSGreg Clayton {
65107e66e3eSGreg Clayton     if (m_comm.LocalBreakpointsAreSupported ())
65207e66e3eSGreg Clayton     {
65307e66e3eSGreg Clayton         Error error;
6545b88216dSGreg Clayton         if (bp_site->IsEnabled())
6555b88216dSGreg Clayton         {
6565b88216dSGreg Clayton             BreakpointSite::Type bp_type = bp_site->GetType();
6575b88216dSGreg Clayton             if (bp_type == BreakpointSite::eExternal)
6585b88216dSGreg Clayton             {
65997d5cf05SGreg Clayton                 if (m_destroy_in_process && m_comm.IsRunning())
66097d5cf05SGreg Clayton                 {
66197d5cf05SGreg Clayton                     // We are trying to destroy our connection and we are running
66297d5cf05SGreg Clayton                     bp_site->SetEnabled(false);
66397d5cf05SGreg Clayton                 }
66497d5cf05SGreg Clayton                 else
66597d5cf05SGreg Clayton                 {
6665b88216dSGreg Clayton                     if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
6675b88216dSGreg Clayton                         bp_site->SetEnabled(false);
6685b88216dSGreg Clayton                     else
66907e66e3eSGreg Clayton                         error.SetErrorString ("KDP remove breakpoint failed");
6705b88216dSGreg Clayton                 }
67197d5cf05SGreg Clayton             }
6725b88216dSGreg Clayton             else
6735b88216dSGreg Clayton             {
6745b88216dSGreg Clayton                 error = DisableSoftwareBreakpoint (bp_site);
6755b88216dSGreg Clayton             }
6765b88216dSGreg Clayton         }
67707e66e3eSGreg Clayton         return error;
67807e66e3eSGreg Clayton     }
679f9765acdSGreg Clayton     return DisableSoftwareBreakpoint (bp_site);
680f9765acdSGreg Clayton }
681f9765acdSGreg Clayton 
682f9765acdSGreg Clayton Error
6831b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
684f9765acdSGreg Clayton {
685f9765acdSGreg Clayton     Error error;
686f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
687f9765acdSGreg Clayton     return error;
688f9765acdSGreg Clayton }
689f9765acdSGreg Clayton 
690f9765acdSGreg Clayton Error
6911b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
692f9765acdSGreg Clayton {
693f9765acdSGreg Clayton     Error error;
694f9765acdSGreg Clayton     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
695f9765acdSGreg Clayton     return error;
696f9765acdSGreg Clayton }
697f9765acdSGreg Clayton 
698f9765acdSGreg Clayton void
699f9765acdSGreg Clayton ProcessKDP::Clear()
700f9765acdSGreg Clayton {
701f9765acdSGreg Clayton     m_thread_list.Clear();
702f9765acdSGreg Clayton }
703f9765acdSGreg Clayton 
704f9765acdSGreg Clayton Error
705f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo)
706f9765acdSGreg Clayton {
707f9765acdSGreg Clayton     Error error;
708f9765acdSGreg Clayton     error.SetErrorString ("sending signals is not suppported in kdp remote debugging");
709f9765acdSGreg Clayton     return error;
710f9765acdSGreg Clayton }
711f9765acdSGreg Clayton 
712f9765acdSGreg Clayton void
713f9765acdSGreg Clayton ProcessKDP::Initialize()
714f9765acdSGreg Clayton {
715f9765acdSGreg Clayton     static bool g_initialized = false;
716f9765acdSGreg Clayton 
717f9765acdSGreg Clayton     if (g_initialized == false)
718f9765acdSGreg Clayton     {
719f9765acdSGreg Clayton         g_initialized = true;
720f9765acdSGreg Clayton         PluginManager::RegisterPlugin (GetPluginNameStatic(),
721f9765acdSGreg Clayton                                        GetPluginDescriptionStatic(),
722f9765acdSGreg Clayton                                        CreateInstance);
723f9765acdSGreg Clayton 
724f9765acdSGreg Clayton         Log::Callbacks log_callbacks = {
725f9765acdSGreg Clayton             ProcessKDPLog::DisableLog,
726f9765acdSGreg Clayton             ProcessKDPLog::EnableLog,
727f9765acdSGreg Clayton             ProcessKDPLog::ListLogCategories
728f9765acdSGreg Clayton         };
729f9765acdSGreg Clayton 
730f9765acdSGreg Clayton         Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks);
731f9765acdSGreg Clayton     }
732f9765acdSGreg Clayton }
733f9765acdSGreg Clayton 
734f9765acdSGreg Clayton bool
735f9765acdSGreg Clayton ProcessKDP::StartAsyncThread ()
736f9765acdSGreg Clayton {
7375160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
738f9765acdSGreg Clayton 
739f9765acdSGreg Clayton     if (log)
7407925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StartAsyncThread ()");
741f9765acdSGreg Clayton 
7427925fbbaSGreg Clayton     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
7437925fbbaSGreg Clayton         return true;
7447925fbbaSGreg Clayton 
745f9765acdSGreg Clayton     m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
746f9765acdSGreg Clayton     return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
747f9765acdSGreg Clayton }
748f9765acdSGreg Clayton 
749f9765acdSGreg Clayton void
750f9765acdSGreg Clayton ProcessKDP::StopAsyncThread ()
751f9765acdSGreg Clayton {
7525160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
753f9765acdSGreg Clayton 
754f9765acdSGreg Clayton     if (log)
7557925fbbaSGreg Clayton         log->Printf ("ProcessKDP::StopAsyncThread ()");
756f9765acdSGreg Clayton 
757f9765acdSGreg Clayton     m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
758f9765acdSGreg Clayton 
759f9765acdSGreg Clayton     // Stop the stdio thread
760f9765acdSGreg Clayton     if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
761f9765acdSGreg Clayton     {
762f9765acdSGreg Clayton         Host::ThreadJoin (m_async_thread, NULL, NULL);
7637925fbbaSGreg Clayton         m_async_thread = LLDB_INVALID_HOST_THREAD;
764f9765acdSGreg Clayton     }
765f9765acdSGreg Clayton }
766f9765acdSGreg Clayton 
767f9765acdSGreg Clayton 
768f9765acdSGreg Clayton void *
769f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg)
770f9765acdSGreg Clayton {
771f9765acdSGreg Clayton     ProcessKDP *process = (ProcessKDP*) arg;
772f9765acdSGreg Clayton 
7737925fbbaSGreg Clayton     const lldb::pid_t pid = process->GetID();
7747925fbbaSGreg Clayton 
7755160ce5cSGreg Clayton     Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
776f9765acdSGreg Clayton     if (log)
777d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid);
778f9765acdSGreg Clayton 
779f9765acdSGreg Clayton     Listener listener ("ProcessKDP::AsyncThread");
780f9765acdSGreg Clayton     EventSP event_sp;
781f9765acdSGreg Clayton     const uint32_t desired_event_mask = eBroadcastBitAsyncContinue |
782f9765acdSGreg Clayton                                         eBroadcastBitAsyncThreadShouldExit;
783f9765acdSGreg Clayton 
7847925fbbaSGreg Clayton 
785f9765acdSGreg Clayton     if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask)
786f9765acdSGreg Clayton     {
787f9765acdSGreg Clayton         bool done = false;
788f9765acdSGreg Clayton         while (!done)
789f9765acdSGreg Clayton         {
790f9765acdSGreg Clayton             if (log)
791d01b2953SDaniel Malea                 log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...",
7927925fbbaSGreg Clayton                              pid);
793f9765acdSGreg Clayton             if (listener.WaitForEvent (NULL, event_sp))
794f9765acdSGreg Clayton             {
7957925fbbaSGreg Clayton                 uint32_t event_type = event_sp->GetType();
796f9765acdSGreg Clayton                 if (log)
797d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...",
7987925fbbaSGreg Clayton                                  pid,
7997925fbbaSGreg Clayton                                  event_type);
800f9765acdSGreg Clayton 
8017925fbbaSGreg Clayton                 // When we are running, poll for 1 second to try and get an exception
8027925fbbaSGreg Clayton                 // to indicate the process has stopped. If we don't get one, check to
8037925fbbaSGreg Clayton                 // make sure no one asked us to exit
8047925fbbaSGreg Clayton                 bool is_running = false;
8057925fbbaSGreg Clayton                 DataExtractor exc_reply_packet;
8067925fbbaSGreg Clayton                 do
8077925fbbaSGreg Clayton                 {
808f9765acdSGreg Clayton                     switch (event_type)
809f9765acdSGreg Clayton                     {
810f9765acdSGreg Clayton                     case eBroadcastBitAsyncContinue:
811f9765acdSGreg Clayton                         {
8127925fbbaSGreg Clayton                             is_running = true;
8137925fbbaSGreg Clayton                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
814f9765acdSGreg Clayton                             {
815ba4e61d3SAndrew Kaylor                                 ThreadSP thread_sp (process->GetKernelThread());
8161afa68edSGreg Clayton                                 if (thread_sp)
8171afa68edSGreg Clayton                                 {
8181afa68edSGreg Clayton                                     lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());
8191afa68edSGreg Clayton                                     if (reg_ctx_sp)
8201afa68edSGreg Clayton                                         reg_ctx_sp->InvalidateAllRegisters();
82197d5cf05SGreg Clayton                                     static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet);
8221afa68edSGreg Clayton                                 }
82397d5cf05SGreg Clayton 
8247925fbbaSGreg Clayton                                 // TODO: parse the stop reply packet
8257925fbbaSGreg Clayton                                 is_running = false;
8267925fbbaSGreg Clayton                                 process->SetPrivateState(eStateStopped);
8277925fbbaSGreg Clayton                             }
8287925fbbaSGreg Clayton                             else
8297925fbbaSGreg Clayton                             {
8307925fbbaSGreg Clayton                                 // Check to see if we are supposed to exit. There is no way to
8317925fbbaSGreg Clayton                                 // interrupt a running kernel, so all we can do is wait for an
8327925fbbaSGreg Clayton                                 // exception or detach...
8337925fbbaSGreg Clayton                                 if (listener.GetNextEvent(event_sp))
8347925fbbaSGreg Clayton                                 {
8357925fbbaSGreg Clayton                                     // We got an event, go through the loop again
8367925fbbaSGreg Clayton                                     event_type = event_sp->GetType();
8377925fbbaSGreg Clayton                                 }
838f9765acdSGreg Clayton                             }
839f9765acdSGreg Clayton                         }
840f9765acdSGreg Clayton                         break;
841f9765acdSGreg Clayton 
842f9765acdSGreg Clayton                     case eBroadcastBitAsyncThreadShouldExit:
843f9765acdSGreg Clayton                         if (log)
844d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...",
8457925fbbaSGreg Clayton                                          pid);
846f9765acdSGreg Clayton                         done = true;
8477925fbbaSGreg Clayton                         is_running = false;
848f9765acdSGreg Clayton                         break;
849f9765acdSGreg Clayton 
850f9765acdSGreg Clayton                     default:
851f9765acdSGreg Clayton                         if (log)
852d01b2953SDaniel Malea                             log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x",
8537925fbbaSGreg Clayton                                          pid,
8547925fbbaSGreg Clayton                                          event_type);
855f9765acdSGreg Clayton                         done = true;
8567925fbbaSGreg Clayton                         is_running = false;
857f9765acdSGreg Clayton                         break;
858f9765acdSGreg Clayton                     }
8597925fbbaSGreg Clayton                 } while (is_running);
860f9765acdSGreg Clayton             }
861f9765acdSGreg Clayton             else
862f9765acdSGreg Clayton             {
863f9765acdSGreg Clayton                 if (log)
864d01b2953SDaniel Malea                     log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false",
8657925fbbaSGreg Clayton                                  pid);
866f9765acdSGreg Clayton                 done = true;
867f9765acdSGreg Clayton             }
868f9765acdSGreg Clayton         }
869f9765acdSGreg Clayton     }
870f9765acdSGreg Clayton 
871f9765acdSGreg Clayton     if (log)
872d01b2953SDaniel Malea         log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
8737925fbbaSGreg Clayton                      arg,
8747925fbbaSGreg Clayton                      pid);
875f9765acdSGreg Clayton 
876f9765acdSGreg Clayton     process->m_async_thread = LLDB_INVALID_HOST_THREAD;
877f9765acdSGreg Clayton     return NULL;
878f9765acdSGreg Clayton }
879f9765acdSGreg Clayton 
880f9765acdSGreg Clayton 
8811d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed
8821d19a2f2SGreg Clayton {
8831d19a2f2SGreg Clayton private:
8841d19a2f2SGreg Clayton 
8851d19a2f2SGreg Clayton     OptionGroupOptions m_option_group;
8861d19a2f2SGreg Clayton     OptionGroupUInt64 m_command_byte;
8871d19a2f2SGreg Clayton     OptionGroupString m_packet_data;
8881d19a2f2SGreg Clayton 
8891d19a2f2SGreg Clayton     virtual Options *
8901d19a2f2SGreg Clayton     GetOptions ()
8911d19a2f2SGreg Clayton     {
8921d19a2f2SGreg Clayton         return &m_option_group;
8931d19a2f2SGreg Clayton     }
8941d19a2f2SGreg Clayton 
8951d19a2f2SGreg Clayton 
8961d19a2f2SGreg Clayton public:
8971d19a2f2SGreg Clayton     CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) :
8981d19a2f2SGreg Clayton         CommandObjectParsed (interpreter,
8991d19a2f2SGreg Clayton                              "process plugin packet send",
9001d19a2f2SGreg 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. ",
9011d19a2f2SGreg Clayton                              NULL),
9021d19a2f2SGreg Clayton         m_option_group (interpreter),
9031d19a2f2SGreg 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),
9041d19a2f2SGreg 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)
9051d19a2f2SGreg Clayton     {
9061d19a2f2SGreg Clayton         m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
9071d19a2f2SGreg Clayton         m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
9081d19a2f2SGreg Clayton         m_option_group.Finalize();
9091d19a2f2SGreg Clayton     }
9101d19a2f2SGreg Clayton 
9111d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacketSend ()
9121d19a2f2SGreg Clayton     {
9131d19a2f2SGreg Clayton     }
9141d19a2f2SGreg Clayton 
9151d19a2f2SGreg Clayton     bool
9161d19a2f2SGreg Clayton     DoExecute (Args& command, CommandReturnObject &result)
9171d19a2f2SGreg Clayton     {
9181d19a2f2SGreg Clayton         const size_t argc = command.GetArgumentCount();
9191d19a2f2SGreg Clayton         if (argc == 0)
9201d19a2f2SGreg Clayton         {
9211d19a2f2SGreg Clayton             if (!m_command_byte.GetOptionValue().OptionWasSet())
9221d19a2f2SGreg Clayton             {
9231d19a2f2SGreg Clayton                 result.AppendError ("the --command option must be set to a valid command byte");
9241d19a2f2SGreg Clayton                 result.SetStatus (eReturnStatusFailed);
9251d19a2f2SGreg Clayton             }
9261d19a2f2SGreg Clayton             else
9271d19a2f2SGreg Clayton             {
9281d19a2f2SGreg Clayton                 const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0);
9291d19a2f2SGreg Clayton                 if (command_byte > 0 && command_byte <= UINT8_MAX)
9301d19a2f2SGreg Clayton                 {
9311d19a2f2SGreg Clayton                     ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
9321d19a2f2SGreg Clayton                     if (process)
9331d19a2f2SGreg Clayton                     {
9341d19a2f2SGreg Clayton                         const StateType state = process->GetState();
9351d19a2f2SGreg Clayton 
9361d19a2f2SGreg Clayton                         if (StateIsStoppedState (state, true))
9371d19a2f2SGreg Clayton                         {
9381d19a2f2SGreg Clayton                             std::vector<uint8_t> payload_bytes;
9391d19a2f2SGreg Clayton                             const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue();
9401d19a2f2SGreg Clayton                             if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0])
9411d19a2f2SGreg Clayton                             {
9421d19a2f2SGreg Clayton                                 StringExtractor extractor(ascii_hex_bytes_cstr);
9431d19a2f2SGreg Clayton                                 const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size();
9441d19a2f2SGreg Clayton                                 if (ascii_hex_bytes_cstr_len & 1)
9451d19a2f2SGreg Clayton                                 {
9461d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr);
9471d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
9481d19a2f2SGreg Clayton                                     return false;
9491d19a2f2SGreg Clayton                                 }
9501d19a2f2SGreg Clayton                                 payload_bytes.resize(ascii_hex_bytes_cstr_len/2);
9511d19a2f2SGreg Clayton                                 if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size())
9521d19a2f2SGreg Clayton                                 {
9531d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr);
9541d19a2f2SGreg Clayton                                     result.SetStatus (eReturnStatusFailed);
9551d19a2f2SGreg Clayton                                     return false;
9561d19a2f2SGreg Clayton                                 }
9571d19a2f2SGreg Clayton                             }
9581d19a2f2SGreg Clayton                             Error error;
9591d19a2f2SGreg Clayton                             DataExtractor reply;
9601d19a2f2SGreg Clayton                             process->GetCommunication().SendRawRequest (command_byte,
9611d19a2f2SGreg Clayton                                                                         payload_bytes.empty() ? NULL : payload_bytes.data(),
9621d19a2f2SGreg Clayton                                                                         payload_bytes.size(),
9631d19a2f2SGreg Clayton                                                                         reply,
9641d19a2f2SGreg Clayton                                                                         error);
9651d19a2f2SGreg Clayton 
9661d19a2f2SGreg Clayton                             if (error.Success())
9671d19a2f2SGreg Clayton                             {
9681d19a2f2SGreg Clayton                                 // Copy the binary bytes into a hex ASCII string for the result
9691d19a2f2SGreg Clayton                                 StreamString packet;
9701d19a2f2SGreg Clayton                                 packet.PutBytesAsRawHex8(reply.GetDataStart(),
9711d19a2f2SGreg Clayton                                                          reply.GetByteSize(),
9721d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder(),
9731d19a2f2SGreg Clayton                                                          lldb::endian::InlHostByteOrder());
9741d19a2f2SGreg Clayton                                 result.AppendMessage(packet.GetString().c_str());
9751d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusSuccessFinishResult);
9761d19a2f2SGreg Clayton                                 return true;
9771d19a2f2SGreg Clayton                             }
9781d19a2f2SGreg Clayton                             else
9791d19a2f2SGreg Clayton                             {
9801d19a2f2SGreg Clayton                                 const char *error_cstr = error.AsCString();
9811d19a2f2SGreg Clayton                                 if (error_cstr && error_cstr[0])
9821d19a2f2SGreg Clayton                                     result.AppendError (error_cstr);
9831d19a2f2SGreg Clayton                                 else
9841d19a2f2SGreg Clayton                                     result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError());
9851d19a2f2SGreg Clayton                                 result.SetStatus (eReturnStatusFailed);
9861d19a2f2SGreg Clayton                                 return false;
9871d19a2f2SGreg Clayton                             }
9881d19a2f2SGreg Clayton                         }
9891d19a2f2SGreg Clayton                         else
9901d19a2f2SGreg Clayton                         {
9911d19a2f2SGreg Clayton                             result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state));
9921d19a2f2SGreg Clayton                             result.SetStatus (eReturnStatusFailed);
9931d19a2f2SGreg Clayton                         }
9941d19a2f2SGreg Clayton                     }
9951d19a2f2SGreg Clayton                     else
9961d19a2f2SGreg Clayton                     {
9971d19a2f2SGreg Clayton                         result.AppendError ("invalid process");
9981d19a2f2SGreg Clayton                         result.SetStatus (eReturnStatusFailed);
9991d19a2f2SGreg Clayton                     }
10001d19a2f2SGreg Clayton                 }
10011d19a2f2SGreg Clayton                 else
10021d19a2f2SGreg Clayton                 {
1003d01b2953SDaniel Malea                     result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte);
10041d19a2f2SGreg Clayton                     result.SetStatus (eReturnStatusFailed);
10051d19a2f2SGreg Clayton                 }
10061d19a2f2SGreg Clayton             }
10071d19a2f2SGreg Clayton         }
10081d19a2f2SGreg Clayton         else
10091d19a2f2SGreg Clayton         {
10101d19a2f2SGreg Clayton             result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str());
10111d19a2f2SGreg Clayton             result.SetStatus (eReturnStatusFailed);
10121d19a2f2SGreg Clayton         }
10131d19a2f2SGreg Clayton         return false;
10141d19a2f2SGreg Clayton     }
10151d19a2f2SGreg Clayton };
10161d19a2f2SGreg Clayton 
10171d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword
10181d19a2f2SGreg Clayton {
10191d19a2f2SGreg Clayton private:
10201d19a2f2SGreg Clayton 
10211d19a2f2SGreg Clayton public:
10221d19a2f2SGreg Clayton     CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) :
10231d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
10241d19a2f2SGreg Clayton                             "process plugin packet",
10251d19a2f2SGreg Clayton                             "Commands that deal with KDP remote packets.",
10261d19a2f2SGreg Clayton                             NULL)
10271d19a2f2SGreg Clayton     {
10281d19a2f2SGreg Clayton         LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter)));
10291d19a2f2SGreg Clayton     }
10301d19a2f2SGreg Clayton 
10311d19a2f2SGreg Clayton     ~CommandObjectProcessKDPPacket ()
10321d19a2f2SGreg Clayton     {
10331d19a2f2SGreg Clayton     }
10341d19a2f2SGreg Clayton };
10351d19a2f2SGreg Clayton 
10361d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword
10371d19a2f2SGreg Clayton {
10381d19a2f2SGreg Clayton public:
10391d19a2f2SGreg Clayton     CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) :
10401d19a2f2SGreg Clayton     CommandObjectMultiword (interpreter,
10411d19a2f2SGreg Clayton                             "process plugin",
10421d19a2f2SGreg Clayton                             "A set of commands for operating on a ProcessKDP process.",
10431d19a2f2SGreg Clayton                             "process plugin <subcommand> [<subcommand-options>]")
10441d19a2f2SGreg Clayton     {
10451d19a2f2SGreg Clayton         LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket    (interpreter)));
10461d19a2f2SGreg Clayton     }
10471d19a2f2SGreg Clayton 
10481d19a2f2SGreg Clayton     ~CommandObjectMultiwordProcessKDP ()
10491d19a2f2SGreg Clayton     {
10501d19a2f2SGreg Clayton     }
10511d19a2f2SGreg Clayton };
10521d19a2f2SGreg Clayton 
10531d19a2f2SGreg Clayton CommandObject *
10541d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject()
10551d19a2f2SGreg Clayton {
10561d19a2f2SGreg Clayton     if (!m_command_sp)
10571d19a2f2SGreg Clayton         m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter()));
10581d19a2f2SGreg Clayton     return m_command_sp.get();
10591d19a2f2SGreg Clayton }
10601d19a2f2SGreg Clayton 
1061