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 36510938e5SCharles Davis #define USEC_PER_SEC 1000000 37510938e5SCharles Davis 38f9765acdSGreg Clayton // Project includes 39f9765acdSGreg Clayton #include "ProcessKDP.h" 40f9765acdSGreg Clayton #include "ProcessKDPLog.h" 41a63d08c9SGreg Clayton #include "ThreadKDP.h" 425e8534efSJason Molenda #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" 43840f12cfSJason Molenda #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" 441d19a2f2SGreg Clayton #include "Utility/StringExtractor.h" 45f9765acdSGreg Clayton 46f9765acdSGreg Clayton using namespace lldb; 47f9765acdSGreg Clayton using namespace lldb_private; 48f9765acdSGreg Clayton 497f98240dSGreg Clayton namespace { 507f98240dSGreg Clayton 517f98240dSGreg Clayton static PropertyDefinition 527f98240dSGreg Clayton g_properties[] = 537f98240dSGreg Clayton { 547f98240dSGreg Clayton { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." }, 557f98240dSGreg Clayton { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } 567f98240dSGreg Clayton }; 577f98240dSGreg Clayton 587f98240dSGreg Clayton enum 597f98240dSGreg Clayton { 607f98240dSGreg Clayton ePropertyPacketTimeout 617f98240dSGreg Clayton }; 627f98240dSGreg Clayton 637f98240dSGreg Clayton class PluginProperties : public Properties 647f98240dSGreg Clayton { 657f98240dSGreg Clayton public: 667f98240dSGreg Clayton 677f98240dSGreg Clayton static ConstString 687f98240dSGreg Clayton GetSettingName () 697f98240dSGreg Clayton { 707f98240dSGreg Clayton return ProcessKDP::GetPluginNameStatic(); 717f98240dSGreg Clayton } 727f98240dSGreg Clayton 737f98240dSGreg Clayton PluginProperties() : 747f98240dSGreg Clayton Properties () 757f98240dSGreg Clayton { 767f98240dSGreg Clayton m_collection_sp.reset (new OptionValueProperties(GetSettingName())); 777f98240dSGreg Clayton m_collection_sp->Initialize(g_properties); 787f98240dSGreg Clayton } 797f98240dSGreg Clayton 807f98240dSGreg Clayton virtual 817f98240dSGreg Clayton ~PluginProperties() 827f98240dSGreg Clayton { 837f98240dSGreg Clayton } 847f98240dSGreg Clayton 857f98240dSGreg Clayton uint64_t 867f98240dSGreg Clayton GetPacketTimeout() 877f98240dSGreg Clayton { 887f98240dSGreg Clayton const uint32_t idx = ePropertyPacketTimeout; 897f98240dSGreg Clayton return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); 907f98240dSGreg Clayton } 917f98240dSGreg Clayton }; 927f98240dSGreg Clayton 937f98240dSGreg Clayton typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; 947f98240dSGreg Clayton 957f98240dSGreg Clayton static const ProcessKDPPropertiesSP & 967f98240dSGreg Clayton GetGlobalPluginProperties() 977f98240dSGreg Clayton { 987f98240dSGreg Clayton static ProcessKDPPropertiesSP g_settings_sp; 997f98240dSGreg Clayton if (!g_settings_sp) 1007f98240dSGreg Clayton g_settings_sp.reset (new PluginProperties ()); 1017f98240dSGreg Clayton return g_settings_sp; 1027f98240dSGreg Clayton } 1037f98240dSGreg Clayton 1047f98240dSGreg Clayton } // anonymous namespace end 1057f98240dSGreg Clayton 106ba4e61d3SAndrew Kaylor static const lldb::tid_t g_kernel_tid = 1; 107ba4e61d3SAndrew Kaylor 10857abc5d6SGreg Clayton ConstString 109f9765acdSGreg Clayton ProcessKDP::GetPluginNameStatic() 110f9765acdSGreg Clayton { 11157abc5d6SGreg Clayton static ConstString g_name("kdp-remote"); 11257abc5d6SGreg Clayton return g_name; 113f9765acdSGreg Clayton } 114f9765acdSGreg Clayton 115f9765acdSGreg Clayton const char * 116f9765acdSGreg Clayton ProcessKDP::GetPluginDescriptionStatic() 117f9765acdSGreg Clayton { 118f9765acdSGreg Clayton return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; 119f9765acdSGreg Clayton } 120f9765acdSGreg Clayton 121f9765acdSGreg Clayton void 122f9765acdSGreg Clayton ProcessKDP::Terminate() 123f9765acdSGreg Clayton { 124f9765acdSGreg Clayton PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); 125f9765acdSGreg Clayton } 126f9765acdSGreg Clayton 127f9765acdSGreg Clayton 128c3776bf2SGreg Clayton lldb::ProcessSP 129c3776bf2SGreg Clayton ProcessKDP::CreateInstance (Target &target, 130c3776bf2SGreg Clayton Listener &listener, 131c3776bf2SGreg Clayton const FileSpec *crash_file_path) 132f9765acdSGreg Clayton { 133c3776bf2SGreg Clayton lldb::ProcessSP process_sp; 134c3776bf2SGreg Clayton if (crash_file_path == NULL) 135c3776bf2SGreg Clayton process_sp.reset(new ProcessKDP (target, listener)); 136c3776bf2SGreg Clayton return process_sp; 137f9765acdSGreg Clayton } 138f9765acdSGreg Clayton 139f9765acdSGreg Clayton bool 1403a29bdbeSGreg Clayton ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) 141f9765acdSGreg Clayton { 142596ed24eSGreg Clayton if (plugin_specified_by_name) 143596ed24eSGreg Clayton return true; 144596ed24eSGreg Clayton 145f9765acdSGreg Clayton // For now we are just making sure the file exists for a given module 146aa149cbdSGreg Clayton Module *exe_module = target.GetExecutableModulePointer(); 147aa149cbdSGreg Clayton if (exe_module) 148f9765acdSGreg Clayton { 149f9765acdSGreg Clayton const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); 15070512317SGreg Clayton switch (triple_ref.getOS()) 15170512317SGreg Clayton { 15270512317SGreg Clayton case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case 15370512317SGreg Clayton case llvm::Triple::MacOSX: // For desktop targets 15470512317SGreg Clayton case llvm::Triple::IOS: // For arm targets 15570512317SGreg Clayton if (triple_ref.getVendor() == llvm::Triple::Apple) 156f9765acdSGreg Clayton { 157aa149cbdSGreg Clayton ObjectFile *exe_objfile = exe_module->GetObjectFile(); 158f9765acdSGreg Clayton if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && 159f9765acdSGreg Clayton exe_objfile->GetStrata() == ObjectFile::eStrataKernel) 160f9765acdSGreg Clayton return true; 161f9765acdSGreg Clayton } 16270512317SGreg Clayton break; 16370512317SGreg Clayton 16470512317SGreg Clayton default: 16570512317SGreg Clayton break; 16670512317SGreg Clayton } 167f9765acdSGreg Clayton } 168596ed24eSGreg Clayton return false; 1693a29bdbeSGreg Clayton } 170f9765acdSGreg Clayton 171f9765acdSGreg Clayton //---------------------------------------------------------------------- 172f9765acdSGreg Clayton // ProcessKDP constructor 173f9765acdSGreg Clayton //---------------------------------------------------------------------- 174f9765acdSGreg Clayton ProcessKDP::ProcessKDP(Target& target, Listener &listener) : 175f9765acdSGreg Clayton Process (target, listener), 176f9765acdSGreg Clayton m_comm("lldb.process.kdp-remote.communication"), 1774bddaeb5SJim Ingham m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), 17897d5cf05SGreg Clayton m_async_thread (LLDB_INVALID_HOST_THREAD), 1795e8534efSJason Molenda m_dyld_plugin_name (), 1801d19a2f2SGreg Clayton m_kernel_load_addr (LLDB_INVALID_ADDRESS), 181ba4e61d3SAndrew Kaylor m_command_sp(), 182ba4e61d3SAndrew Kaylor m_kernel_thread_wp() 183f9765acdSGreg Clayton { 1847925fbbaSGreg Clayton m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); 1857925fbbaSGreg Clayton m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); 1867f98240dSGreg Clayton const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); 1877f98240dSGreg Clayton if (timeout_seconds > 0) 1887f98240dSGreg Clayton m_comm.SetPacketTimeout(timeout_seconds); 189f9765acdSGreg Clayton } 190f9765acdSGreg Clayton 191f9765acdSGreg Clayton //---------------------------------------------------------------------- 192f9765acdSGreg Clayton // Destructor 193f9765acdSGreg Clayton //---------------------------------------------------------------------- 194f9765acdSGreg Clayton ProcessKDP::~ProcessKDP() 195f9765acdSGreg Clayton { 196f9765acdSGreg Clayton Clear(); 197e24c4acfSGreg Clayton // We need to call finalize on the process before destroying ourselves 198e24c4acfSGreg Clayton // to make sure all of the broadcaster cleanup goes as planned. If we 199e24c4acfSGreg Clayton // destruct this class, then Process::~Process() might have problems 200e24c4acfSGreg Clayton // trying to fully destroy the broadcaster. 201e24c4acfSGreg Clayton Finalize(); 202f9765acdSGreg Clayton } 203f9765acdSGreg Clayton 204f9765acdSGreg Clayton //---------------------------------------------------------------------- 205f9765acdSGreg Clayton // PluginInterface 206f9765acdSGreg Clayton //---------------------------------------------------------------------- 20757abc5d6SGreg Clayton lldb_private::ConstString 208f9765acdSGreg Clayton ProcessKDP::GetPluginName() 209f9765acdSGreg Clayton { 210f9765acdSGreg Clayton return GetPluginNameStatic(); 211f9765acdSGreg Clayton } 212f9765acdSGreg Clayton 213f9765acdSGreg Clayton uint32_t 214f9765acdSGreg Clayton ProcessKDP::GetPluginVersion() 215f9765acdSGreg Clayton { 216f9765acdSGreg Clayton return 1; 217f9765acdSGreg Clayton } 218f9765acdSGreg Clayton 219f9765acdSGreg Clayton Error 220f9765acdSGreg Clayton ProcessKDP::WillLaunch (Module* module) 221f9765acdSGreg Clayton { 222f9765acdSGreg Clayton Error error; 223f9765acdSGreg Clayton error.SetErrorString ("launching not supported in kdp-remote plug-in"); 224f9765acdSGreg Clayton return error; 225f9765acdSGreg Clayton } 226f9765acdSGreg Clayton 227f9765acdSGreg Clayton Error 228f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) 229f9765acdSGreg Clayton { 230f9765acdSGreg Clayton Error error; 231f9765acdSGreg Clayton error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); 232f9765acdSGreg Clayton return error; 233f9765acdSGreg Clayton } 234f9765acdSGreg Clayton 235f9765acdSGreg Clayton Error 236f9765acdSGreg Clayton ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) 237f9765acdSGreg Clayton { 238f9765acdSGreg Clayton Error error; 239f9765acdSGreg Clayton error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); 240f9765acdSGreg Clayton return error; 241f9765acdSGreg Clayton } 242f9765acdSGreg Clayton 243f9765acdSGreg Clayton Error 2444bd4e7e3SJason Molenda ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) 245f9765acdSGreg Clayton { 246f9765acdSGreg Clayton Error error; 2473a29bdbeSGreg Clayton 2487925fbbaSGreg Clayton // Don't let any JIT happen when doing KDP as we can't allocate 2497925fbbaSGreg Clayton // memory and we don't want to be mucking with threads that might 2507925fbbaSGreg Clayton // already be handling exceptions 2517925fbbaSGreg Clayton SetCanJIT(false); 2527925fbbaSGreg Clayton 2533a29bdbeSGreg Clayton if (remote_url == NULL || remote_url[0] == '\0') 2547925fbbaSGreg Clayton { 2557925fbbaSGreg Clayton error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); 2567925fbbaSGreg Clayton return error; 2577925fbbaSGreg Clayton } 2583a29bdbeSGreg Clayton 2597b0992d9SGreg Clayton std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); 2603a29bdbeSGreg Clayton if (conn_ap.get()) 2613a29bdbeSGreg Clayton { 2623a29bdbeSGreg Clayton // Only try once for now. 2633a29bdbeSGreg Clayton // TODO: check if we should be retrying? 2643a29bdbeSGreg Clayton const uint32_t max_retry_count = 1; 2653a29bdbeSGreg Clayton for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) 2663a29bdbeSGreg Clayton { 2673a29bdbeSGreg Clayton if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) 2683a29bdbeSGreg Clayton break; 2693a29bdbeSGreg Clayton usleep (100000); 2703a29bdbeSGreg Clayton } 2713a29bdbeSGreg Clayton } 2723a29bdbeSGreg Clayton 2733a29bdbeSGreg Clayton if (conn_ap->IsConnected()) 2743a29bdbeSGreg Clayton { 2753a29bdbeSGreg Clayton const uint16_t reply_port = conn_ap->GetReadPort (); 2763a29bdbeSGreg Clayton 2773a29bdbeSGreg Clayton if (reply_port != 0) 2783a29bdbeSGreg Clayton { 2793a29bdbeSGreg Clayton m_comm.SetConnection(conn_ap.release()); 2803a29bdbeSGreg Clayton 2813a29bdbeSGreg Clayton if (m_comm.SendRequestReattach(reply_port)) 2823a29bdbeSGreg Clayton { 2833a29bdbeSGreg Clayton if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) 2843a29bdbeSGreg Clayton { 2853a29bdbeSGreg Clayton m_comm.GetVersion(); 2863a29bdbeSGreg Clayton uint32_t cpu = m_comm.GetCPUType(); 2873a29bdbeSGreg Clayton uint32_t sub = m_comm.GetCPUSubtype(); 2883a29bdbeSGreg Clayton ArchSpec kernel_arch; 2893a29bdbeSGreg Clayton kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); 2903a29bdbeSGreg Clayton m_target.SetArchitecture(kernel_arch); 2914bd4e7e3SJason Molenda 292840f12cfSJason Molenda /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet. */ 293840f12cfSJason Molenda /* An EFI kdp session has neither UUID nor load address. */ 2944bd4e7e3SJason Molenda 2954bd4e7e3SJason Molenda UUID kernel_uuid = m_comm.GetUUID (); 2964bd4e7e3SJason Molenda addr_t kernel_load_addr = m_comm.GetLoadAddress (); 2974bd4e7e3SJason Molenda 298840f12cfSJason Molenda if (m_comm.RemoteIsEFI ()) 299840f12cfSJason Molenda { 300840f12cfSJason Molenda m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic(); 301840f12cfSJason Molenda } 302ca2ffa7eSJason Molenda else if (m_comm.RemoteIsDarwinKernel ()) 303a8ea4baeSJason Molenda { 304ca2ffa7eSJason Molenda m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 305a8ea4baeSJason Molenda if (kernel_load_addr != LLDB_INVALID_ADDRESS) 3064bd4e7e3SJason Molenda { 3075e8534efSJason Molenda m_kernel_load_addr = kernel_load_addr; 308a8ea4baeSJason Molenda } 3094bd4e7e3SJason Molenda } 3104bd4e7e3SJason Molenda 31197d5cf05SGreg Clayton // Set the thread ID 31297d5cf05SGreg Clayton UpdateThreadListIfNeeded (); 313a63d08c9SGreg Clayton SetID (1); 31456d9a1b3SGreg Clayton GetThreadList (); 315a63d08c9SGreg Clayton SetPrivateState (eStateStopped); 31607e66e3eSGreg Clayton StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); 31707e66e3eSGreg Clayton if (async_strm_sp) 31807e66e3eSGreg Clayton { 3195b88216dSGreg Clayton const char *cstr; 3205b88216dSGreg Clayton if ((cstr = m_comm.GetKernelVersion ()) != NULL) 32107e66e3eSGreg Clayton { 3225b88216dSGreg Clayton async_strm_sp->Printf ("Version: %s\n", cstr); 32307e66e3eSGreg Clayton async_strm_sp->Flush(); 32407e66e3eSGreg Clayton } 3255b88216dSGreg Clayton // if ((cstr = m_comm.GetImagePath ()) != NULL) 3265b88216dSGreg Clayton // { 3275b88216dSGreg Clayton // async_strm_sp->Printf ("Image Path: %s\n", cstr); 3285b88216dSGreg Clayton // async_strm_sp->Flush(); 3295b88216dSGreg Clayton // } 33007e66e3eSGreg Clayton } 3313a29bdbeSGreg Clayton } 33297d5cf05SGreg Clayton else 33397d5cf05SGreg Clayton { 33497d5cf05SGreg Clayton error.SetErrorString("KDP_REATTACH failed"); 33597d5cf05SGreg Clayton } 3363a29bdbeSGreg Clayton } 3373a29bdbeSGreg Clayton else 3383a29bdbeSGreg Clayton { 33997d5cf05SGreg Clayton error.SetErrorString("KDP_REATTACH failed"); 3403a29bdbeSGreg Clayton } 3413a29bdbeSGreg Clayton } 3423a29bdbeSGreg Clayton else 3433a29bdbeSGreg Clayton { 3443a29bdbeSGreg Clayton error.SetErrorString("invalid reply port from UDP connection"); 3453a29bdbeSGreg Clayton } 3463a29bdbeSGreg Clayton } 3473a29bdbeSGreg Clayton else 3483a29bdbeSGreg Clayton { 3493a29bdbeSGreg Clayton if (error.Success()) 3503a29bdbeSGreg Clayton error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); 3513a29bdbeSGreg Clayton } 3523a29bdbeSGreg Clayton if (error.Fail()) 3533a29bdbeSGreg Clayton m_comm.Disconnect(); 3543a29bdbeSGreg Clayton 355f9765acdSGreg Clayton return error; 356f9765acdSGreg Clayton } 357f9765acdSGreg Clayton 358f9765acdSGreg Clayton //---------------------------------------------------------------------- 359f9765acdSGreg Clayton // Process Control 360f9765acdSGreg Clayton //---------------------------------------------------------------------- 361f9765acdSGreg Clayton Error 362982c9762SGreg Clayton ProcessKDP::DoLaunch (Module *exe_module, 363*7782de92SJean-Daniel Dupas ProcessLaunchInfo &launch_info) 364f9765acdSGreg Clayton { 365f9765acdSGreg Clayton Error error; 366f9765acdSGreg Clayton error.SetErrorString ("launching not supported in kdp-remote plug-in"); 367f9765acdSGreg Clayton return error; 368f9765acdSGreg Clayton } 369f9765acdSGreg Clayton 370f9765acdSGreg Clayton 371f9765acdSGreg Clayton Error 372f9765acdSGreg Clayton ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) 373f9765acdSGreg Clayton { 374f9765acdSGreg Clayton Error error; 375f9765acdSGreg Clayton error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); 376f9765acdSGreg Clayton return error; 377f9765acdSGreg Clayton } 378f9765acdSGreg Clayton 379f9765acdSGreg Clayton Error 38084647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) 38184647048SHan Ming Ong { 38284647048SHan Ming Ong Error error; 38384647048SHan Ming Ong error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); 38484647048SHan Ming Ong return error; 38584647048SHan Ming Ong } 38684647048SHan Ming Ong 38784647048SHan Ming Ong Error 38884647048SHan Ming Ong ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) 389f9765acdSGreg Clayton { 390f9765acdSGreg Clayton Error error; 391f9765acdSGreg Clayton error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); 392f9765acdSGreg Clayton return error; 393f9765acdSGreg Clayton } 394f9765acdSGreg Clayton 395f9765acdSGreg Clayton 396f9765acdSGreg Clayton void 397f9765acdSGreg Clayton ProcessKDP::DidAttach () 398f9765acdSGreg Clayton { 3995160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 400f9765acdSGreg Clayton if (log) 40154cb8f83SJohnny Chen log->Printf ("ProcessKDP::DidAttach()"); 402f9765acdSGreg Clayton if (GetID() != LLDB_INVALID_PROCESS_ID) 403f9765acdSGreg Clayton { 404f9765acdSGreg Clayton // TODO: figure out the register context that we will use 405f9765acdSGreg Clayton } 406f9765acdSGreg Clayton } 407f9765acdSGreg Clayton 4085e8534efSJason Molenda addr_t 4095e8534efSJason Molenda ProcessKDP::GetImageInfoAddress() 4105e8534efSJason Molenda { 4115e8534efSJason Molenda return m_kernel_load_addr; 4125e8534efSJason Molenda } 4135e8534efSJason Molenda 4145e8534efSJason Molenda lldb_private::DynamicLoader * 4155e8534efSJason Molenda ProcessKDP::GetDynamicLoader () 4165e8534efSJason Molenda { 4175e8534efSJason Molenda if (m_dyld_ap.get() == NULL) 4182e56a254SJason Molenda m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); 4195e8534efSJason Molenda return m_dyld_ap.get(); 4205e8534efSJason Molenda } 4215e8534efSJason Molenda 422f9765acdSGreg Clayton Error 423f9765acdSGreg Clayton ProcessKDP::WillResume () 424f9765acdSGreg Clayton { 425f9765acdSGreg Clayton return Error(); 426f9765acdSGreg Clayton } 427f9765acdSGreg Clayton 428f9765acdSGreg Clayton Error 429f9765acdSGreg Clayton ProcessKDP::DoResume () 430f9765acdSGreg Clayton { 431f9765acdSGreg Clayton Error error; 4325160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 4337925fbbaSGreg Clayton // Only start the async thread if we try to do any process control 4347925fbbaSGreg Clayton if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 4357925fbbaSGreg Clayton StartAsyncThread (); 4367925fbbaSGreg Clayton 43797d5cf05SGreg Clayton bool resume = false; 4387925fbbaSGreg Clayton 43997d5cf05SGreg Clayton // With KDP there is only one thread we can tell what to do 440ba4e61d3SAndrew Kaylor ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid)); 441ba4e61d3SAndrew Kaylor 44297d5cf05SGreg Clayton if (kernel_thread_sp) 4434b1b8b3eSGreg Clayton { 44497d5cf05SGreg Clayton const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); 4456e0ff1a3SGreg Clayton 4466e0ff1a3SGreg Clayton if (log) 4476e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state)); 4487925fbbaSGreg Clayton switch (thread_resume_state) 4494b1b8b3eSGreg Clayton { 4507925fbbaSGreg Clayton case eStateSuspended: 4517925fbbaSGreg Clayton // Nothing to do here when a thread will stay suspended 4527925fbbaSGreg Clayton // we just leave the CPU mask bit set to zero for the thread 4536e0ff1a3SGreg Clayton if (log) 4546e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume() = suspended???"); 4557925fbbaSGreg Clayton break; 4567925fbbaSGreg Clayton 4577925fbbaSGreg Clayton case eStateStepping: 4581afa68edSGreg Clayton { 4591afa68edSGreg Clayton lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); 4601afa68edSGreg Clayton 4611afa68edSGreg Clayton if (reg_ctx_sp) 4621afa68edSGreg Clayton { 4636e0ff1a3SGreg Clayton if (log) 4646e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);"); 4651afa68edSGreg Clayton reg_ctx_sp->HardwareSingleStep (true); 46697d5cf05SGreg Clayton resume = true; 4671afa68edSGreg Clayton } 4681afa68edSGreg Clayton else 4691afa68edSGreg Clayton { 4701afa68edSGreg Clayton error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); 4711afa68edSGreg Clayton } 4721afa68edSGreg Clayton } 4737925fbbaSGreg Clayton break; 4747925fbbaSGreg Clayton 47597d5cf05SGreg Clayton case eStateRunning: 4761afa68edSGreg Clayton { 4771afa68edSGreg Clayton lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); 4781afa68edSGreg Clayton 4791afa68edSGreg Clayton if (reg_ctx_sp) 4801afa68edSGreg Clayton { 4816e0ff1a3SGreg Clayton if (log) 4826e0ff1a3SGreg Clayton log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);"); 4831afa68edSGreg Clayton reg_ctx_sp->HardwareSingleStep (false); 48497d5cf05SGreg Clayton resume = true; 4851afa68edSGreg Clayton } 4861afa68edSGreg Clayton else 4871afa68edSGreg Clayton { 4881afa68edSGreg Clayton error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); 4891afa68edSGreg Clayton } 4901afa68edSGreg Clayton } 4917925fbbaSGreg Clayton break; 4927925fbbaSGreg Clayton 4937925fbbaSGreg Clayton default: 49497d5cf05SGreg Clayton // The only valid thread resume states are listed above 4957925fbbaSGreg Clayton assert (!"invalid thread resume state"); 4967925fbbaSGreg Clayton break; 4974b1b8b3eSGreg Clayton } 4984b1b8b3eSGreg Clayton } 4997925fbbaSGreg Clayton 50097d5cf05SGreg Clayton if (resume) 50197d5cf05SGreg Clayton { 50297d5cf05SGreg Clayton if (log) 50397d5cf05SGreg Clayton log->Printf ("ProcessKDP::DoResume () sending resume"); 50497d5cf05SGreg Clayton 50597d5cf05SGreg Clayton if (m_comm.SendRequestResume ()) 5067925fbbaSGreg Clayton { 5077925fbbaSGreg Clayton m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); 5087925fbbaSGreg Clayton SetPrivateState(eStateRunning); 5097925fbbaSGreg Clayton } 5104b1b8b3eSGreg Clayton else 51107e66e3eSGreg Clayton error.SetErrorString ("KDP resume failed"); 5127925fbbaSGreg Clayton } 5137925fbbaSGreg Clayton else 5147925fbbaSGreg Clayton { 51597d5cf05SGreg Clayton error.SetErrorString ("kernel thread is suspended"); 5167925fbbaSGreg Clayton } 5177925fbbaSGreg Clayton 518f9765acdSGreg Clayton return error; 519f9765acdSGreg Clayton } 520f9765acdSGreg Clayton 52197d5cf05SGreg Clayton lldb::ThreadSP 522ba4e61d3SAndrew Kaylor ProcessKDP::GetKernelThread() 52397d5cf05SGreg Clayton { 52497d5cf05SGreg Clayton // KDP only tells us about one thread/core. Any other threads will usually 52597d5cf05SGreg Clayton // be the ones that are read from memory by the OS plug-ins. 526ba4e61d3SAndrew Kaylor 527ba4e61d3SAndrew Kaylor ThreadSP thread_sp (m_kernel_thread_wp.lock()); 52897d5cf05SGreg Clayton if (!thread_sp) 529ba4e61d3SAndrew Kaylor { 530ba4e61d3SAndrew Kaylor thread_sp.reset(new ThreadKDP (*this, g_kernel_tid)); 531ba4e61d3SAndrew Kaylor m_kernel_thread_wp = thread_sp; 532ba4e61d3SAndrew Kaylor } 53397d5cf05SGreg Clayton return thread_sp; 53497d5cf05SGreg Clayton } 53597d5cf05SGreg Clayton 53697d5cf05SGreg Clayton 53797d5cf05SGreg Clayton 53897d5cf05SGreg Clayton 5399fc13556SGreg Clayton bool 54056d9a1b3SGreg Clayton ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 541f9765acdSGreg Clayton { 542f9765acdSGreg Clayton // locker will keep a mutex locked until it goes out of scope 5435160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); 544f9765acdSGreg Clayton if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) 545d01b2953SDaniel Malea log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); 546f9765acdSGreg Clayton 54739da3efdSGreg Clayton // Even though there is a CPU mask, it doesn't mean we can see each CPU 54897d5cf05SGreg Clayton // indivudually, there is really only one. Lets call this thread 1. 549ba4e61d3SAndrew Kaylor ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false)); 550ba4e61d3SAndrew Kaylor if (!thread_sp) 551ba4e61d3SAndrew Kaylor thread_sp = GetKernelThread (); 552ba4e61d3SAndrew Kaylor new_thread_list.AddThread(thread_sp); 55397d5cf05SGreg Clayton 5549fc13556SGreg Clayton return new_thread_list.GetSize(false) > 0; 555f9765acdSGreg Clayton } 556f9765acdSGreg Clayton 557f9765acdSGreg Clayton void 558f9765acdSGreg Clayton ProcessKDP::RefreshStateAfterStop () 559f9765acdSGreg Clayton { 560f9765acdSGreg Clayton // Let all threads recover from stopping and do any clean up based 561f9765acdSGreg Clayton // on the previous thread state (if any). 562f9765acdSGreg Clayton m_thread_list.RefreshStateAfterStop(); 563f9765acdSGreg Clayton } 564f9765acdSGreg Clayton 565f9765acdSGreg Clayton Error 566f9765acdSGreg Clayton ProcessKDP::DoHalt (bool &caused_stop) 567f9765acdSGreg Clayton { 568f9765acdSGreg Clayton Error error; 569f9765acdSGreg Clayton 57097d5cf05SGreg Clayton if (m_comm.IsRunning()) 571f9765acdSGreg Clayton { 57297d5cf05SGreg Clayton if (m_destroy_in_process) 57397d5cf05SGreg Clayton { 57497d5cf05SGreg Clayton // If we are attemping to destroy, we need to not return an error to 57597d5cf05SGreg Clayton // Halt or DoDestroy won't get called. 57697d5cf05SGreg Clayton // We are also currently running, so send a process stopped event 57797d5cf05SGreg Clayton SetPrivateState (eStateStopped); 578f9765acdSGreg Clayton } 579f9765acdSGreg Clayton else 580f9765acdSGreg Clayton { 58197d5cf05SGreg Clayton error.SetErrorString ("KDP cannot interrupt a running kernel"); 582f9765acdSGreg Clayton } 583f9765acdSGreg Clayton } 584f9765acdSGreg Clayton return error; 585f9765acdSGreg Clayton } 586f9765acdSGreg Clayton 587f9765acdSGreg Clayton Error 588acff8950SJim Ingham ProcessKDP::DoDetach(bool keep_stopped) 589f9765acdSGreg Clayton { 590f9765acdSGreg Clayton Error error; 5915160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 592f9765acdSGreg Clayton if (log) 593acff8950SJim Ingham log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped); 594f9765acdSGreg Clayton 59597d5cf05SGreg Clayton if (m_comm.IsRunning()) 59697d5cf05SGreg Clayton { 59797d5cf05SGreg Clayton // We are running and we can't interrupt a running kernel, so we need 59897d5cf05SGreg Clayton // to just close the connection to the kernel and hope for the best 59997d5cf05SGreg Clayton } 60097d5cf05SGreg Clayton else 60197d5cf05SGreg Clayton { 602f9765acdSGreg Clayton DisableAllBreakpointSites (); 603f9765acdSGreg Clayton 604f9765acdSGreg Clayton m_thread_list.DiscardThreadPlans(); 605f9765acdSGreg Clayton 606acff8950SJim Ingham // If we are going to keep the target stopped, then don't send the disconnect message. 607acff8950SJim Ingham if (!keep_stopped && m_comm.IsConnected()) 6083a29bdbeSGreg Clayton { 6096e0ff1a3SGreg Clayton const bool success = m_comm.SendRequestDisconnect(); 610f9765acdSGreg Clayton if (log) 611f9765acdSGreg Clayton { 6126e0ff1a3SGreg Clayton if (success) 6136e0ff1a3SGreg Clayton log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); 614f9765acdSGreg Clayton else 61577e82d1eSJim Ingham log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed"); 616f9765acdSGreg Clayton } 6176e0ff1a3SGreg Clayton m_comm.Disconnect (); 6183a29bdbeSGreg Clayton } 61997d5cf05SGreg Clayton } 620f9765acdSGreg Clayton StopAsyncThread (); 62174d4193eSGreg Clayton m_comm.Clear(); 622f9765acdSGreg Clayton 623f9765acdSGreg Clayton SetPrivateState (eStateDetached); 624f9765acdSGreg Clayton ResumePrivateStateThread(); 625f9765acdSGreg Clayton 626f9765acdSGreg Clayton //KillDebugserverProcess (); 627f9765acdSGreg Clayton return error; 628f9765acdSGreg Clayton } 629f9765acdSGreg Clayton 630f9765acdSGreg Clayton Error 631f9765acdSGreg Clayton ProcessKDP::DoDestroy () 632f9765acdSGreg Clayton { 6337925fbbaSGreg Clayton // For KDP there really is no difference between destroy and detach 634acff8950SJim Ingham bool keep_stopped = false; 635acff8950SJim Ingham return DoDetach(keep_stopped); 636f9765acdSGreg Clayton } 637f9765acdSGreg Clayton 638f9765acdSGreg Clayton //------------------------------------------------------------------ 639f9765acdSGreg Clayton // Process Queries 640f9765acdSGreg Clayton //------------------------------------------------------------------ 641f9765acdSGreg Clayton 642f9765acdSGreg Clayton bool 643f9765acdSGreg Clayton ProcessKDP::IsAlive () 644f9765acdSGreg Clayton { 645f9765acdSGreg Clayton return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; 646f9765acdSGreg Clayton } 647f9765acdSGreg Clayton 648f9765acdSGreg Clayton //------------------------------------------------------------------ 649f9765acdSGreg Clayton // Process Memory 650f9765acdSGreg Clayton //------------------------------------------------------------------ 651f9765acdSGreg Clayton size_t 652f9765acdSGreg Clayton ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) 653f9765acdSGreg Clayton { 654a63d08c9SGreg Clayton if (m_comm.IsConnected()) 655a63d08c9SGreg Clayton return m_comm.SendRequestReadMemory (addr, buf, size, error); 656a63d08c9SGreg Clayton error.SetErrorString ("not connected"); 657f9765acdSGreg Clayton return 0; 658f9765acdSGreg Clayton } 659f9765acdSGreg Clayton 660f9765acdSGreg Clayton size_t 661f9765acdSGreg Clayton ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) 662f9765acdSGreg Clayton { 6637925fbbaSGreg Clayton if (m_comm.IsConnected()) 6647925fbbaSGreg Clayton return m_comm.SendRequestWriteMemory (addr, buf, size, error); 6657925fbbaSGreg Clayton error.SetErrorString ("not connected"); 666f9765acdSGreg Clayton return 0; 667f9765acdSGreg Clayton } 668f9765acdSGreg Clayton 669f9765acdSGreg Clayton lldb::addr_t 670f9765acdSGreg Clayton ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) 671f9765acdSGreg Clayton { 672f9765acdSGreg Clayton error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); 673f9765acdSGreg Clayton return LLDB_INVALID_ADDRESS; 674f9765acdSGreg Clayton } 675f9765acdSGreg Clayton 676f9765acdSGreg Clayton Error 677f9765acdSGreg Clayton ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) 678f9765acdSGreg Clayton { 679f9765acdSGreg Clayton Error error; 680f9765acdSGreg Clayton error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); 681f9765acdSGreg Clayton return error; 682f9765acdSGreg Clayton } 683f9765acdSGreg Clayton 684f9765acdSGreg Clayton Error 685299c0c1cSJim Ingham ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site) 686f9765acdSGreg Clayton { 68707e66e3eSGreg Clayton if (m_comm.LocalBreakpointsAreSupported ()) 68807e66e3eSGreg Clayton { 68907e66e3eSGreg Clayton Error error; 6905b88216dSGreg Clayton if (!bp_site->IsEnabled()) 6915b88216dSGreg Clayton { 6925b88216dSGreg Clayton if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) 6935b88216dSGreg Clayton { 6945b88216dSGreg Clayton bp_site->SetEnabled(true); 6955b88216dSGreg Clayton bp_site->SetType (BreakpointSite::eExternal); 6965b88216dSGreg Clayton } 6975b88216dSGreg Clayton else 6985b88216dSGreg Clayton { 69907e66e3eSGreg Clayton error.SetErrorString ("KDP set breakpoint failed"); 7005b88216dSGreg Clayton } 7015b88216dSGreg Clayton } 70207e66e3eSGreg Clayton return error; 70307e66e3eSGreg Clayton } 704f9765acdSGreg Clayton return EnableSoftwareBreakpoint (bp_site); 705f9765acdSGreg Clayton } 706f9765acdSGreg Clayton 707f9765acdSGreg Clayton Error 708299c0c1cSJim Ingham ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site) 709f9765acdSGreg Clayton { 71007e66e3eSGreg Clayton if (m_comm.LocalBreakpointsAreSupported ()) 71107e66e3eSGreg Clayton { 71207e66e3eSGreg Clayton Error error; 7135b88216dSGreg Clayton if (bp_site->IsEnabled()) 7145b88216dSGreg Clayton { 7155b88216dSGreg Clayton BreakpointSite::Type bp_type = bp_site->GetType(); 7165b88216dSGreg Clayton if (bp_type == BreakpointSite::eExternal) 7175b88216dSGreg Clayton { 71897d5cf05SGreg Clayton if (m_destroy_in_process && m_comm.IsRunning()) 71997d5cf05SGreg Clayton { 72097d5cf05SGreg Clayton // We are trying to destroy our connection and we are running 72197d5cf05SGreg Clayton bp_site->SetEnabled(false); 72297d5cf05SGreg Clayton } 72397d5cf05SGreg Clayton else 72497d5cf05SGreg Clayton { 7255b88216dSGreg Clayton if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) 7265b88216dSGreg Clayton bp_site->SetEnabled(false); 7275b88216dSGreg Clayton else 72807e66e3eSGreg Clayton error.SetErrorString ("KDP remove breakpoint failed"); 7295b88216dSGreg Clayton } 73097d5cf05SGreg Clayton } 7315b88216dSGreg Clayton else 7325b88216dSGreg Clayton { 7335b88216dSGreg Clayton error = DisableSoftwareBreakpoint (bp_site); 7345b88216dSGreg Clayton } 7355b88216dSGreg Clayton } 73607e66e3eSGreg Clayton return error; 73707e66e3eSGreg Clayton } 738f9765acdSGreg Clayton return DisableSoftwareBreakpoint (bp_site); 739f9765acdSGreg Clayton } 740f9765acdSGreg Clayton 741f9765acdSGreg Clayton Error 7421b5792e5SJim Ingham ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify) 743f9765acdSGreg Clayton { 744f9765acdSGreg Clayton Error error; 745f9765acdSGreg Clayton error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); 746f9765acdSGreg Clayton return error; 747f9765acdSGreg Clayton } 748f9765acdSGreg Clayton 749f9765acdSGreg Clayton Error 7501b5792e5SJim Ingham ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify) 751f9765acdSGreg Clayton { 752f9765acdSGreg Clayton Error error; 753f9765acdSGreg Clayton error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); 754f9765acdSGreg Clayton return error; 755f9765acdSGreg Clayton } 756f9765acdSGreg Clayton 757f9765acdSGreg Clayton void 758f9765acdSGreg Clayton ProcessKDP::Clear() 759f9765acdSGreg Clayton { 760f9765acdSGreg Clayton m_thread_list.Clear(); 761f9765acdSGreg Clayton } 762f9765acdSGreg Clayton 763f9765acdSGreg Clayton Error 764f9765acdSGreg Clayton ProcessKDP::DoSignal (int signo) 765f9765acdSGreg Clayton { 766f9765acdSGreg Clayton Error error; 767f9765acdSGreg Clayton error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); 768f9765acdSGreg Clayton return error; 769f9765acdSGreg Clayton } 770f9765acdSGreg Clayton 771f9765acdSGreg Clayton void 772f9765acdSGreg Clayton ProcessKDP::Initialize() 773f9765acdSGreg Clayton { 774f9765acdSGreg Clayton static bool g_initialized = false; 775f9765acdSGreg Clayton 776f9765acdSGreg Clayton if (g_initialized == false) 777f9765acdSGreg Clayton { 778f9765acdSGreg Clayton g_initialized = true; 779f9765acdSGreg Clayton PluginManager::RegisterPlugin (GetPluginNameStatic(), 780f9765acdSGreg Clayton GetPluginDescriptionStatic(), 7817f98240dSGreg Clayton CreateInstance, 7827f98240dSGreg Clayton DebuggerInitialize); 783f9765acdSGreg Clayton 784f9765acdSGreg Clayton Log::Callbacks log_callbacks = { 785f9765acdSGreg Clayton ProcessKDPLog::DisableLog, 786f9765acdSGreg Clayton ProcessKDPLog::EnableLog, 787f9765acdSGreg Clayton ProcessKDPLog::ListLogCategories 788f9765acdSGreg Clayton }; 789f9765acdSGreg Clayton 790f9765acdSGreg Clayton Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); 791f9765acdSGreg Clayton } 792f9765acdSGreg Clayton } 793f9765acdSGreg Clayton 7947f98240dSGreg Clayton void 7957f98240dSGreg Clayton ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger) 7967f98240dSGreg Clayton { 7977f98240dSGreg Clayton if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) 7987f98240dSGreg Clayton { 7997f98240dSGreg Clayton const bool is_global_setting = true; 8007f98240dSGreg Clayton PluginManager::CreateSettingForProcessPlugin (debugger, 8017f98240dSGreg Clayton GetGlobalPluginProperties()->GetValueProperties(), 8027f98240dSGreg Clayton ConstString ("Properties for the kdp-remote process plug-in."), 8037f98240dSGreg Clayton is_global_setting); 8047f98240dSGreg Clayton } 8057f98240dSGreg Clayton } 8067f98240dSGreg Clayton 807f9765acdSGreg Clayton bool 808f9765acdSGreg Clayton ProcessKDP::StartAsyncThread () 809f9765acdSGreg Clayton { 8105160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 811f9765acdSGreg Clayton 812f9765acdSGreg Clayton if (log) 8137925fbbaSGreg Clayton log->Printf ("ProcessKDP::StartAsyncThread ()"); 814f9765acdSGreg Clayton 8157925fbbaSGreg Clayton if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 8167925fbbaSGreg Clayton return true; 8177925fbbaSGreg Clayton 818f9765acdSGreg Clayton m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); 819f9765acdSGreg Clayton return IS_VALID_LLDB_HOST_THREAD(m_async_thread); 820f9765acdSGreg Clayton } 821f9765acdSGreg Clayton 822f9765acdSGreg Clayton void 823f9765acdSGreg Clayton ProcessKDP::StopAsyncThread () 824f9765acdSGreg Clayton { 8255160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); 826f9765acdSGreg Clayton 827f9765acdSGreg Clayton if (log) 8287925fbbaSGreg Clayton log->Printf ("ProcessKDP::StopAsyncThread ()"); 829f9765acdSGreg Clayton 830f9765acdSGreg Clayton m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); 831f9765acdSGreg Clayton 832f9765acdSGreg Clayton // Stop the stdio thread 833f9765acdSGreg Clayton if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) 834f9765acdSGreg Clayton { 835f9765acdSGreg Clayton Host::ThreadJoin (m_async_thread, NULL, NULL); 8367925fbbaSGreg Clayton m_async_thread = LLDB_INVALID_HOST_THREAD; 837f9765acdSGreg Clayton } 838f9765acdSGreg Clayton } 839f9765acdSGreg Clayton 840f9765acdSGreg Clayton 841f9765acdSGreg Clayton void * 842f9765acdSGreg Clayton ProcessKDP::AsyncThread (void *arg) 843f9765acdSGreg Clayton { 844f9765acdSGreg Clayton ProcessKDP *process = (ProcessKDP*) arg; 845f9765acdSGreg Clayton 8467925fbbaSGreg Clayton const lldb::pid_t pid = process->GetID(); 8477925fbbaSGreg Clayton 8485160ce5cSGreg Clayton Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); 849f9765acdSGreg Clayton if (log) 850d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid); 851f9765acdSGreg Clayton 852f9765acdSGreg Clayton Listener listener ("ProcessKDP::AsyncThread"); 853f9765acdSGreg Clayton EventSP event_sp; 854f9765acdSGreg Clayton const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | 855f9765acdSGreg Clayton eBroadcastBitAsyncThreadShouldExit; 856f9765acdSGreg Clayton 8577925fbbaSGreg Clayton 858f9765acdSGreg Clayton if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) 859f9765acdSGreg Clayton { 860f9765acdSGreg Clayton bool done = false; 861f9765acdSGreg Clayton while (!done) 862f9765acdSGreg Clayton { 863f9765acdSGreg Clayton if (log) 864d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", 8657925fbbaSGreg Clayton pid); 866f9765acdSGreg Clayton if (listener.WaitForEvent (NULL, event_sp)) 867f9765acdSGreg Clayton { 8687925fbbaSGreg Clayton uint32_t event_type = event_sp->GetType(); 869f9765acdSGreg Clayton if (log) 870d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...", 8717925fbbaSGreg Clayton pid, 8727925fbbaSGreg Clayton event_type); 873f9765acdSGreg Clayton 8747925fbbaSGreg Clayton // When we are running, poll for 1 second to try and get an exception 8757925fbbaSGreg Clayton // to indicate the process has stopped. If we don't get one, check to 8767925fbbaSGreg Clayton // make sure no one asked us to exit 8777925fbbaSGreg Clayton bool is_running = false; 8787925fbbaSGreg Clayton DataExtractor exc_reply_packet; 8797925fbbaSGreg Clayton do 8807925fbbaSGreg Clayton { 881f9765acdSGreg Clayton switch (event_type) 882f9765acdSGreg Clayton { 883f9765acdSGreg Clayton case eBroadcastBitAsyncContinue: 884f9765acdSGreg Clayton { 8857925fbbaSGreg Clayton is_running = true; 8867925fbbaSGreg Clayton if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) 887f9765acdSGreg Clayton { 888ba4e61d3SAndrew Kaylor ThreadSP thread_sp (process->GetKernelThread()); 8891afa68edSGreg Clayton if (thread_sp) 8901afa68edSGreg Clayton { 8911afa68edSGreg Clayton lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); 8921afa68edSGreg Clayton if (reg_ctx_sp) 8931afa68edSGreg Clayton reg_ctx_sp->InvalidateAllRegisters(); 89497d5cf05SGreg Clayton static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); 8951afa68edSGreg Clayton } 89697d5cf05SGreg Clayton 8977925fbbaSGreg Clayton // TODO: parse the stop reply packet 8987925fbbaSGreg Clayton is_running = false; 8997925fbbaSGreg Clayton process->SetPrivateState(eStateStopped); 9007925fbbaSGreg Clayton } 9017925fbbaSGreg Clayton else 9027925fbbaSGreg Clayton { 9037925fbbaSGreg Clayton // Check to see if we are supposed to exit. There is no way to 9047925fbbaSGreg Clayton // interrupt a running kernel, so all we can do is wait for an 9057925fbbaSGreg Clayton // exception or detach... 9067925fbbaSGreg Clayton if (listener.GetNextEvent(event_sp)) 9077925fbbaSGreg Clayton { 9087925fbbaSGreg Clayton // We got an event, go through the loop again 9097925fbbaSGreg Clayton event_type = event_sp->GetType(); 9107925fbbaSGreg Clayton } 911f9765acdSGreg Clayton } 912f9765acdSGreg Clayton } 913f9765acdSGreg Clayton break; 914f9765acdSGreg Clayton 915f9765acdSGreg Clayton case eBroadcastBitAsyncThreadShouldExit: 916f9765acdSGreg Clayton if (log) 917d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", 9187925fbbaSGreg Clayton pid); 919f9765acdSGreg Clayton done = true; 9207925fbbaSGreg Clayton is_running = false; 921f9765acdSGreg Clayton break; 922f9765acdSGreg Clayton 923f9765acdSGreg Clayton default: 924f9765acdSGreg Clayton if (log) 925d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x", 9267925fbbaSGreg Clayton pid, 9277925fbbaSGreg Clayton event_type); 928f9765acdSGreg Clayton done = true; 9297925fbbaSGreg Clayton is_running = false; 930f9765acdSGreg Clayton break; 931f9765acdSGreg Clayton } 9327925fbbaSGreg Clayton } while (is_running); 933f9765acdSGreg Clayton } 934f9765acdSGreg Clayton else 935f9765acdSGreg Clayton { 936f9765acdSGreg Clayton if (log) 937d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", 9387925fbbaSGreg Clayton pid); 939f9765acdSGreg Clayton done = true; 940f9765acdSGreg Clayton } 941f9765acdSGreg Clayton } 942f9765acdSGreg Clayton } 943f9765acdSGreg Clayton 944f9765acdSGreg Clayton if (log) 945d01b2953SDaniel Malea log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...", 9467925fbbaSGreg Clayton arg, 9477925fbbaSGreg Clayton pid); 948f9765acdSGreg Clayton 949f9765acdSGreg Clayton process->m_async_thread = LLDB_INVALID_HOST_THREAD; 950f9765acdSGreg Clayton return NULL; 951f9765acdSGreg Clayton } 952f9765acdSGreg Clayton 953f9765acdSGreg Clayton 9541d19a2f2SGreg Clayton class CommandObjectProcessKDPPacketSend : public CommandObjectParsed 9551d19a2f2SGreg Clayton { 9561d19a2f2SGreg Clayton private: 9571d19a2f2SGreg Clayton 9581d19a2f2SGreg Clayton OptionGroupOptions m_option_group; 9591d19a2f2SGreg Clayton OptionGroupUInt64 m_command_byte; 9601d19a2f2SGreg Clayton OptionGroupString m_packet_data; 9611d19a2f2SGreg Clayton 9621d19a2f2SGreg Clayton virtual Options * 9631d19a2f2SGreg Clayton GetOptions () 9641d19a2f2SGreg Clayton { 9651d19a2f2SGreg Clayton return &m_option_group; 9661d19a2f2SGreg Clayton } 9671d19a2f2SGreg Clayton 9681d19a2f2SGreg Clayton 9691d19a2f2SGreg Clayton public: 9701d19a2f2SGreg Clayton CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) : 9711d19a2f2SGreg Clayton CommandObjectParsed (interpreter, 9721d19a2f2SGreg Clayton "process plugin packet send", 9731d19a2f2SGreg 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. ", 9741d19a2f2SGreg Clayton NULL), 9751d19a2f2SGreg Clayton m_option_group (interpreter), 9761d19a2f2SGreg 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), 9771d19a2f2SGreg 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) 9781d19a2f2SGreg Clayton { 9791d19a2f2SGreg Clayton m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 9801d19a2f2SGreg Clayton m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 9811d19a2f2SGreg Clayton m_option_group.Finalize(); 9821d19a2f2SGreg Clayton } 9831d19a2f2SGreg Clayton 9841d19a2f2SGreg Clayton ~CommandObjectProcessKDPPacketSend () 9851d19a2f2SGreg Clayton { 9861d19a2f2SGreg Clayton } 9871d19a2f2SGreg Clayton 9881d19a2f2SGreg Clayton bool 9891d19a2f2SGreg Clayton DoExecute (Args& command, CommandReturnObject &result) 9901d19a2f2SGreg Clayton { 9911d19a2f2SGreg Clayton const size_t argc = command.GetArgumentCount(); 9921d19a2f2SGreg Clayton if (argc == 0) 9931d19a2f2SGreg Clayton { 9941d19a2f2SGreg Clayton if (!m_command_byte.GetOptionValue().OptionWasSet()) 9951d19a2f2SGreg Clayton { 9961d19a2f2SGreg Clayton result.AppendError ("the --command option must be set to a valid command byte"); 9971d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 9981d19a2f2SGreg Clayton } 9991d19a2f2SGreg Clayton else 10001d19a2f2SGreg Clayton { 10011d19a2f2SGreg Clayton const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0); 10021d19a2f2SGreg Clayton if (command_byte > 0 && command_byte <= UINT8_MAX) 10031d19a2f2SGreg Clayton { 10041d19a2f2SGreg Clayton ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr(); 10051d19a2f2SGreg Clayton if (process) 10061d19a2f2SGreg Clayton { 10071d19a2f2SGreg Clayton const StateType state = process->GetState(); 10081d19a2f2SGreg Clayton 10091d19a2f2SGreg Clayton if (StateIsStoppedState (state, true)) 10101d19a2f2SGreg Clayton { 10111d19a2f2SGreg Clayton std::vector<uint8_t> payload_bytes; 10121d19a2f2SGreg Clayton const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue(); 10131d19a2f2SGreg Clayton if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) 10141d19a2f2SGreg Clayton { 10151d19a2f2SGreg Clayton StringExtractor extractor(ascii_hex_bytes_cstr); 10161d19a2f2SGreg Clayton const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size(); 10171d19a2f2SGreg Clayton if (ascii_hex_bytes_cstr_len & 1) 10181d19a2f2SGreg Clayton { 10191d19a2f2SGreg Clayton result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr); 10201d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10211d19a2f2SGreg Clayton return false; 10221d19a2f2SGreg Clayton } 10231d19a2f2SGreg Clayton payload_bytes.resize(ascii_hex_bytes_cstr_len/2); 10241d19a2f2SGreg Clayton if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size()) 10251d19a2f2SGreg Clayton { 10261d19a2f2SGreg Clayton result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr); 10271d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10281d19a2f2SGreg Clayton return false; 10291d19a2f2SGreg Clayton } 10301d19a2f2SGreg Clayton } 10311d19a2f2SGreg Clayton Error error; 10321d19a2f2SGreg Clayton DataExtractor reply; 10331d19a2f2SGreg Clayton process->GetCommunication().SendRawRequest (command_byte, 10341d19a2f2SGreg Clayton payload_bytes.empty() ? NULL : payload_bytes.data(), 10351d19a2f2SGreg Clayton payload_bytes.size(), 10361d19a2f2SGreg Clayton reply, 10371d19a2f2SGreg Clayton error); 10381d19a2f2SGreg Clayton 10391d19a2f2SGreg Clayton if (error.Success()) 10401d19a2f2SGreg Clayton { 10411d19a2f2SGreg Clayton // Copy the binary bytes into a hex ASCII string for the result 10421d19a2f2SGreg Clayton StreamString packet; 10431d19a2f2SGreg Clayton packet.PutBytesAsRawHex8(reply.GetDataStart(), 10441d19a2f2SGreg Clayton reply.GetByteSize(), 10451d19a2f2SGreg Clayton lldb::endian::InlHostByteOrder(), 10461d19a2f2SGreg Clayton lldb::endian::InlHostByteOrder()); 10471d19a2f2SGreg Clayton result.AppendMessage(packet.GetString().c_str()); 10481d19a2f2SGreg Clayton result.SetStatus (eReturnStatusSuccessFinishResult); 10491d19a2f2SGreg Clayton return true; 10501d19a2f2SGreg Clayton } 10511d19a2f2SGreg Clayton else 10521d19a2f2SGreg Clayton { 10531d19a2f2SGreg Clayton const char *error_cstr = error.AsCString(); 10541d19a2f2SGreg Clayton if (error_cstr && error_cstr[0]) 10551d19a2f2SGreg Clayton result.AppendError (error_cstr); 10561d19a2f2SGreg Clayton else 10571d19a2f2SGreg Clayton result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError()); 10581d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10591d19a2f2SGreg Clayton return false; 10601d19a2f2SGreg Clayton } 10611d19a2f2SGreg Clayton } 10621d19a2f2SGreg Clayton else 10631d19a2f2SGreg Clayton { 10641d19a2f2SGreg Clayton result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state)); 10651d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10661d19a2f2SGreg Clayton } 10671d19a2f2SGreg Clayton } 10681d19a2f2SGreg Clayton else 10691d19a2f2SGreg Clayton { 10701d19a2f2SGreg Clayton result.AppendError ("invalid process"); 10711d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10721d19a2f2SGreg Clayton } 10731d19a2f2SGreg Clayton } 10741d19a2f2SGreg Clayton else 10751d19a2f2SGreg Clayton { 1076d01b2953SDaniel Malea result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte); 10771d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10781d19a2f2SGreg Clayton } 10791d19a2f2SGreg Clayton } 10801d19a2f2SGreg Clayton } 10811d19a2f2SGreg Clayton else 10821d19a2f2SGreg Clayton { 10831d19a2f2SGreg Clayton result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str()); 10841d19a2f2SGreg Clayton result.SetStatus (eReturnStatusFailed); 10851d19a2f2SGreg Clayton } 10861d19a2f2SGreg Clayton return false; 10871d19a2f2SGreg Clayton } 10881d19a2f2SGreg Clayton }; 10891d19a2f2SGreg Clayton 10901d19a2f2SGreg Clayton class CommandObjectProcessKDPPacket : public CommandObjectMultiword 10911d19a2f2SGreg Clayton { 10921d19a2f2SGreg Clayton private: 10931d19a2f2SGreg Clayton 10941d19a2f2SGreg Clayton public: 10951d19a2f2SGreg Clayton CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) : 10961d19a2f2SGreg Clayton CommandObjectMultiword (interpreter, 10971d19a2f2SGreg Clayton "process plugin packet", 10981d19a2f2SGreg Clayton "Commands that deal with KDP remote packets.", 10991d19a2f2SGreg Clayton NULL) 11001d19a2f2SGreg Clayton { 11011d19a2f2SGreg Clayton LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter))); 11021d19a2f2SGreg Clayton } 11031d19a2f2SGreg Clayton 11041d19a2f2SGreg Clayton ~CommandObjectProcessKDPPacket () 11051d19a2f2SGreg Clayton { 11061d19a2f2SGreg Clayton } 11071d19a2f2SGreg Clayton }; 11081d19a2f2SGreg Clayton 11091d19a2f2SGreg Clayton class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword 11101d19a2f2SGreg Clayton { 11111d19a2f2SGreg Clayton public: 11121d19a2f2SGreg Clayton CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) : 11131d19a2f2SGreg Clayton CommandObjectMultiword (interpreter, 11141d19a2f2SGreg Clayton "process plugin", 11151d19a2f2SGreg Clayton "A set of commands for operating on a ProcessKDP process.", 11161d19a2f2SGreg Clayton "process plugin <subcommand> [<subcommand-options>]") 11171d19a2f2SGreg Clayton { 11181d19a2f2SGreg Clayton LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket (interpreter))); 11191d19a2f2SGreg Clayton } 11201d19a2f2SGreg Clayton 11211d19a2f2SGreg Clayton ~CommandObjectMultiwordProcessKDP () 11221d19a2f2SGreg Clayton { 11231d19a2f2SGreg Clayton } 11241d19a2f2SGreg Clayton }; 11251d19a2f2SGreg Clayton 11261d19a2f2SGreg Clayton CommandObject * 11271d19a2f2SGreg Clayton ProcessKDP::GetPluginCommandObject() 11281d19a2f2SGreg Clayton { 11291d19a2f2SGreg Clayton if (!m_command_sp) 11301d19a2f2SGreg Clayton m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter())); 11311d19a2f2SGreg Clayton return m_command_sp.get(); 11321d19a2f2SGreg Clayton } 11331d19a2f2SGreg Clayton 1134