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 4957abc5d6SGreg Clayton ConstString 50f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic() 51f9765acdSGreg Clayton { 5257abc5d6SGreg Clayton static ConstString g_name("kdp-remote"); 5357abc5d6SGreg Clayton return g_name; 54f9765acdSGreg Clayton } 55f9765acdSGreg Clayton 56f9765acdSGreg Clayton const char * 57f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic() 58f9765acdSGreg Clayton { 59f9765acdSGreg Clayton return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; 60f9765acdSGreg Clayton } 61f9765acdSGreg Clayton 62f9765acdSGreg Clayton void 63f9765acdSGreg Clayton ProcessKDP::Terminate() 64f9765acdSGreg Clayton { 65f9765acdSGreg Clayton PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); 66f9765acdSGreg Clayton } 67f9765acdSGreg Clayton 68f9765acdSGreg Clayton 69c3776bf2SGreg Clayton lldb::ProcessSP 70c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target, 71c3776bf2SGreg Clayton Listener &listener, 72c3776bf2SGreg Clayton const FileSpec *crash_file_path) 73f9765acdSGreg Clayton { 74c3776bf2SGreg Clayton lldb::ProcessSP process_sp; 75c3776bf2SGreg Clayton if (crash_file_path == NULL) 76c3776bf2SGreg Clayton process_sp.reset(new ProcessKDP (target, listener)); 77c3776bf2SGreg Clayton return process_sp; 78f9765acdSGreg Clayton } 79f9765acdSGreg Clayton 80f9765acdSGreg Clayton bool 813a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) 82f9765acdSGreg Clayton { 83596ed24eSGreg Clayton if (plugin_specified_by_name) 84596ed24eSGreg Clayton return true; 85596ed24eSGreg Clayton 86f9765acdSGreg Clayton // For now we are just making sure the file exists for a given module 87aa149cbdSGreg Clayton Module *exe_module = target.GetExecutableModulePointer(); 88aa149cbdSGreg Clayton if (exe_module) 89f9765acdSGreg Clayton { 90f9765acdSGreg Clayton const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); 9170512317SGreg Clayton switch (triple_ref.getOS()) 9270512317SGreg Clayton { 9370512317SGreg Clayton case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case 9470512317SGreg Clayton case llvm::Triple::MacOSX: // For desktop targets 9570512317SGreg Clayton case llvm::Triple::IOS: // For arm targets 9670512317SGreg Clayton if (triple_ref.getVendor() == llvm::Triple::Apple) 97f9765acdSGreg Clayton { 98aa149cbdSGreg Clayton ObjectFile *exe_objfile = exe_module->GetObjectFile(); 99f9765acdSGreg Clayton if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && 100f9765acdSGreg Clayton exe_objfile->GetStrata() == ObjectFile::eStrataKernel) 101f9765acdSGreg Clayton return true; 102f9765acdSGreg Clayton } 10370512317SGreg Clayton break; 10470512317SGreg Clayton 10570512317SGreg Clayton default: 10670512317SGreg Clayton break; 10770512317SGreg Clayton } 108f9765acdSGreg Clayton } 109596ed24eSGreg Clayton return false; 1103a29bdbeSGreg Clayton } 111f9765acdSGreg Clayton 112f9765acdSGreg Clayton //---------------------------------------------------------------------- 113f9765acdSGreg Clayton // ProcessKDP constructor 114f9765acdSGreg Clayton //---------------------------------------------------------------------- 115f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) : 116f9765acdSGreg Clayton Process (target, listener), 117f9765acdSGreg Clayton m_comm("lldb.process.kdp-remote.communication"), 1184bddaeb5SJim Ingham m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), 11997d5cf05SGreg Clayton m_async_thread (LLDB_INVALID_HOST_THREAD), 1205e8534efSJason Molenda m_dyld_plugin_name (), 1211d19a2f2SGreg Clayton m_kernel_load_addr (LLDB_INVALID_ADDRESS), 122ba4e61d3SAndrew Kaylor m_command_sp(), 123ba4e61d3SAndrew Kaylor m_kernel_thread_wp() 124f9765acdSGreg Clayton { 1257925fbbaSGreg Clayton m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); 1267925fbbaSGreg Clayton m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); 127f9765acdSGreg Clayton } 128f9765acdSGreg Clayton 129f9765acdSGreg Clayton //---------------------------------------------------------------------- 130f9765acdSGreg Clayton // Destructor 131f9765acdSGreg Clayton //---------------------------------------------------------------------- 132f9765acdSGreg Clayton ProcessKDP::~ProcessKDP() 133f9765acdSGreg Clayton { 134f9765acdSGreg Clayton Clear(); 135e24c4acfSGreg Clayton // We need to call finalize on the process before destroying ourselves 136e24c4acfSGreg Clayton // to make sure all of the broadcaster cleanup goes as planned. If we 137e24c4acfSGreg Clayton // destruct this class, then Process::~Process() might have problems 138e24c4acfSGreg Clayton // trying to fully destroy the broadcaster. 139e24c4acfSGreg Clayton Finalize(); 140f9765acdSGreg Clayton } 141f9765acdSGreg Clayton 142f9765acdSGreg Clayton //---------------------------------------------------------------------- 143f9765acdSGreg Clayton // PluginInterface 144f9765acdSGreg Clayton //---------------------------------------------------------------------- 14557abc5d6SGreg Clayton lldb_private::ConstString 146f9765acdSGreg Clayton ProcessKDP::GetPluginName() 147f9765acdSGreg Clayton { 148f9765acdSGreg Clayton return GetPluginNameStatic(); 149f9765acdSGreg Clayton } 150f9765acdSGreg Clayton 151f9765acdSGreg Clayton uint32_t 152f9765acdSGreg Clayton ProcessKDP::GetPluginVersion() 153f9765acdSGreg Clayton { 154f9765acdSGreg Clayton return 1; 155f9765acdSGreg Clayton } 156f9765acdSGreg Clayton 157f9765acdSGreg Clayton Error 158f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module) 159f9765acdSGreg Clayton { 160f9765acdSGreg Clayton Error error; 161f9765acdSGreg Clayton error.SetErrorString ("launching not supported in kdp-remote plug-in"); 162f9765acdSGreg Clayton return error; 163f9765acdSGreg Clayton } 164f9765acdSGreg Clayton 165f9765acdSGreg Clayton Error 166f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) 167f9765acdSGreg Clayton { 168f9765acdSGreg Clayton Error error; 169f9765acdSGreg Clayton error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); 170f9765acdSGreg Clayton return error; 171f9765acdSGreg Clayton } 172f9765acdSGreg Clayton 173f9765acdSGreg Clayton Error 174f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) 175f9765acdSGreg Clayton { 176f9765acdSGreg Clayton Error error; 177f9765acdSGreg Clayton error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); 178f9765acdSGreg Clayton return error; 179f9765acdSGreg Clayton } 180f9765acdSGreg Clayton 181f9765acdSGreg Clayton Error 1824bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) 183f9765acdSGreg Clayton { 184f9765acdSGreg Clayton Error error; 1853a29bdbeSGreg Clayton 1867925fbbaSGreg Clayton // Don't let any JIT happen when doing KDP as we can't allocate 1877925fbbaSGreg Clayton // memory and we don't want to be mucking with threads that might 1887925fbbaSGreg Clayton // already be handling exceptions 1897925fbbaSGreg Clayton SetCanJIT(false); 1907925fbbaSGreg Clayton 1913a29bdbeSGreg Clayton if (remote_url == NULL || remote_url[0] == '\0') 1927925fbbaSGreg Clayton { 1937925fbbaSGreg Clayton error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); 1947925fbbaSGreg Clayton return error; 1957925fbbaSGreg Clayton } 1963a29bdbeSGreg Clayton 1977b0992d9SGreg Clayton std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); 1983a29bdbeSGreg Clayton if (conn_ap.get()) 1993a29bdbeSGreg Clayton { 2003a29bdbeSGreg Clayton // Only try once for now. 2013a29bdbeSGreg Clayton // TODO: check if we should be retrying? 2023a29bdbeSGreg Clayton const uint32_t max_retry_count = 1; 2033a29bdbeSGreg Clayton for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) 2043a29bdbeSGreg Clayton { 2053a29bdbeSGreg Clayton if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) 2063a29bdbeSGreg Clayton break; 2073a29bdbeSGreg Clayton usleep (100000); 2083a29bdbeSGreg Clayton } 2093a29bdbeSGreg Clayton } 2103a29bdbeSGreg Clayton 2113a29bdbeSGreg Clayton if (conn_ap->IsConnected()) 2123a29bdbeSGreg Clayton { 2133a29bdbeSGreg Clayton const uint16_t reply_port = conn_ap->GetReadPort (); 2143a29bdbeSGreg Clayton 2153a29bdbeSGreg Clayton if (reply_port != 0) 2163a29bdbeSGreg Clayton { 2173a29bdbeSGreg Clayton m_comm.SetConnection(conn_ap.release()); 2183a29bdbeSGreg Clayton 2193a29bdbeSGreg Clayton if (m_comm.SendRequestReattach(reply_port)) 2203a29bdbeSGreg Clayton { 2213a29bdbeSGreg Clayton if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) 2223a29bdbeSGreg Clayton { 2233a29bdbeSGreg Clayton m_comm.GetVersion(); 2243a29bdbeSGreg Clayton uint32_t cpu = m_comm.GetCPUType(); 2253a29bdbeSGreg Clayton uint32_t sub = m_comm.GetCPUSubtype(); 2263a29bdbeSGreg Clayton ArchSpec kernel_arch; 2273a29bdbeSGreg Clayton kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); 2283a29bdbeSGreg Clayton m_target.SetArchitecture(kernel_arch); 2294bd4e7e3SJason Molenda 230840f12cfSJason Molenda /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */ 231840f12cfSJason Molenda /* An EFI kdp session has neither UUID nor load address. */ 2324bd4e7e3SJason Molenda 2334bd4e7e3SJason Molenda UUID kernel_uuid = m_comm.GetUUID (); 2344bd4e7e3SJason Molenda addr_t kernel_load_addr = m_comm.GetLoadAddress (); 2354bd4e7e3SJason Molenda 236840f12cfSJason Molenda if (m_comm.RemoteIsEFI ()) 237840f12cfSJason Molenda { 238840f12cfSJason Molenda m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic(); 239840f12cfSJason Molenda } 240ca2ffa7eSJason Molenda else if (m_comm.RemoteIsDarwinKernel ()) 241a8ea4baeSJason Molenda { 242ca2ffa7eSJason Molenda m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 243a8ea4baeSJason Molenda if (kernel_load_addr != LLDB_INVALID_ADDRESS) 2444bd4e7e3SJason Molenda { 2455e8534efSJason Molenda m_kernel_load_addr = kernel_load_addr; 246a8ea4baeSJason Molenda } 2474bd4e7e3SJason Molenda } 2484bd4e7e3SJason Molenda 24997d5cf05SGreg Clayton // Set the thread ID 25097d5cf05SGreg Clayton UpdateThreadListIfNeeded (); 251a63d08c9SGreg Clayton SetID (1); 25256d9a1b3SGreg Clayton GetThreadList (); 253a63d08c9SGreg Clayton SetPrivateState (eStateStopped); 25407e66e3eSGreg Clayton StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); 25507e66e3eSGreg Clayton if (async_strm_sp) 25607e66e3eSGreg Clayton { 2575b88216dSGreg Clayton const char *cstr; 2585b88216dSGreg Clayton if ((cstr = m_comm.GetKernelVersion ()) != NULL) 25907e66e3eSGreg Clayton { 2605b88216dSGreg Clayton async_strm_sp->Printf ("Version: %s\n", cstr); 26107e66e3eSGreg Clayton async_strm_sp->Flush(); 26207e66e3eSGreg Clayton } 2635b88216dSGreg Clayton // if ((cstr = m_comm.GetImagePath ()) != NULL) 2645b88216dSGreg Clayton // { 2655b88216dSGreg Clayton // async_strm_sp->Printf ("Image Path: %s\n", cstr); 2665b88216dSGreg Clayton // async_strm_sp->Flush(); 2675b88216dSGreg Clayton // } 26807e66e3eSGreg Clayton } 2693a29bdbeSGreg Clayton } 27097d5cf05SGreg Clayton else 27197d5cf05SGreg Clayton { 27297d5cf05SGreg Clayton error.SetErrorString("KDP_REATTACH failed"); 27397d5cf05SGreg Clayton } 2743a29bdbeSGreg Clayton } 2753a29bdbeSGreg Clayton else 2763a29bdbeSGreg Clayton { 27797d5cf05SGreg Clayton error.SetErrorString("KDP_REATTACH failed"); 2783a29bdbeSGreg Clayton } 2793a29bdbeSGreg Clayton } 2803a29bdbeSGreg Clayton else 2813a29bdbeSGreg Clayton { 2823a29bdbeSGreg Clayton error.SetErrorString("invalid reply port from UDP connection"); 2833a29bdbeSGreg Clayton } 2843a29bdbeSGreg Clayton } 2853a29bdbeSGreg Clayton else 2863a29bdbeSGreg Clayton { 2873a29bdbeSGreg Clayton if (error.Success()) 2883a29bdbeSGreg Clayton error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); 2893a29bdbeSGreg Clayton } 2903a29bdbeSGreg Clayton if (error.Fail()) 2913a29bdbeSGreg Clayton m_comm.Disconnect(); 2923a29bdbeSGreg Clayton 293f9765acdSGreg Clayton return error; 294f9765acdSGreg Clayton } 295f9765acdSGreg Clayton 296f9765acdSGreg Clayton //---------------------------------------------------------------------- 297f9765acdSGreg Clayton // Process Control 298f9765acdSGreg Clayton //---------------------------------------------------------------------- 299f9765acdSGreg Clayton Error 300982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module, 301982c9762SGreg Clayton const ProcessLaunchInfo &launch_info) 302f9765acdSGreg Clayton { 303f9765acdSGreg Clayton Error error; 304f9765acdSGreg Clayton error.SetErrorString ("launching not supported in kdp-remote plug-in"); 305f9765acdSGreg Clayton return error; 306f9765acdSGreg Clayton } 307f9765acdSGreg Clayton 308f9765acdSGreg Clayton 309f9765acdSGreg Clayton Error 310f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) 311f9765acdSGreg Clayton { 312f9765acdSGreg Clayton Error error; 313f9765acdSGreg Clayton error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); 314f9765acdSGreg Clayton return error; 315f9765acdSGreg Clayton } 316f9765acdSGreg Clayton 317f9765acdSGreg Clayton Error 31884647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) 31984647048SHan Ming Ong { 32084647048SHan Ming Ong Error error; 32184647048SHan Ming Ong error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); 32284647048SHan Ming Ong return error; 32384647048SHan Ming Ong } 32484647048SHan Ming Ong 32584647048SHan Ming Ong Error 32684647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) 327f9765acdSGreg Clayton { 328f9765acdSGreg Clayton Error error; 329f9765acdSGreg Clayton error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); 330f9765acdSGreg Clayton return error; 331f9765acdSGreg Clayton } 332f9765acdSGreg Clayton 333f9765acdSGreg Clayton 334f9765acdSGreg Clayton void 335f9765acdSGreg Clayton ProcessKDP::DidAttach () 336f9765acdSGreg Clayton { 3375160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 338f9765acdSGreg Clayton if (log) 33954cb8f83SJohnny Chen log->Printf ("ProcessKDP::DidAttach()"); 340f9765acdSGreg Clayton if (GetID() != LLDB_INVALID_PROCESS_ID) 341f9765acdSGreg Clayton { 342f9765acdSGreg Clayton // TODO: figure out the register context that we will use 343f9765acdSGreg Clayton } 344f9765acdSGreg Clayton } 345f9765acdSGreg Clayton 3465e8534efSJason Molenda addr_t 3475e8534efSJason Molenda ProcessKDP::GetImageInfoAddress() 3485e8534efSJason Molenda { 3495e8534efSJason Molenda return m_kernel_load_addr; 3505e8534efSJason Molenda } 3515e8534efSJason Molenda 3525e8534efSJason Molenda lldb_private::DynamicLoader * 3535e8534efSJason Molenda ProcessKDP::GetDynamicLoader () 3545e8534efSJason Molenda { 3555e8534efSJason Molenda if (m_dyld_ap.get() == NULL) 356*2e56a254SJason Molenda m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); 3575e8534efSJason Molenda return m_dyld_ap.get(); 3585e8534efSJason Molenda } 3595e8534efSJason Molenda 360f9765acdSGreg Clayton Error 361f9765acdSGreg Clayton ProcessKDP::WillResume () 362f9765acdSGreg Clayton { 363f9765acdSGreg Clayton return Error(); 364f9765acdSGreg Clayton } 365f9765acdSGreg Clayton 366f9765acdSGreg Clayton Error 367f9765acdSGreg Clayton ProcessKDP::DoResume () 368f9765acdSGreg Clayton { 369f9765acdSGreg Clayton Error error; 3705160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 3717925fbbaSGreg Clayton // Only start the async thread if we try to do any process control 3727925fbbaSGreg Clayton if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 3737925fbbaSGreg Clayton StartAsyncThread (); 3747925fbbaSGreg Clayton 37597d5cf05SGreg Clayton bool resume = false; 3767925fbbaSGreg Clayton 37797d5cf05SGreg Clayton // With KDP there is only one thread we can tell what to do 378ba4e61d3SAndrew Kaylor ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid)); 379ba4e61d3SAndrew Kaylor 38097d5cf05SGreg Clayton if (kernel_thread_sp) 3814b1b8b3eSGreg Clayton { 38297d5cf05SGreg Clayton const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); 3836e0ff1a3SGreg Clayton 3846e0ff1a3SGreg Clayton if (log) 3856e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state)); 3867925fbbaSGreg Clayton switch (thread_resume_state) 3874b1b8b3eSGreg Clayton { 3887925fbbaSGreg Clayton case eStateSuspended: 3897925fbbaSGreg Clayton // Nothing to do here when a thread will stay suspended 3907925fbbaSGreg Clayton // we just leave the CPU mask bit set to zero for the thread 3916e0ff1a3SGreg Clayton if (log) 3926e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume() = suspended???"); 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 { 4016e0ff1a3SGreg Clayton if (log) 4026e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);"); 4031afa68edSGreg Clayton reg_ctx_sp->HardwareSingleStep (true); 40497d5cf05SGreg Clayton resume = true; 4051afa68edSGreg Clayton } 4061afa68edSGreg Clayton else 4071afa68edSGreg Clayton { 4081afa68edSGreg Clayton error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); 4091afa68edSGreg Clayton } 4101afa68edSGreg Clayton } 4117925fbbaSGreg Clayton break; 4127925fbbaSGreg Clayton 41397d5cf05SGreg Clayton case eStateRunning: 4141afa68edSGreg Clayton { 4151afa68edSGreg Clayton lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); 4161afa68edSGreg Clayton 4171afa68edSGreg Clayton if (reg_ctx_sp) 4181afa68edSGreg Clayton { 4196e0ff1a3SGreg Clayton if (log) 4206e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);"); 4211afa68edSGreg Clayton reg_ctx_sp->HardwareSingleStep (false); 42297d5cf05SGreg Clayton resume = true; 4231afa68edSGreg Clayton } 4241afa68edSGreg Clayton else 4251afa68edSGreg Clayton { 4261afa68edSGreg Clayton error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); 4271afa68edSGreg Clayton } 4281afa68edSGreg Clayton } 4297925fbbaSGreg Clayton break; 4307925fbbaSGreg Clayton 4317925fbbaSGreg Clayton default: 43297d5cf05SGreg Clayton // The only valid thread resume states are listed above 4337925fbbaSGreg Clayton assert (!"invalid thread resume state"); 4347925fbbaSGreg Clayton break; 4354b1b8b3eSGreg Clayton } 4364b1b8b3eSGreg Clayton } 4377925fbbaSGreg Clayton 43897d5cf05SGreg Clayton if (resume) 43997d5cf05SGreg Clayton { 44097d5cf05SGreg Clayton if (log) 44197d5cf05SGreg Clayton log->Printf ("ProcessKDP::DoResume () sending resume"); 44297d5cf05SGreg Clayton 44397d5cf05SGreg Clayton if (m_comm.SendRequestResume ()) 4447925fbbaSGreg Clayton { 4457925fbbaSGreg Clayton m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); 4467925fbbaSGreg Clayton SetPrivateState(eStateRunning); 4477925fbbaSGreg Clayton } 4484b1b8b3eSGreg Clayton else 44907e66e3eSGreg Clayton error.SetErrorString ("KDP resume failed"); 4507925fbbaSGreg Clayton } 4517925fbbaSGreg Clayton else 4527925fbbaSGreg Clayton { 45397d5cf05SGreg Clayton error.SetErrorString ("kernel thread is suspended"); 4547925fbbaSGreg Clayton } 4557925fbbaSGreg Clayton 456f9765acdSGreg Clayton return error; 457f9765acdSGreg Clayton } 458f9765acdSGreg Clayton 45997d5cf05SGreg Clayton lldb::ThreadSP 460ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread() 46197d5cf05SGreg Clayton { 46297d5cf05SGreg Clayton // KDP only tells us about one thread/core. Any other threads will usually 46397d5cf05SGreg Clayton // be the ones that are read from memory by the OS plug-ins. 464ba4e61d3SAndrew Kaylor 465ba4e61d3SAndrew Kaylor ThreadSP thread_sp (m_kernel_thread_wp.lock()); 46697d5cf05SGreg Clayton if (!thread_sp) 467ba4e61d3SAndrew Kaylor { 468ba4e61d3SAndrew Kaylor thread_sp.reset(new ThreadKDP (*this, g_kernel_tid)); 469ba4e61d3SAndrew Kaylor m_kernel_thread_wp = thread_sp; 470ba4e61d3SAndrew Kaylor } 47197d5cf05SGreg Clayton return thread_sp; 47297d5cf05SGreg Clayton } 47397d5cf05SGreg Clayton 47497d5cf05SGreg Clayton 47597d5cf05SGreg Clayton 47697d5cf05SGreg Clayton 4779fc13556SGreg Clayton bool 47856d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 479f9765acdSGreg Clayton { 480f9765acdSGreg Clayton // locker will keep a mutex locked until it goes out of scope 4815160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); 482f9765acdSGreg Clayton if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) 483d01b2953SDaniel Malea log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); 484f9765acdSGreg Clayton 48539da3efdSGreg Clayton // Even though there is a CPU mask, it doesn't mean we can see each CPU 48697d5cf05SGreg Clayton // indivudually, there is really only one. Lets call this thread 1. 487ba4e61d3SAndrew Kaylor ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false)); 488ba4e61d3SAndrew Kaylor if (!thread_sp) 489ba4e61d3SAndrew Kaylor thread_sp = GetKernelThread (); 490ba4e61d3SAndrew Kaylor new_thread_list.AddThread(thread_sp); 49197d5cf05SGreg Clayton 4929fc13556SGreg Clayton return new_thread_list.GetSize(false) > 0; 493f9765acdSGreg Clayton } 494f9765acdSGreg Clayton 495f9765acdSGreg Clayton void 496f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop () 497f9765acdSGreg Clayton { 498f9765acdSGreg Clayton // Let all threads recover from stopping and do any clean up based 499f9765acdSGreg Clayton // on the previous thread state (if any). 500f9765acdSGreg Clayton m_thread_list.RefreshStateAfterStop(); 501f9765acdSGreg Clayton } 502f9765acdSGreg Clayton 503f9765acdSGreg Clayton Error 504f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop) 505f9765acdSGreg Clayton { 506f9765acdSGreg Clayton Error error; 507f9765acdSGreg Clayton 50897d5cf05SGreg Clayton if (m_comm.IsRunning()) 509f9765acdSGreg Clayton { 51097d5cf05SGreg Clayton if (m_destroy_in_process) 51197d5cf05SGreg Clayton { 51297d5cf05SGreg Clayton // If we are attemping to destroy, we need to not return an error to 51397d5cf05SGreg Clayton // Halt or DoDestroy won't get called. 51497d5cf05SGreg Clayton // We are also currently running, so send a process stopped event 51597d5cf05SGreg Clayton SetPrivateState (eStateStopped); 516f9765acdSGreg Clayton } 517f9765acdSGreg Clayton else 518f9765acdSGreg Clayton { 51997d5cf05SGreg Clayton error.SetErrorString ("KDP cannot interrupt a running kernel"); 520f9765acdSGreg Clayton } 521f9765acdSGreg Clayton } 522f9765acdSGreg Clayton return error; 523f9765acdSGreg Clayton } 524f9765acdSGreg Clayton 525f9765acdSGreg Clayton Error 526acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped) 527f9765acdSGreg Clayton { 528f9765acdSGreg Clayton Error error; 5295160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 530f9765acdSGreg Clayton if (log) 531acff8950SJim Ingham log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped); 532f9765acdSGreg Clayton 53397d5cf05SGreg Clayton if (m_comm.IsRunning()) 53497d5cf05SGreg Clayton { 53597d5cf05SGreg Clayton // We are running and we can't interrupt a running kernel, so we need 53697d5cf05SGreg Clayton // to just close the connection to the kernel and hope for the best 53797d5cf05SGreg Clayton } 53897d5cf05SGreg Clayton else 53997d5cf05SGreg Clayton { 540f9765acdSGreg Clayton DisableAllBreakpointSites (); 541f9765acdSGreg Clayton 542f9765acdSGreg Clayton m_thread_list.DiscardThreadPlans(); 543f9765acdSGreg Clayton 544acff8950SJim Ingham // If we are going to keep the target stopped, then don't send the disconnect message. 545acff8950SJim Ingham if (!keep_stopped && m_comm.IsConnected()) 5463a29bdbeSGreg Clayton { 5476e0ff1a3SGreg Clayton const bool success = m_comm.SendRequestDisconnect(); 548f9765acdSGreg Clayton if (log) 549f9765acdSGreg Clayton { 5506e0ff1a3SGreg Clayton if (success) 5516e0ff1a3SGreg Clayton log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); 552f9765acdSGreg Clayton else 55377e82d1eSJim Ingham log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed"); 554f9765acdSGreg Clayton } 5556e0ff1a3SGreg Clayton m_comm.Disconnect (); 5563a29bdbeSGreg Clayton } 55797d5cf05SGreg Clayton } 558f9765acdSGreg Clayton StopAsyncThread (); 55974d4193eSGreg Clayton m_comm.Clear(); 560f9765acdSGreg Clayton 561f9765acdSGreg Clayton SetPrivateState (eStateDetached); 562f9765acdSGreg Clayton ResumePrivateStateThread(); 563f9765acdSGreg Clayton 564f9765acdSGreg Clayton //KillDebugserverProcess (); 565f9765acdSGreg Clayton return error; 566f9765acdSGreg Clayton } 567f9765acdSGreg Clayton 568f9765acdSGreg Clayton Error 569f9765acdSGreg Clayton ProcessKDP::DoDestroy () 570f9765acdSGreg Clayton { 5717925fbbaSGreg Clayton // For KDP there really is no difference between destroy and detach 572acff8950SJim Ingham bool keep_stopped = false; 573acff8950SJim Ingham return DoDetach(keep_stopped); 574f9765acdSGreg Clayton } 575f9765acdSGreg Clayton 576f9765acdSGreg Clayton //------------------------------------------------------------------ 577f9765acdSGreg Clayton // Process Queries 578f9765acdSGreg Clayton //------------------------------------------------------------------ 579f9765acdSGreg Clayton 580f9765acdSGreg Clayton bool 581f9765acdSGreg Clayton ProcessKDP::IsAlive () 582f9765acdSGreg Clayton { 583f9765acdSGreg Clayton return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; 584f9765acdSGreg Clayton } 585f9765acdSGreg Clayton 586f9765acdSGreg Clayton //------------------------------------------------------------------ 587f9765acdSGreg Clayton // Process Memory 588f9765acdSGreg Clayton //------------------------------------------------------------------ 589f9765acdSGreg Clayton size_t 590f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) 591f9765acdSGreg Clayton { 592a63d08c9SGreg Clayton if (m_comm.IsConnected()) 593a63d08c9SGreg Clayton return m_comm.SendRequestReadMemory (addr, buf, size, error); 594a63d08c9SGreg Clayton error.SetErrorString ("not connected"); 595f9765acdSGreg Clayton return 0; 596f9765acdSGreg Clayton } 597f9765acdSGreg Clayton 598f9765acdSGreg Clayton size_t 599f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) 600f9765acdSGreg Clayton { 6017925fbbaSGreg Clayton if (m_comm.IsConnected()) 6027925fbbaSGreg Clayton return m_comm.SendRequestWriteMemory (addr, buf, size, error); 6037925fbbaSGreg Clayton error.SetErrorString ("not connected"); 604f9765acdSGreg Clayton return 0; 605f9765acdSGreg Clayton } 606f9765acdSGreg Clayton 607f9765acdSGreg Clayton lldb::addr_t 608f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) 609f9765acdSGreg Clayton { 610f9765acdSGreg Clayton error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); 611f9765acdSGreg Clayton return LLDB_INVALID_ADDRESS; 612f9765acdSGreg Clayton } 613f9765acdSGreg Clayton 614f9765acdSGreg Clayton Error 615f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) 616f9765acdSGreg Clayton { 617f9765acdSGreg Clayton Error error; 618f9765acdSGreg Clayton error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); 619f9765acdSGreg Clayton return error; 620f9765acdSGreg Clayton } 621f9765acdSGreg Clayton 622f9765acdSGreg Clayton Error 623299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site) 624f9765acdSGreg Clayton { 62507e66e3eSGreg Clayton if (m_comm.LocalBreakpointsAreSupported ()) 62607e66e3eSGreg Clayton { 62707e66e3eSGreg Clayton Error error; 6285b88216dSGreg Clayton if (!bp_site->IsEnabled()) 6295b88216dSGreg Clayton { 6305b88216dSGreg Clayton if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) 6315b88216dSGreg Clayton { 6325b88216dSGreg Clayton bp_site->SetEnabled(true); 6335b88216dSGreg Clayton bp_site->SetType (BreakpointSite::eExternal); 6345b88216dSGreg Clayton } 6355b88216dSGreg Clayton else 6365b88216dSGreg Clayton { 63707e66e3eSGreg Clayton error.SetErrorString ("KDP set breakpoint failed"); 6385b88216dSGreg Clayton } 6395b88216dSGreg Clayton } 64007e66e3eSGreg Clayton return error; 64107e66e3eSGreg Clayton } 642f9765acdSGreg Clayton return EnableSoftwareBreakpoint (bp_site); 643f9765acdSGreg Clayton } 644f9765acdSGreg Clayton 645f9765acdSGreg Clayton Error 646299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site) 647f9765acdSGreg Clayton { 64807e66e3eSGreg Clayton if (m_comm.LocalBreakpointsAreSupported ()) 64907e66e3eSGreg Clayton { 65007e66e3eSGreg Clayton Error error; 6515b88216dSGreg Clayton if (bp_site->IsEnabled()) 6525b88216dSGreg Clayton { 6535b88216dSGreg Clayton BreakpointSite::Type bp_type = bp_site->GetType(); 6545b88216dSGreg Clayton if (bp_type == BreakpointSite::eExternal) 6555b88216dSGreg Clayton { 65697d5cf05SGreg Clayton if (m_destroy_in_process && m_comm.IsRunning()) 65797d5cf05SGreg Clayton { 65897d5cf05SGreg Clayton // We are trying to destroy our connection and we are running 65997d5cf05SGreg Clayton bp_site->SetEnabled(false); 66097d5cf05SGreg Clayton } 66197d5cf05SGreg Clayton else 66297d5cf05SGreg Clayton { 6635b88216dSGreg Clayton if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) 6645b88216dSGreg Clayton bp_site->SetEnabled(false); 6655b88216dSGreg Clayton else 66607e66e3eSGreg Clayton error.SetErrorString ("KDP remove breakpoint failed"); 6675b88216dSGreg Clayton } 66897d5cf05SGreg Clayton } 6695b88216dSGreg Clayton else 6705b88216dSGreg Clayton { 6715b88216dSGreg Clayton error = DisableSoftwareBreakpoint (bp_site); 6725b88216dSGreg Clayton } 6735b88216dSGreg Clayton } 67407e66e3eSGreg Clayton return error; 67507e66e3eSGreg Clayton } 676f9765acdSGreg Clayton return DisableSoftwareBreakpoint (bp_site); 677f9765acdSGreg Clayton } 678f9765acdSGreg Clayton 679f9765acdSGreg Clayton Error 6801b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify) 681f9765acdSGreg Clayton { 682f9765acdSGreg Clayton Error error; 683f9765acdSGreg Clayton error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); 684f9765acdSGreg Clayton return error; 685f9765acdSGreg Clayton } 686f9765acdSGreg Clayton 687f9765acdSGreg Clayton Error 6881b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify) 689f9765acdSGreg Clayton { 690f9765acdSGreg Clayton Error error; 691f9765acdSGreg Clayton error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); 692f9765acdSGreg Clayton return error; 693f9765acdSGreg Clayton } 694f9765acdSGreg Clayton 695f9765acdSGreg Clayton void 696f9765acdSGreg Clayton ProcessKDP::Clear() 697f9765acdSGreg Clayton { 698f9765acdSGreg Clayton m_thread_list.Clear(); 699f9765acdSGreg Clayton } 700f9765acdSGreg Clayton 701f9765acdSGreg Clayton Error 702f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo) 703f9765acdSGreg Clayton { 704f9765acdSGreg Clayton Error error; 705f9765acdSGreg Clayton error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); 706f9765acdSGreg Clayton return error; 707f9765acdSGreg Clayton } 708f9765acdSGreg Clayton 709f9765acdSGreg Clayton void 710f9765acdSGreg Clayton ProcessKDP::Initialize() 711f9765acdSGreg Clayton { 712f9765acdSGreg Clayton static bool g_initialized = false; 713f9765acdSGreg Clayton 714f9765acdSGreg Clayton if (g_initialized == false) 715f9765acdSGreg Clayton { 716f9765acdSGreg Clayton g_initialized = true; 717f9765acdSGreg Clayton PluginManager::RegisterPlugin (GetPluginNameStatic(), 718f9765acdSGreg Clayton GetPluginDescriptionStatic(), 719f9765acdSGreg Clayton CreateInstance); 720f9765acdSGreg Clayton 721f9765acdSGreg Clayton Log::Callbacks log_callbacks = { 722f9765acdSGreg Clayton ProcessKDPLog::DisableLog, 723f9765acdSGreg Clayton ProcessKDPLog::EnableLog, 724f9765acdSGreg Clayton ProcessKDPLog::ListLogCategories 725f9765acdSGreg Clayton }; 726f9765acdSGreg Clayton 727f9765acdSGreg Clayton Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); 728f9765acdSGreg Clayton } 729f9765acdSGreg Clayton } 730f9765acdSGreg Clayton 731f9765acdSGreg Clayton bool 732f9765acdSGreg Clayton ProcessKDP::StartAsyncThread () 733f9765acdSGreg Clayton { 7345160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 735f9765acdSGreg Clayton 736f9765acdSGreg Clayton if (log) 7377925fbbaSGreg Clayton log->Printf ("ProcessKDP::StartAsyncThread ()"); 738f9765acdSGreg Clayton 7397925fbbaSGreg Clayton if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 7407925fbbaSGreg Clayton return true; 7417925fbbaSGreg Clayton 742f9765acdSGreg Clayton m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); 743f9765acdSGreg Clayton return IS_VALID_LLDB_HOST_THREAD(m_async_thread); 744f9765acdSGreg Clayton } 745f9765acdSGreg Clayton 746f9765acdSGreg Clayton void 747f9765acdSGreg Clayton ProcessKDP::StopAsyncThread () 748f9765acdSGreg Clayton { 7495160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 750f9765acdSGreg Clayton 751f9765acdSGreg Clayton if (log) 7527925fbbaSGreg Clayton log->Printf ("ProcessKDP::StopAsyncThread ()"); 753f9765acdSGreg Clayton 754f9765acdSGreg Clayton m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); 755f9765acdSGreg Clayton 756f9765acdSGreg Clayton // Stop the stdio thread 757f9765acdSGreg Clayton if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 758f9765acdSGreg Clayton { 759f9765acdSGreg Clayton Host::ThreadJoin (m_async_thread, NULL, NULL); 7607925fbbaSGreg Clayton m_async_thread = LLDB_INVALID_HOST_THREAD; 761f9765acdSGreg Clayton } 762f9765acdSGreg Clayton } 763f9765acdSGreg Clayton 764f9765acdSGreg Clayton 765f9765acdSGreg Clayton void * 766f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg) 767f9765acdSGreg Clayton { 768f9765acdSGreg Clayton ProcessKDP *process = (ProcessKDP*) arg; 769f9765acdSGreg Clayton 7707925fbbaSGreg Clayton const lldb::pid_t pid = process->GetID(); 7717925fbbaSGreg Clayton 7725160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 773f9765acdSGreg Clayton if (log) 774d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid); 775f9765acdSGreg Clayton 776f9765acdSGreg Clayton Listener listener ("ProcessKDP::AsyncThread"); 777f9765acdSGreg Clayton EventSP event_sp; 778f9765acdSGreg Clayton const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | 779f9765acdSGreg Clayton eBroadcastBitAsyncThreadShouldExit; 780f9765acdSGreg Clayton 7817925fbbaSGreg Clayton 782f9765acdSGreg Clayton if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) 783f9765acdSGreg Clayton { 784f9765acdSGreg Clayton bool done = false; 785f9765acdSGreg Clayton while (!done) 786f9765acdSGreg Clayton { 787f9765acdSGreg Clayton if (log) 788d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", 7897925fbbaSGreg Clayton pid); 790f9765acdSGreg Clayton if (listener.WaitForEvent (NULL, event_sp)) 791f9765acdSGreg Clayton { 7927925fbbaSGreg Clayton uint32_t event_type = event_sp->GetType(); 793f9765acdSGreg Clayton if (log) 794d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...", 7957925fbbaSGreg Clayton pid, 7967925fbbaSGreg Clayton event_type); 797f9765acdSGreg Clayton 7987925fbbaSGreg Clayton // When we are running, poll for 1 second to try and get an exception 7997925fbbaSGreg Clayton // to indicate the process has stopped. If we don't get one, check to 8007925fbbaSGreg Clayton // make sure no one asked us to exit 8017925fbbaSGreg Clayton bool is_running = false; 8027925fbbaSGreg Clayton DataExtractor exc_reply_packet; 8037925fbbaSGreg Clayton do 8047925fbbaSGreg Clayton { 805f9765acdSGreg Clayton switch (event_type) 806f9765acdSGreg Clayton { 807f9765acdSGreg Clayton case eBroadcastBitAsyncContinue: 808f9765acdSGreg Clayton { 8097925fbbaSGreg Clayton is_running = true; 8107925fbbaSGreg Clayton if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) 811f9765acdSGreg Clayton { 812ba4e61d3SAndrew Kaylor ThreadSP thread_sp (process->GetKernelThread()); 8131afa68edSGreg Clayton if (thread_sp) 8141afa68edSGreg Clayton { 8151afa68edSGreg Clayton lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); 8161afa68edSGreg Clayton if (reg_ctx_sp) 8171afa68edSGreg Clayton reg_ctx_sp->InvalidateAllRegisters(); 81897d5cf05SGreg Clayton static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); 8191afa68edSGreg Clayton } 82097d5cf05SGreg Clayton 8217925fbbaSGreg Clayton // TODO: parse the stop reply packet 8227925fbbaSGreg Clayton is_running = false; 8237925fbbaSGreg Clayton process->SetPrivateState(eStateStopped); 8247925fbbaSGreg Clayton } 8257925fbbaSGreg Clayton else 8267925fbbaSGreg Clayton { 8277925fbbaSGreg Clayton // Check to see if we are supposed to exit. There is no way to 8287925fbbaSGreg Clayton // interrupt a running kernel, so all we can do is wait for an 8297925fbbaSGreg Clayton // exception or detach... 8307925fbbaSGreg Clayton if (listener.GetNextEvent(event_sp)) 8317925fbbaSGreg Clayton { 8327925fbbaSGreg Clayton // We got an event, go through the loop again 8337925fbbaSGreg Clayton event_type = event_sp->GetType(); 8347925fbbaSGreg Clayton } 835f9765acdSGreg Clayton } 836f9765acdSGreg Clayton } 837f9765acdSGreg Clayton break; 838f9765acdSGreg Clayton 839f9765acdSGreg Clayton case eBroadcastBitAsyncThreadShouldExit: 840f9765acdSGreg Clayton if (log) 841d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", 8427925fbbaSGreg Clayton pid); 843f9765acdSGreg Clayton done = true; 8447925fbbaSGreg Clayton is_running = false; 845f9765acdSGreg Clayton break; 846f9765acdSGreg Clayton 847f9765acdSGreg Clayton default: 848f9765acdSGreg Clayton if (log) 849d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x", 8507925fbbaSGreg Clayton pid, 8517925fbbaSGreg Clayton event_type); 852f9765acdSGreg Clayton done = true; 8537925fbbaSGreg Clayton is_running = false; 854f9765acdSGreg Clayton break; 855f9765acdSGreg Clayton } 8567925fbbaSGreg Clayton } while (is_running); 857f9765acdSGreg Clayton } 858f9765acdSGreg Clayton else 859f9765acdSGreg Clayton { 860f9765acdSGreg Clayton if (log) 861d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", 8627925fbbaSGreg Clayton pid); 863f9765acdSGreg Clayton done = true; 864f9765acdSGreg Clayton } 865f9765acdSGreg Clayton } 866f9765acdSGreg Clayton } 867f9765acdSGreg Clayton 868f9765acdSGreg Clayton if (log) 869d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...", 8707925fbbaSGreg Clayton arg, 8717925fbbaSGreg Clayton pid); 872f9765acdSGreg Clayton 873f9765acdSGreg Clayton process->m_async_thread = LLDB_INVALID_HOST_THREAD; 874f9765acdSGreg Clayton return NULL; 875f9765acdSGreg Clayton } 876f9765acdSGreg Clayton 877f9765acdSGreg Clayton 8781d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed 8791d19a2f2SGreg Clayton { 8801d19a2f2SGreg Clayton private: 8811d19a2f2SGreg Clayton 8821d19a2f2SGreg Clayton OptionGroupOptions m_option_group; 8831d19a2f2SGreg Clayton OptionGroupUInt64 m_command_byte; 8841d19a2f2SGreg Clayton OptionGroupString m_packet_data; 8851d19a2f2SGreg Clayton 8861d19a2f2SGreg Clayton virtual Options * 8871d19a2f2SGreg Clayton GetOptions () 8881d19a2f2SGreg Clayton { 8891d19a2f2SGreg Clayton return &m_option_group; 8901d19a2f2SGreg Clayton } 8911d19a2f2SGreg Clayton 8921d19a2f2SGreg Clayton 8931d19a2f2SGreg Clayton public: 8941d19a2f2SGreg Clayton CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) : 8951d19a2f2SGreg Clayton CommandObjectParsed (interpreter, 8961d19a2f2SGreg Clayton "process plugin packet send", 8971d19a2f2SGreg 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. ", 8981d19a2f2SGreg Clayton NULL), 8991d19a2f2SGreg Clayton m_option_group (interpreter), 9001d19a2f2SGreg 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), 9011d19a2f2SGreg 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) 9021d19a2f2SGreg Clayton { 9031d19a2f2SGreg Clayton m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 9041d19a2f2SGreg Clayton m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 9051d19a2f2SGreg Clayton m_option_group.Finalize(); 9061d19a2f2SGreg Clayton } 9071d19a2f2SGreg Clayton 9081d19a2f2SGreg Clayton ~CommandObjectProcessKDPPacketSend () 9091d19a2f2SGreg Clayton { 9101d19a2f2SGreg Clayton } 9111d19a2f2SGreg Clayton 9121d19a2f2SGreg Clayton bool 9131d19a2f2SGreg Clayton DoExecute (Args& command, CommandReturnObject &result) 9141d19a2f2SGreg Clayton { 9151d19a2f2SGreg Clayton const size_t argc = command.GetArgumentCount(); 9161d19a2f2SGreg Clayton if (argc == 0) 9171d19a2f2SGreg Clayton { 9181d19a2f2SGreg Clayton if (!m_command_byte.GetOptionValue().OptionWasSet()) 9191d19a2f2SGreg Clayton { 9201d19a2f2SGreg Clayton result.AppendError ("the --command option must be set to a valid command byte"); 9211d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9221d19a2f2SGreg Clayton } 9231d19a2f2SGreg Clayton else 9241d19a2f2SGreg Clayton { 9251d19a2f2SGreg Clayton const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0); 9261d19a2f2SGreg Clayton if (command_byte > 0 && command_byte <= UINT8_MAX) 9271d19a2f2SGreg Clayton { 9281d19a2f2SGreg Clayton ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr(); 9291d19a2f2SGreg Clayton if (process) 9301d19a2f2SGreg Clayton { 9311d19a2f2SGreg Clayton const StateType state = process->GetState(); 9321d19a2f2SGreg Clayton 9331d19a2f2SGreg Clayton if (StateIsStoppedState (state, true)) 9341d19a2f2SGreg Clayton { 9351d19a2f2SGreg Clayton std::vector<uint8_t> payload_bytes; 9361d19a2f2SGreg Clayton const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue(); 9371d19a2f2SGreg Clayton if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) 9381d19a2f2SGreg Clayton { 9391d19a2f2SGreg Clayton StringExtractor extractor(ascii_hex_bytes_cstr); 9401d19a2f2SGreg Clayton const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size(); 9411d19a2f2SGreg Clayton if (ascii_hex_bytes_cstr_len & 1) 9421d19a2f2SGreg Clayton { 9431d19a2f2SGreg Clayton result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr); 9441d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9451d19a2f2SGreg Clayton return false; 9461d19a2f2SGreg Clayton } 9471d19a2f2SGreg Clayton payload_bytes.resize(ascii_hex_bytes_cstr_len/2); 9481d19a2f2SGreg Clayton if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size()) 9491d19a2f2SGreg Clayton { 9501d19a2f2SGreg Clayton result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr); 9511d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9521d19a2f2SGreg Clayton return false; 9531d19a2f2SGreg Clayton } 9541d19a2f2SGreg Clayton } 9551d19a2f2SGreg Clayton Error error; 9561d19a2f2SGreg Clayton DataExtractor reply; 9571d19a2f2SGreg Clayton process->GetCommunication().SendRawRequest (command_byte, 9581d19a2f2SGreg Clayton payload_bytes.empty() ? NULL : payload_bytes.data(), 9591d19a2f2SGreg Clayton payload_bytes.size(), 9601d19a2f2SGreg Clayton reply, 9611d19a2f2SGreg Clayton error); 9621d19a2f2SGreg Clayton 9631d19a2f2SGreg Clayton if (error.Success()) 9641d19a2f2SGreg Clayton { 9651d19a2f2SGreg Clayton // Copy the binary bytes into a hex ASCII string for the result 9661d19a2f2SGreg Clayton StreamString packet; 9671d19a2f2SGreg Clayton packet.PutBytesAsRawHex8(reply.GetDataStart(), 9681d19a2f2SGreg Clayton reply.GetByteSize(), 9691d19a2f2SGreg Clayton lldb::endian::InlHostByteOrder(), 9701d19a2f2SGreg Clayton lldb::endian::InlHostByteOrder()); 9711d19a2f2SGreg Clayton result.AppendMessage(packet.GetString().c_str()); 9721d19a2f2SGreg Clayton result.SetStatus (eReturnStatusSuccessFinishResult); 9731d19a2f2SGreg Clayton return true; 9741d19a2f2SGreg Clayton } 9751d19a2f2SGreg Clayton else 9761d19a2f2SGreg Clayton { 9771d19a2f2SGreg Clayton const char *error_cstr = error.AsCString(); 9781d19a2f2SGreg Clayton if (error_cstr && error_cstr[0]) 9791d19a2f2SGreg Clayton result.AppendError (error_cstr); 9801d19a2f2SGreg Clayton else 9811d19a2f2SGreg Clayton result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError()); 9821d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9831d19a2f2SGreg Clayton return false; 9841d19a2f2SGreg Clayton } 9851d19a2f2SGreg Clayton } 9861d19a2f2SGreg Clayton else 9871d19a2f2SGreg Clayton { 9881d19a2f2SGreg Clayton result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state)); 9891d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9901d19a2f2SGreg Clayton } 9911d19a2f2SGreg Clayton } 9921d19a2f2SGreg Clayton else 9931d19a2f2SGreg Clayton { 9941d19a2f2SGreg Clayton result.AppendError ("invalid process"); 9951d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9961d19a2f2SGreg Clayton } 9971d19a2f2SGreg Clayton } 9981d19a2f2SGreg Clayton else 9991d19a2f2SGreg Clayton { 1000d01b2953SDaniel Malea result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte); 10011d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10021d19a2f2SGreg Clayton } 10031d19a2f2SGreg Clayton } 10041d19a2f2SGreg Clayton } 10051d19a2f2SGreg Clayton else 10061d19a2f2SGreg Clayton { 10071d19a2f2SGreg Clayton result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str()); 10081d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10091d19a2f2SGreg Clayton } 10101d19a2f2SGreg Clayton return false; 10111d19a2f2SGreg Clayton } 10121d19a2f2SGreg Clayton }; 10131d19a2f2SGreg Clayton 10141d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword 10151d19a2f2SGreg Clayton { 10161d19a2f2SGreg Clayton private: 10171d19a2f2SGreg Clayton 10181d19a2f2SGreg Clayton public: 10191d19a2f2SGreg Clayton CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) : 10201d19a2f2SGreg Clayton CommandObjectMultiword (interpreter, 10211d19a2f2SGreg Clayton "process plugin packet", 10221d19a2f2SGreg Clayton "Commands that deal with KDP remote packets.", 10231d19a2f2SGreg Clayton NULL) 10241d19a2f2SGreg Clayton { 10251d19a2f2SGreg Clayton LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter))); 10261d19a2f2SGreg Clayton } 10271d19a2f2SGreg Clayton 10281d19a2f2SGreg Clayton ~CommandObjectProcessKDPPacket () 10291d19a2f2SGreg Clayton { 10301d19a2f2SGreg Clayton } 10311d19a2f2SGreg Clayton }; 10321d19a2f2SGreg Clayton 10331d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword 10341d19a2f2SGreg Clayton { 10351d19a2f2SGreg Clayton public: 10361d19a2f2SGreg Clayton CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) : 10371d19a2f2SGreg Clayton CommandObjectMultiword (interpreter, 10381d19a2f2SGreg Clayton "process plugin", 10391d19a2f2SGreg Clayton "A set of commands for operating on a ProcessKDP process.", 10401d19a2f2SGreg Clayton "process plugin <subcommand> [<subcommand-options>]") 10411d19a2f2SGreg Clayton { 10421d19a2f2SGreg Clayton LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter))); 10431d19a2f2SGreg Clayton } 10441d19a2f2SGreg Clayton 10451d19a2f2SGreg Clayton ~CommandObjectMultiwordProcessKDP () 10461d19a2f2SGreg Clayton { 10471d19a2f2SGreg Clayton } 10481d19a2f2SGreg Clayton }; 10491d19a2f2SGreg Clayton 10501d19a2f2SGreg Clayton CommandObject * 10511d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject() 10521d19a2f2SGreg Clayton { 10531d19a2f2SGreg Clayton if (!m_command_sp) 10541d19a2f2SGreg Clayton m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter())); 10551d19a2f2SGreg Clayton return m_command_sp.get(); 10561d19a2f2SGreg Clayton } 10571d19a2f2SGreg Clayton 1058