15ffd83dbSDimitry Andric //===-- ProcessGDBRemote.cpp ----------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/Host/Config.h" 100b57cec5SDimitry Andric 11fe6060f1SDimitry Andric #include <cerrno> 12fe6060f1SDimitry Andric #include <cstdlib> 13480093f4SDimitry Andric #if LLDB_ENABLE_POSIX 140b57cec5SDimitry Andric #include <netinet/in.h> 150b57cec5SDimitry Andric #include <sys/mman.h> 160b57cec5SDimitry Andric #include <sys/socket.h> 170b57cec5SDimitry Andric #include <unistd.h> 180b57cec5SDimitry Andric #endif 190b57cec5SDimitry Andric #include <sys/stat.h> 205ffd83dbSDimitry Andric #if defined(__APPLE__) 215ffd83dbSDimitry Andric #include <sys/sysctl.h> 225ffd83dbSDimitry Andric #endif 23fe6060f1SDimitry Andric #include <ctime> 240b57cec5SDimitry Andric #include <sys/types.h> 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #include <algorithm> 270b57cec5SDimitry Andric #include <csignal> 280b57cec5SDimitry Andric #include <map> 290b57cec5SDimitry Andric #include <memory> 300b57cec5SDimitry Andric #include <mutex> 310b57cec5SDimitry Andric #include <sstream> 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h" 340b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 350b57cec5SDimitry Andric #include "lldb/Core/Module.h" 360b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h" 370b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h" 380b57cec5SDimitry Andric #include "lldb/Core/StreamFile.h" 390b57cec5SDimitry Andric #include "lldb/Core/Value.h" 400b57cec5SDimitry Andric #include "lldb/DataFormatters/FormatManager.h" 410b57cec5SDimitry Andric #include "lldb/Host/ConnectionFileDescriptor.h" 420b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h" 430b57cec5SDimitry Andric #include "lldb/Host/HostThread.h" 440b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h" 450b57cec5SDimitry Andric #include "lldb/Host/PseudoTerminal.h" 460b57cec5SDimitry Andric #include "lldb/Host/ThreadLauncher.h" 470b57cec5SDimitry Andric #include "lldb/Host/XML.h" 480b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 490b57cec5SDimitry Andric #include "lldb/Interpreter/CommandObject.h" 500b57cec5SDimitry Andric #include "lldb/Interpreter/CommandObjectMultiword.h" 510b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 520b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 530b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupBoolean.h" 540b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupUInt64.h" 550b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValueProperties.h" 560b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h" 570b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h" 580b57cec5SDimitry Andric #include "lldb/Symbol/LocateSymbolFile.h" 590b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h" 600b57cec5SDimitry Andric #include "lldb/Target/ABI.h" 610b57cec5SDimitry Andric #include "lldb/Target/DynamicLoader.h" 620b57cec5SDimitry Andric #include "lldb/Target/MemoryRegionInfo.h" 630b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h" 640b57cec5SDimitry Andric #include "lldb/Target/Target.h" 650b57cec5SDimitry Andric #include "lldb/Target/TargetList.h" 660b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanCallFunction.h" 670b57cec5SDimitry Andric #include "lldb/Utility/Args.h" 680b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h" 690b57cec5SDimitry Andric #include "lldb/Utility/Reproducer.h" 700b57cec5SDimitry Andric #include "lldb/Utility/State.h" 710b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h" 720b57cec5SDimitry Andric #include "lldb/Utility/Timer.h" 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric #include "GDBRemoteRegisterContext.h" 75580012d6SDimitry Andric #ifdef LLDB_ENABLE_ALL 760b57cec5SDimitry Andric #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" 77580012d6SDimitry Andric #endif // LLDB_ENABLE_ALL 780b57cec5SDimitry Andric #include "Plugins/Process/Utility/GDBRemoteSignals.h" 790b57cec5SDimitry Andric #include "Plugins/Process/Utility/InferiorCallPOSIX.h" 800b57cec5SDimitry Andric #include "Plugins/Process/Utility/StopInfoMachException.h" 810b57cec5SDimitry Andric #include "ProcessGDBRemote.h" 820b57cec5SDimitry Andric #include "ProcessGDBRemoteLog.h" 830b57cec5SDimitry Andric #include "ThreadGDBRemote.h" 840b57cec5SDimitry Andric #include "lldb/Host/Host.h" 850b57cec5SDimitry Andric #include "lldb/Utility/StringExtractorGDBRemote.h" 860b57cec5SDimitry Andric 879dba64beSDimitry Andric #include "llvm/ADT/ScopeExit.h" 880b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h" 890b57cec5SDimitry Andric #include "llvm/Support/Threading.h" 900b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric #define DEBUGSERVER_BASENAME "debugserver" 930b57cec5SDimitry Andric using namespace lldb; 940b57cec5SDimitry Andric using namespace lldb_private; 950b57cec5SDimitry Andric using namespace lldb_private::process_gdb_remote; 960b57cec5SDimitry Andric 975ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(ProcessGDBRemote) 985ffd83dbSDimitry Andric 990b57cec5SDimitry Andric namespace lldb { 1000b57cec5SDimitry Andric // Provide a function that can easily dump the packet history if we know a 1010b57cec5SDimitry Andric // ProcessGDBRemote * value (which we can get from logs or from debugging). We 1020b57cec5SDimitry Andric // need the function in the lldb namespace so it makes it into the final 1030b57cec5SDimitry Andric // executable since the LLDB shared library only exports stuff in the lldb 1040b57cec5SDimitry Andric // namespace. This allows you to attach with a debugger and call this function 1050b57cec5SDimitry Andric // and get the packet history dumped to a file. 1060b57cec5SDimitry Andric void DumpProcessGDBRemotePacketHistory(void *p, const char *path) { 1079dba64beSDimitry Andric auto file = FileSystem::Instance().Open( 108349cc55cSDimitry Andric FileSpec(path), File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate); 1099dba64beSDimitry Andric if (!file) { 1109dba64beSDimitry Andric llvm::consumeError(file.takeError()); 1119dba64beSDimitry Andric return; 1129dba64beSDimitry Andric } 1139dba64beSDimitry Andric StreamFile stream(std::move(file.get())); 1149dba64beSDimitry Andric ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory(stream); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric } // namespace lldb 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric namespace { 1190b57cec5SDimitry Andric 1209dba64beSDimitry Andric #define LLDB_PROPERTIES_processgdbremote 1219dba64beSDimitry Andric #include "ProcessGDBRemoteProperties.inc" 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric enum { 1249dba64beSDimitry Andric #define LLDB_PROPERTIES_processgdbremote 1259dba64beSDimitry Andric #include "ProcessGDBRemotePropertiesEnum.inc" 1260b57cec5SDimitry Andric }; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric class PluginProperties : public Properties { 1290b57cec5SDimitry Andric public: 1300b57cec5SDimitry Andric static ConstString GetSettingName() { 131349cc55cSDimitry Andric return ConstString(ProcessGDBRemote::GetPluginNameStatic()); 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric PluginProperties() : Properties() { 1350b57cec5SDimitry Andric m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName()); 1369dba64beSDimitry Andric m_collection_sp->Initialize(g_processgdbremote_properties); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric 139fe6060f1SDimitry Andric ~PluginProperties() override = default; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric uint64_t GetPacketTimeout() { 1420b57cec5SDimitry Andric const uint32_t idx = ePropertyPacketTimeout; 1430b57cec5SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsUInt64( 1449dba64beSDimitry Andric nullptr, idx, g_processgdbremote_properties[idx].default_uint_value); 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric bool SetPacketTimeout(uint64_t timeout) { 1480b57cec5SDimitry Andric const uint32_t idx = ePropertyPacketTimeout; 1490b57cec5SDimitry Andric return m_collection_sp->SetPropertyAtIndexAsUInt64(nullptr, idx, timeout); 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric FileSpec GetTargetDefinitionFile() const { 1530b57cec5SDimitry Andric const uint32_t idx = ePropertyTargetDefinitionFile; 1540b57cec5SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric bool GetUseSVR4() const { 1580b57cec5SDimitry Andric const uint32_t idx = ePropertyUseSVR4; 1590b57cec5SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsBoolean( 1609dba64beSDimitry Andric nullptr, idx, 1619dba64beSDimitry Andric g_processgdbremote_properties[idx].default_uint_value != 0); 1620b57cec5SDimitry Andric } 163480093f4SDimitry Andric 164480093f4SDimitry Andric bool GetUseGPacketForReading() const { 165480093f4SDimitry Andric const uint32_t idx = ePropertyUseGPacketForReading; 166480093f4SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true); 167480093f4SDimitry Andric } 1680b57cec5SDimitry Andric }; 1690b57cec5SDimitry Andric 170349cc55cSDimitry Andric static PluginProperties &GetGlobalPluginProperties() { 171349cc55cSDimitry Andric static PluginProperties g_settings; 172349cc55cSDimitry Andric return g_settings; 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric } // namespace 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric // TODO Randomly assigning a port is unsafe. We should get an unused 1780b57cec5SDimitry Andric // ephemeral port from the kernel and make sure we reserve it before passing it 1790b57cec5SDimitry Andric // to debugserver. 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric #if defined(__APPLE__) 1820b57cec5SDimitry Andric #define LOW_PORT (IPPORT_RESERVED) 1830b57cec5SDimitry Andric #define HIGH_PORT (IPPORT_HIFIRSTAUTO) 1840b57cec5SDimitry Andric #else 1850b57cec5SDimitry Andric #define LOW_PORT (1024u) 1860b57cec5SDimitry Andric #define HIGH_PORT (49151u) 1870b57cec5SDimitry Andric #endif 1880b57cec5SDimitry Andric 189349cc55cSDimitry Andric llvm::StringRef ProcessGDBRemote::GetPluginDescriptionStatic() { 1900b57cec5SDimitry Andric return "GDB Remote protocol based debugging plug-in."; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void ProcessGDBRemote::Terminate() { 1940b57cec5SDimitry Andric PluginManager::UnregisterPlugin(ProcessGDBRemote::CreateInstance); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric lldb::ProcessSP 1980b57cec5SDimitry Andric ProcessGDBRemote::CreateInstance(lldb::TargetSP target_sp, 1990b57cec5SDimitry Andric ListenerSP listener_sp, 200e8d8bef9SDimitry Andric const FileSpec *crash_file_path, 201e8d8bef9SDimitry Andric bool can_connect) { 2020b57cec5SDimitry Andric lldb::ProcessSP process_sp; 2030b57cec5SDimitry Andric if (crash_file_path == nullptr) 2040b57cec5SDimitry Andric process_sp = std::make_shared<ProcessGDBRemote>(target_sp, listener_sp); 2050b57cec5SDimitry Andric return process_sp; 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 208fe6060f1SDimitry Andric std::chrono::seconds ProcessGDBRemote::GetPacketTimeout() { 209349cc55cSDimitry Andric return std::chrono::seconds(GetGlobalPluginProperties().GetPacketTimeout()); 210fe6060f1SDimitry Andric } 211fe6060f1SDimitry Andric 2120b57cec5SDimitry Andric bool ProcessGDBRemote::CanDebug(lldb::TargetSP target_sp, 2130b57cec5SDimitry Andric bool plugin_specified_by_name) { 2140b57cec5SDimitry Andric if (plugin_specified_by_name) 2150b57cec5SDimitry Andric return true; 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // For now we are just making sure the file exists for a given module 2180b57cec5SDimitry Andric Module *exe_module = target_sp->GetExecutableModulePointer(); 2190b57cec5SDimitry Andric if (exe_module) { 2200b57cec5SDimitry Andric ObjectFile *exe_objfile = exe_module->GetObjectFile(); 2210b57cec5SDimitry Andric // We can't debug core files... 2220b57cec5SDimitry Andric switch (exe_objfile->GetType()) { 2230b57cec5SDimitry Andric case ObjectFile::eTypeInvalid: 2240b57cec5SDimitry Andric case ObjectFile::eTypeCoreFile: 2250b57cec5SDimitry Andric case ObjectFile::eTypeDebugInfo: 2260b57cec5SDimitry Andric case ObjectFile::eTypeObjectFile: 2270b57cec5SDimitry Andric case ObjectFile::eTypeSharedLibrary: 2280b57cec5SDimitry Andric case ObjectFile::eTypeStubLibrary: 2290b57cec5SDimitry Andric case ObjectFile::eTypeJIT: 2300b57cec5SDimitry Andric return false; 2310b57cec5SDimitry Andric case ObjectFile::eTypeExecutable: 2320b57cec5SDimitry Andric case ObjectFile::eTypeDynamicLinker: 2330b57cec5SDimitry Andric case ObjectFile::eTypeUnknown: 2340b57cec5SDimitry Andric break; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric return FileSystem::Instance().Exists(exe_module->GetFileSpec()); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric // However, if there is no executable module, we return true since we might 2390b57cec5SDimitry Andric // be preparing to attach. 2400b57cec5SDimitry Andric return true; 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // ProcessGDBRemote constructor 2440b57cec5SDimitry Andric ProcessGDBRemote::ProcessGDBRemote(lldb::TargetSP target_sp, 2450b57cec5SDimitry Andric ListenerSP listener_sp) 2460b57cec5SDimitry Andric : Process(target_sp, listener_sp), 247349cc55cSDimitry Andric m_debugserver_pid(LLDB_INVALID_PROCESS_ID), m_register_info_sp(nullptr), 2480b57cec5SDimitry Andric m_async_broadcaster(nullptr, "lldb.process.gdb-remote.async-broadcaster"), 2490b57cec5SDimitry Andric m_async_listener_sp( 2500b57cec5SDimitry Andric Listener::MakeListener("lldb.process.gdb-remote.async-listener")), 2510b57cec5SDimitry Andric m_async_thread_state_mutex(), m_thread_ids(), m_thread_pcs(), 2520b57cec5SDimitry Andric m_jstopinfo_sp(), m_jthreadsinfo_sp(), m_continue_c_tids(), 2530b57cec5SDimitry Andric m_continue_C_tids(), m_continue_s_tids(), m_continue_S_tids(), 2540b57cec5SDimitry Andric m_max_memory_size(0), m_remote_stub_max_memory_size(0), 2550b57cec5SDimitry Andric m_addr_to_mmap_size(), m_thread_create_bp_sp(), 2560b57cec5SDimitry Andric m_waiting_for_attach(false), m_destroy_tried_resuming(false), 2570b57cec5SDimitry Andric m_command_sp(), m_breakpoint_pc_offset(0), 2580b57cec5SDimitry Andric m_initial_tid(LLDB_INVALID_THREAD_ID), m_replay_mode(false), 259349cc55cSDimitry Andric m_allow_flash_writes(false), m_erased_flash_ranges(), 260349cc55cSDimitry Andric m_vfork_in_progress(false) { 2610b57cec5SDimitry Andric m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit, 2620b57cec5SDimitry Andric "async thread should exit"); 2630b57cec5SDimitry Andric m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue, 2640b57cec5SDimitry Andric "async thread continue"); 2650b57cec5SDimitry Andric m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadDidExit, 2660b57cec5SDimitry Andric "async thread did exit"); 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) { 269480093f4SDimitry Andric repro::GDBRemoteProvider &provider = 270480093f4SDimitry Andric g->GetOrCreate<repro::GDBRemoteProvider>(); 271480093f4SDimitry Andric m_gdb_comm.SetPacketRecorder(provider.GetNewPacketRecorder()); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_ASYNC)); 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric const uint32_t async_event_mask = 2770b57cec5SDimitry Andric eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric if (m_async_listener_sp->StartListeningForEvents( 2800b57cec5SDimitry Andric &m_async_broadcaster, async_event_mask) != async_event_mask) { 2819dba64beSDimitry Andric LLDB_LOGF(log, 2829dba64beSDimitry Andric "ProcessGDBRemote::%s failed to listen for " 2830b57cec5SDimitry Andric "m_async_broadcaster events", 2840b57cec5SDimitry Andric __FUNCTION__); 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric 2874824e7fdSDimitry Andric const uint32_t gdb_event_mask = Communication::eBroadcastBitReadThreadDidExit; 2880b57cec5SDimitry Andric if (m_async_listener_sp->StartListeningForEvents( 2890b57cec5SDimitry Andric &m_gdb_comm, gdb_event_mask) != gdb_event_mask) { 2909dba64beSDimitry Andric LLDB_LOGF(log, 2919dba64beSDimitry Andric "ProcessGDBRemote::%s failed to listen for m_gdb_comm events", 2920b57cec5SDimitry Andric __FUNCTION__); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric const uint64_t timeout_seconds = 296349cc55cSDimitry Andric GetGlobalPluginProperties().GetPacketTimeout(); 2970b57cec5SDimitry Andric if (timeout_seconds > 0) 2980b57cec5SDimitry Andric m_gdb_comm.SetPacketTimeout(std::chrono::seconds(timeout_seconds)); 299480093f4SDimitry Andric 300480093f4SDimitry Andric m_use_g_packet_for_reading = 301349cc55cSDimitry Andric GetGlobalPluginProperties().GetUseGPacketForReading(); 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // Destructor 3050b57cec5SDimitry Andric ProcessGDBRemote::~ProcessGDBRemote() { 3060b57cec5SDimitry Andric // m_mach_process.UnregisterNotificationCallbacks (this); 3070b57cec5SDimitry Andric Clear(); 3080b57cec5SDimitry Andric // We need to call finalize on the process before destroying ourselves to 3090b57cec5SDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct 3100b57cec5SDimitry Andric // this class, then Process::~Process() might have problems trying to fully 3110b57cec5SDimitry Andric // destroy the broadcaster. 3120b57cec5SDimitry Andric Finalize(); 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric // The general Finalize is going to try to destroy the process and that 3150b57cec5SDimitry Andric // SHOULD shut down the async thread. However, if we don't kill it it will 3160b57cec5SDimitry Andric // get stranded and its connection will go away so when it wakes up it will 3170b57cec5SDimitry Andric // crash. So kill it for sure here. 3180b57cec5SDimitry Andric StopAsyncThread(); 3190b57cec5SDimitry Andric KillDebugserverProcess(); 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric bool ProcessGDBRemote::ParsePythonTargetDefinition( 3230b57cec5SDimitry Andric const FileSpec &target_definition_fspec) { 3240b57cec5SDimitry Andric ScriptInterpreter *interpreter = 3250b57cec5SDimitry Andric GetTarget().GetDebugger().GetScriptInterpreter(); 3260b57cec5SDimitry Andric Status error; 3270b57cec5SDimitry Andric StructuredData::ObjectSP module_object_sp( 3280b57cec5SDimitry Andric interpreter->LoadPluginModule(target_definition_fspec, error)); 3290b57cec5SDimitry Andric if (module_object_sp) { 3300b57cec5SDimitry Andric StructuredData::DictionarySP target_definition_sp( 3310b57cec5SDimitry Andric interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), 3320b57cec5SDimitry Andric "gdb-server-target-definition", error)); 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric if (target_definition_sp) { 3350b57cec5SDimitry Andric StructuredData::ObjectSP target_object( 3360b57cec5SDimitry Andric target_definition_sp->GetValueForKey("host-info")); 3370b57cec5SDimitry Andric if (target_object) { 3380b57cec5SDimitry Andric if (auto host_info_dict = target_object->GetAsDictionary()) { 3390b57cec5SDimitry Andric StructuredData::ObjectSP triple_value = 3400b57cec5SDimitry Andric host_info_dict->GetValueForKey("triple"); 3410b57cec5SDimitry Andric if (auto triple_string_value = triple_value->GetAsString()) { 3425ffd83dbSDimitry Andric std::string triple_string = 3435ffd83dbSDimitry Andric std::string(triple_string_value->GetValue()); 3440b57cec5SDimitry Andric ArchSpec host_arch(triple_string.c_str()); 3450b57cec5SDimitry Andric if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) { 3460b57cec5SDimitry Andric GetTarget().SetArchitecture(host_arch); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric m_breakpoint_pc_offset = 0; 3520b57cec5SDimitry Andric StructuredData::ObjectSP breakpoint_pc_offset_value = 3530b57cec5SDimitry Andric target_definition_sp->GetValueForKey("breakpoint-pc-offset"); 3540b57cec5SDimitry Andric if (breakpoint_pc_offset_value) { 3550b57cec5SDimitry Andric if (auto breakpoint_pc_int_value = 3560b57cec5SDimitry Andric breakpoint_pc_offset_value->GetAsInteger()) 3570b57cec5SDimitry Andric m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue(); 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 360e8d8bef9SDimitry Andric if (m_register_info_sp->SetRegisterInfo( 361e8d8bef9SDimitry Andric *target_definition_sp, GetTarget().GetArchitecture()) > 0) { 3620b57cec5SDimitry Andric return true; 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric return false; 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric static size_t SplitCommaSeparatedRegisterNumberString( 370349cc55cSDimitry Andric const llvm::StringRef &comma_separated_register_numbers, 3710b57cec5SDimitry Andric std::vector<uint32_t> ®nums, int base) { 3720b57cec5SDimitry Andric regnums.clear(); 373349cc55cSDimitry Andric for (llvm::StringRef x : llvm::split(comma_separated_register_numbers, ',')) { 374349cc55cSDimitry Andric uint32_t reg; 375349cc55cSDimitry Andric if (llvm::to_integer(x, reg, base)) 3760b57cec5SDimitry Andric regnums.push_back(reg); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric return regnums.size(); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric void ProcessGDBRemote::BuildDynamicRegisterInfo(bool force) { 382e8d8bef9SDimitry Andric if (!force && m_register_info_sp) 3830b57cec5SDimitry Andric return; 3840b57cec5SDimitry Andric 385e8d8bef9SDimitry Andric m_register_info_sp = std::make_shared<GDBRemoteDynamicRegisterInfo>(); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // Check if qHostInfo specified a specific packet timeout for this 3880b57cec5SDimitry Andric // connection. If so then lets update our setting so the user knows what the 3890b57cec5SDimitry Andric // timeout is and can see it. 3900b57cec5SDimitry Andric const auto host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); 3910b57cec5SDimitry Andric if (host_packet_timeout > std::chrono::seconds(0)) { 392349cc55cSDimitry Andric GetGlobalPluginProperties().SetPacketTimeout(host_packet_timeout.count()); 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // Register info search order: 3960b57cec5SDimitry Andric // 1 - Use the target definition python file if one is specified. 3970b57cec5SDimitry Andric // 2 - If the target definition doesn't have any of the info from the 3980b57cec5SDimitry Andric // target.xml (registers) then proceed to read the target.xml. 3990b57cec5SDimitry Andric // 3 - Fall back on the qRegisterInfo packets. 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric FileSpec target_definition_fspec = 402349cc55cSDimitry Andric GetGlobalPluginProperties().GetTargetDefinitionFile(); 4030b57cec5SDimitry Andric if (!FileSystem::Instance().Exists(target_definition_fspec)) { 4040b57cec5SDimitry Andric // If the filename doesn't exist, it may be a ~ not having been expanded - 4050b57cec5SDimitry Andric // try to resolve it. 4060b57cec5SDimitry Andric FileSystem::Instance().Resolve(target_definition_fspec); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric if (target_definition_fspec) { 4090b57cec5SDimitry Andric // See if we can get register definitions from a python file 4100b57cec5SDimitry Andric if (ParsePythonTargetDefinition(target_definition_fspec)) { 4110b57cec5SDimitry Andric return; 4120b57cec5SDimitry Andric } else { 4130b57cec5SDimitry Andric StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); 4140b57cec5SDimitry Andric stream_sp->Printf("ERROR: target description file %s failed to parse.\n", 4150b57cec5SDimitry Andric target_definition_fspec.GetPath().c_str()); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric const ArchSpec &target_arch = GetTarget().GetArchitecture(); 4200b57cec5SDimitry Andric const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); 4210b57cec5SDimitry Andric const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric // Use the process' architecture instead of the host arch, if available 4240b57cec5SDimitry Andric ArchSpec arch_to_use; 4250b57cec5SDimitry Andric if (remote_process_arch.IsValid()) 4260b57cec5SDimitry Andric arch_to_use = remote_process_arch; 4270b57cec5SDimitry Andric else 4280b57cec5SDimitry Andric arch_to_use = remote_host_arch; 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric if (!arch_to_use.IsValid()) 4310b57cec5SDimitry Andric arch_to_use = target_arch; 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric if (GetGDBServerRegisterInfo(arch_to_use)) 4340b57cec5SDimitry Andric return; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric char packet[128]; 437349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> registers; 4380b57cec5SDimitry Andric uint32_t reg_num = 0; 4390b57cec5SDimitry Andric for (StringExtractorGDBRemote::ResponseType response_type = 4400b57cec5SDimitry Andric StringExtractorGDBRemote::eResponse; 4410b57cec5SDimitry Andric response_type == StringExtractorGDBRemote::eResponse; ++reg_num) { 4420b57cec5SDimitry Andric const int packet_len = 4430b57cec5SDimitry Andric ::snprintf(packet, sizeof(packet), "qRegisterInfo%x", reg_num); 4440b57cec5SDimitry Andric assert(packet_len < (int)sizeof(packet)); 4450b57cec5SDimitry Andric UNUSED_IF_ASSERT_DISABLED(packet_len); 4460b57cec5SDimitry Andric StringExtractorGDBRemote response; 447fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet, response) == 4480b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 4490b57cec5SDimitry Andric response_type = response.GetResponseType(); 4500b57cec5SDimitry Andric if (response_type == StringExtractorGDBRemote::eResponse) { 4510b57cec5SDimitry Andric llvm::StringRef name; 4520b57cec5SDimitry Andric llvm::StringRef value; 453349cc55cSDimitry Andric DynamicRegisterInfo::Register reg_info; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric while (response.GetNameColonValue(name, value)) { 4560b57cec5SDimitry Andric if (name.equals("name")) { 457349cc55cSDimitry Andric reg_info.name.SetString(value); 4580b57cec5SDimitry Andric } else if (name.equals("alt-name")) { 459349cc55cSDimitry Andric reg_info.alt_name.SetString(value); 4600b57cec5SDimitry Andric } else if (name.equals("bitsize")) { 461349cc55cSDimitry Andric if (!value.getAsInteger(0, reg_info.byte_size)) 4620b57cec5SDimitry Andric reg_info.byte_size /= CHAR_BIT; 4630b57cec5SDimitry Andric } else if (name.equals("offset")) { 464349cc55cSDimitry Andric value.getAsInteger(0, reg_info.byte_offset); 4650b57cec5SDimitry Andric } else if (name.equals("encoding")) { 4660b57cec5SDimitry Andric const Encoding encoding = Args::StringToEncoding(value); 4670b57cec5SDimitry Andric if (encoding != eEncodingInvalid) 4680b57cec5SDimitry Andric reg_info.encoding = encoding; 4690b57cec5SDimitry Andric } else if (name.equals("format")) { 470349cc55cSDimitry Andric if (!OptionArgParser::ToFormat(value.str().c_str(), reg_info.format, nullptr) 4710b57cec5SDimitry Andric .Success()) 4720b57cec5SDimitry Andric reg_info.format = 4730b57cec5SDimitry Andric llvm::StringSwitch<Format>(value) 4740b57cec5SDimitry Andric .Case("binary", eFormatBinary) 4750b57cec5SDimitry Andric .Case("decimal", eFormatDecimal) 4760b57cec5SDimitry Andric .Case("hex", eFormatHex) 4770b57cec5SDimitry Andric .Case("float", eFormatFloat) 4780b57cec5SDimitry Andric .Case("vector-sint8", eFormatVectorOfSInt8) 4790b57cec5SDimitry Andric .Case("vector-uint8", eFormatVectorOfUInt8) 4800b57cec5SDimitry Andric .Case("vector-sint16", eFormatVectorOfSInt16) 4810b57cec5SDimitry Andric .Case("vector-uint16", eFormatVectorOfUInt16) 4820b57cec5SDimitry Andric .Case("vector-sint32", eFormatVectorOfSInt32) 4830b57cec5SDimitry Andric .Case("vector-uint32", eFormatVectorOfUInt32) 4840b57cec5SDimitry Andric .Case("vector-float32", eFormatVectorOfFloat32) 4850b57cec5SDimitry Andric .Case("vector-uint64", eFormatVectorOfUInt64) 4860b57cec5SDimitry Andric .Case("vector-uint128", eFormatVectorOfUInt128) 4870b57cec5SDimitry Andric .Default(eFormatInvalid); 4880b57cec5SDimitry Andric } else if (name.equals("set")) { 489349cc55cSDimitry Andric reg_info.set_name.SetString(value); 4900b57cec5SDimitry Andric } else if (name.equals("gcc") || name.equals("ehframe")) { 491349cc55cSDimitry Andric value.getAsInteger(0, reg_info.regnum_ehframe); 4920b57cec5SDimitry Andric } else if (name.equals("dwarf")) { 493349cc55cSDimitry Andric value.getAsInteger(0, reg_info.regnum_dwarf); 4940b57cec5SDimitry Andric } else if (name.equals("generic")) { 495349cc55cSDimitry Andric reg_info.regnum_generic = Args::StringToGenericRegister(value); 4960b57cec5SDimitry Andric } else if (name.equals("container-regs")) { 497349cc55cSDimitry Andric SplitCommaSeparatedRegisterNumberString(value, reg_info.value_regs, 16); 4980b57cec5SDimitry Andric } else if (name.equals("invalidate-regs")) { 499349cc55cSDimitry Andric SplitCommaSeparatedRegisterNumberString(value, reg_info.invalidate_regs, 16); 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric assert(reg_info.byte_size != 0); 504349cc55cSDimitry Andric registers.push_back(reg_info); 5050b57cec5SDimitry Andric } else { 5060b57cec5SDimitry Andric break; // ensure exit before reg_num is incremented 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric } else { 5090b57cec5SDimitry Andric break; 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 513349cc55cSDimitry Andric AddRemoteRegisters(registers, arch_to_use); 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric Status ProcessGDBRemote::WillLaunch(lldb_private::Module *module) { 5170b57cec5SDimitry Andric return WillLaunchOrAttach(); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric Status ProcessGDBRemote::WillAttachToProcessWithID(lldb::pid_t pid) { 5210b57cec5SDimitry Andric return WillLaunchOrAttach(); 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric Status ProcessGDBRemote::WillAttachToProcessWithName(const char *process_name, 5250b57cec5SDimitry Andric bool wait_for_launch) { 5260b57cec5SDimitry Andric return WillLaunchOrAttach(); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5295ffd83dbSDimitry Andric Status ProcessGDBRemote::DoConnectRemote(llvm::StringRef remote_url) { 5300b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 531*0eae32dcSDimitry Andric 5320b57cec5SDimitry Andric Status error(WillLaunchOrAttach()); 5330b57cec5SDimitry Andric if (error.Fail()) 5340b57cec5SDimitry Andric return error; 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric error = ConnectToDebugserver(remote_url); 5370b57cec5SDimitry Andric if (error.Fail()) 5380b57cec5SDimitry Andric return error; 539*0eae32dcSDimitry Andric 5400b57cec5SDimitry Andric StartAsyncThread(); 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); 5430b57cec5SDimitry Andric if (pid == LLDB_INVALID_PROCESS_ID) { 5440b57cec5SDimitry Andric // We don't have a valid process ID, so note that we are connected and 5450b57cec5SDimitry Andric // could now request to launch or attach, or get remote process listings... 5460b57cec5SDimitry Andric SetPrivateState(eStateConnected); 5470b57cec5SDimitry Andric } else { 5480b57cec5SDimitry Andric // We have a valid process 5490b57cec5SDimitry Andric SetID(pid); 5500b57cec5SDimitry Andric GetThreadList(); 5510b57cec5SDimitry Andric StringExtractorGDBRemote response; 5520b57cec5SDimitry Andric if (m_gdb_comm.GetStopReply(response)) { 5530b57cec5SDimitry Andric SetLastStopPacket(response); 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric Target &target = GetTarget(); 5560b57cec5SDimitry Andric if (!target.GetArchitecture().IsValid()) { 5570b57cec5SDimitry Andric if (m_gdb_comm.GetProcessArchitecture().IsValid()) { 5580b57cec5SDimitry Andric target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); 5590b57cec5SDimitry Andric } else { 5600b57cec5SDimitry Andric if (m_gdb_comm.GetHostArchitecture().IsValid()) { 5610b57cec5SDimitry Andric target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 566*0eae32dcSDimitry Andric // The remote stub may know about the "main binary" in 567*0eae32dcSDimitry Andric // the context of a firmware debug session, and can 568*0eae32dcSDimitry Andric // give us a UUID and an address/slide of where the 569*0eae32dcSDimitry Andric // binary is loaded in memory. 570*0eae32dcSDimitry Andric UUID standalone_uuid; 571*0eae32dcSDimitry Andric addr_t standalone_value; 572*0eae32dcSDimitry Andric bool standalone_value_is_offset; 573*0eae32dcSDimitry Andric if (m_gdb_comm.GetProcessStandaloneBinary( 574*0eae32dcSDimitry Andric standalone_uuid, standalone_value, standalone_value_is_offset)) { 575*0eae32dcSDimitry Andric ModuleSP module_sp; 576*0eae32dcSDimitry Andric 577*0eae32dcSDimitry Andric if (standalone_uuid.IsValid()) { 578*0eae32dcSDimitry Andric ModuleSpec module_spec; 579*0eae32dcSDimitry Andric module_spec.GetUUID() = standalone_uuid; 580*0eae32dcSDimitry Andric 581*0eae32dcSDimitry Andric // Look up UUID in global module cache before attempting 582*0eae32dcSDimitry Andric // a more expensive search. 583*0eae32dcSDimitry Andric Status error = ModuleList::GetSharedModule(module_spec, module_sp, 584*0eae32dcSDimitry Andric nullptr, nullptr, nullptr); 585*0eae32dcSDimitry Andric 586*0eae32dcSDimitry Andric if (!module_sp) { 587*0eae32dcSDimitry Andric // Force a an external lookup, if that tool is available. 588*0eae32dcSDimitry Andric if (!module_spec.GetSymbolFileSpec()) 589*0eae32dcSDimitry Andric Symbols::DownloadObjectAndSymbolFile(module_spec, true); 590*0eae32dcSDimitry Andric 591*0eae32dcSDimitry Andric if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { 592*0eae32dcSDimitry Andric module_sp = std::make_shared<Module>(module_spec); 593*0eae32dcSDimitry Andric } 594*0eae32dcSDimitry Andric } 595*0eae32dcSDimitry Andric 596*0eae32dcSDimitry Andric // If we couldn't find the binary anywhere else, as a last resort, 597*0eae32dcSDimitry Andric // read it out of memory. 598*0eae32dcSDimitry Andric if (!module_sp.get() && standalone_value != LLDB_INVALID_ADDRESS && 599*0eae32dcSDimitry Andric !standalone_value_is_offset) { 600*0eae32dcSDimitry Andric char namebuf[80]; 601*0eae32dcSDimitry Andric snprintf(namebuf, sizeof(namebuf), "mem-image-0x%" PRIx64, 602*0eae32dcSDimitry Andric standalone_value); 603*0eae32dcSDimitry Andric module_sp = 604*0eae32dcSDimitry Andric ReadModuleFromMemory(FileSpec(namebuf), standalone_value); 605*0eae32dcSDimitry Andric } 606*0eae32dcSDimitry Andric 607*0eae32dcSDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet( 608*0eae32dcSDimitry Andric LIBLLDB_LOG_DYNAMIC_LOADER)); 609*0eae32dcSDimitry Andric if (module_sp.get()) { 610*0eae32dcSDimitry Andric target.GetImages().AppendIfNeeded(module_sp, false); 611*0eae32dcSDimitry Andric 612*0eae32dcSDimitry Andric bool changed = false; 613*0eae32dcSDimitry Andric if (module_sp->GetObjectFile()) { 614*0eae32dcSDimitry Andric if (standalone_value != LLDB_INVALID_ADDRESS) { 615*0eae32dcSDimitry Andric if (log) 616*0eae32dcSDimitry Andric log->Printf("Loading binary UUID %s at %s 0x%" PRIx64, 617*0eae32dcSDimitry Andric standalone_uuid.GetAsString().c_str(), 618*0eae32dcSDimitry Andric standalone_value_is_offset ? "offset" : "address", 619*0eae32dcSDimitry Andric standalone_value); 620*0eae32dcSDimitry Andric module_sp->SetLoadAddress(target, standalone_value, 621*0eae32dcSDimitry Andric standalone_value_is_offset, changed); 622*0eae32dcSDimitry Andric } else { 623*0eae32dcSDimitry Andric // No address/offset/slide, load the binary at file address, 624*0eae32dcSDimitry Andric // offset 0. 625*0eae32dcSDimitry Andric if (log) 626*0eae32dcSDimitry Andric log->Printf("Loading binary UUID %s at file address", 627*0eae32dcSDimitry Andric standalone_uuid.GetAsString().c_str()); 628*0eae32dcSDimitry Andric const bool value_is_slide = true; 629*0eae32dcSDimitry Andric module_sp->SetLoadAddress(target, 0, value_is_slide, changed); 630*0eae32dcSDimitry Andric } 631*0eae32dcSDimitry Andric } else { 632*0eae32dcSDimitry Andric // In-memory image, load at its true address, offset 0. 633*0eae32dcSDimitry Andric if (log) 634*0eae32dcSDimitry Andric log->Printf("Loading binary UUID %s from memory", 635*0eae32dcSDimitry Andric standalone_uuid.GetAsString().c_str()); 636*0eae32dcSDimitry Andric const bool value_is_slide = true; 637*0eae32dcSDimitry Andric module_sp->SetLoadAddress(target, 0, value_is_slide, changed); 638*0eae32dcSDimitry Andric } 639*0eae32dcSDimitry Andric 640*0eae32dcSDimitry Andric ModuleList added_module; 641*0eae32dcSDimitry Andric added_module.Append(module_sp, false); 642*0eae32dcSDimitry Andric target.ModulesDidLoad(added_module); 643*0eae32dcSDimitry Andric } else { 644*0eae32dcSDimitry Andric if (log) 645*0eae32dcSDimitry Andric log->Printf("Unable to find binary with UUID %s and load it at " 646*0eae32dcSDimitry Andric "%s 0x%" PRIx64, 647*0eae32dcSDimitry Andric standalone_uuid.GetAsString().c_str(), 648*0eae32dcSDimitry Andric standalone_value_is_offset ? "offset" : "address", 649*0eae32dcSDimitry Andric standalone_value); 650*0eae32dcSDimitry Andric } 651*0eae32dcSDimitry Andric } 652*0eae32dcSDimitry Andric } 653*0eae32dcSDimitry Andric 6540b57cec5SDimitry Andric const StateType state = SetThreadStopInfo(response); 6550b57cec5SDimitry Andric if (state != eStateInvalid) { 6560b57cec5SDimitry Andric SetPrivateState(state); 6570b57cec5SDimitry Andric } else 6580b57cec5SDimitry Andric error.SetErrorStringWithFormat( 6590b57cec5SDimitry Andric "Process %" PRIu64 " was reported after connecting to " 6600b57cec5SDimitry Andric "'%s', but state was not stopped: %s", 6610b57cec5SDimitry Andric pid, remote_url.str().c_str(), StateAsCString(state)); 6620b57cec5SDimitry Andric } else 6630b57cec5SDimitry Andric error.SetErrorStringWithFormat("Process %" PRIu64 6640b57cec5SDimitry Andric " was reported after connecting to '%s', " 6650b57cec5SDimitry Andric "but no stop reply packet was received", 6660b57cec5SDimitry Andric pid, remote_url.str().c_str()); 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric 6699dba64beSDimitry Andric LLDB_LOGF(log, 6709dba64beSDimitry Andric "ProcessGDBRemote::%s pid %" PRIu64 6710b57cec5SDimitry Andric ": normalizing target architecture initial triple: %s " 6720b57cec5SDimitry Andric "(GetTarget().GetArchitecture().IsValid() %s, " 6730b57cec5SDimitry Andric "m_gdb_comm.GetHostArchitecture().IsValid(): %s)", 6740b57cec5SDimitry Andric __FUNCTION__, GetID(), 6750b57cec5SDimitry Andric GetTarget().GetArchitecture().GetTriple().getTriple().c_str(), 6760b57cec5SDimitry Andric GetTarget().GetArchitecture().IsValid() ? "true" : "false", 6770b57cec5SDimitry Andric m_gdb_comm.GetHostArchitecture().IsValid() ? "true" : "false"); 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric if (error.Success() && !GetTarget().GetArchitecture().IsValid() && 6800b57cec5SDimitry Andric m_gdb_comm.GetHostArchitecture().IsValid()) { 6810b57cec5SDimitry Andric // Prefer the *process'* architecture over that of the *host*, if 6820b57cec5SDimitry Andric // available. 6830b57cec5SDimitry Andric if (m_gdb_comm.GetProcessArchitecture().IsValid()) 6840b57cec5SDimitry Andric GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); 6850b57cec5SDimitry Andric else 6860b57cec5SDimitry Andric GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6899dba64beSDimitry Andric LLDB_LOGF(log, 6909dba64beSDimitry Andric "ProcessGDBRemote::%s pid %" PRIu64 6910b57cec5SDimitry Andric ": normalized target architecture triple: %s", 6920b57cec5SDimitry Andric __FUNCTION__, GetID(), 6930b57cec5SDimitry Andric GetTarget().GetArchitecture().GetTriple().getTriple().c_str()); 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric return error; 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric Status ProcessGDBRemote::WillLaunchOrAttach() { 6990b57cec5SDimitry Andric Status error; 7000b57cec5SDimitry Andric m_stdio_communication.Clear(); 7010b57cec5SDimitry Andric return error; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric // Process Control 7050b57cec5SDimitry Andric Status ProcessGDBRemote::DoLaunch(lldb_private::Module *exe_module, 7060b57cec5SDimitry Andric ProcessLaunchInfo &launch_info) { 7070b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 7080b57cec5SDimitry Andric Status error; 7090b57cec5SDimitry Andric 7109dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s() entered", __FUNCTION__); 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric uint32_t launch_flags = launch_info.GetFlags().Get(); 7130b57cec5SDimitry Andric FileSpec stdin_file_spec{}; 7140b57cec5SDimitry Andric FileSpec stdout_file_spec{}; 7150b57cec5SDimitry Andric FileSpec stderr_file_spec{}; 7160b57cec5SDimitry Andric FileSpec working_dir = launch_info.GetWorkingDirectory(); 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric const FileAction *file_action; 7190b57cec5SDimitry Andric file_action = launch_info.GetFileActionForFD(STDIN_FILENO); 7200b57cec5SDimitry Andric if (file_action) { 7210b57cec5SDimitry Andric if (file_action->GetAction() == FileAction::eFileActionOpen) 7220b57cec5SDimitry Andric stdin_file_spec = file_action->GetFileSpec(); 7230b57cec5SDimitry Andric } 7240b57cec5SDimitry Andric file_action = launch_info.GetFileActionForFD(STDOUT_FILENO); 7250b57cec5SDimitry Andric if (file_action) { 7260b57cec5SDimitry Andric if (file_action->GetAction() == FileAction::eFileActionOpen) 7270b57cec5SDimitry Andric stdout_file_spec = file_action->GetFileSpec(); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric file_action = launch_info.GetFileActionForFD(STDERR_FILENO); 7300b57cec5SDimitry Andric if (file_action) { 7310b57cec5SDimitry Andric if (file_action->GetAction() == FileAction::eFileActionOpen) 7320b57cec5SDimitry Andric stderr_file_spec = file_action->GetFileSpec(); 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric if (log) { 7360b57cec5SDimitry Andric if (stdin_file_spec || stdout_file_spec || stderr_file_spec) 7379dba64beSDimitry Andric LLDB_LOGF(log, 7389dba64beSDimitry Andric "ProcessGDBRemote::%s provided with STDIO paths via " 7390b57cec5SDimitry Andric "launch_info: stdin=%s, stdout=%s, stderr=%s", 7400b57cec5SDimitry Andric __FUNCTION__, 7410b57cec5SDimitry Andric stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", 7420b57cec5SDimitry Andric stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", 7430b57cec5SDimitry Andric stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); 7440b57cec5SDimitry Andric else 7459dba64beSDimitry Andric LLDB_LOGF(log, 7469dba64beSDimitry Andric "ProcessGDBRemote::%s no STDIO paths given via launch_info", 7470b57cec5SDimitry Andric __FUNCTION__); 7480b57cec5SDimitry Andric } 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; 7510b57cec5SDimitry Andric if (stdin_file_spec || disable_stdio) { 7520b57cec5SDimitry Andric // the inferior will be reading stdin from the specified file or stdio is 7530b57cec5SDimitry Andric // completely disabled 7540b57cec5SDimitry Andric m_stdin_forward = false; 7550b57cec5SDimitry Andric } else { 7560b57cec5SDimitry Andric m_stdin_forward = true; 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // ::LogSetBitMask (GDBR_LOG_DEFAULT); 7600b57cec5SDimitry Andric // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | 7610b57cec5SDimitry Andric // LLDB_LOG_OPTION_PREPEND_TIMESTAMP | 7620b57cec5SDimitry Andric // LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); 7630b57cec5SDimitry Andric // ::LogSetLogFile ("/dev/stdout"); 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric error = EstablishConnectionIfNeeded(launch_info); 7660b57cec5SDimitry Andric if (error.Success()) { 7670b57cec5SDimitry Andric PseudoTerminal pty; 7680b57cec5SDimitry Andric const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric PlatformSP platform_sp(GetTarget().GetPlatform()); 7710b57cec5SDimitry Andric if (disable_stdio) { 7720b57cec5SDimitry Andric // set to /dev/null unless redirected to a file above 7730b57cec5SDimitry Andric if (!stdin_file_spec) 7740b57cec5SDimitry Andric stdin_file_spec.SetFile(FileSystem::DEV_NULL, 7750b57cec5SDimitry Andric FileSpec::Style::native); 7760b57cec5SDimitry Andric if (!stdout_file_spec) 7770b57cec5SDimitry Andric stdout_file_spec.SetFile(FileSystem::DEV_NULL, 7780b57cec5SDimitry Andric FileSpec::Style::native); 7790b57cec5SDimitry Andric if (!stderr_file_spec) 7800b57cec5SDimitry Andric stderr_file_spec.SetFile(FileSystem::DEV_NULL, 7810b57cec5SDimitry Andric FileSpec::Style::native); 7820b57cec5SDimitry Andric } else if (platform_sp && platform_sp->IsHost()) { 7830b57cec5SDimitry Andric // If the debugserver is local and we aren't disabling STDIO, lets use 7840b57cec5SDimitry Andric // a pseudo terminal to instead of relying on the 'O' packets for stdio 7850b57cec5SDimitry Andric // since 'O' packets can really slow down debugging if the inferior 7860b57cec5SDimitry Andric // does a lot of output. 7870b57cec5SDimitry Andric if ((!stdin_file_spec || !stdout_file_spec || !stderr_file_spec) && 788e8d8bef9SDimitry Andric !errorToBool(pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY))) { 789e8d8bef9SDimitry Andric FileSpec secondary_name(pty.GetSecondaryName()); 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric if (!stdin_file_spec) 7925ffd83dbSDimitry Andric stdin_file_spec = secondary_name; 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric if (!stdout_file_spec) 7955ffd83dbSDimitry Andric stdout_file_spec = secondary_name; 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric if (!stderr_file_spec) 7985ffd83dbSDimitry Andric stderr_file_spec = secondary_name; 7990b57cec5SDimitry Andric } 8009dba64beSDimitry Andric LLDB_LOGF( 8019dba64beSDimitry Andric log, 8020b57cec5SDimitry Andric "ProcessGDBRemote::%s adjusted STDIO paths for local platform " 8035ffd83dbSDimitry Andric "(IsHost() is true) using secondary: stdin=%s, stdout=%s, " 8045ffd83dbSDimitry Andric "stderr=%s", 8050b57cec5SDimitry Andric __FUNCTION__, 8060b57cec5SDimitry Andric stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", 8070b57cec5SDimitry Andric stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", 8080b57cec5SDimitry Andric stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric 8119dba64beSDimitry Andric LLDB_LOGF(log, 8129dba64beSDimitry Andric "ProcessGDBRemote::%s final STDIO paths after all " 8130b57cec5SDimitry Andric "adjustments: stdin=%s, stdout=%s, stderr=%s", 8140b57cec5SDimitry Andric __FUNCTION__, 8150b57cec5SDimitry Andric stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", 8160b57cec5SDimitry Andric stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", 8179dba64beSDimitry Andric stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric if (stdin_file_spec) 8200b57cec5SDimitry Andric m_gdb_comm.SetSTDIN(stdin_file_spec); 8210b57cec5SDimitry Andric if (stdout_file_spec) 8220b57cec5SDimitry Andric m_gdb_comm.SetSTDOUT(stdout_file_spec); 8230b57cec5SDimitry Andric if (stderr_file_spec) 8240b57cec5SDimitry Andric m_gdb_comm.SetSTDERR(stderr_file_spec); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric m_gdb_comm.SetDisableASLR(launch_flags & eLaunchFlagDisableASLR); 8270b57cec5SDimitry Andric m_gdb_comm.SetDetachOnError(launch_flags & eLaunchFlagDetachOnError); 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric m_gdb_comm.SendLaunchArchPacket( 8300b57cec5SDimitry Andric GetTarget().GetArchitecture().GetArchitectureName()); 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric const char *launch_event_data = launch_info.GetLaunchEventData(); 8330b57cec5SDimitry Andric if (launch_event_data != nullptr && *launch_event_data != '\0') 8340b57cec5SDimitry Andric m_gdb_comm.SendLaunchEventDataPacket(launch_event_data); 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric if (working_dir) { 8370b57cec5SDimitry Andric m_gdb_comm.SetWorkingDir(working_dir); 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric // Send the environment and the program + arguments after we connect 8410b57cec5SDimitry Andric m_gdb_comm.SendEnvironment(launch_info.GetEnvironment()); 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric { 8440b57cec5SDimitry Andric // Scope for the scoped timeout object 8450b57cec5SDimitry Andric GDBRemoteCommunication::ScopedTimeout timeout(m_gdb_comm, 8460b57cec5SDimitry Andric std::chrono::seconds(10)); 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric int arg_packet_err = m_gdb_comm.SendArgumentsPacket(launch_info); 8490b57cec5SDimitry Andric if (arg_packet_err == 0) { 8500b57cec5SDimitry Andric std::string error_str; 8510b57cec5SDimitry Andric if (m_gdb_comm.GetLaunchSuccess(error_str)) { 8520b57cec5SDimitry Andric SetID(m_gdb_comm.GetCurrentProcessID()); 8530b57cec5SDimitry Andric } else { 8540b57cec5SDimitry Andric error.SetErrorString(error_str.c_str()); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric } else { 8570b57cec5SDimitry Andric error.SetErrorStringWithFormat("'A' packet returned an error: %i", 8580b57cec5SDimitry Andric arg_packet_err); 8590b57cec5SDimitry Andric } 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric if (GetID() == LLDB_INVALID_PROCESS_ID) { 8639dba64beSDimitry Andric LLDB_LOGF(log, "failed to connect to debugserver: %s", 8640b57cec5SDimitry Andric error.AsCString()); 8650b57cec5SDimitry Andric KillDebugserverProcess(); 8660b57cec5SDimitry Andric return error; 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric StringExtractorGDBRemote response; 8700b57cec5SDimitry Andric if (m_gdb_comm.GetStopReply(response)) { 8710b57cec5SDimitry Andric SetLastStopPacket(response); 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture(); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric if (process_arch.IsValid()) { 8760b57cec5SDimitry Andric GetTarget().MergeArchitecture(process_arch); 8770b57cec5SDimitry Andric } else { 8780b57cec5SDimitry Andric const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture(); 8790b57cec5SDimitry Andric if (host_arch.IsValid()) 8800b57cec5SDimitry Andric GetTarget().MergeArchitecture(host_arch); 8810b57cec5SDimitry Andric } 8820b57cec5SDimitry Andric 8830b57cec5SDimitry Andric SetPrivateState(SetThreadStopInfo(response)); 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric if (!disable_stdio) { 8865ffd83dbSDimitry Andric if (pty.GetPrimaryFileDescriptor() != PseudoTerminal::invalid_fd) 8875ffd83dbSDimitry Andric SetSTDIOFileDescriptor(pty.ReleasePrimaryFileDescriptor()); 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric } else { 8919dba64beSDimitry Andric LLDB_LOGF(log, "failed to connect to debugserver: %s", error.AsCString()); 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric return error; 8940b57cec5SDimitry Andric } 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric Status ProcessGDBRemote::ConnectToDebugserver(llvm::StringRef connect_url) { 8970b57cec5SDimitry Andric Status error; 8980b57cec5SDimitry Andric // Only connect if we have a valid connect URL 8990b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric if (!connect_url.empty()) { 9029dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, 9030b57cec5SDimitry Andric connect_url.str().c_str()); 9040b57cec5SDimitry Andric std::unique_ptr<ConnectionFileDescriptor> conn_up( 9050b57cec5SDimitry Andric new ConnectionFileDescriptor()); 9060b57cec5SDimitry Andric if (conn_up) { 9070b57cec5SDimitry Andric const uint32_t max_retry_count = 50; 9080b57cec5SDimitry Andric uint32_t retry_count = 0; 9090b57cec5SDimitry Andric while (!m_gdb_comm.IsConnected()) { 9100b57cec5SDimitry Andric if (conn_up->Connect(connect_url, &error) == eConnectionStatusSuccess) { 9115ffd83dbSDimitry Andric m_gdb_comm.SetConnection(std::move(conn_up)); 9120b57cec5SDimitry Andric break; 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric retry_count++; 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric if (retry_count >= max_retry_count) 9180b57cec5SDimitry Andric break; 9190b57cec5SDimitry Andric 9209dba64beSDimitry Andric std::this_thread::sleep_for(std::chrono::milliseconds(100)); 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric if (!m_gdb_comm.IsConnected()) { 9260b57cec5SDimitry Andric if (error.Success()) 9270b57cec5SDimitry Andric error.SetErrorString("not connected to remote gdb server"); 9280b57cec5SDimitry Andric return error; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric // We always seem to be able to open a connection to a local port so we need 9320b57cec5SDimitry Andric // to make sure we can then send data to it. If we can't then we aren't 9330b57cec5SDimitry Andric // actually connected to anything, so try and do the handshake with the 9340b57cec5SDimitry Andric // remote GDB server and make sure that goes alright. 9350b57cec5SDimitry Andric if (!m_gdb_comm.HandshakeWithServer(&error)) { 9360b57cec5SDimitry Andric m_gdb_comm.Disconnect(); 9370b57cec5SDimitry Andric if (error.Success()) 9380b57cec5SDimitry Andric error.SetErrorString("not connected to remote gdb server"); 9390b57cec5SDimitry Andric return error; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric m_gdb_comm.GetEchoSupported(); 9430b57cec5SDimitry Andric m_gdb_comm.GetThreadSuffixSupported(); 9440b57cec5SDimitry Andric m_gdb_comm.GetListThreadsInStopReplySupported(); 9450b57cec5SDimitry Andric m_gdb_comm.GetHostInfo(); 9460b57cec5SDimitry Andric m_gdb_comm.GetVContSupported('c'); 9470b57cec5SDimitry Andric m_gdb_comm.GetVAttachOrWaitSupported(); 9480b57cec5SDimitry Andric m_gdb_comm.EnableErrorStringInPacket(); 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); 9510b57cec5SDimitry Andric for (size_t idx = 0; idx < num_cmds; idx++) { 9520b57cec5SDimitry Andric StringExtractorGDBRemote response; 9530b57cec5SDimitry Andric m_gdb_comm.SendPacketAndWaitForResponse( 954fe6060f1SDimitry Andric GetExtraStartupCommands().GetArgumentAtIndex(idx), response); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric return error; 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric void ProcessGDBRemote::DidLaunchOrAttach(ArchSpec &process_arch) { 9600b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 9610b57cec5SDimitry Andric BuildDynamicRegisterInfo(false); 9620b57cec5SDimitry Andric 9635ffd83dbSDimitry Andric // See if the GDB server supports qHostInfo or qProcessInfo packets. Prefer 9645ffd83dbSDimitry Andric // qProcessInfo as it will be more specific to our process. 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); 9670b57cec5SDimitry Andric if (remote_process_arch.IsValid()) { 9680b57cec5SDimitry Andric process_arch = remote_process_arch; 9695ffd83dbSDimitry Andric LLDB_LOG(log, "gdb-remote had process architecture, using {0} {1}", 9705ffd83dbSDimitry Andric process_arch.GetArchitectureName(), 9715ffd83dbSDimitry Andric process_arch.GetTriple().getTriple()); 9720b57cec5SDimitry Andric } else { 9730b57cec5SDimitry Andric process_arch = m_gdb_comm.GetHostArchitecture(); 9745ffd83dbSDimitry Andric LLDB_LOG(log, 9755ffd83dbSDimitry Andric "gdb-remote did not have process architecture, using gdb-remote " 9765ffd83dbSDimitry Andric "host architecture {0} {1}", 9775ffd83dbSDimitry Andric process_arch.GetArchitectureName(), 9785ffd83dbSDimitry Andric process_arch.GetTriple().getTriple()); 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric 981fe6060f1SDimitry Andric if (int addresssable_bits = m_gdb_comm.GetAddressingBits()) { 982fe6060f1SDimitry Andric lldb::addr_t address_mask = ~((1ULL << addresssable_bits) - 1); 983fe6060f1SDimitry Andric SetCodeAddressMask(address_mask); 984fe6060f1SDimitry Andric SetDataAddressMask(address_mask); 985fe6060f1SDimitry Andric } 986fe6060f1SDimitry Andric 9870b57cec5SDimitry Andric if (process_arch.IsValid()) { 9880b57cec5SDimitry Andric const ArchSpec &target_arch = GetTarget().GetArchitecture(); 9890b57cec5SDimitry Andric if (target_arch.IsValid()) { 9905ffd83dbSDimitry Andric LLDB_LOG(log, "analyzing target arch, currently {0} {1}", 9915ffd83dbSDimitry Andric target_arch.GetArchitectureName(), 9925ffd83dbSDimitry Andric target_arch.GetTriple().getTriple()); 9930b57cec5SDimitry Andric 9940b57cec5SDimitry Andric // If the remote host is ARM and we have apple as the vendor, then 9950b57cec5SDimitry Andric // ARM executables and shared libraries can have mixed ARM 9960b57cec5SDimitry Andric // architectures. 9970b57cec5SDimitry Andric // You can have an armv6 executable, and if the host is armv7, then the 9980b57cec5SDimitry Andric // system will load the best possible architecture for all shared 9990b57cec5SDimitry Andric // libraries it has, so we really need to take the remote host 10000b57cec5SDimitry Andric // architecture as our defacto architecture in this case. 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric if ((process_arch.GetMachine() == llvm::Triple::arm || 10030b57cec5SDimitry Andric process_arch.GetMachine() == llvm::Triple::thumb) && 10040b57cec5SDimitry Andric process_arch.GetTriple().getVendor() == llvm::Triple::Apple) { 10050b57cec5SDimitry Andric GetTarget().SetArchitecture(process_arch); 10065ffd83dbSDimitry Andric LLDB_LOG(log, 10075ffd83dbSDimitry Andric "remote process is ARM/Apple, " 10085ffd83dbSDimitry Andric "setting target arch to {0} {1}", 10095ffd83dbSDimitry Andric process_arch.GetArchitectureName(), 10105ffd83dbSDimitry Andric process_arch.GetTriple().getTriple()); 10110b57cec5SDimitry Andric } else { 10120b57cec5SDimitry Andric // Fill in what is missing in the triple 10130b57cec5SDimitry Andric const llvm::Triple &remote_triple = process_arch.GetTriple(); 10140b57cec5SDimitry Andric llvm::Triple new_target_triple = target_arch.GetTriple(); 10150b57cec5SDimitry Andric if (new_target_triple.getVendorName().size() == 0) { 10160b57cec5SDimitry Andric new_target_triple.setVendor(remote_triple.getVendor()); 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric if (new_target_triple.getOSName().size() == 0) { 10190b57cec5SDimitry Andric new_target_triple.setOS(remote_triple.getOS()); 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric if (new_target_triple.getEnvironmentName().size() == 0) 10225ffd83dbSDimitry Andric new_target_triple.setEnvironment(remote_triple.getEnvironment()); 10230b57cec5SDimitry Andric } 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric ArchSpec new_target_arch = target_arch; 10260b57cec5SDimitry Andric new_target_arch.SetTriple(new_target_triple); 10270b57cec5SDimitry Andric GetTarget().SetArchitecture(new_target_arch); 10280b57cec5SDimitry Andric } 10290b57cec5SDimitry Andric } 10300b57cec5SDimitry Andric 10315ffd83dbSDimitry Andric LLDB_LOG(log, 10325ffd83dbSDimitry Andric "final target arch after adjustments for remote architecture: " 10335ffd83dbSDimitry Andric "{0} {1}", 10345ffd83dbSDimitry Andric target_arch.GetArchitectureName(), 10355ffd83dbSDimitry Andric target_arch.GetTriple().getTriple()); 10360b57cec5SDimitry Andric } else { 10370b57cec5SDimitry Andric // The target doesn't have a valid architecture yet, set it from the 10380b57cec5SDimitry Andric // architecture we got from the remote GDB server 10390b57cec5SDimitry Andric GetTarget().SetArchitecture(process_arch); 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric 10435ffd83dbSDimitry Andric MaybeLoadExecutableModule(); 10445ffd83dbSDimitry Andric 10450b57cec5SDimitry Andric // Find out which StructuredDataPlugins are supported by the debug monitor. 10460b57cec5SDimitry Andric // These plugins transmit data over async $J packets. 10475ffd83dbSDimitry Andric if (StructuredData::Array *supported_packets = 10485ffd83dbSDimitry Andric m_gdb_comm.GetSupportedStructuredDataPlugins()) 10495ffd83dbSDimitry Andric MapSupportedStructuredDataPlugins(*supported_packets); 1050349cc55cSDimitry Andric 1051349cc55cSDimitry Andric // If connected to LLDB ("native-signals+"), use signal defs for 1052349cc55cSDimitry Andric // the remote platform. If connected to GDB, just use the standard set. 1053349cc55cSDimitry Andric if (!m_gdb_comm.UsesNativeSignals()) { 1054349cc55cSDimitry Andric SetUnixSignals(std::make_shared<GDBRemoteSignals>()); 1055349cc55cSDimitry Andric } else { 1056349cc55cSDimitry Andric PlatformSP platform_sp = GetTarget().GetPlatform(); 1057349cc55cSDimitry Andric if (platform_sp && platform_sp->IsConnected()) 1058349cc55cSDimitry Andric SetUnixSignals(platform_sp->GetUnixSignals()); 1059349cc55cSDimitry Andric else 1060349cc55cSDimitry Andric SetUnixSignals(UnixSignals::Create(GetTarget().GetArchitecture())); 1061349cc55cSDimitry Andric } 10625ffd83dbSDimitry Andric } 10635ffd83dbSDimitry Andric 10645ffd83dbSDimitry Andric void ProcessGDBRemote::MaybeLoadExecutableModule() { 10655ffd83dbSDimitry Andric ModuleSP module_sp = GetTarget().GetExecutableModule(); 10665ffd83dbSDimitry Andric if (!module_sp) 10675ffd83dbSDimitry Andric return; 10685ffd83dbSDimitry Andric 10695ffd83dbSDimitry Andric llvm::Optional<QOffsets> offsets = m_gdb_comm.GetQOffsets(); 10705ffd83dbSDimitry Andric if (!offsets) 10715ffd83dbSDimitry Andric return; 10725ffd83dbSDimitry Andric 10735ffd83dbSDimitry Andric bool is_uniform = 10745ffd83dbSDimitry Andric size_t(llvm::count(offsets->offsets, offsets->offsets[0])) == 10755ffd83dbSDimitry Andric offsets->offsets.size(); 10765ffd83dbSDimitry Andric if (!is_uniform) 10775ffd83dbSDimitry Andric return; // TODO: Handle non-uniform responses. 10785ffd83dbSDimitry Andric 10795ffd83dbSDimitry Andric bool changed = false; 10805ffd83dbSDimitry Andric module_sp->SetLoadAddress(GetTarget(), offsets->offsets[0], 10815ffd83dbSDimitry Andric /*value_is_offset=*/true, changed); 10825ffd83dbSDimitry Andric if (changed) { 10835ffd83dbSDimitry Andric ModuleList list; 10845ffd83dbSDimitry Andric list.Append(module_sp); 10855ffd83dbSDimitry Andric m_process->GetTarget().ModulesDidLoad(list); 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric } 10880b57cec5SDimitry Andric 10890b57cec5SDimitry Andric void ProcessGDBRemote::DidLaunch() { 10900b57cec5SDimitry Andric ArchSpec process_arch; 10910b57cec5SDimitry Andric DidLaunchOrAttach(process_arch); 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric Status ProcessGDBRemote::DoAttachToProcessWithID( 10950b57cec5SDimitry Andric lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) { 10960b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 10970b57cec5SDimitry Andric Status error; 10980b57cec5SDimitry Andric 10999dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s()", __FUNCTION__); 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric // Clear out and clean up from any current state 11020b57cec5SDimitry Andric Clear(); 11030b57cec5SDimitry Andric if (attach_pid != LLDB_INVALID_PROCESS_ID) { 11040b57cec5SDimitry Andric error = EstablishConnectionIfNeeded(attach_info); 11050b57cec5SDimitry Andric if (error.Success()) { 11060b57cec5SDimitry Andric m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric char packet[64]; 11090b57cec5SDimitry Andric const int packet_len = 11100b57cec5SDimitry Andric ::snprintf(packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); 11110b57cec5SDimitry Andric SetID(attach_pid); 11120b57cec5SDimitry Andric m_async_broadcaster.BroadcastEvent( 11130b57cec5SDimitry Andric eBroadcastBitAsyncContinue, new EventDataBytes(packet, packet_len)); 11140b57cec5SDimitry Andric } else 11150b57cec5SDimitry Andric SetExitStatus(-1, error.AsCString()); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric return error; 11190b57cec5SDimitry Andric } 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andric Status ProcessGDBRemote::DoAttachToProcessWithName( 11220b57cec5SDimitry Andric const char *process_name, const ProcessAttachInfo &attach_info) { 11230b57cec5SDimitry Andric Status error; 11240b57cec5SDimitry Andric // Clear out and clean up from any current state 11250b57cec5SDimitry Andric Clear(); 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric if (process_name && process_name[0]) { 11280b57cec5SDimitry Andric error = EstablishConnectionIfNeeded(attach_info); 11290b57cec5SDimitry Andric if (error.Success()) { 11300b57cec5SDimitry Andric StreamString packet; 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andric if (attach_info.GetWaitForLaunch()) { 11350b57cec5SDimitry Andric if (!m_gdb_comm.GetVAttachOrWaitSupported()) { 11360b57cec5SDimitry Andric packet.PutCString("vAttachWait"); 11370b57cec5SDimitry Andric } else { 11380b57cec5SDimitry Andric if (attach_info.GetIgnoreExisting()) 11390b57cec5SDimitry Andric packet.PutCString("vAttachWait"); 11400b57cec5SDimitry Andric else 11410b57cec5SDimitry Andric packet.PutCString("vAttachOrWait"); 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric } else 11440b57cec5SDimitry Andric packet.PutCString("vAttachName"); 11450b57cec5SDimitry Andric packet.PutChar(';'); 11460b57cec5SDimitry Andric packet.PutBytesAsRawHex8(process_name, strlen(process_name), 11470b57cec5SDimitry Andric endian::InlHostByteOrder(), 11480b57cec5SDimitry Andric endian::InlHostByteOrder()); 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric m_async_broadcaster.BroadcastEvent( 11510b57cec5SDimitry Andric eBroadcastBitAsyncContinue, 11520b57cec5SDimitry Andric new EventDataBytes(packet.GetString().data(), packet.GetSize())); 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric } else 11550b57cec5SDimitry Andric SetExitStatus(-1, error.AsCString()); 11560b57cec5SDimitry Andric } 11570b57cec5SDimitry Andric return error; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric 1160fe6060f1SDimitry Andric llvm::Expected<TraceSupportedResponse> ProcessGDBRemote::TraceSupported() { 1161fe6060f1SDimitry Andric return m_gdb_comm.SendTraceSupported(GetInterruptTimeout()); 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 1164fe6060f1SDimitry Andric llvm::Error ProcessGDBRemote::TraceStop(const TraceStopRequest &request) { 1165fe6060f1SDimitry Andric return m_gdb_comm.SendTraceStop(request, GetInterruptTimeout()); 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 1168fe6060f1SDimitry Andric llvm::Error ProcessGDBRemote::TraceStart(const llvm::json::Value &request) { 1169fe6060f1SDimitry Andric return m_gdb_comm.SendTraceStart(request, GetInterruptTimeout()); 11700b57cec5SDimitry Andric } 11710b57cec5SDimitry Andric 1172fe6060f1SDimitry Andric llvm::Expected<std::string> 1173fe6060f1SDimitry Andric ProcessGDBRemote::TraceGetState(llvm::StringRef type) { 1174fe6060f1SDimitry Andric return m_gdb_comm.SendTraceGetState(type, GetInterruptTimeout()); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 1177fe6060f1SDimitry Andric llvm::Expected<std::vector<uint8_t>> 1178fe6060f1SDimitry Andric ProcessGDBRemote::TraceGetBinaryData(const TraceGetBinaryDataRequest &request) { 1179fe6060f1SDimitry Andric return m_gdb_comm.SendTraceGetBinaryData(request, GetInterruptTimeout()); 1180e8d8bef9SDimitry Andric } 1181e8d8bef9SDimitry Andric 11820b57cec5SDimitry Andric void ProcessGDBRemote::DidExit() { 11830b57cec5SDimitry Andric // When we exit, disconnect from the GDB server communications 11840b57cec5SDimitry Andric m_gdb_comm.Disconnect(); 11850b57cec5SDimitry Andric } 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric void ProcessGDBRemote::DidAttach(ArchSpec &process_arch) { 11880b57cec5SDimitry Andric // If you can figure out what the architecture is, fill it in here. 11890b57cec5SDimitry Andric process_arch.Clear(); 11900b57cec5SDimitry Andric DidLaunchOrAttach(process_arch); 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric Status ProcessGDBRemote::WillResume() { 11940b57cec5SDimitry Andric m_continue_c_tids.clear(); 11950b57cec5SDimitry Andric m_continue_C_tids.clear(); 11960b57cec5SDimitry Andric m_continue_s_tids.clear(); 11970b57cec5SDimitry Andric m_continue_S_tids.clear(); 11980b57cec5SDimitry Andric m_jstopinfo_sp.reset(); 11990b57cec5SDimitry Andric m_jthreadsinfo_sp.reset(); 12000b57cec5SDimitry Andric return Status(); 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric Status ProcessGDBRemote::DoResume() { 12040b57cec5SDimitry Andric Status error; 12050b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 12069dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::Resume()"); 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric ListenerSP listener_sp( 12090b57cec5SDimitry Andric Listener::MakeListener("gdb-remote.resume-packet-sent")); 12100b57cec5SDimitry Andric if (listener_sp->StartListeningForEvents( 12110b57cec5SDimitry Andric &m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) { 12120b57cec5SDimitry Andric listener_sp->StartListeningForEvents( 12130b57cec5SDimitry Andric &m_async_broadcaster, 12140b57cec5SDimitry Andric ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric const size_t num_threads = GetThreadList().GetSize(); 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric StreamString continue_packet; 12190b57cec5SDimitry Andric bool continue_packet_error = false; 12200b57cec5SDimitry Andric if (m_gdb_comm.HasAnyVContSupport()) { 1221349cc55cSDimitry Andric if (m_continue_c_tids.size() == num_threads || 12220b57cec5SDimitry Andric (m_continue_c_tids.empty() && m_continue_C_tids.empty() && 1223349cc55cSDimitry Andric m_continue_s_tids.empty() && m_continue_S_tids.empty())) { 12240b57cec5SDimitry Andric // All threads are continuing, just send a "c" packet 12250b57cec5SDimitry Andric continue_packet.PutCString("c"); 12260b57cec5SDimitry Andric } else { 12270b57cec5SDimitry Andric continue_packet.PutCString("vCont"); 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric if (!m_continue_c_tids.empty()) { 12300b57cec5SDimitry Andric if (m_gdb_comm.GetVContSupported('c')) { 12310b57cec5SDimitry Andric for (tid_collection::const_iterator 12320b57cec5SDimitry Andric t_pos = m_continue_c_tids.begin(), 12330b57cec5SDimitry Andric t_end = m_continue_c_tids.end(); 12340b57cec5SDimitry Andric t_pos != t_end; ++t_pos) 12350b57cec5SDimitry Andric continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); 12360b57cec5SDimitry Andric } else 12370b57cec5SDimitry Andric continue_packet_error = true; 12380b57cec5SDimitry Andric } 12390b57cec5SDimitry Andric 12400b57cec5SDimitry Andric if (!continue_packet_error && !m_continue_C_tids.empty()) { 12410b57cec5SDimitry Andric if (m_gdb_comm.GetVContSupported('C')) { 12420b57cec5SDimitry Andric for (tid_sig_collection::const_iterator 12430b57cec5SDimitry Andric s_pos = m_continue_C_tids.begin(), 12440b57cec5SDimitry Andric s_end = m_continue_C_tids.end(); 12450b57cec5SDimitry Andric s_pos != s_end; ++s_pos) 12460b57cec5SDimitry Andric continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, 12470b57cec5SDimitry Andric s_pos->first); 12480b57cec5SDimitry Andric } else 12490b57cec5SDimitry Andric continue_packet_error = true; 12500b57cec5SDimitry Andric } 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric if (!continue_packet_error && !m_continue_s_tids.empty()) { 12530b57cec5SDimitry Andric if (m_gdb_comm.GetVContSupported('s')) { 12540b57cec5SDimitry Andric for (tid_collection::const_iterator 12550b57cec5SDimitry Andric t_pos = m_continue_s_tids.begin(), 12560b57cec5SDimitry Andric t_end = m_continue_s_tids.end(); 12570b57cec5SDimitry Andric t_pos != t_end; ++t_pos) 12580b57cec5SDimitry Andric continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); 12590b57cec5SDimitry Andric } else 12600b57cec5SDimitry Andric continue_packet_error = true; 12610b57cec5SDimitry Andric } 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric if (!continue_packet_error && !m_continue_S_tids.empty()) { 12640b57cec5SDimitry Andric if (m_gdb_comm.GetVContSupported('S')) { 12650b57cec5SDimitry Andric for (tid_sig_collection::const_iterator 12660b57cec5SDimitry Andric s_pos = m_continue_S_tids.begin(), 12670b57cec5SDimitry Andric s_end = m_continue_S_tids.end(); 12680b57cec5SDimitry Andric s_pos != s_end; ++s_pos) 12690b57cec5SDimitry Andric continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, 12700b57cec5SDimitry Andric s_pos->first); 12710b57cec5SDimitry Andric } else 12720b57cec5SDimitry Andric continue_packet_error = true; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric if (continue_packet_error) 12760b57cec5SDimitry Andric continue_packet.Clear(); 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric } else 12790b57cec5SDimitry Andric continue_packet_error = true; 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric if (continue_packet_error) { 12820b57cec5SDimitry Andric // Either no vCont support, or we tried to use part of the vCont packet 12830b57cec5SDimitry Andric // that wasn't supported by the remote GDB server. We need to try and 12840b57cec5SDimitry Andric // make a simple packet that can do our continue 12850b57cec5SDimitry Andric const size_t num_continue_c_tids = m_continue_c_tids.size(); 12860b57cec5SDimitry Andric const size_t num_continue_C_tids = m_continue_C_tids.size(); 12870b57cec5SDimitry Andric const size_t num_continue_s_tids = m_continue_s_tids.size(); 12880b57cec5SDimitry Andric const size_t num_continue_S_tids = m_continue_S_tids.size(); 12890b57cec5SDimitry Andric if (num_continue_c_tids > 0) { 12900b57cec5SDimitry Andric if (num_continue_c_tids == num_threads) { 12910b57cec5SDimitry Andric // All threads are resuming... 12920b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(-1); 12930b57cec5SDimitry Andric continue_packet.PutChar('c'); 12940b57cec5SDimitry Andric continue_packet_error = false; 12950b57cec5SDimitry Andric } else if (num_continue_c_tids == 1 && num_continue_C_tids == 0 && 12960b57cec5SDimitry Andric num_continue_s_tids == 0 && num_continue_S_tids == 0) { 12970b57cec5SDimitry Andric // Only one thread is continuing 12980b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(m_continue_c_tids.front()); 12990b57cec5SDimitry Andric continue_packet.PutChar('c'); 13000b57cec5SDimitry Andric continue_packet_error = false; 13010b57cec5SDimitry Andric } 13020b57cec5SDimitry Andric } 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric if (continue_packet_error && num_continue_C_tids > 0) { 13050b57cec5SDimitry Andric if ((num_continue_C_tids + num_continue_c_tids) == num_threads && 13060b57cec5SDimitry Andric num_continue_C_tids > 0 && num_continue_s_tids == 0 && 13070b57cec5SDimitry Andric num_continue_S_tids == 0) { 13080b57cec5SDimitry Andric const int continue_signo = m_continue_C_tids.front().second; 13090b57cec5SDimitry Andric // Only one thread is continuing 13100b57cec5SDimitry Andric if (num_continue_C_tids > 1) { 13110b57cec5SDimitry Andric // More that one thread with a signal, yet we don't have vCont 13120b57cec5SDimitry Andric // support and we are being asked to resume each thread with a 13130b57cec5SDimitry Andric // signal, we need to make sure they are all the same signal, or we 13140b57cec5SDimitry Andric // can't issue the continue accurately with the current support... 13150b57cec5SDimitry Andric if (num_continue_C_tids > 1) { 13160b57cec5SDimitry Andric continue_packet_error = false; 13170b57cec5SDimitry Andric for (size_t i = 1; i < m_continue_C_tids.size(); ++i) { 13180b57cec5SDimitry Andric if (m_continue_C_tids[i].second != continue_signo) 13190b57cec5SDimitry Andric continue_packet_error = true; 13200b57cec5SDimitry Andric } 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric if (!continue_packet_error) 13230b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(-1); 13240b57cec5SDimitry Andric } else { 13250b57cec5SDimitry Andric // Set the continue thread ID 13260b57cec5SDimitry Andric continue_packet_error = false; 13270b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(m_continue_C_tids.front().first); 13280b57cec5SDimitry Andric } 13290b57cec5SDimitry Andric if (!continue_packet_error) { 13300b57cec5SDimitry Andric // Add threads continuing with the same signo... 13310b57cec5SDimitry Andric continue_packet.Printf("C%2.2x", continue_signo); 13320b57cec5SDimitry Andric } 13330b57cec5SDimitry Andric } 13340b57cec5SDimitry Andric } 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric if (continue_packet_error && num_continue_s_tids > 0) { 13370b57cec5SDimitry Andric if (num_continue_s_tids == num_threads) { 13380b57cec5SDimitry Andric // All threads are resuming... 13390b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(-1); 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric continue_packet.PutChar('s'); 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric continue_packet_error = false; 13440b57cec5SDimitry Andric } else if (num_continue_c_tids == 0 && num_continue_C_tids == 0 && 13450b57cec5SDimitry Andric num_continue_s_tids == 1 && num_continue_S_tids == 0) { 13460b57cec5SDimitry Andric // Only one thread is stepping 13470b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(m_continue_s_tids.front()); 13480b57cec5SDimitry Andric continue_packet.PutChar('s'); 13490b57cec5SDimitry Andric continue_packet_error = false; 13500b57cec5SDimitry Andric } 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric if (!continue_packet_error && num_continue_S_tids > 0) { 13540b57cec5SDimitry Andric if (num_continue_S_tids == num_threads) { 13550b57cec5SDimitry Andric const int step_signo = m_continue_S_tids.front().second; 13560b57cec5SDimitry Andric // Are all threads trying to step with the same signal? 13570b57cec5SDimitry Andric continue_packet_error = false; 13580b57cec5SDimitry Andric if (num_continue_S_tids > 1) { 13590b57cec5SDimitry Andric for (size_t i = 1; i < num_threads; ++i) { 13600b57cec5SDimitry Andric if (m_continue_S_tids[i].second != step_signo) 13610b57cec5SDimitry Andric continue_packet_error = true; 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric if (!continue_packet_error) { 13650b57cec5SDimitry Andric // Add threads stepping with the same signo... 13660b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(-1); 13670b57cec5SDimitry Andric continue_packet.Printf("S%2.2x", step_signo); 13680b57cec5SDimitry Andric } 13690b57cec5SDimitry Andric } else if (num_continue_c_tids == 0 && num_continue_C_tids == 0 && 13700b57cec5SDimitry Andric num_continue_s_tids == 0 && num_continue_S_tids == 1) { 13710b57cec5SDimitry Andric // Only one thread is stepping with signal 13720b57cec5SDimitry Andric m_gdb_comm.SetCurrentThreadForRun(m_continue_S_tids.front().first); 13730b57cec5SDimitry Andric continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); 13740b57cec5SDimitry Andric continue_packet_error = false; 13750b57cec5SDimitry Andric } 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric if (continue_packet_error) { 13800b57cec5SDimitry Andric error.SetErrorString("can't make continue packet for this resume"); 13810b57cec5SDimitry Andric } else { 13820b57cec5SDimitry Andric EventSP event_sp; 13830b57cec5SDimitry Andric if (!m_async_thread.IsJoinable()) { 13840b57cec5SDimitry Andric error.SetErrorString("Trying to resume but the async thread is dead."); 13859dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoResume: Trying to resume but the " 13860b57cec5SDimitry Andric "async thread is dead."); 13870b57cec5SDimitry Andric return error; 13880b57cec5SDimitry Andric } 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric m_async_broadcaster.BroadcastEvent( 13910b57cec5SDimitry Andric eBroadcastBitAsyncContinue, 13920b57cec5SDimitry Andric new EventDataBytes(continue_packet.GetString().data(), 13930b57cec5SDimitry Andric continue_packet.GetSize())); 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric if (!listener_sp->GetEvent(event_sp, std::chrono::seconds(5))) { 13960b57cec5SDimitry Andric error.SetErrorString("Resume timed out."); 13979dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoResume: Resume timed out."); 13980b57cec5SDimitry Andric } else if (event_sp->BroadcasterIs(&m_async_broadcaster)) { 13990b57cec5SDimitry Andric error.SetErrorString("Broadcast continue, but the async thread was " 14000b57cec5SDimitry Andric "killed before we got an ack back."); 14019dba64beSDimitry Andric LLDB_LOGF(log, 14029dba64beSDimitry Andric "ProcessGDBRemote::DoResume: Broadcast continue, but the " 14030b57cec5SDimitry Andric "async thread was killed before we got an ack back."); 14040b57cec5SDimitry Andric return error; 14050b57cec5SDimitry Andric } 14060b57cec5SDimitry Andric } 14070b57cec5SDimitry Andric } 14080b57cec5SDimitry Andric 14090b57cec5SDimitry Andric return error; 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric 14120b57cec5SDimitry Andric void ProcessGDBRemote::ClearThreadIDList() { 14130b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); 14140b57cec5SDimitry Andric m_thread_ids.clear(); 14150b57cec5SDimitry Andric m_thread_pcs.clear(); 14160b57cec5SDimitry Andric } 14170b57cec5SDimitry Andric 1418fe6060f1SDimitry Andric size_t ProcessGDBRemote::UpdateThreadIDsFromStopReplyThreadsValue( 1419fe6060f1SDimitry Andric llvm::StringRef value) { 14200b57cec5SDimitry Andric m_thread_ids.clear(); 1421fe6060f1SDimitry Andric lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); 1422fe6060f1SDimitry Andric StringExtractorGDBRemote thread_ids{value}; 1423fe6060f1SDimitry Andric 1424fe6060f1SDimitry Andric do { 1425fe6060f1SDimitry Andric auto pid_tid = thread_ids.GetPidTid(pid); 1426fe6060f1SDimitry Andric if (pid_tid && pid_tid->first == pid) { 1427fe6060f1SDimitry Andric lldb::tid_t tid = pid_tid->second; 1428fe6060f1SDimitry Andric if (tid != LLDB_INVALID_THREAD_ID && 1429fe6060f1SDimitry Andric tid != StringExtractorGDBRemote::AllProcesses) 14300b57cec5SDimitry Andric m_thread_ids.push_back(tid); 14310b57cec5SDimitry Andric } 1432fe6060f1SDimitry Andric } while (thread_ids.GetChar() == ','); 1433fe6060f1SDimitry Andric 14340b57cec5SDimitry Andric return m_thread_ids.size(); 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric 1437349cc55cSDimitry Andric size_t ProcessGDBRemote::UpdateThreadPCsFromStopReplyThreadsValue( 1438349cc55cSDimitry Andric llvm::StringRef value) { 14390b57cec5SDimitry Andric m_thread_pcs.clear(); 1440349cc55cSDimitry Andric for (llvm::StringRef x : llvm::split(value, ',')) { 14410b57cec5SDimitry Andric lldb::addr_t pc; 1442349cc55cSDimitry Andric if (llvm::to_integer(x, pc, 16)) 14430b57cec5SDimitry Andric m_thread_pcs.push_back(pc); 14440b57cec5SDimitry Andric } 14450b57cec5SDimitry Andric return m_thread_pcs.size(); 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric 14480b57cec5SDimitry Andric bool ProcessGDBRemote::UpdateThreadIDList() { 14490b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); 14500b57cec5SDimitry Andric 14510b57cec5SDimitry Andric if (m_jthreadsinfo_sp) { 14520b57cec5SDimitry Andric // If we have the JSON threads info, we can get the thread list from that 14530b57cec5SDimitry Andric StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); 14540b57cec5SDimitry Andric if (thread_infos && thread_infos->GetSize() > 0) { 14550b57cec5SDimitry Andric m_thread_ids.clear(); 14560b57cec5SDimitry Andric m_thread_pcs.clear(); 14570b57cec5SDimitry Andric thread_infos->ForEach([this](StructuredData::Object *object) -> bool { 14580b57cec5SDimitry Andric StructuredData::Dictionary *thread_dict = object->GetAsDictionary(); 14590b57cec5SDimitry Andric if (thread_dict) { 14600b57cec5SDimitry Andric // Set the thread stop info from the JSON dictionary 14610b57cec5SDimitry Andric SetThreadStopInfo(thread_dict); 14620b57cec5SDimitry Andric lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 14630b57cec5SDimitry Andric if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>("tid", tid)) 14640b57cec5SDimitry Andric m_thread_ids.push_back(tid); 14650b57cec5SDimitry Andric } 14660b57cec5SDimitry Andric return true; // Keep iterating through all thread_info objects 14670b57cec5SDimitry Andric }); 14680b57cec5SDimitry Andric } 14690b57cec5SDimitry Andric if (!m_thread_ids.empty()) 14700b57cec5SDimitry Andric return true; 14710b57cec5SDimitry Andric } else { 14720b57cec5SDimitry Andric // See if we can get the thread IDs from the current stop reply packets 14730b57cec5SDimitry Andric // that might contain a "threads" key/value pair 14740b57cec5SDimitry Andric 1475349cc55cSDimitry Andric if (m_last_stop_packet) { 14760b57cec5SDimitry Andric // Get the thread stop info 1477349cc55cSDimitry Andric StringExtractorGDBRemote &stop_info = *m_last_stop_packet; 1478349cc55cSDimitry Andric const std::string &stop_info_str = std::string(stop_info.GetStringRef()); 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric m_thread_pcs.clear(); 14810b57cec5SDimitry Andric const size_t thread_pcs_pos = stop_info_str.find(";thread-pcs:"); 14820b57cec5SDimitry Andric if (thread_pcs_pos != std::string::npos) { 14830b57cec5SDimitry Andric const size_t start = thread_pcs_pos + strlen(";thread-pcs:"); 14840b57cec5SDimitry Andric const size_t end = stop_info_str.find(';', start); 14850b57cec5SDimitry Andric if (end != std::string::npos) { 14860b57cec5SDimitry Andric std::string value = stop_info_str.substr(start, end - start); 14870b57cec5SDimitry Andric UpdateThreadPCsFromStopReplyThreadsValue(value); 14880b57cec5SDimitry Andric } 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric 14910b57cec5SDimitry Andric const size_t threads_pos = stop_info_str.find(";threads:"); 14920b57cec5SDimitry Andric if (threads_pos != std::string::npos) { 14930b57cec5SDimitry Andric const size_t start = threads_pos + strlen(";threads:"); 14940b57cec5SDimitry Andric const size_t end = stop_info_str.find(';', start); 14950b57cec5SDimitry Andric if (end != std::string::npos) { 14960b57cec5SDimitry Andric std::string value = stop_info_str.substr(start, end - start); 14970b57cec5SDimitry Andric if (UpdateThreadIDsFromStopReplyThreadsValue(value)) 14980b57cec5SDimitry Andric return true; 14990b57cec5SDimitry Andric } 15000b57cec5SDimitry Andric } 15010b57cec5SDimitry Andric } 15020b57cec5SDimitry Andric } 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andric bool sequence_mutex_unavailable = false; 15050b57cec5SDimitry Andric m_gdb_comm.GetCurrentThreadIDs(m_thread_ids, sequence_mutex_unavailable); 15060b57cec5SDimitry Andric if (sequence_mutex_unavailable) { 15070b57cec5SDimitry Andric return false; // We just didn't get the list 15080b57cec5SDimitry Andric } 15090b57cec5SDimitry Andric return true; 15100b57cec5SDimitry Andric } 15110b57cec5SDimitry Andric 1512e8d8bef9SDimitry Andric bool ProcessGDBRemote::DoUpdateThreadList(ThreadList &old_thread_list, 15130b57cec5SDimitry Andric ThreadList &new_thread_list) { 15140b57cec5SDimitry Andric // locker will keep a mutex locked until it goes out of scope 15150b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_THREAD)); 15160b57cec5SDimitry Andric LLDB_LOGV(log, "pid = {0}", GetID()); 15170b57cec5SDimitry Andric 15180b57cec5SDimitry Andric size_t num_thread_ids = m_thread_ids.size(); 15190b57cec5SDimitry Andric // The "m_thread_ids" thread ID list should always be updated after each stop 15200b57cec5SDimitry Andric // reply packet, but in case it isn't, update it here. 15210b57cec5SDimitry Andric if (num_thread_ids == 0) { 15220b57cec5SDimitry Andric if (!UpdateThreadIDList()) 15230b57cec5SDimitry Andric return false; 15240b57cec5SDimitry Andric num_thread_ids = m_thread_ids.size(); 15250b57cec5SDimitry Andric } 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric ThreadList old_thread_list_copy(old_thread_list); 15280b57cec5SDimitry Andric if (num_thread_ids > 0) { 15290b57cec5SDimitry Andric for (size_t i = 0; i < num_thread_ids; ++i) { 15300b57cec5SDimitry Andric tid_t tid = m_thread_ids[i]; 15310b57cec5SDimitry Andric ThreadSP thread_sp( 15320b57cec5SDimitry Andric old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); 15330b57cec5SDimitry Andric if (!thread_sp) { 15340b57cec5SDimitry Andric thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid); 15350b57cec5SDimitry Andric LLDB_LOGV(log, "Making new thread: {0} for thread ID: {1:x}.", 15360b57cec5SDimitry Andric thread_sp.get(), thread_sp->GetID()); 15370b57cec5SDimitry Andric } else { 15380b57cec5SDimitry Andric LLDB_LOGV(log, "Found old thread: {0} for thread ID: {1:x}.", 15390b57cec5SDimitry Andric thread_sp.get(), thread_sp->GetID()); 15400b57cec5SDimitry Andric } 15410b57cec5SDimitry Andric 15420b57cec5SDimitry Andric SetThreadPc(thread_sp, i); 15430b57cec5SDimitry Andric new_thread_list.AddThreadSortedByIndexID(thread_sp); 15440b57cec5SDimitry Andric } 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric // Whatever that is left in old_thread_list_copy are not present in 15480b57cec5SDimitry Andric // new_thread_list. Remove non-existent threads from internal id table. 15490b57cec5SDimitry Andric size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); 15500b57cec5SDimitry Andric for (size_t i = 0; i < old_num_thread_ids; i++) { 15510b57cec5SDimitry Andric ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); 15520b57cec5SDimitry Andric if (old_thread_sp) { 15530b57cec5SDimitry Andric lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); 15540b57cec5SDimitry Andric m_thread_id_to_index_id_map.erase(old_thread_id); 15550b57cec5SDimitry Andric } 15560b57cec5SDimitry Andric } 15570b57cec5SDimitry Andric 15580b57cec5SDimitry Andric return true; 15590b57cec5SDimitry Andric } 15600b57cec5SDimitry Andric 15610b57cec5SDimitry Andric void ProcessGDBRemote::SetThreadPc(const ThreadSP &thread_sp, uint64_t index) { 15620b57cec5SDimitry Andric if (m_thread_ids.size() == m_thread_pcs.size() && thread_sp.get() && 15630b57cec5SDimitry Andric GetByteOrder() != eByteOrderInvalid) { 15640b57cec5SDimitry Andric ThreadGDBRemote *gdb_thread = 15650b57cec5SDimitry Andric static_cast<ThreadGDBRemote *>(thread_sp.get()); 15660b57cec5SDimitry Andric RegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext()); 15670b57cec5SDimitry Andric if (reg_ctx_sp) { 15680b57cec5SDimitry Andric uint32_t pc_regnum = reg_ctx_sp->ConvertRegisterKindToRegisterNumber( 15690b57cec5SDimitry Andric eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 15700b57cec5SDimitry Andric if (pc_regnum != LLDB_INVALID_REGNUM) { 15710b57cec5SDimitry Andric gdb_thread->PrivateSetRegisterValue(pc_regnum, m_thread_pcs[index]); 15720b57cec5SDimitry Andric } 15730b57cec5SDimitry Andric } 15740b57cec5SDimitry Andric } 15750b57cec5SDimitry Andric } 15760b57cec5SDimitry Andric 15770b57cec5SDimitry Andric bool ProcessGDBRemote::GetThreadStopInfoFromJSON( 15780b57cec5SDimitry Andric ThreadGDBRemote *thread, const StructuredData::ObjectSP &thread_infos_sp) { 15790b57cec5SDimitry Andric // See if we got thread stop infos for all threads via the "jThreadsInfo" 15800b57cec5SDimitry Andric // packet 15810b57cec5SDimitry Andric if (thread_infos_sp) { 15820b57cec5SDimitry Andric StructuredData::Array *thread_infos = thread_infos_sp->GetAsArray(); 15830b57cec5SDimitry Andric if (thread_infos) { 15840b57cec5SDimitry Andric lldb::tid_t tid; 15850b57cec5SDimitry Andric const size_t n = thread_infos->GetSize(); 15860b57cec5SDimitry Andric for (size_t i = 0; i < n; ++i) { 15870b57cec5SDimitry Andric StructuredData::Dictionary *thread_dict = 15880b57cec5SDimitry Andric thread_infos->GetItemAtIndex(i)->GetAsDictionary(); 15890b57cec5SDimitry Andric if (thread_dict) { 15900b57cec5SDimitry Andric if (thread_dict->GetValueForKeyAsInteger<lldb::tid_t>( 15910b57cec5SDimitry Andric "tid", tid, LLDB_INVALID_THREAD_ID)) { 15920b57cec5SDimitry Andric if (tid == thread->GetID()) 15930b57cec5SDimitry Andric return (bool)SetThreadStopInfo(thread_dict); 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric } 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric } 15980b57cec5SDimitry Andric } 15990b57cec5SDimitry Andric return false; 16000b57cec5SDimitry Andric } 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric bool ProcessGDBRemote::CalculateThreadStopInfo(ThreadGDBRemote *thread) { 16030b57cec5SDimitry Andric // See if we got thread stop infos for all threads via the "jThreadsInfo" 16040b57cec5SDimitry Andric // packet 16050b57cec5SDimitry Andric if (GetThreadStopInfoFromJSON(thread, m_jthreadsinfo_sp)) 16060b57cec5SDimitry Andric return true; 16070b57cec5SDimitry Andric 16080b57cec5SDimitry Andric // See if we got thread stop info for any threads valid stop info reasons 16090b57cec5SDimitry Andric // threads via the "jstopinfo" packet stop reply packet key/value pair? 16100b57cec5SDimitry Andric if (m_jstopinfo_sp) { 16110b57cec5SDimitry Andric // If we have "jstopinfo" then we have stop descriptions for all threads 16120b57cec5SDimitry Andric // that have stop reasons, and if there is no entry for a thread, then it 16130b57cec5SDimitry Andric // has no stop reason. 16140b57cec5SDimitry Andric thread->GetRegisterContext()->InvalidateIfNeeded(true); 16150b57cec5SDimitry Andric if (!GetThreadStopInfoFromJSON(thread, m_jstopinfo_sp)) { 16160b57cec5SDimitry Andric thread->SetStopInfo(StopInfoSP()); 16170b57cec5SDimitry Andric } 16180b57cec5SDimitry Andric return true; 16190b57cec5SDimitry Andric } 16200b57cec5SDimitry Andric 16210b57cec5SDimitry Andric // Fall back to using the qThreadStopInfo packet 16220b57cec5SDimitry Andric StringExtractorGDBRemote stop_packet; 16230b57cec5SDimitry Andric if (GetGDBRemote().GetThreadStopInfo(thread->GetProtocolID(), stop_packet)) 16240b57cec5SDimitry Andric return SetThreadStopInfo(stop_packet) == eStateStopped; 16250b57cec5SDimitry Andric return false; 16260b57cec5SDimitry Andric } 16270b57cec5SDimitry Andric 16280b57cec5SDimitry Andric ThreadSP ProcessGDBRemote::SetThreadStopInfo( 16290b57cec5SDimitry Andric lldb::tid_t tid, ExpeditedRegisterMap &expedited_register_map, 16300b57cec5SDimitry Andric uint8_t signo, const std::string &thread_name, const std::string &reason, 16310b57cec5SDimitry Andric const std::string &description, uint32_t exc_type, 16320b57cec5SDimitry Andric const std::vector<addr_t> &exc_data, addr_t thread_dispatch_qaddr, 16330b57cec5SDimitry Andric bool queue_vars_valid, // Set to true if queue_name, queue_kind and 16340b57cec5SDimitry Andric // queue_serial are valid 16350b57cec5SDimitry Andric LazyBool associated_with_dispatch_queue, addr_t dispatch_queue_t, 16360b57cec5SDimitry Andric std::string &queue_name, QueueKind queue_kind, uint64_t queue_serial) { 16370b57cec5SDimitry Andric ThreadSP thread_sp; 16380b57cec5SDimitry Andric if (tid != LLDB_INVALID_THREAD_ID) { 16390b57cec5SDimitry Andric // Scope for "locker" below 16400b57cec5SDimitry Andric { 16410b57cec5SDimitry Andric // m_thread_list_real does have its own mutex, but we need to hold onto 16420b57cec5SDimitry Andric // the mutex between the call to m_thread_list_real.FindThreadByID(...) 16430b57cec5SDimitry Andric // and the m_thread_list_real.AddThread(...) so it doesn't change on us 16440b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 16450b57cec5SDimitry Andric m_thread_list_real.GetMutex()); 16460b57cec5SDimitry Andric thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); 16470b57cec5SDimitry Andric 16480b57cec5SDimitry Andric if (!thread_sp) { 16490b57cec5SDimitry Andric // Create the thread if we need to 16500b57cec5SDimitry Andric thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid); 16510b57cec5SDimitry Andric m_thread_list_real.AddThread(thread_sp); 16520b57cec5SDimitry Andric } 16530b57cec5SDimitry Andric } 16540b57cec5SDimitry Andric 16550b57cec5SDimitry Andric if (thread_sp) { 16560b57cec5SDimitry Andric ThreadGDBRemote *gdb_thread = 16570b57cec5SDimitry Andric static_cast<ThreadGDBRemote *>(thread_sp.get()); 1658fe6060f1SDimitry Andric RegisterContextSP gdb_reg_ctx_sp(gdb_thread->GetRegisterContext()); 1659fe6060f1SDimitry Andric 1660fe6060f1SDimitry Andric gdb_reg_ctx_sp->InvalidateIfNeeded(true); 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric auto iter = std::find(m_thread_ids.begin(), m_thread_ids.end(), tid); 16630b57cec5SDimitry Andric if (iter != m_thread_ids.end()) { 16640b57cec5SDimitry Andric SetThreadPc(thread_sp, iter - m_thread_ids.begin()); 16650b57cec5SDimitry Andric } 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric for (const auto &pair : expedited_register_map) { 16689dba64beSDimitry Andric StringExtractor reg_value_extractor(pair.second); 16690b57cec5SDimitry Andric DataBufferSP buffer_sp(new DataBufferHeap( 16700b57cec5SDimitry Andric reg_value_extractor.GetStringRef().size() / 2, 0)); 16710b57cec5SDimitry Andric reg_value_extractor.GetHexBytes(buffer_sp->GetData(), '\xcc'); 1672fe6060f1SDimitry Andric uint32_t lldb_regnum = 1673fe6060f1SDimitry Andric gdb_reg_ctx_sp->ConvertRegisterKindToRegisterNumber( 1674fe6060f1SDimitry Andric eRegisterKindProcessPlugin, pair.first); 1675fe6060f1SDimitry Andric gdb_thread->PrivateSetRegisterValue(lldb_regnum, buffer_sp->GetData()); 16760b57cec5SDimitry Andric } 16770b57cec5SDimitry Andric 1678e8d8bef9SDimitry Andric // AArch64 SVE specific code below calls AArch64SVEReconfigure to update 1679e8d8bef9SDimitry Andric // SVE register sizes and offsets if value of VG register has changed 1680e8d8bef9SDimitry Andric // since last stop. 1681e8d8bef9SDimitry Andric const ArchSpec &arch = GetTarget().GetArchitecture(); 1682e8d8bef9SDimitry Andric if (arch.IsValid() && arch.GetTriple().isAArch64()) { 1683e8d8bef9SDimitry Andric GDBRemoteRegisterContext *reg_ctx_sp = 1684e8d8bef9SDimitry Andric static_cast<GDBRemoteRegisterContext *>( 1685e8d8bef9SDimitry Andric gdb_thread->GetRegisterContext().get()); 1686e8d8bef9SDimitry Andric 1687e8d8bef9SDimitry Andric if (reg_ctx_sp) 1688e8d8bef9SDimitry Andric reg_ctx_sp->AArch64SVEReconfigure(); 1689e8d8bef9SDimitry Andric } 1690e8d8bef9SDimitry Andric 16910b57cec5SDimitry Andric thread_sp->SetName(thread_name.empty() ? nullptr : thread_name.c_str()); 16920b57cec5SDimitry Andric 16930b57cec5SDimitry Andric gdb_thread->SetThreadDispatchQAddr(thread_dispatch_qaddr); 16940b57cec5SDimitry Andric // Check if the GDB server was able to provide the queue name, kind and 16950b57cec5SDimitry Andric // serial number 16960b57cec5SDimitry Andric if (queue_vars_valid) 16970b57cec5SDimitry Andric gdb_thread->SetQueueInfo(std::move(queue_name), queue_kind, 16980b57cec5SDimitry Andric queue_serial, dispatch_queue_t, 16990b57cec5SDimitry Andric associated_with_dispatch_queue); 17000b57cec5SDimitry Andric else 17010b57cec5SDimitry Andric gdb_thread->ClearQueueInfo(); 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric gdb_thread->SetAssociatedWithLibdispatchQueue( 17040b57cec5SDimitry Andric associated_with_dispatch_queue); 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric if (dispatch_queue_t != LLDB_INVALID_ADDRESS) 17070b57cec5SDimitry Andric gdb_thread->SetQueueLibdispatchQueueAddress(dispatch_queue_t); 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric // Make sure we update our thread stop reason just once 17100b57cec5SDimitry Andric if (!thread_sp->StopInfoIsUpToDate()) { 17110b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfoSP()); 17120b57cec5SDimitry Andric // If there's a memory thread backed by this thread, we need to use it 17130b57cec5SDimitry Andric // to calculate StopInfo. 17140b57cec5SDimitry Andric if (ThreadSP memory_thread_sp = 17150b57cec5SDimitry Andric m_thread_list.GetBackingThread(thread_sp)) 17160b57cec5SDimitry Andric thread_sp = memory_thread_sp; 17170b57cec5SDimitry Andric 17180b57cec5SDimitry Andric if (exc_type != 0) { 17190b57cec5SDimitry Andric const size_t exc_data_size = exc_data.size(); 17200b57cec5SDimitry Andric 17210b57cec5SDimitry Andric thread_sp->SetStopInfo( 17220b57cec5SDimitry Andric StopInfoMachException::CreateStopReasonWithMachException( 17230b57cec5SDimitry Andric *thread_sp, exc_type, exc_data_size, 17240b57cec5SDimitry Andric exc_data_size >= 1 ? exc_data[0] : 0, 17250b57cec5SDimitry Andric exc_data_size >= 2 ? exc_data[1] : 0, 17260b57cec5SDimitry Andric exc_data_size >= 3 ? exc_data[2] : 0)); 17270b57cec5SDimitry Andric } else { 17280b57cec5SDimitry Andric bool handled = false; 17290b57cec5SDimitry Andric bool did_exec = false; 17300b57cec5SDimitry Andric if (!reason.empty()) { 17310b57cec5SDimitry Andric if (reason == "trace") { 17320b57cec5SDimitry Andric addr_t pc = thread_sp->GetRegisterContext()->GetPC(); 17330b57cec5SDimitry Andric lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() 17340b57cec5SDimitry Andric ->GetBreakpointSiteList() 17350b57cec5SDimitry Andric .FindByAddress(pc); 17360b57cec5SDimitry Andric 17370b57cec5SDimitry Andric // If the current pc is a breakpoint site then the StopInfo 17380b57cec5SDimitry Andric // should be set to Breakpoint Otherwise, it will be set to 17390b57cec5SDimitry Andric // Trace. 1740fe6060f1SDimitry Andric if (bp_site_sp && bp_site_sp->ValidForThisThread(*thread_sp)) { 17410b57cec5SDimitry Andric thread_sp->SetStopInfo( 17420b57cec5SDimitry Andric StopInfo::CreateStopReasonWithBreakpointSiteID( 17430b57cec5SDimitry Andric *thread_sp, bp_site_sp->GetID())); 17440b57cec5SDimitry Andric } else 17450b57cec5SDimitry Andric thread_sp->SetStopInfo( 17460b57cec5SDimitry Andric StopInfo::CreateStopReasonToTrace(*thread_sp)); 17470b57cec5SDimitry Andric handled = true; 17480b57cec5SDimitry Andric } else if (reason == "breakpoint") { 17490b57cec5SDimitry Andric addr_t pc = thread_sp->GetRegisterContext()->GetPC(); 17500b57cec5SDimitry Andric lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() 17510b57cec5SDimitry Andric ->GetBreakpointSiteList() 17520b57cec5SDimitry Andric .FindByAddress(pc); 17530b57cec5SDimitry Andric if (bp_site_sp) { 17540b57cec5SDimitry Andric // If the breakpoint is for this thread, then we'll report the 17550b57cec5SDimitry Andric // hit, but if it is for another thread, we can just report no 17560b57cec5SDimitry Andric // reason. We don't need to worry about stepping over the 17570b57cec5SDimitry Andric // breakpoint here, that will be taken care of when the thread 17580b57cec5SDimitry Andric // resumes and notices that there's a breakpoint under the pc. 17590b57cec5SDimitry Andric handled = true; 1760fe6060f1SDimitry Andric if (bp_site_sp->ValidForThisThread(*thread_sp)) { 17610b57cec5SDimitry Andric thread_sp->SetStopInfo( 17620b57cec5SDimitry Andric StopInfo::CreateStopReasonWithBreakpointSiteID( 17630b57cec5SDimitry Andric *thread_sp, bp_site_sp->GetID())); 17640b57cec5SDimitry Andric } else { 17650b57cec5SDimitry Andric StopInfoSP invalid_stop_info_sp; 17660b57cec5SDimitry Andric thread_sp->SetStopInfo(invalid_stop_info_sp); 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric } 17690b57cec5SDimitry Andric } else if (reason == "trap") { 17700b57cec5SDimitry Andric // Let the trap just use the standard signal stop reason below... 17710b57cec5SDimitry Andric } else if (reason == "watchpoint") { 17720b57cec5SDimitry Andric StringExtractor desc_extractor(description.c_str()); 17730b57cec5SDimitry Andric addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); 17740b57cec5SDimitry Andric uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32); 17750b57cec5SDimitry Andric addr_t wp_hit_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); 17760b57cec5SDimitry Andric watch_id_t watch_id = LLDB_INVALID_WATCH_ID; 17770b57cec5SDimitry Andric if (wp_addr != LLDB_INVALID_ADDRESS) { 17780b57cec5SDimitry Andric WatchpointSP wp_sp; 17790b57cec5SDimitry Andric ArchSpec::Core core = GetTarget().GetArchitecture().GetCore(); 17800b57cec5SDimitry Andric if ((core >= ArchSpec::kCore_mips_first && 17810b57cec5SDimitry Andric core <= ArchSpec::kCore_mips_last) || 17820b57cec5SDimitry Andric (core >= ArchSpec::eCore_arm_generic && 17830b57cec5SDimitry Andric core <= ArchSpec::eCore_arm_aarch64)) 17840b57cec5SDimitry Andric wp_sp = GetTarget().GetWatchpointList().FindByAddress( 17850b57cec5SDimitry Andric wp_hit_addr); 17860b57cec5SDimitry Andric if (!wp_sp) 17870b57cec5SDimitry Andric wp_sp = 17880b57cec5SDimitry Andric GetTarget().GetWatchpointList().FindByAddress(wp_addr); 17890b57cec5SDimitry Andric if (wp_sp) { 17900b57cec5SDimitry Andric wp_sp->SetHardwareIndex(wp_index); 17910b57cec5SDimitry Andric watch_id = wp_sp->GetID(); 17920b57cec5SDimitry Andric } 17930b57cec5SDimitry Andric } 17940b57cec5SDimitry Andric if (watch_id == LLDB_INVALID_WATCH_ID) { 17950b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet( 17960b57cec5SDimitry Andric GDBR_LOG_WATCHPOINTS)); 17979dba64beSDimitry Andric LLDB_LOGF(log, "failed to find watchpoint"); 17980b57cec5SDimitry Andric } 17990b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithWatchpointID( 18000b57cec5SDimitry Andric *thread_sp, watch_id, wp_hit_addr)); 18010b57cec5SDimitry Andric handled = true; 18020b57cec5SDimitry Andric } else if (reason == "exception") { 18030b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithException( 18040b57cec5SDimitry Andric *thread_sp, description.c_str())); 18050b57cec5SDimitry Andric handled = true; 18060b57cec5SDimitry Andric } else if (reason == "exec") { 18070b57cec5SDimitry Andric did_exec = true; 18080b57cec5SDimitry Andric thread_sp->SetStopInfo( 18090b57cec5SDimitry Andric StopInfo::CreateStopReasonWithExec(*thread_sp)); 18100b57cec5SDimitry Andric handled = true; 1811fe6060f1SDimitry Andric } else if (reason == "processor trace") { 1812fe6060f1SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonProcessorTrace( 1813fe6060f1SDimitry Andric *thread_sp, description.c_str())); 1814349cc55cSDimitry Andric } else if (reason == "fork") { 1815349cc55cSDimitry Andric StringExtractor desc_extractor(description.c_str()); 1816349cc55cSDimitry Andric lldb::pid_t child_pid = desc_extractor.GetU64( 1817349cc55cSDimitry Andric LLDB_INVALID_PROCESS_ID); 1818349cc55cSDimitry Andric lldb::tid_t child_tid = desc_extractor.GetU64( 1819349cc55cSDimitry Andric LLDB_INVALID_THREAD_ID); 1820349cc55cSDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonFork( 1821349cc55cSDimitry Andric *thread_sp, child_pid, child_tid)); 1822349cc55cSDimitry Andric handled = true; 1823349cc55cSDimitry Andric } else if (reason == "vfork") { 1824349cc55cSDimitry Andric StringExtractor desc_extractor(description.c_str()); 1825349cc55cSDimitry Andric lldb::pid_t child_pid = desc_extractor.GetU64( 1826349cc55cSDimitry Andric LLDB_INVALID_PROCESS_ID); 1827349cc55cSDimitry Andric lldb::tid_t child_tid = desc_extractor.GetU64( 1828349cc55cSDimitry Andric LLDB_INVALID_THREAD_ID); 1829349cc55cSDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonVFork( 1830349cc55cSDimitry Andric *thread_sp, child_pid, child_tid)); 1831349cc55cSDimitry Andric handled = true; 1832349cc55cSDimitry Andric } else if (reason == "vforkdone") { 1833349cc55cSDimitry Andric thread_sp->SetStopInfo( 1834349cc55cSDimitry Andric StopInfo::CreateStopReasonVForkDone(*thread_sp)); 1835349cc55cSDimitry Andric handled = true; 18360b57cec5SDimitry Andric } 18370b57cec5SDimitry Andric } else if (!signo) { 18380b57cec5SDimitry Andric addr_t pc = thread_sp->GetRegisterContext()->GetPC(); 18390b57cec5SDimitry Andric lldb::BreakpointSiteSP bp_site_sp = 18400b57cec5SDimitry Andric thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress( 18410b57cec5SDimitry Andric pc); 18420b57cec5SDimitry Andric 18430b57cec5SDimitry Andric // If the current pc is a breakpoint site then the StopInfo should 18440b57cec5SDimitry Andric // be set to Breakpoint even though the remote stub did not set it 18450b57cec5SDimitry Andric // as such. This can happen when the thread is involuntarily 18460b57cec5SDimitry Andric // interrupted (e.g. due to stops on other threads) just as it is 18470b57cec5SDimitry Andric // about to execute the breakpoint instruction. 1848fe6060f1SDimitry Andric if (bp_site_sp && bp_site_sp->ValidForThisThread(*thread_sp)) { 18490b57cec5SDimitry Andric thread_sp->SetStopInfo( 18500b57cec5SDimitry Andric StopInfo::CreateStopReasonWithBreakpointSiteID( 18510b57cec5SDimitry Andric *thread_sp, bp_site_sp->GetID())); 18520b57cec5SDimitry Andric handled = true; 18530b57cec5SDimitry Andric } 18540b57cec5SDimitry Andric } 18550b57cec5SDimitry Andric 18560b57cec5SDimitry Andric if (!handled && signo && !did_exec) { 18570b57cec5SDimitry Andric if (signo == SIGTRAP) { 18580b57cec5SDimitry Andric // Currently we are going to assume SIGTRAP means we are either 18590b57cec5SDimitry Andric // hitting a breakpoint or hardware single stepping. 18600b57cec5SDimitry Andric handled = true; 18610b57cec5SDimitry Andric addr_t pc = thread_sp->GetRegisterContext()->GetPC() + 18620b57cec5SDimitry Andric m_breakpoint_pc_offset; 18630b57cec5SDimitry Andric lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess() 18640b57cec5SDimitry Andric ->GetBreakpointSiteList() 18650b57cec5SDimitry Andric .FindByAddress(pc); 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric if (bp_site_sp) { 18680b57cec5SDimitry Andric // If the breakpoint is for this thread, then we'll report the 18690b57cec5SDimitry Andric // hit, but if it is for another thread, we can just report no 18700b57cec5SDimitry Andric // reason. We don't need to worry about stepping over the 18710b57cec5SDimitry Andric // breakpoint here, that will be taken care of when the thread 18720b57cec5SDimitry Andric // resumes and notices that there's a breakpoint under the pc. 1873fe6060f1SDimitry Andric if (bp_site_sp->ValidForThisThread(*thread_sp)) { 18740b57cec5SDimitry Andric if (m_breakpoint_pc_offset != 0) 18750b57cec5SDimitry Andric thread_sp->GetRegisterContext()->SetPC(pc); 18760b57cec5SDimitry Andric thread_sp->SetStopInfo( 18770b57cec5SDimitry Andric StopInfo::CreateStopReasonWithBreakpointSiteID( 18780b57cec5SDimitry Andric *thread_sp, bp_site_sp->GetID())); 18790b57cec5SDimitry Andric } else { 18800b57cec5SDimitry Andric StopInfoSP invalid_stop_info_sp; 18810b57cec5SDimitry Andric thread_sp->SetStopInfo(invalid_stop_info_sp); 18820b57cec5SDimitry Andric } 18830b57cec5SDimitry Andric } else { 18840b57cec5SDimitry Andric // If we were stepping then assume the stop was the result of 18850b57cec5SDimitry Andric // the trace. If we were not stepping then report the SIGTRAP. 18860b57cec5SDimitry Andric // FIXME: We are still missing the case where we single step 18870b57cec5SDimitry Andric // over a trap instruction. 18880b57cec5SDimitry Andric if (thread_sp->GetTemporaryResumeState() == eStateStepping) 18890b57cec5SDimitry Andric thread_sp->SetStopInfo( 18900b57cec5SDimitry Andric StopInfo::CreateStopReasonToTrace(*thread_sp)); 18910b57cec5SDimitry Andric else 18920b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithSignal( 18930b57cec5SDimitry Andric *thread_sp, signo, description.c_str())); 18940b57cec5SDimitry Andric } 18950b57cec5SDimitry Andric } 18960b57cec5SDimitry Andric if (!handled) 18970b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithSignal( 18980b57cec5SDimitry Andric *thread_sp, signo, description.c_str())); 18990b57cec5SDimitry Andric } 19000b57cec5SDimitry Andric 19010b57cec5SDimitry Andric if (!description.empty()) { 19020b57cec5SDimitry Andric lldb::StopInfoSP stop_info_sp(thread_sp->GetStopInfo()); 19030b57cec5SDimitry Andric if (stop_info_sp) { 19040b57cec5SDimitry Andric const char *stop_info_desc = stop_info_sp->GetDescription(); 19050b57cec5SDimitry Andric if (!stop_info_desc || !stop_info_desc[0]) 19060b57cec5SDimitry Andric stop_info_sp->SetDescription(description.c_str()); 19070b57cec5SDimitry Andric } else { 19080b57cec5SDimitry Andric thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithException( 19090b57cec5SDimitry Andric *thread_sp, description.c_str())); 19100b57cec5SDimitry Andric } 19110b57cec5SDimitry Andric } 19120b57cec5SDimitry Andric } 19130b57cec5SDimitry Andric } 19140b57cec5SDimitry Andric } 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric return thread_sp; 19170b57cec5SDimitry Andric } 19180b57cec5SDimitry Andric 19190b57cec5SDimitry Andric lldb::ThreadSP 19200b57cec5SDimitry Andric ProcessGDBRemote::SetThreadStopInfo(StructuredData::Dictionary *thread_dict) { 19210b57cec5SDimitry Andric static ConstString g_key_tid("tid"); 19220b57cec5SDimitry Andric static ConstString g_key_name("name"); 19230b57cec5SDimitry Andric static ConstString g_key_reason("reason"); 19240b57cec5SDimitry Andric static ConstString g_key_metype("metype"); 19250b57cec5SDimitry Andric static ConstString g_key_medata("medata"); 19260b57cec5SDimitry Andric static ConstString g_key_qaddr("qaddr"); 19270b57cec5SDimitry Andric static ConstString g_key_dispatch_queue_t("dispatch_queue_t"); 19280b57cec5SDimitry Andric static ConstString g_key_associated_with_dispatch_queue( 19290b57cec5SDimitry Andric "associated_with_dispatch_queue"); 19300b57cec5SDimitry Andric static ConstString g_key_queue_name("qname"); 19310b57cec5SDimitry Andric static ConstString g_key_queue_kind("qkind"); 19320b57cec5SDimitry Andric static ConstString g_key_queue_serial_number("qserialnum"); 19330b57cec5SDimitry Andric static ConstString g_key_registers("registers"); 19340b57cec5SDimitry Andric static ConstString g_key_memory("memory"); 19350b57cec5SDimitry Andric static ConstString g_key_address("address"); 19360b57cec5SDimitry Andric static ConstString g_key_bytes("bytes"); 19370b57cec5SDimitry Andric static ConstString g_key_description("description"); 19380b57cec5SDimitry Andric static ConstString g_key_signal("signal"); 19390b57cec5SDimitry Andric 19400b57cec5SDimitry Andric // Stop with signal and thread info 19410b57cec5SDimitry Andric lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 19420b57cec5SDimitry Andric uint8_t signo = 0; 19430b57cec5SDimitry Andric std::string value; 19440b57cec5SDimitry Andric std::string thread_name; 19450b57cec5SDimitry Andric std::string reason; 19460b57cec5SDimitry Andric std::string description; 19470b57cec5SDimitry Andric uint32_t exc_type = 0; 19480b57cec5SDimitry Andric std::vector<addr_t> exc_data; 19490b57cec5SDimitry Andric addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; 19500b57cec5SDimitry Andric ExpeditedRegisterMap expedited_register_map; 19510b57cec5SDimitry Andric bool queue_vars_valid = false; 19520b57cec5SDimitry Andric addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; 19530b57cec5SDimitry Andric LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; 19540b57cec5SDimitry Andric std::string queue_name; 19550b57cec5SDimitry Andric QueueKind queue_kind = eQueueKindUnknown; 19560b57cec5SDimitry Andric uint64_t queue_serial_number = 0; 19570b57cec5SDimitry Andric // Iterate through all of the thread dictionary key/value pairs from the 19580b57cec5SDimitry Andric // structured data dictionary 19590b57cec5SDimitry Andric 1960349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 19610b57cec5SDimitry Andric thread_dict->ForEach([this, &tid, &expedited_register_map, &thread_name, 19620b57cec5SDimitry Andric &signo, &reason, &description, &exc_type, &exc_data, 19630b57cec5SDimitry Andric &thread_dispatch_qaddr, &queue_vars_valid, 19640b57cec5SDimitry Andric &associated_with_dispatch_queue, &dispatch_queue_t, 19650b57cec5SDimitry Andric &queue_name, &queue_kind, &queue_serial_number]( 19660b57cec5SDimitry Andric ConstString key, 19670b57cec5SDimitry Andric StructuredData::Object *object) -> bool { 19680b57cec5SDimitry Andric if (key == g_key_tid) { 19690b57cec5SDimitry Andric // thread in big endian hex 19700b57cec5SDimitry Andric tid = object->GetIntegerValue(LLDB_INVALID_THREAD_ID); 19710b57cec5SDimitry Andric } else if (key == g_key_metype) { 19720b57cec5SDimitry Andric // exception type in big endian hex 19730b57cec5SDimitry Andric exc_type = object->GetIntegerValue(0); 19740b57cec5SDimitry Andric } else if (key == g_key_medata) { 19750b57cec5SDimitry Andric // exception data in big endian hex 19760b57cec5SDimitry Andric StructuredData::Array *array = object->GetAsArray(); 19770b57cec5SDimitry Andric if (array) { 19780b57cec5SDimitry Andric array->ForEach([&exc_data](StructuredData::Object *object) -> bool { 19790b57cec5SDimitry Andric exc_data.push_back(object->GetIntegerValue()); 19800b57cec5SDimitry Andric return true; // Keep iterating through all array items 19810b57cec5SDimitry Andric }); 19820b57cec5SDimitry Andric } 19830b57cec5SDimitry Andric } else if (key == g_key_name) { 19845ffd83dbSDimitry Andric thread_name = std::string(object->GetStringValue()); 19850b57cec5SDimitry Andric } else if (key == g_key_qaddr) { 19860b57cec5SDimitry Andric thread_dispatch_qaddr = object->GetIntegerValue(LLDB_INVALID_ADDRESS); 19870b57cec5SDimitry Andric } else if (key == g_key_queue_name) { 19880b57cec5SDimitry Andric queue_vars_valid = true; 19895ffd83dbSDimitry Andric queue_name = std::string(object->GetStringValue()); 19900b57cec5SDimitry Andric } else if (key == g_key_queue_kind) { 19915ffd83dbSDimitry Andric std::string queue_kind_str = std::string(object->GetStringValue()); 19920b57cec5SDimitry Andric if (queue_kind_str == "serial") { 19930b57cec5SDimitry Andric queue_vars_valid = true; 19940b57cec5SDimitry Andric queue_kind = eQueueKindSerial; 19950b57cec5SDimitry Andric } else if (queue_kind_str == "concurrent") { 19960b57cec5SDimitry Andric queue_vars_valid = true; 19970b57cec5SDimitry Andric queue_kind = eQueueKindConcurrent; 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric } else if (key == g_key_queue_serial_number) { 20000b57cec5SDimitry Andric queue_serial_number = object->GetIntegerValue(0); 20010b57cec5SDimitry Andric if (queue_serial_number != 0) 20020b57cec5SDimitry Andric queue_vars_valid = true; 20030b57cec5SDimitry Andric } else if (key == g_key_dispatch_queue_t) { 20040b57cec5SDimitry Andric dispatch_queue_t = object->GetIntegerValue(0); 20050b57cec5SDimitry Andric if (dispatch_queue_t != 0 && dispatch_queue_t != LLDB_INVALID_ADDRESS) 20060b57cec5SDimitry Andric queue_vars_valid = true; 20070b57cec5SDimitry Andric } else if (key == g_key_associated_with_dispatch_queue) { 20080b57cec5SDimitry Andric queue_vars_valid = true; 20090b57cec5SDimitry Andric bool associated = object->GetBooleanValue(); 20100b57cec5SDimitry Andric if (associated) 20110b57cec5SDimitry Andric associated_with_dispatch_queue = eLazyBoolYes; 20120b57cec5SDimitry Andric else 20130b57cec5SDimitry Andric associated_with_dispatch_queue = eLazyBoolNo; 20140b57cec5SDimitry Andric } else if (key == g_key_reason) { 20155ffd83dbSDimitry Andric reason = std::string(object->GetStringValue()); 20160b57cec5SDimitry Andric } else if (key == g_key_description) { 20175ffd83dbSDimitry Andric description = std::string(object->GetStringValue()); 20180b57cec5SDimitry Andric } else if (key == g_key_registers) { 20190b57cec5SDimitry Andric StructuredData::Dictionary *registers_dict = object->GetAsDictionary(); 20200b57cec5SDimitry Andric 20210b57cec5SDimitry Andric if (registers_dict) { 20220b57cec5SDimitry Andric registers_dict->ForEach( 20230b57cec5SDimitry Andric [&expedited_register_map](ConstString key, 20240b57cec5SDimitry Andric StructuredData::Object *object) -> bool { 2025349cc55cSDimitry Andric uint32_t reg; 2026349cc55cSDimitry Andric if (llvm::to_integer(key.AsCString(), reg)) 20275ffd83dbSDimitry Andric expedited_register_map[reg] = 20285ffd83dbSDimitry Andric std::string(object->GetStringValue()); 20290b57cec5SDimitry Andric return true; // Keep iterating through all array items 20300b57cec5SDimitry Andric }); 20310b57cec5SDimitry Andric } 20320b57cec5SDimitry Andric } else if (key == g_key_memory) { 20330b57cec5SDimitry Andric StructuredData::Array *array = object->GetAsArray(); 20340b57cec5SDimitry Andric if (array) { 20350b57cec5SDimitry Andric array->ForEach([this](StructuredData::Object *object) -> bool { 20360b57cec5SDimitry Andric StructuredData::Dictionary *mem_cache_dict = 20370b57cec5SDimitry Andric object->GetAsDictionary(); 20380b57cec5SDimitry Andric if (mem_cache_dict) { 20390b57cec5SDimitry Andric lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; 20400b57cec5SDimitry Andric if (mem_cache_dict->GetValueForKeyAsInteger<lldb::addr_t>( 20410b57cec5SDimitry Andric "address", mem_cache_addr)) { 20420b57cec5SDimitry Andric if (mem_cache_addr != LLDB_INVALID_ADDRESS) { 20430b57cec5SDimitry Andric llvm::StringRef str; 20440b57cec5SDimitry Andric if (mem_cache_dict->GetValueForKeyAsString("bytes", str)) { 20450b57cec5SDimitry Andric StringExtractor bytes(str); 20460b57cec5SDimitry Andric bytes.SetFilePos(0); 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric const size_t byte_size = bytes.GetStringRef().size() / 2; 20490b57cec5SDimitry Andric DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); 20500b57cec5SDimitry Andric const size_t bytes_copied = 20510b57cec5SDimitry Andric bytes.GetHexBytes(data_buffer_sp->GetData(), 0); 20520b57cec5SDimitry Andric if (bytes_copied == byte_size) 20530b57cec5SDimitry Andric m_memory_cache.AddL1CacheData(mem_cache_addr, 20540b57cec5SDimitry Andric data_buffer_sp); 20550b57cec5SDimitry Andric } 20560b57cec5SDimitry Andric } 20570b57cec5SDimitry Andric } 20580b57cec5SDimitry Andric } 20590b57cec5SDimitry Andric return true; // Keep iterating through all array items 20600b57cec5SDimitry Andric }); 20610b57cec5SDimitry Andric } 20620b57cec5SDimitry Andric 20630b57cec5SDimitry Andric } else if (key == g_key_signal) 20640b57cec5SDimitry Andric signo = object->GetIntegerValue(LLDB_INVALID_SIGNAL_NUMBER); 20650b57cec5SDimitry Andric return true; // Keep iterating through all dictionary key/value pairs 20660b57cec5SDimitry Andric }); 20670b57cec5SDimitry Andric 20680b57cec5SDimitry Andric return SetThreadStopInfo(tid, expedited_register_map, signo, thread_name, 20690b57cec5SDimitry Andric reason, description, exc_type, exc_data, 20700b57cec5SDimitry Andric thread_dispatch_qaddr, queue_vars_valid, 20710b57cec5SDimitry Andric associated_with_dispatch_queue, dispatch_queue_t, 20720b57cec5SDimitry Andric queue_name, queue_kind, queue_serial_number); 20730b57cec5SDimitry Andric } 20740b57cec5SDimitry Andric 20750b57cec5SDimitry Andric StateType ProcessGDBRemote::SetThreadStopInfo(StringExtractor &stop_packet) { 2076fe6060f1SDimitry Andric lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); 20770b57cec5SDimitry Andric stop_packet.SetFilePos(0); 20780b57cec5SDimitry Andric const char stop_type = stop_packet.GetChar(); 20790b57cec5SDimitry Andric switch (stop_type) { 20800b57cec5SDimitry Andric case 'T': 20810b57cec5SDimitry Andric case 'S': { 20820b57cec5SDimitry Andric // This is a bit of a hack, but is is required. If we did exec, we need to 20830b57cec5SDimitry Andric // clear our thread lists and also know to rebuild our dynamic register 20840b57cec5SDimitry Andric // info before we lookup and threads and populate the expedited register 20850b57cec5SDimitry Andric // values so we need to know this right away so we can cleanup and update 20860b57cec5SDimitry Andric // our registers. 20870b57cec5SDimitry Andric const uint32_t stop_id = GetStopID(); 20880b57cec5SDimitry Andric if (stop_id == 0) { 20890b57cec5SDimitry Andric // Our first stop, make sure we have a process ID, and also make sure we 20900b57cec5SDimitry Andric // know about our registers 2091fe6060f1SDimitry Andric if (GetID() == LLDB_INVALID_PROCESS_ID && pid != LLDB_INVALID_PROCESS_ID) 20920b57cec5SDimitry Andric SetID(pid); 20930b57cec5SDimitry Andric BuildDynamicRegisterInfo(true); 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric // Stop with signal and thread info 2096fe6060f1SDimitry Andric lldb::pid_t stop_pid = LLDB_INVALID_PROCESS_ID; 20970b57cec5SDimitry Andric lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 20980b57cec5SDimitry Andric const uint8_t signo = stop_packet.GetHexU8(); 20990b57cec5SDimitry Andric llvm::StringRef key; 21000b57cec5SDimitry Andric llvm::StringRef value; 21010b57cec5SDimitry Andric std::string thread_name; 21020b57cec5SDimitry Andric std::string reason; 21030b57cec5SDimitry Andric std::string description; 21040b57cec5SDimitry Andric uint32_t exc_type = 0; 21050b57cec5SDimitry Andric std::vector<addr_t> exc_data; 21060b57cec5SDimitry Andric addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; 21070b57cec5SDimitry Andric bool queue_vars_valid = 21080b57cec5SDimitry Andric false; // says if locals below that start with "queue_" are valid 21090b57cec5SDimitry Andric addr_t dispatch_queue_t = LLDB_INVALID_ADDRESS; 21100b57cec5SDimitry Andric LazyBool associated_with_dispatch_queue = eLazyBoolCalculate; 21110b57cec5SDimitry Andric std::string queue_name; 21120b57cec5SDimitry Andric QueueKind queue_kind = eQueueKindUnknown; 21130b57cec5SDimitry Andric uint64_t queue_serial_number = 0; 21140b57cec5SDimitry Andric ExpeditedRegisterMap expedited_register_map; 21150b57cec5SDimitry Andric while (stop_packet.GetNameColonValue(key, value)) { 21160b57cec5SDimitry Andric if (key.compare("metype") == 0) { 21170b57cec5SDimitry Andric // exception type in big endian hex 21180b57cec5SDimitry Andric value.getAsInteger(16, exc_type); 21190b57cec5SDimitry Andric } else if (key.compare("medata") == 0) { 21200b57cec5SDimitry Andric // exception data in big endian hex 21210b57cec5SDimitry Andric uint64_t x; 21220b57cec5SDimitry Andric value.getAsInteger(16, x); 21230b57cec5SDimitry Andric exc_data.push_back(x); 21240b57cec5SDimitry Andric } else if (key.compare("thread") == 0) { 2125fe6060f1SDimitry Andric // thread-id 2126fe6060f1SDimitry Andric StringExtractorGDBRemote thread_id{value}; 2127fe6060f1SDimitry Andric auto pid_tid = thread_id.GetPidTid(pid); 2128fe6060f1SDimitry Andric if (pid_tid) { 2129fe6060f1SDimitry Andric stop_pid = pid_tid->first; 2130fe6060f1SDimitry Andric tid = pid_tid->second; 2131fe6060f1SDimitry Andric } else 21320b57cec5SDimitry Andric tid = LLDB_INVALID_THREAD_ID; 21330b57cec5SDimitry Andric } else if (key.compare("threads") == 0) { 21340b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 21350b57cec5SDimitry Andric m_thread_list_real.GetMutex()); 2136fe6060f1SDimitry Andric UpdateThreadIDsFromStopReplyThreadsValue(value); 21370b57cec5SDimitry Andric } else if (key.compare("thread-pcs") == 0) { 21380b57cec5SDimitry Andric m_thread_pcs.clear(); 21390b57cec5SDimitry Andric // A comma separated list of all threads in the current 21400b57cec5SDimitry Andric // process that includes the thread for this stop reply packet 21410b57cec5SDimitry Andric lldb::addr_t pc; 21420b57cec5SDimitry Andric while (!value.empty()) { 21430b57cec5SDimitry Andric llvm::StringRef pc_str; 21440b57cec5SDimitry Andric std::tie(pc_str, value) = value.split(','); 21450b57cec5SDimitry Andric if (pc_str.getAsInteger(16, pc)) 21460b57cec5SDimitry Andric pc = LLDB_INVALID_ADDRESS; 21470b57cec5SDimitry Andric m_thread_pcs.push_back(pc); 21480b57cec5SDimitry Andric } 21490b57cec5SDimitry Andric } else if (key.compare("jstopinfo") == 0) { 21500b57cec5SDimitry Andric StringExtractor json_extractor(value); 21510b57cec5SDimitry Andric std::string json; 21520b57cec5SDimitry Andric // Now convert the HEX bytes into a string value 21530b57cec5SDimitry Andric json_extractor.GetHexByteString(json); 21540b57cec5SDimitry Andric 21550b57cec5SDimitry Andric // This JSON contains thread IDs and thread stop info for all threads. 21560b57cec5SDimitry Andric // It doesn't contain expedited registers, memory or queue info. 21570b57cec5SDimitry Andric m_jstopinfo_sp = StructuredData::ParseJSON(json); 21580b57cec5SDimitry Andric } else if (key.compare("hexname") == 0) { 21590b57cec5SDimitry Andric StringExtractor name_extractor(value); 21600b57cec5SDimitry Andric std::string name; 21610b57cec5SDimitry Andric // Now convert the HEX bytes into a string value 21620b57cec5SDimitry Andric name_extractor.GetHexByteString(thread_name); 21630b57cec5SDimitry Andric } else if (key.compare("name") == 0) { 21645ffd83dbSDimitry Andric thread_name = std::string(value); 21650b57cec5SDimitry Andric } else if (key.compare("qaddr") == 0) { 21660b57cec5SDimitry Andric value.getAsInteger(16, thread_dispatch_qaddr); 21670b57cec5SDimitry Andric } else if (key.compare("dispatch_queue_t") == 0) { 21680b57cec5SDimitry Andric queue_vars_valid = true; 21690b57cec5SDimitry Andric value.getAsInteger(16, dispatch_queue_t); 21700b57cec5SDimitry Andric } else if (key.compare("qname") == 0) { 21710b57cec5SDimitry Andric queue_vars_valid = true; 21720b57cec5SDimitry Andric StringExtractor name_extractor(value); 21730b57cec5SDimitry Andric // Now convert the HEX bytes into a string value 21740b57cec5SDimitry Andric name_extractor.GetHexByteString(queue_name); 21750b57cec5SDimitry Andric } else if (key.compare("qkind") == 0) { 21760b57cec5SDimitry Andric queue_kind = llvm::StringSwitch<QueueKind>(value) 21770b57cec5SDimitry Andric .Case("serial", eQueueKindSerial) 21780b57cec5SDimitry Andric .Case("concurrent", eQueueKindConcurrent) 21790b57cec5SDimitry Andric .Default(eQueueKindUnknown); 21800b57cec5SDimitry Andric queue_vars_valid = queue_kind != eQueueKindUnknown; 21810b57cec5SDimitry Andric } else if (key.compare("qserialnum") == 0) { 21820b57cec5SDimitry Andric if (!value.getAsInteger(0, queue_serial_number)) 21830b57cec5SDimitry Andric queue_vars_valid = true; 21840b57cec5SDimitry Andric } else if (key.compare("reason") == 0) { 21855ffd83dbSDimitry Andric reason = std::string(value); 21860b57cec5SDimitry Andric } else if (key.compare("description") == 0) { 21870b57cec5SDimitry Andric StringExtractor desc_extractor(value); 21880b57cec5SDimitry Andric // Now convert the HEX bytes into a string value 21890b57cec5SDimitry Andric desc_extractor.GetHexByteString(description); 21900b57cec5SDimitry Andric } else if (key.compare("memory") == 0) { 21910b57cec5SDimitry Andric // Expedited memory. GDB servers can choose to send back expedited 21920b57cec5SDimitry Andric // memory that can populate the L1 memory cache in the process so that 21930b57cec5SDimitry Andric // things like the frame pointer backchain can be expedited. This will 21940b57cec5SDimitry Andric // help stack backtracing be more efficient by not having to send as 21950b57cec5SDimitry Andric // many memory read requests down the remote GDB server. 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric // Key/value pair format: memory:<addr>=<bytes>; 21980b57cec5SDimitry Andric // <addr> is a number whose base will be interpreted by the prefix: 21990b57cec5SDimitry Andric // "0x[0-9a-fA-F]+" for hex 22000b57cec5SDimitry Andric // "0[0-7]+" for octal 22010b57cec5SDimitry Andric // "[1-9]+" for decimal 22020b57cec5SDimitry Andric // <bytes> is native endian ASCII hex bytes just like the register 22030b57cec5SDimitry Andric // values 22040b57cec5SDimitry Andric llvm::StringRef addr_str, bytes_str; 22050b57cec5SDimitry Andric std::tie(addr_str, bytes_str) = value.split('='); 22060b57cec5SDimitry Andric if (!addr_str.empty() && !bytes_str.empty()) { 22070b57cec5SDimitry Andric lldb::addr_t mem_cache_addr = LLDB_INVALID_ADDRESS; 22080b57cec5SDimitry Andric if (!addr_str.getAsInteger(0, mem_cache_addr)) { 22090b57cec5SDimitry Andric StringExtractor bytes(bytes_str); 22100b57cec5SDimitry Andric const size_t byte_size = bytes.GetBytesLeft() / 2; 22110b57cec5SDimitry Andric DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0)); 22120b57cec5SDimitry Andric const size_t bytes_copied = 22130b57cec5SDimitry Andric bytes.GetHexBytes(data_buffer_sp->GetData(), 0); 22140b57cec5SDimitry Andric if (bytes_copied == byte_size) 22150b57cec5SDimitry Andric m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp); 22160b57cec5SDimitry Andric } 22170b57cec5SDimitry Andric } 22180b57cec5SDimitry Andric } else if (key.compare("watch") == 0 || key.compare("rwatch") == 0 || 22190b57cec5SDimitry Andric key.compare("awatch") == 0) { 22200b57cec5SDimitry Andric // Support standard GDB remote stop reply packet 'TAAwatch:addr' 22210b57cec5SDimitry Andric lldb::addr_t wp_addr = LLDB_INVALID_ADDRESS; 22220b57cec5SDimitry Andric value.getAsInteger(16, wp_addr); 22230b57cec5SDimitry Andric 22240b57cec5SDimitry Andric WatchpointSP wp_sp = 22250b57cec5SDimitry Andric GetTarget().GetWatchpointList().FindByAddress(wp_addr); 22260b57cec5SDimitry Andric uint32_t wp_index = LLDB_INVALID_INDEX32; 22270b57cec5SDimitry Andric 22280b57cec5SDimitry Andric if (wp_sp) 22290b57cec5SDimitry Andric wp_index = wp_sp->GetHardwareIndex(); 22300b57cec5SDimitry Andric 22310b57cec5SDimitry Andric reason = "watchpoint"; 22320b57cec5SDimitry Andric StreamString ostr; 22330b57cec5SDimitry Andric ostr.Printf("%" PRIu64 " %" PRIu32, wp_addr, wp_index); 22345ffd83dbSDimitry Andric description = std::string(ostr.GetString()); 22350b57cec5SDimitry Andric } else if (key.compare("library") == 0) { 22369dba64beSDimitry Andric auto error = LoadModules(); 22379dba64beSDimitry Andric if (error) { 22389dba64beSDimitry Andric Log *log( 22399dba64beSDimitry Andric ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 22409dba64beSDimitry Andric LLDB_LOG_ERROR(log, std::move(error), "Failed to load modules: {0}"); 22419dba64beSDimitry Andric } 2242349cc55cSDimitry Andric } else if (key.compare("fork") == 0 || key.compare("vfork") == 0) { 2243349cc55cSDimitry Andric // fork includes child pid/tid in thread-id format 2244349cc55cSDimitry Andric StringExtractorGDBRemote thread_id{value}; 2245349cc55cSDimitry Andric auto pid_tid = thread_id.GetPidTid(LLDB_INVALID_PROCESS_ID); 2246349cc55cSDimitry Andric if (!pid_tid) { 2247349cc55cSDimitry Andric Log *log( 2248349cc55cSDimitry Andric ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 2249349cc55cSDimitry Andric LLDB_LOG(log, "Invalid PID/TID to fork: {0}", value); 2250349cc55cSDimitry Andric pid_tid = {{LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID}}; 2251349cc55cSDimitry Andric } 2252349cc55cSDimitry Andric 2253349cc55cSDimitry Andric reason = key.str(); 2254349cc55cSDimitry Andric StreamString ostr; 2255349cc55cSDimitry Andric ostr.Printf("%" PRIu64 " %" PRIu64, pid_tid->first, pid_tid->second); 2256349cc55cSDimitry Andric description = std::string(ostr.GetString()); 22570b57cec5SDimitry Andric } else if (key.size() == 2 && ::isxdigit(key[0]) && ::isxdigit(key[1])) { 22580b57cec5SDimitry Andric uint32_t reg = UINT32_MAX; 22590b57cec5SDimitry Andric if (!key.getAsInteger(16, reg)) 22605ffd83dbSDimitry Andric expedited_register_map[reg] = std::string(std::move(value)); 22610b57cec5SDimitry Andric } 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric 2264fe6060f1SDimitry Andric if (stop_pid != LLDB_INVALID_PROCESS_ID && stop_pid != pid) { 2265fe6060f1SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 2266fe6060f1SDimitry Andric LLDB_LOG(log, 2267fe6060f1SDimitry Andric "Received stop for incorrect PID = {0} (inferior PID = {1})", 2268fe6060f1SDimitry Andric stop_pid, pid); 2269fe6060f1SDimitry Andric return eStateInvalid; 2270fe6060f1SDimitry Andric } 2271fe6060f1SDimitry Andric 22720b57cec5SDimitry Andric if (tid == LLDB_INVALID_THREAD_ID) { 22730b57cec5SDimitry Andric // A thread id may be invalid if the response is old style 'S' packet 22740b57cec5SDimitry Andric // which does not provide the 22750b57cec5SDimitry Andric // thread information. So update the thread list and choose the first 22760b57cec5SDimitry Andric // one. 22770b57cec5SDimitry Andric UpdateThreadIDList(); 22780b57cec5SDimitry Andric 22790b57cec5SDimitry Andric if (!m_thread_ids.empty()) { 22800b57cec5SDimitry Andric tid = m_thread_ids.front(); 22810b57cec5SDimitry Andric } 22820b57cec5SDimitry Andric } 22830b57cec5SDimitry Andric 22840b57cec5SDimitry Andric ThreadSP thread_sp = SetThreadStopInfo( 22850b57cec5SDimitry Andric tid, expedited_register_map, signo, thread_name, reason, description, 22860b57cec5SDimitry Andric exc_type, exc_data, thread_dispatch_qaddr, queue_vars_valid, 22870b57cec5SDimitry Andric associated_with_dispatch_queue, dispatch_queue_t, queue_name, 22880b57cec5SDimitry Andric queue_kind, queue_serial_number); 22890b57cec5SDimitry Andric 22900b57cec5SDimitry Andric return eStateStopped; 22910b57cec5SDimitry Andric } break; 22920b57cec5SDimitry Andric 22930b57cec5SDimitry Andric case 'W': 22940b57cec5SDimitry Andric case 'X': 22950b57cec5SDimitry Andric // process exited 22960b57cec5SDimitry Andric return eStateExited; 22970b57cec5SDimitry Andric 22980b57cec5SDimitry Andric default: 22990b57cec5SDimitry Andric break; 23000b57cec5SDimitry Andric } 23010b57cec5SDimitry Andric return eStateInvalid; 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric 23040b57cec5SDimitry Andric void ProcessGDBRemote::RefreshStateAfterStop() { 23050b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_thread_list_real.GetMutex()); 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric m_thread_ids.clear(); 23080b57cec5SDimitry Andric m_thread_pcs.clear(); 2309480093f4SDimitry Andric 23100b57cec5SDimitry Andric // Set the thread stop info. It might have a "threads" key whose value is a 23110b57cec5SDimitry Andric // list of all thread IDs in the current process, so m_thread_ids might get 23120b57cec5SDimitry Andric // set. 23130b57cec5SDimitry Andric // Check to see if SetThreadStopInfo() filled in m_thread_ids? 23140b57cec5SDimitry Andric if (m_thread_ids.empty()) { 23150b57cec5SDimitry Andric // No, we need to fetch the thread list manually 23160b57cec5SDimitry Andric UpdateThreadIDList(); 23170b57cec5SDimitry Andric } 2318480093f4SDimitry Andric 23190b57cec5SDimitry Andric // We might set some stop info's so make sure the thread list is up to 23200b57cec5SDimitry Andric // date before we do that or we might overwrite what was computed here. 23210b57cec5SDimitry Andric UpdateThreadListIfNeeded(); 23220b57cec5SDimitry Andric 2323349cc55cSDimitry Andric if (m_last_stop_packet) 2324349cc55cSDimitry Andric SetThreadStopInfo(*m_last_stop_packet); 2325349cc55cSDimitry Andric m_last_stop_packet.reset(); 23260b57cec5SDimitry Andric 23270b57cec5SDimitry Andric // If we have queried for a default thread id 23280b57cec5SDimitry Andric if (m_initial_tid != LLDB_INVALID_THREAD_ID) { 23290b57cec5SDimitry Andric m_thread_list.SetSelectedThreadByID(m_initial_tid); 23300b57cec5SDimitry Andric m_initial_tid = LLDB_INVALID_THREAD_ID; 23310b57cec5SDimitry Andric } 23320b57cec5SDimitry Andric 23330b57cec5SDimitry Andric // Let all threads recover from stopping and do any clean up based on the 23340b57cec5SDimitry Andric // previous thread state (if any). 23350b57cec5SDimitry Andric m_thread_list_real.RefreshStateAfterStop(); 23360b57cec5SDimitry Andric } 23370b57cec5SDimitry Andric 23380b57cec5SDimitry Andric Status ProcessGDBRemote::DoHalt(bool &caused_stop) { 23390b57cec5SDimitry Andric Status error; 23400b57cec5SDimitry Andric 23410b57cec5SDimitry Andric if (m_public_state.GetValue() == eStateAttaching) { 23420b57cec5SDimitry Andric // We are being asked to halt during an attach. We need to just close our 23430b57cec5SDimitry Andric // file handle and debugserver will go away, and we can be done... 23440b57cec5SDimitry Andric m_gdb_comm.Disconnect(); 23450b57cec5SDimitry Andric } else 2346fe6060f1SDimitry Andric caused_stop = m_gdb_comm.Interrupt(GetInterruptTimeout()); 23470b57cec5SDimitry Andric return error; 23480b57cec5SDimitry Andric } 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric Status ProcessGDBRemote::DoDetach(bool keep_stopped) { 23510b57cec5SDimitry Andric Status error; 23520b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 23539dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); 23540b57cec5SDimitry Andric 23550b57cec5SDimitry Andric error = m_gdb_comm.Detach(keep_stopped); 23560b57cec5SDimitry Andric if (log) { 23570b57cec5SDimitry Andric if (error.Success()) 23580b57cec5SDimitry Andric log->PutCString( 23590b57cec5SDimitry Andric "ProcessGDBRemote::DoDetach() detach packet sent successfully"); 23600b57cec5SDimitry Andric else 23619dba64beSDimitry Andric LLDB_LOGF(log, 23629dba64beSDimitry Andric "ProcessGDBRemote::DoDetach() detach packet send failed: %s", 23630b57cec5SDimitry Andric error.AsCString() ? error.AsCString() : "<unknown error>"); 23640b57cec5SDimitry Andric } 23650b57cec5SDimitry Andric 23660b57cec5SDimitry Andric if (!error.Success()) 23670b57cec5SDimitry Andric return error; 23680b57cec5SDimitry Andric 23690b57cec5SDimitry Andric // Sleep for one second to let the process get all detached... 23700b57cec5SDimitry Andric StopAsyncThread(); 23710b57cec5SDimitry Andric 23720b57cec5SDimitry Andric SetPrivateState(eStateDetached); 23730b57cec5SDimitry Andric ResumePrivateStateThread(); 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric // KillDebugserverProcess (); 23760b57cec5SDimitry Andric return error; 23770b57cec5SDimitry Andric } 23780b57cec5SDimitry Andric 23790b57cec5SDimitry Andric Status ProcessGDBRemote::DoDestroy() { 23800b57cec5SDimitry Andric Status error; 23810b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 23829dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoDestroy()"); 23830b57cec5SDimitry Andric 2384580012d6SDimitry Andric #ifdef LLDB_ENABLE_ALL // XXX Currently no iOS target support on FreeBSD 23850b57cec5SDimitry Andric // There is a bug in older iOS debugservers where they don't shut down the 23860b57cec5SDimitry Andric // process they are debugging properly. If the process is sitting at a 23870b57cec5SDimitry Andric // breakpoint or an exception, this can cause problems with restarting. So 23880b57cec5SDimitry Andric // we check to see if any of our threads are stopped at a breakpoint, and if 23890b57cec5SDimitry Andric // so we remove all the breakpoints, resume the process, and THEN destroy it 23900b57cec5SDimitry Andric // again. 23910b57cec5SDimitry Andric // 23920b57cec5SDimitry Andric // Note, we don't have a good way to test the version of debugserver, but I 23930b57cec5SDimitry Andric // happen to know that the set of all the iOS debugservers which don't 23940b57cec5SDimitry Andric // support GetThreadSuffixSupported() and that of the debugservers with this 23950b57cec5SDimitry Andric // bug are equal. There really should be a better way to test this! 23960b57cec5SDimitry Andric // 23970b57cec5SDimitry Andric // We also use m_destroy_tried_resuming to make sure we only do this once, if 23980b57cec5SDimitry Andric // we resume and then halt and get called here to destroy again and we're 23990b57cec5SDimitry Andric // still at a breakpoint or exception, then we should just do the straight- 24000b57cec5SDimitry Andric // forward kill. 24010b57cec5SDimitry Andric // 24020b57cec5SDimitry Andric // And of course, if we weren't able to stop the process by the time we get 24030b57cec5SDimitry Andric // here, it isn't necessary (or helpful) to do any of this. 24040b57cec5SDimitry Andric 24050b57cec5SDimitry Andric if (!m_gdb_comm.GetThreadSuffixSupported() && 24060b57cec5SDimitry Andric m_public_state.GetValue() != eStateRunning) { 24070b57cec5SDimitry Andric PlatformSP platform_sp = GetTarget().GetPlatform(); 24080b57cec5SDimitry Andric 24090b57cec5SDimitry Andric if (platform_sp && platform_sp->GetName() && 2410349cc55cSDimitry Andric platform_sp->GetName().GetStringRef() == 2411349cc55cSDimitry Andric PlatformRemoteiOS::GetPluginNameStatic()) { 24120b57cec5SDimitry Andric if (m_destroy_tried_resuming) { 24130b57cec5SDimitry Andric if (log) 24140b57cec5SDimitry Andric log->PutCString("ProcessGDBRemote::DoDestroy() - Tried resuming to " 24150b57cec5SDimitry Andric "destroy once already, not doing it again."); 24160b57cec5SDimitry Andric } else { 24170b57cec5SDimitry Andric // At present, the plans are discarded and the breakpoints disabled 24180b57cec5SDimitry Andric // Process::Destroy, but we really need it to happen here and it 24190b57cec5SDimitry Andric // doesn't matter if we do it twice. 24200b57cec5SDimitry Andric m_thread_list.DiscardThreadPlans(); 24210b57cec5SDimitry Andric DisableAllBreakpointSites(); 24220b57cec5SDimitry Andric 24230b57cec5SDimitry Andric bool stop_looks_like_crash = false; 24240b57cec5SDimitry Andric ThreadList &threads = GetThreadList(); 24250b57cec5SDimitry Andric 24260b57cec5SDimitry Andric { 24270b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric size_t num_threads = threads.GetSize(); 24300b57cec5SDimitry Andric for (size_t i = 0; i < num_threads; i++) { 24310b57cec5SDimitry Andric ThreadSP thread_sp = threads.GetThreadAtIndex(i); 24320b57cec5SDimitry Andric StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); 24330b57cec5SDimitry Andric StopReason reason = eStopReasonInvalid; 24340b57cec5SDimitry Andric if (stop_info_sp) 24350b57cec5SDimitry Andric reason = stop_info_sp->GetStopReason(); 24360b57cec5SDimitry Andric if (reason == eStopReasonBreakpoint || 24370b57cec5SDimitry Andric reason == eStopReasonException) { 24389dba64beSDimitry Andric LLDB_LOGF(log, 24390b57cec5SDimitry Andric "ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 24400b57cec5SDimitry Andric " stopped with reason: %s.", 24419dba64beSDimitry Andric thread_sp->GetProtocolID(), 24429dba64beSDimitry Andric stop_info_sp->GetDescription()); 24430b57cec5SDimitry Andric stop_looks_like_crash = true; 24440b57cec5SDimitry Andric break; 24450b57cec5SDimitry Andric } 24460b57cec5SDimitry Andric } 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 24490b57cec5SDimitry Andric if (stop_looks_like_crash) { 24500b57cec5SDimitry Andric if (log) 24510b57cec5SDimitry Andric log->PutCString("ProcessGDBRemote::DoDestroy() - Stopped at a " 24520b57cec5SDimitry Andric "breakpoint, continue and then kill."); 24530b57cec5SDimitry Andric m_destroy_tried_resuming = true; 24540b57cec5SDimitry Andric 24550b57cec5SDimitry Andric // If we are going to run again before killing, it would be good to 24560b57cec5SDimitry Andric // suspend all the threads before resuming so they won't get into 24570b57cec5SDimitry Andric // more trouble. Sadly, for the threads stopped with the breakpoint 24580b57cec5SDimitry Andric // or exception, the exception doesn't get cleared if it is 24590b57cec5SDimitry Andric // suspended, so we do have to run the risk of letting those threads 24600b57cec5SDimitry Andric // proceed a bit. 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric { 24630b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); 24640b57cec5SDimitry Andric 24650b57cec5SDimitry Andric size_t num_threads = threads.GetSize(); 24660b57cec5SDimitry Andric for (size_t i = 0; i < num_threads; i++) { 24670b57cec5SDimitry Andric ThreadSP thread_sp = threads.GetThreadAtIndex(i); 24680b57cec5SDimitry Andric StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); 24690b57cec5SDimitry Andric StopReason reason = eStopReasonInvalid; 24700b57cec5SDimitry Andric if (stop_info_sp) 24710b57cec5SDimitry Andric reason = stop_info_sp->GetStopReason(); 24720b57cec5SDimitry Andric if (reason != eStopReasonBreakpoint && 24730b57cec5SDimitry Andric reason != eStopReasonException) { 24749dba64beSDimitry Andric LLDB_LOGF(log, 24759dba64beSDimitry Andric "ProcessGDBRemote::DoDestroy() - Suspending " 24760b57cec5SDimitry Andric "thread: 0x%4.4" PRIx64 " before running.", 24770b57cec5SDimitry Andric thread_sp->GetProtocolID()); 24780b57cec5SDimitry Andric thread_sp->SetResumeState(eStateSuspended); 24790b57cec5SDimitry Andric } 24800b57cec5SDimitry Andric } 24810b57cec5SDimitry Andric } 24820b57cec5SDimitry Andric Resume(); 24830b57cec5SDimitry Andric return Destroy(false); 24840b57cec5SDimitry Andric } 24850b57cec5SDimitry Andric } 24860b57cec5SDimitry Andric } 24870b57cec5SDimitry Andric } 2488580012d6SDimitry Andric #endif // LLDB_ENABLE_ALL 24890b57cec5SDimitry Andric 24900b57cec5SDimitry Andric // Interrupt if our inferior is running... 24910b57cec5SDimitry Andric int exit_status = SIGABRT; 24920b57cec5SDimitry Andric std::string exit_string; 24930b57cec5SDimitry Andric 24940b57cec5SDimitry Andric if (m_gdb_comm.IsConnected()) { 24950b57cec5SDimitry Andric if (m_public_state.GetValue() != eStateAttaching) { 24960b57cec5SDimitry Andric StringExtractorGDBRemote response; 24970b57cec5SDimitry Andric GDBRemoteCommunication::ScopedTimeout(m_gdb_comm, 24980b57cec5SDimitry Andric std::chrono::seconds(3)); 24990b57cec5SDimitry Andric 2500fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse("k", response, 2501fe6060f1SDimitry Andric GetInterruptTimeout()) == 25020b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 25030b57cec5SDimitry Andric char packet_cmd = response.GetChar(0); 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric if (packet_cmd == 'W' || packet_cmd == 'X') { 25060b57cec5SDimitry Andric #if defined(__APPLE__) 25070b57cec5SDimitry Andric // For Native processes on Mac OS X, we launch through the Host 25080b57cec5SDimitry Andric // Platform, then hand the process off to debugserver, which becomes 25090b57cec5SDimitry Andric // the parent process through "PT_ATTACH". Then when we go to kill 25100b57cec5SDimitry Andric // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then 25110b57cec5SDimitry Andric // we call waitpid which returns with no error and the correct 25120b57cec5SDimitry Andric // status. But amusingly enough that doesn't seem to actually reap 25130b57cec5SDimitry Andric // the process, but instead it is left around as a Zombie. Probably 25140b57cec5SDimitry Andric // the kernel is in the process of switching ownership back to lldb 25150b57cec5SDimitry Andric // which was the original parent, and gets confused in the handoff. 25160b57cec5SDimitry Andric // Anyway, so call waitpid here to finally reap it. 25170b57cec5SDimitry Andric PlatformSP platform_sp(GetTarget().GetPlatform()); 25180b57cec5SDimitry Andric if (platform_sp && platform_sp->IsHost()) { 25190b57cec5SDimitry Andric int status; 25200b57cec5SDimitry Andric ::pid_t reap_pid; 25210b57cec5SDimitry Andric reap_pid = waitpid(GetID(), &status, WNOHANG); 25229dba64beSDimitry Andric LLDB_LOGF(log, "Reaped pid: %d, status: %d.\n", reap_pid, status); 25230b57cec5SDimitry Andric } 25240b57cec5SDimitry Andric #endif 25250b57cec5SDimitry Andric SetLastStopPacket(response); 25260b57cec5SDimitry Andric ClearThreadIDList(); 25270b57cec5SDimitry Andric exit_status = response.GetHexU8(); 25280b57cec5SDimitry Andric } else { 25299dba64beSDimitry Andric LLDB_LOGF(log, 25309dba64beSDimitry Andric "ProcessGDBRemote::DoDestroy - got unexpected response " 25310b57cec5SDimitry Andric "to k packet: %s", 25329dba64beSDimitry Andric response.GetStringRef().data()); 25330b57cec5SDimitry Andric exit_string.assign("got unexpected response to k packet: "); 25345ffd83dbSDimitry Andric exit_string.append(std::string(response.GetStringRef())); 25350b57cec5SDimitry Andric } 25360b57cec5SDimitry Andric } else { 25379dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoDestroy - failed to send k packet"); 25380b57cec5SDimitry Andric exit_string.assign("failed to send the k packet"); 25390b57cec5SDimitry Andric } 25400b57cec5SDimitry Andric } else { 25419dba64beSDimitry Andric LLDB_LOGF(log, 25429dba64beSDimitry Andric "ProcessGDBRemote::DoDestroy - killed or interrupted while " 25430b57cec5SDimitry Andric "attaching"); 25440b57cec5SDimitry Andric exit_string.assign("killed or interrupted while attaching."); 25450b57cec5SDimitry Andric } 25460b57cec5SDimitry Andric } else { 25470b57cec5SDimitry Andric // If we missed setting the exit status on the way out, do it here. 25480b57cec5SDimitry Andric // NB set exit status can be called multiple times, the first one sets the 25490b57cec5SDimitry Andric // status. 25500b57cec5SDimitry Andric exit_string.assign("destroying when not connected to debugserver"); 25510b57cec5SDimitry Andric } 25520b57cec5SDimitry Andric 25530b57cec5SDimitry Andric SetExitStatus(exit_status, exit_string.c_str()); 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric StopAsyncThread(); 25560b57cec5SDimitry Andric KillDebugserverProcess(); 25570b57cec5SDimitry Andric return error; 25580b57cec5SDimitry Andric } 25590b57cec5SDimitry Andric 25600b57cec5SDimitry Andric void ProcessGDBRemote::SetLastStopPacket( 25610b57cec5SDimitry Andric const StringExtractorGDBRemote &response) { 25620b57cec5SDimitry Andric const bool did_exec = 25630b57cec5SDimitry Andric response.GetStringRef().find(";reason:exec;") != std::string::npos; 25640b57cec5SDimitry Andric if (did_exec) { 25650b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 25669dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::SetLastStopPacket () - detected exec"); 25670b57cec5SDimitry Andric 25680b57cec5SDimitry Andric m_thread_list_real.Clear(); 25690b57cec5SDimitry Andric m_thread_list.Clear(); 25700b57cec5SDimitry Andric BuildDynamicRegisterInfo(true); 25710b57cec5SDimitry Andric m_gdb_comm.ResetDiscoverableSettings(did_exec); 25720b57cec5SDimitry Andric } 25730b57cec5SDimitry Andric 2574349cc55cSDimitry Andric m_last_stop_packet = response; 25750b57cec5SDimitry Andric } 25760b57cec5SDimitry Andric 25770b57cec5SDimitry Andric void ProcessGDBRemote::SetUnixSignals(const UnixSignalsSP &signals_sp) { 25780b57cec5SDimitry Andric Process::SetUnixSignals(std::make_shared<GDBRemoteSignals>(signals_sp)); 25790b57cec5SDimitry Andric } 25800b57cec5SDimitry Andric 25810b57cec5SDimitry Andric // Process Queries 25820b57cec5SDimitry Andric 25830b57cec5SDimitry Andric bool ProcessGDBRemote::IsAlive() { 25840b57cec5SDimitry Andric return m_gdb_comm.IsConnected() && Process::IsAlive(); 25850b57cec5SDimitry Andric } 25860b57cec5SDimitry Andric 25870b57cec5SDimitry Andric addr_t ProcessGDBRemote::GetImageInfoAddress() { 25880b57cec5SDimitry Andric // request the link map address via the $qShlibInfoAddr packet 25890b57cec5SDimitry Andric lldb::addr_t addr = m_gdb_comm.GetShlibInfoAddr(); 25900b57cec5SDimitry Andric 25910b57cec5SDimitry Andric // the loaded module list can also provides a link map address 25920b57cec5SDimitry Andric if (addr == LLDB_INVALID_ADDRESS) { 25939dba64beSDimitry Andric llvm::Expected<LoadedModuleInfoList> list = GetLoadedModuleList(); 25949dba64beSDimitry Andric if (!list) { 25959dba64beSDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 2596e8d8bef9SDimitry Andric LLDB_LOG_ERROR(log, list.takeError(), "Failed to read module list: {0}."); 25979dba64beSDimitry Andric } else { 25989dba64beSDimitry Andric addr = list->m_link_map; 25999dba64beSDimitry Andric } 26000b57cec5SDimitry Andric } 26010b57cec5SDimitry Andric 26020b57cec5SDimitry Andric return addr; 26030b57cec5SDimitry Andric } 26040b57cec5SDimitry Andric 26050b57cec5SDimitry Andric void ProcessGDBRemote::WillPublicStop() { 26060b57cec5SDimitry Andric // See if the GDB remote client supports the JSON threads info. If so, we 26070b57cec5SDimitry Andric // gather stop info for all threads, expedited registers, expedited memory, 26080b57cec5SDimitry Andric // runtime queue information (iOS and MacOSX only), and more. Expediting 26090b57cec5SDimitry Andric // memory will help stack backtracing be much faster. Expediting registers 26100b57cec5SDimitry Andric // will make sure we don't have to read the thread registers for GPRs. 26110b57cec5SDimitry Andric m_jthreadsinfo_sp = m_gdb_comm.GetThreadsInfo(); 26120b57cec5SDimitry Andric 26130b57cec5SDimitry Andric if (m_jthreadsinfo_sp) { 26140b57cec5SDimitry Andric // Now set the stop info for each thread and also expedite any registers 26150b57cec5SDimitry Andric // and memory that was in the jThreadsInfo response. 26160b57cec5SDimitry Andric StructuredData::Array *thread_infos = m_jthreadsinfo_sp->GetAsArray(); 26170b57cec5SDimitry Andric if (thread_infos) { 26180b57cec5SDimitry Andric const size_t n = thread_infos->GetSize(); 26190b57cec5SDimitry Andric for (size_t i = 0; i < n; ++i) { 26200b57cec5SDimitry Andric StructuredData::Dictionary *thread_dict = 26210b57cec5SDimitry Andric thread_infos->GetItemAtIndex(i)->GetAsDictionary(); 26220b57cec5SDimitry Andric if (thread_dict) 26230b57cec5SDimitry Andric SetThreadStopInfo(thread_dict); 26240b57cec5SDimitry Andric } 26250b57cec5SDimitry Andric } 26260b57cec5SDimitry Andric } 26270b57cec5SDimitry Andric } 26280b57cec5SDimitry Andric 26290b57cec5SDimitry Andric // Process Memory 26300b57cec5SDimitry Andric size_t ProcessGDBRemote::DoReadMemory(addr_t addr, void *buf, size_t size, 26310b57cec5SDimitry Andric Status &error) { 26320b57cec5SDimitry Andric GetMaxMemorySize(); 26330b57cec5SDimitry Andric bool binary_memory_read = m_gdb_comm.GetxPacketSupported(); 26340b57cec5SDimitry Andric // M and m packets take 2 bytes for 1 byte of memory 26350b57cec5SDimitry Andric size_t max_memory_size = 26360b57cec5SDimitry Andric binary_memory_read ? m_max_memory_size : m_max_memory_size / 2; 26370b57cec5SDimitry Andric if (size > max_memory_size) { 26380b57cec5SDimitry Andric // Keep memory read sizes down to a sane limit. This function will be 26390b57cec5SDimitry Andric // called multiple times in order to complete the task by 26400b57cec5SDimitry Andric // lldb_private::Process so it is ok to do this. 26410b57cec5SDimitry Andric size = max_memory_size; 26420b57cec5SDimitry Andric } 26430b57cec5SDimitry Andric 26440b57cec5SDimitry Andric char packet[64]; 26450b57cec5SDimitry Andric int packet_len; 26460b57cec5SDimitry Andric packet_len = ::snprintf(packet, sizeof(packet), "%c%" PRIx64 ",%" PRIx64, 26470b57cec5SDimitry Andric binary_memory_read ? 'x' : 'm', (uint64_t)addr, 26480b57cec5SDimitry Andric (uint64_t)size); 26490b57cec5SDimitry Andric assert(packet_len + 1 < (int)sizeof(packet)); 26500b57cec5SDimitry Andric UNUSED_IF_ASSERT_DISABLED(packet_len); 26510b57cec5SDimitry Andric StringExtractorGDBRemote response; 2652fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet, response, 2653fe6060f1SDimitry Andric GetInterruptTimeout()) == 26540b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 26550b57cec5SDimitry Andric if (response.IsNormalResponse()) { 26560b57cec5SDimitry Andric error.Clear(); 26570b57cec5SDimitry Andric if (binary_memory_read) { 26580b57cec5SDimitry Andric // The lower level GDBRemoteCommunication packet receive layer has 26590b57cec5SDimitry Andric // already de-quoted any 0x7d character escaping that was present in 26600b57cec5SDimitry Andric // the packet 26610b57cec5SDimitry Andric 26620b57cec5SDimitry Andric size_t data_received_size = response.GetBytesLeft(); 26630b57cec5SDimitry Andric if (data_received_size > size) { 26640b57cec5SDimitry Andric // Don't write past the end of BUF if the remote debug server gave us 26650b57cec5SDimitry Andric // too much data for some reason. 26660b57cec5SDimitry Andric data_received_size = size; 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric memcpy(buf, response.GetStringRef().data(), data_received_size); 26690b57cec5SDimitry Andric return data_received_size; 26700b57cec5SDimitry Andric } else { 26710b57cec5SDimitry Andric return response.GetHexBytes( 26720b57cec5SDimitry Andric llvm::MutableArrayRef<uint8_t>((uint8_t *)buf, size), '\xdd'); 26730b57cec5SDimitry Andric } 26740b57cec5SDimitry Andric } else if (response.IsErrorResponse()) 26750b57cec5SDimitry Andric error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); 26760b57cec5SDimitry Andric else if (response.IsUnsupportedResponse()) 26770b57cec5SDimitry Andric error.SetErrorStringWithFormat( 26780b57cec5SDimitry Andric "GDB server does not support reading memory"); 26790b57cec5SDimitry Andric else 26800b57cec5SDimitry Andric error.SetErrorStringWithFormat( 26810b57cec5SDimitry Andric "unexpected response to GDB server memory read packet '%s': '%s'", 26829dba64beSDimitry Andric packet, response.GetStringRef().data()); 26830b57cec5SDimitry Andric } else { 26840b57cec5SDimitry Andric error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); 26850b57cec5SDimitry Andric } 26860b57cec5SDimitry Andric return 0; 26870b57cec5SDimitry Andric } 26880b57cec5SDimitry Andric 2689fe6060f1SDimitry Andric bool ProcessGDBRemote::SupportsMemoryTagging() { 2690fe6060f1SDimitry Andric return m_gdb_comm.GetMemoryTaggingSupported(); 2691fe6060f1SDimitry Andric } 2692fe6060f1SDimitry Andric 2693fe6060f1SDimitry Andric llvm::Expected<std::vector<uint8_t>> 2694fe6060f1SDimitry Andric ProcessGDBRemote::DoReadMemoryTags(lldb::addr_t addr, size_t len, 2695fe6060f1SDimitry Andric int32_t type) { 2696fe6060f1SDimitry Andric // By this point ReadMemoryTags has validated that tagging is enabled 2697fe6060f1SDimitry Andric // for this target/process/address. 2698fe6060f1SDimitry Andric DataBufferSP buffer_sp = m_gdb_comm.ReadMemoryTags(addr, len, type); 2699fe6060f1SDimitry Andric if (!buffer_sp) { 2700fe6060f1SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 2701fe6060f1SDimitry Andric "Error reading memory tags from remote"); 2702fe6060f1SDimitry Andric } 2703fe6060f1SDimitry Andric 2704fe6060f1SDimitry Andric // Return the raw tag data 2705fe6060f1SDimitry Andric llvm::ArrayRef<uint8_t> tag_data = buffer_sp->GetData(); 2706fe6060f1SDimitry Andric std::vector<uint8_t> got; 2707fe6060f1SDimitry Andric got.reserve(tag_data.size()); 2708fe6060f1SDimitry Andric std::copy(tag_data.begin(), tag_data.end(), std::back_inserter(got)); 2709fe6060f1SDimitry Andric return got; 2710fe6060f1SDimitry Andric } 2711fe6060f1SDimitry Andric 2712fe6060f1SDimitry Andric Status ProcessGDBRemote::DoWriteMemoryTags(lldb::addr_t addr, size_t len, 2713fe6060f1SDimitry Andric int32_t type, 2714fe6060f1SDimitry Andric const std::vector<uint8_t> &tags) { 2715fe6060f1SDimitry Andric // By now WriteMemoryTags should have validated that tagging is enabled 2716fe6060f1SDimitry Andric // for this target/process. 2717fe6060f1SDimitry Andric return m_gdb_comm.WriteMemoryTags(addr, len, type, tags); 2718fe6060f1SDimitry Andric } 2719fe6060f1SDimitry Andric 27200b57cec5SDimitry Andric Status ProcessGDBRemote::WriteObjectFile( 27210b57cec5SDimitry Andric std::vector<ObjectFile::LoadableData> entries) { 27220b57cec5SDimitry Andric Status error; 27230b57cec5SDimitry Andric // Sort the entries by address because some writes, like those to flash 27240b57cec5SDimitry Andric // memory, must happen in order of increasing address. 27250b57cec5SDimitry Andric std::stable_sort( 27260b57cec5SDimitry Andric std::begin(entries), std::end(entries), 27270b57cec5SDimitry Andric [](const ObjectFile::LoadableData a, const ObjectFile::LoadableData b) { 27280b57cec5SDimitry Andric return a.Dest < b.Dest; 27290b57cec5SDimitry Andric }); 27300b57cec5SDimitry Andric m_allow_flash_writes = true; 27310b57cec5SDimitry Andric error = Process::WriteObjectFile(entries); 27320b57cec5SDimitry Andric if (error.Success()) 27330b57cec5SDimitry Andric error = FlashDone(); 27340b57cec5SDimitry Andric else 27350b57cec5SDimitry Andric // Even though some of the writing failed, try to send a flash done if some 27360b57cec5SDimitry Andric // of the writing succeeded so the flash state is reset to normal, but 27370b57cec5SDimitry Andric // don't stomp on the error status that was set in the write failure since 27380b57cec5SDimitry Andric // that's the one we want to report back. 27390b57cec5SDimitry Andric FlashDone(); 27400b57cec5SDimitry Andric m_allow_flash_writes = false; 27410b57cec5SDimitry Andric return error; 27420b57cec5SDimitry Andric } 27430b57cec5SDimitry Andric 27440b57cec5SDimitry Andric bool ProcessGDBRemote::HasErased(FlashRange range) { 27450b57cec5SDimitry Andric auto size = m_erased_flash_ranges.GetSize(); 27460b57cec5SDimitry Andric for (size_t i = 0; i < size; ++i) 27470b57cec5SDimitry Andric if (m_erased_flash_ranges.GetEntryAtIndex(i)->Contains(range)) 27480b57cec5SDimitry Andric return true; 27490b57cec5SDimitry Andric return false; 27500b57cec5SDimitry Andric } 27510b57cec5SDimitry Andric 27520b57cec5SDimitry Andric Status ProcessGDBRemote::FlashErase(lldb::addr_t addr, size_t size) { 27530b57cec5SDimitry Andric Status status; 27540b57cec5SDimitry Andric 27550b57cec5SDimitry Andric MemoryRegionInfo region; 27560b57cec5SDimitry Andric status = GetMemoryRegionInfo(addr, region); 27570b57cec5SDimitry Andric if (!status.Success()) 27580b57cec5SDimitry Andric return status; 27590b57cec5SDimitry Andric 27600b57cec5SDimitry Andric // The gdb spec doesn't say if erasures are allowed across multiple regions, 27610b57cec5SDimitry Andric // but we'll disallow it to be safe and to keep the logic simple by worring 27620b57cec5SDimitry Andric // about only one region's block size. DoMemoryWrite is this function's 27630b57cec5SDimitry Andric // primary user, and it can easily keep writes within a single memory region 27640b57cec5SDimitry Andric if (addr + size > region.GetRange().GetRangeEnd()) { 27650b57cec5SDimitry Andric status.SetErrorString("Unable to erase flash in multiple regions"); 27660b57cec5SDimitry Andric return status; 27670b57cec5SDimitry Andric } 27680b57cec5SDimitry Andric 27690b57cec5SDimitry Andric uint64_t blocksize = region.GetBlocksize(); 27700b57cec5SDimitry Andric if (blocksize == 0) { 27710b57cec5SDimitry Andric status.SetErrorString("Unable to erase flash because blocksize is 0"); 27720b57cec5SDimitry Andric return status; 27730b57cec5SDimitry Andric } 27740b57cec5SDimitry Andric 27750b57cec5SDimitry Andric // Erasures can only be done on block boundary adresses, so round down addr 27760b57cec5SDimitry Andric // and round up size 27770b57cec5SDimitry Andric lldb::addr_t block_start_addr = addr - (addr % blocksize); 27780b57cec5SDimitry Andric size += (addr - block_start_addr); 27790b57cec5SDimitry Andric if ((size % blocksize) != 0) 27800b57cec5SDimitry Andric size += (blocksize - size % blocksize); 27810b57cec5SDimitry Andric 27820b57cec5SDimitry Andric FlashRange range(block_start_addr, size); 27830b57cec5SDimitry Andric 27840b57cec5SDimitry Andric if (HasErased(range)) 27850b57cec5SDimitry Andric return status; 27860b57cec5SDimitry Andric 27870b57cec5SDimitry Andric // We haven't erased the entire range, but we may have erased part of it. 27880b57cec5SDimitry Andric // (e.g., block A is already erased and range starts in A and ends in B). So, 27890b57cec5SDimitry Andric // adjust range if necessary to exclude already erased blocks. 27900b57cec5SDimitry Andric if (!m_erased_flash_ranges.IsEmpty()) { 27910b57cec5SDimitry Andric // Assuming that writes and erasures are done in increasing addr order, 27920b57cec5SDimitry Andric // because that is a requirement of the vFlashWrite command. Therefore, we 27930b57cec5SDimitry Andric // only need to look at the last range in the list for overlap. 27940b57cec5SDimitry Andric const auto &last_range = *m_erased_flash_ranges.Back(); 27950b57cec5SDimitry Andric if (range.GetRangeBase() < last_range.GetRangeEnd()) { 27960b57cec5SDimitry Andric auto overlap = last_range.GetRangeEnd() - range.GetRangeBase(); 27970b57cec5SDimitry Andric // overlap will be less than range.GetByteSize() or else HasErased() 27980b57cec5SDimitry Andric // would have been true 27990b57cec5SDimitry Andric range.SetByteSize(range.GetByteSize() - overlap); 28000b57cec5SDimitry Andric range.SetRangeBase(range.GetRangeBase() + overlap); 28010b57cec5SDimitry Andric } 28020b57cec5SDimitry Andric } 28030b57cec5SDimitry Andric 28040b57cec5SDimitry Andric StreamString packet; 28050b57cec5SDimitry Andric packet.Printf("vFlashErase:%" PRIx64 ",%" PRIx64, range.GetRangeBase(), 28060b57cec5SDimitry Andric (uint64_t)range.GetByteSize()); 28070b57cec5SDimitry Andric 28080b57cec5SDimitry Andric StringExtractorGDBRemote response; 28090b57cec5SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response, 2810fe6060f1SDimitry Andric GetInterruptTimeout()) == 28110b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 28120b57cec5SDimitry Andric if (response.IsOKResponse()) { 28130b57cec5SDimitry Andric m_erased_flash_ranges.Insert(range, true); 28140b57cec5SDimitry Andric } else { 28150b57cec5SDimitry Andric if (response.IsErrorResponse()) 28160b57cec5SDimitry Andric status.SetErrorStringWithFormat("flash erase failed for 0x%" PRIx64, 28170b57cec5SDimitry Andric addr); 28180b57cec5SDimitry Andric else if (response.IsUnsupportedResponse()) 28190b57cec5SDimitry Andric status.SetErrorStringWithFormat("GDB server does not support flashing"); 28200b57cec5SDimitry Andric else 28210b57cec5SDimitry Andric status.SetErrorStringWithFormat( 28220b57cec5SDimitry Andric "unexpected response to GDB server flash erase packet '%s': '%s'", 28239dba64beSDimitry Andric packet.GetData(), response.GetStringRef().data()); 28240b57cec5SDimitry Andric } 28250b57cec5SDimitry Andric } else { 28260b57cec5SDimitry Andric status.SetErrorStringWithFormat("failed to send packet: '%s'", 28270b57cec5SDimitry Andric packet.GetData()); 28280b57cec5SDimitry Andric } 28290b57cec5SDimitry Andric return status; 28300b57cec5SDimitry Andric } 28310b57cec5SDimitry Andric 28320b57cec5SDimitry Andric Status ProcessGDBRemote::FlashDone() { 28330b57cec5SDimitry Andric Status status; 28340b57cec5SDimitry Andric // If we haven't erased any blocks, then we must not have written anything 28350b57cec5SDimitry Andric // either, so there is no need to actually send a vFlashDone command 28360b57cec5SDimitry Andric if (m_erased_flash_ranges.IsEmpty()) 28370b57cec5SDimitry Andric return status; 28380b57cec5SDimitry Andric StringExtractorGDBRemote response; 2839fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse("vFlashDone", response, 2840fe6060f1SDimitry Andric GetInterruptTimeout()) == 28410b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 28420b57cec5SDimitry Andric if (response.IsOKResponse()) { 28430b57cec5SDimitry Andric m_erased_flash_ranges.Clear(); 28440b57cec5SDimitry Andric } else { 28450b57cec5SDimitry Andric if (response.IsErrorResponse()) 28460b57cec5SDimitry Andric status.SetErrorStringWithFormat("flash done failed"); 28470b57cec5SDimitry Andric else if (response.IsUnsupportedResponse()) 28480b57cec5SDimitry Andric status.SetErrorStringWithFormat("GDB server does not support flashing"); 28490b57cec5SDimitry Andric else 28500b57cec5SDimitry Andric status.SetErrorStringWithFormat( 28510b57cec5SDimitry Andric "unexpected response to GDB server flash done packet: '%s'", 28529dba64beSDimitry Andric response.GetStringRef().data()); 28530b57cec5SDimitry Andric } 28540b57cec5SDimitry Andric } else { 28550b57cec5SDimitry Andric status.SetErrorStringWithFormat("failed to send flash done packet"); 28560b57cec5SDimitry Andric } 28570b57cec5SDimitry Andric return status; 28580b57cec5SDimitry Andric } 28590b57cec5SDimitry Andric 28600b57cec5SDimitry Andric size_t ProcessGDBRemote::DoWriteMemory(addr_t addr, const void *buf, 28610b57cec5SDimitry Andric size_t size, Status &error) { 28620b57cec5SDimitry Andric GetMaxMemorySize(); 28630b57cec5SDimitry Andric // M and m packets take 2 bytes for 1 byte of memory 28640b57cec5SDimitry Andric size_t max_memory_size = m_max_memory_size / 2; 28650b57cec5SDimitry Andric if (size > max_memory_size) { 28660b57cec5SDimitry Andric // Keep memory read sizes down to a sane limit. This function will be 28670b57cec5SDimitry Andric // called multiple times in order to complete the task by 28680b57cec5SDimitry Andric // lldb_private::Process so it is ok to do this. 28690b57cec5SDimitry Andric size = max_memory_size; 28700b57cec5SDimitry Andric } 28710b57cec5SDimitry Andric 28720b57cec5SDimitry Andric StreamGDBRemote packet; 28730b57cec5SDimitry Andric 28740b57cec5SDimitry Andric MemoryRegionInfo region; 28750b57cec5SDimitry Andric Status region_status = GetMemoryRegionInfo(addr, region); 28760b57cec5SDimitry Andric 28770b57cec5SDimitry Andric bool is_flash = 28780b57cec5SDimitry Andric region_status.Success() && region.GetFlash() == MemoryRegionInfo::eYes; 28790b57cec5SDimitry Andric 28800b57cec5SDimitry Andric if (is_flash) { 28810b57cec5SDimitry Andric if (!m_allow_flash_writes) { 28820b57cec5SDimitry Andric error.SetErrorString("Writing to flash memory is not allowed"); 28830b57cec5SDimitry Andric return 0; 28840b57cec5SDimitry Andric } 28850b57cec5SDimitry Andric // Keep the write within a flash memory region 28860b57cec5SDimitry Andric if (addr + size > region.GetRange().GetRangeEnd()) 28870b57cec5SDimitry Andric size = region.GetRange().GetRangeEnd() - addr; 28880b57cec5SDimitry Andric // Flash memory must be erased before it can be written 28890b57cec5SDimitry Andric error = FlashErase(addr, size); 28900b57cec5SDimitry Andric if (!error.Success()) 28910b57cec5SDimitry Andric return 0; 28920b57cec5SDimitry Andric packet.Printf("vFlashWrite:%" PRIx64 ":", addr); 28930b57cec5SDimitry Andric packet.PutEscapedBytes(buf, size); 28940b57cec5SDimitry Andric } else { 28950b57cec5SDimitry Andric packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); 28960b57cec5SDimitry Andric packet.PutBytesAsRawHex8(buf, size, endian::InlHostByteOrder(), 28970b57cec5SDimitry Andric endian::InlHostByteOrder()); 28980b57cec5SDimitry Andric } 28990b57cec5SDimitry Andric StringExtractorGDBRemote response; 29000b57cec5SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response, 2901fe6060f1SDimitry Andric GetInterruptTimeout()) == 29020b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 29030b57cec5SDimitry Andric if (response.IsOKResponse()) { 29040b57cec5SDimitry Andric error.Clear(); 29050b57cec5SDimitry Andric return size; 29060b57cec5SDimitry Andric } else if (response.IsErrorResponse()) 29070b57cec5SDimitry Andric error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, 29080b57cec5SDimitry Andric addr); 29090b57cec5SDimitry Andric else if (response.IsUnsupportedResponse()) 29100b57cec5SDimitry Andric error.SetErrorStringWithFormat( 29110b57cec5SDimitry Andric "GDB server does not support writing memory"); 29120b57cec5SDimitry Andric else 29130b57cec5SDimitry Andric error.SetErrorStringWithFormat( 29140b57cec5SDimitry Andric "unexpected response to GDB server memory write packet '%s': '%s'", 29159dba64beSDimitry Andric packet.GetData(), response.GetStringRef().data()); 29160b57cec5SDimitry Andric } else { 29170b57cec5SDimitry Andric error.SetErrorStringWithFormat("failed to send packet: '%s'", 29180b57cec5SDimitry Andric packet.GetData()); 29190b57cec5SDimitry Andric } 29200b57cec5SDimitry Andric return 0; 29210b57cec5SDimitry Andric } 29220b57cec5SDimitry Andric 29230b57cec5SDimitry Andric lldb::addr_t ProcessGDBRemote::DoAllocateMemory(size_t size, 29240b57cec5SDimitry Andric uint32_t permissions, 29250b57cec5SDimitry Andric Status &error) { 29260b57cec5SDimitry Andric Log *log( 29270b57cec5SDimitry Andric GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_EXPRESSIONS)); 29280b57cec5SDimitry Andric addr_t allocated_addr = LLDB_INVALID_ADDRESS; 29290b57cec5SDimitry Andric 29300b57cec5SDimitry Andric if (m_gdb_comm.SupportsAllocDeallocMemory() != eLazyBoolNo) { 29310b57cec5SDimitry Andric allocated_addr = m_gdb_comm.AllocateMemory(size, permissions); 29320b57cec5SDimitry Andric if (allocated_addr != LLDB_INVALID_ADDRESS || 29330b57cec5SDimitry Andric m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolYes) 29340b57cec5SDimitry Andric return allocated_addr; 29350b57cec5SDimitry Andric } 29360b57cec5SDimitry Andric 29370b57cec5SDimitry Andric if (m_gdb_comm.SupportsAllocDeallocMemory() == eLazyBoolNo) { 29380b57cec5SDimitry Andric // Call mmap() to create memory in the inferior.. 29390b57cec5SDimitry Andric unsigned prot = 0; 29400b57cec5SDimitry Andric if (permissions & lldb::ePermissionsReadable) 29410b57cec5SDimitry Andric prot |= eMmapProtRead; 29420b57cec5SDimitry Andric if (permissions & lldb::ePermissionsWritable) 29430b57cec5SDimitry Andric prot |= eMmapProtWrite; 29440b57cec5SDimitry Andric if (permissions & lldb::ePermissionsExecutable) 29450b57cec5SDimitry Andric prot |= eMmapProtExec; 29460b57cec5SDimitry Andric 29470b57cec5SDimitry Andric if (InferiorCallMmap(this, allocated_addr, 0, size, prot, 29480b57cec5SDimitry Andric eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) 29490b57cec5SDimitry Andric m_addr_to_mmap_size[allocated_addr] = size; 29500b57cec5SDimitry Andric else { 29510b57cec5SDimitry Andric allocated_addr = LLDB_INVALID_ADDRESS; 29529dba64beSDimitry Andric LLDB_LOGF(log, 29539dba64beSDimitry Andric "ProcessGDBRemote::%s no direct stub support for memory " 29540b57cec5SDimitry Andric "allocation, and InferiorCallMmap also failed - is stub " 29550b57cec5SDimitry Andric "missing register context save/restore capability?", 29560b57cec5SDimitry Andric __FUNCTION__); 29570b57cec5SDimitry Andric } 29580b57cec5SDimitry Andric } 29590b57cec5SDimitry Andric 29600b57cec5SDimitry Andric if (allocated_addr == LLDB_INVALID_ADDRESS) 29610b57cec5SDimitry Andric error.SetErrorStringWithFormat( 29620b57cec5SDimitry Andric "unable to allocate %" PRIu64 " bytes of memory with permissions %s", 29630b57cec5SDimitry Andric (uint64_t)size, GetPermissionsAsCString(permissions)); 29640b57cec5SDimitry Andric else 29650b57cec5SDimitry Andric error.Clear(); 29660b57cec5SDimitry Andric return allocated_addr; 29670b57cec5SDimitry Andric } 29680b57cec5SDimitry Andric 29694824e7fdSDimitry Andric Status ProcessGDBRemote::GetMemoryRegionInfo(addr_t load_addr, 29700b57cec5SDimitry Andric MemoryRegionInfo ®ion_info) { 29710b57cec5SDimitry Andric 29720b57cec5SDimitry Andric Status error(m_gdb_comm.GetMemoryRegionInfo(load_addr, region_info)); 29730b57cec5SDimitry Andric return error; 29740b57cec5SDimitry Andric } 29750b57cec5SDimitry Andric 29760b57cec5SDimitry Andric Status ProcessGDBRemote::GetWatchpointSupportInfo(uint32_t &num) { 29770b57cec5SDimitry Andric 29780b57cec5SDimitry Andric Status error(m_gdb_comm.GetWatchpointSupportInfo(num)); 29790b57cec5SDimitry Andric return error; 29800b57cec5SDimitry Andric } 29810b57cec5SDimitry Andric 29820b57cec5SDimitry Andric Status ProcessGDBRemote::GetWatchpointSupportInfo(uint32_t &num, bool &after) { 29830b57cec5SDimitry Andric Status error(m_gdb_comm.GetWatchpointSupportInfo( 29840b57cec5SDimitry Andric num, after, GetTarget().GetArchitecture())); 29850b57cec5SDimitry Andric return error; 29860b57cec5SDimitry Andric } 29870b57cec5SDimitry Andric 29880b57cec5SDimitry Andric Status ProcessGDBRemote::DoDeallocateMemory(lldb::addr_t addr) { 29890b57cec5SDimitry Andric Status error; 29900b57cec5SDimitry Andric LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); 29910b57cec5SDimitry Andric 29920b57cec5SDimitry Andric switch (supported) { 29930b57cec5SDimitry Andric case eLazyBoolCalculate: 29940b57cec5SDimitry Andric // We should never be deallocating memory without allocating memory first 29950b57cec5SDimitry Andric // so we should never get eLazyBoolCalculate 29960b57cec5SDimitry Andric error.SetErrorString( 29970b57cec5SDimitry Andric "tried to deallocate memory without ever allocating memory"); 29980b57cec5SDimitry Andric break; 29990b57cec5SDimitry Andric 30000b57cec5SDimitry Andric case eLazyBoolYes: 30010b57cec5SDimitry Andric if (!m_gdb_comm.DeallocateMemory(addr)) 30020b57cec5SDimitry Andric error.SetErrorStringWithFormat( 30030b57cec5SDimitry Andric "unable to deallocate memory at 0x%" PRIx64, addr); 30040b57cec5SDimitry Andric break; 30050b57cec5SDimitry Andric 30060b57cec5SDimitry Andric case eLazyBoolNo: 30070b57cec5SDimitry Andric // Call munmap() to deallocate memory in the inferior.. 30080b57cec5SDimitry Andric { 30090b57cec5SDimitry Andric MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); 30100b57cec5SDimitry Andric if (pos != m_addr_to_mmap_size.end() && 30110b57cec5SDimitry Andric InferiorCallMunmap(this, addr, pos->second)) 30120b57cec5SDimitry Andric m_addr_to_mmap_size.erase(pos); 30130b57cec5SDimitry Andric else 30140b57cec5SDimitry Andric error.SetErrorStringWithFormat( 30150b57cec5SDimitry Andric "unable to deallocate memory at 0x%" PRIx64, addr); 30160b57cec5SDimitry Andric } 30170b57cec5SDimitry Andric break; 30180b57cec5SDimitry Andric } 30190b57cec5SDimitry Andric 30200b57cec5SDimitry Andric return error; 30210b57cec5SDimitry Andric } 30220b57cec5SDimitry Andric 30230b57cec5SDimitry Andric // Process STDIO 30240b57cec5SDimitry Andric size_t ProcessGDBRemote::PutSTDIN(const char *src, size_t src_len, 30250b57cec5SDimitry Andric Status &error) { 30260b57cec5SDimitry Andric if (m_stdio_communication.IsConnected()) { 30270b57cec5SDimitry Andric ConnectionStatus status; 30280b57cec5SDimitry Andric m_stdio_communication.Write(src, src_len, status, nullptr); 30290b57cec5SDimitry Andric } else if (m_stdin_forward) { 30300b57cec5SDimitry Andric m_gdb_comm.SendStdinNotification(src, src_len); 30310b57cec5SDimitry Andric } 30320b57cec5SDimitry Andric return 0; 30330b57cec5SDimitry Andric } 30340b57cec5SDimitry Andric 30350b57cec5SDimitry Andric Status ProcessGDBRemote::EnableBreakpointSite(BreakpointSite *bp_site) { 30360b57cec5SDimitry Andric Status error; 30370b57cec5SDimitry Andric assert(bp_site != nullptr); 30380b57cec5SDimitry Andric 30390b57cec5SDimitry Andric // Get logging info 30400b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); 30410b57cec5SDimitry Andric user_id_t site_id = bp_site->GetID(); 30420b57cec5SDimitry Andric 30430b57cec5SDimitry Andric // Get the breakpoint address 30440b57cec5SDimitry Andric const addr_t addr = bp_site->GetLoadAddress(); 30450b57cec5SDimitry Andric 30460b57cec5SDimitry Andric // Log that a breakpoint was requested 30479dba64beSDimitry Andric LLDB_LOGF(log, 30489dba64beSDimitry Andric "ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 30490b57cec5SDimitry Andric ") address = 0x%" PRIx64, 30500b57cec5SDimitry Andric site_id, (uint64_t)addr); 30510b57cec5SDimitry Andric 30520b57cec5SDimitry Andric // Breakpoint already exists and is enabled 30530b57cec5SDimitry Andric if (bp_site->IsEnabled()) { 30549dba64beSDimitry Andric LLDB_LOGF(log, 30559dba64beSDimitry Andric "ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 30560b57cec5SDimitry Andric ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", 30570b57cec5SDimitry Andric site_id, (uint64_t)addr); 30580b57cec5SDimitry Andric return error; 30590b57cec5SDimitry Andric } 30600b57cec5SDimitry Andric 30610b57cec5SDimitry Andric // Get the software breakpoint trap opcode size 30620b57cec5SDimitry Andric const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); 30630b57cec5SDimitry Andric 30640b57cec5SDimitry Andric // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this 30650b57cec5SDimitry Andric // breakpoint type is supported by the remote stub. These are set to true by 30660b57cec5SDimitry Andric // default, and later set to false only after we receive an unimplemented 30670b57cec5SDimitry Andric // response when sending a breakpoint packet. This means initially that 30680b57cec5SDimitry Andric // unless we were specifically instructed to use a hardware breakpoint, LLDB 30690b57cec5SDimitry Andric // will attempt to set a software breakpoint. HardwareRequired() also queries 30700b57cec5SDimitry Andric // a boolean variable which indicates if the user specifically asked for 30710b57cec5SDimitry Andric // hardware breakpoints. If true then we will skip over software 30720b57cec5SDimitry Andric // breakpoints. 30730b57cec5SDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && 30740b57cec5SDimitry Andric (!bp_site->HardwareRequired())) { 30750b57cec5SDimitry Andric // Try to send off a software breakpoint packet ($Z0) 30760b57cec5SDimitry Andric uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket( 3077fe6060f1SDimitry Andric eBreakpointSoftware, true, addr, bp_op_size, GetInterruptTimeout()); 30780b57cec5SDimitry Andric if (error_no == 0) { 30790b57cec5SDimitry Andric // The breakpoint was placed successfully 30800b57cec5SDimitry Andric bp_site->SetEnabled(true); 30810b57cec5SDimitry Andric bp_site->SetType(BreakpointSite::eExternal); 30820b57cec5SDimitry Andric return error; 30830b57cec5SDimitry Andric } 30840b57cec5SDimitry Andric 30850b57cec5SDimitry Andric // SendGDBStoppointTypePacket() will return an error if it was unable to 30860b57cec5SDimitry Andric // set this breakpoint. We need to differentiate between a error specific 30870b57cec5SDimitry Andric // to placing this breakpoint or if we have learned that this breakpoint 30880b57cec5SDimitry Andric // type is unsupported. To do this, we must test the support boolean for 30890b57cec5SDimitry Andric // this breakpoint type to see if it now indicates that this breakpoint 30900b57cec5SDimitry Andric // type is unsupported. If they are still supported then we should return 30910b57cec5SDimitry Andric // with the error code. If they are now unsupported, then we would like to 30920b57cec5SDimitry Andric // fall through and try another form of breakpoint. 30930b57cec5SDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) { 30940b57cec5SDimitry Andric if (error_no != UINT8_MAX) 30950b57cec5SDimitry Andric error.SetErrorStringWithFormat( 30965ffd83dbSDimitry Andric "error: %d sending the breakpoint request", error_no); 30970b57cec5SDimitry Andric else 30980b57cec5SDimitry Andric error.SetErrorString("error sending the breakpoint request"); 30990b57cec5SDimitry Andric return error; 31000b57cec5SDimitry Andric } 31010b57cec5SDimitry Andric 31020b57cec5SDimitry Andric // We reach here when software breakpoints have been found to be 31030b57cec5SDimitry Andric // unsupported. For future calls to set a breakpoint, we will not attempt 31040b57cec5SDimitry Andric // to set a breakpoint with a type that is known not to be supported. 31059dba64beSDimitry Andric LLDB_LOGF(log, "Software breakpoints are unsupported"); 31060b57cec5SDimitry Andric 31070b57cec5SDimitry Andric // So we will fall through and try a hardware breakpoint 31080b57cec5SDimitry Andric } 31090b57cec5SDimitry Andric 31100b57cec5SDimitry Andric // The process of setting a hardware breakpoint is much the same as above. 31110b57cec5SDimitry Andric // We check the supported boolean for this breakpoint type, and if it is 31120b57cec5SDimitry Andric // thought to be supported then we will try to set this breakpoint with a 31130b57cec5SDimitry Andric // hardware breakpoint. 31140b57cec5SDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) { 31150b57cec5SDimitry Andric // Try to send off a hardware breakpoint packet ($Z1) 31160b57cec5SDimitry Andric uint8_t error_no = m_gdb_comm.SendGDBStoppointTypePacket( 3117fe6060f1SDimitry Andric eBreakpointHardware, true, addr, bp_op_size, GetInterruptTimeout()); 31180b57cec5SDimitry Andric if (error_no == 0) { 31190b57cec5SDimitry Andric // The breakpoint was placed successfully 31200b57cec5SDimitry Andric bp_site->SetEnabled(true); 31210b57cec5SDimitry Andric bp_site->SetType(BreakpointSite::eHardware); 31220b57cec5SDimitry Andric return error; 31230b57cec5SDimitry Andric } 31240b57cec5SDimitry Andric 31250b57cec5SDimitry Andric // Check if the error was something other then an unsupported breakpoint 31260b57cec5SDimitry Andric // type 31270b57cec5SDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) { 31280b57cec5SDimitry Andric // Unable to set this hardware breakpoint 31290b57cec5SDimitry Andric if (error_no != UINT8_MAX) 31300b57cec5SDimitry Andric error.SetErrorStringWithFormat( 31310b57cec5SDimitry Andric "error: %d sending the hardware breakpoint request " 31320b57cec5SDimitry Andric "(hardware breakpoint resources might be exhausted or unavailable)", 31330b57cec5SDimitry Andric error_no); 31340b57cec5SDimitry Andric else 31350b57cec5SDimitry Andric error.SetErrorString("error sending the hardware breakpoint request " 31360b57cec5SDimitry Andric "(hardware breakpoint resources " 31370b57cec5SDimitry Andric "might be exhausted or unavailable)"); 31380b57cec5SDimitry Andric return error; 31390b57cec5SDimitry Andric } 31400b57cec5SDimitry Andric 31410b57cec5SDimitry Andric // We will reach here when the stub gives an unsupported response to a 31420b57cec5SDimitry Andric // hardware breakpoint 31439dba64beSDimitry Andric LLDB_LOGF(log, "Hardware breakpoints are unsupported"); 31440b57cec5SDimitry Andric 31450b57cec5SDimitry Andric // Finally we will falling through to a #trap style breakpoint 31460b57cec5SDimitry Andric } 31470b57cec5SDimitry Andric 31480b57cec5SDimitry Andric // Don't fall through when hardware breakpoints were specifically requested 31490b57cec5SDimitry Andric if (bp_site->HardwareRequired()) { 31500b57cec5SDimitry Andric error.SetErrorString("hardware breakpoints are not supported"); 31510b57cec5SDimitry Andric return error; 31520b57cec5SDimitry Andric } 31530b57cec5SDimitry Andric 31540b57cec5SDimitry Andric // As a last resort we want to place a manual breakpoint. An instruction is 31550b57cec5SDimitry Andric // placed into the process memory using memory write packets. 31560b57cec5SDimitry Andric return EnableSoftwareBreakpoint(bp_site); 31570b57cec5SDimitry Andric } 31580b57cec5SDimitry Andric 31590b57cec5SDimitry Andric Status ProcessGDBRemote::DisableBreakpointSite(BreakpointSite *bp_site) { 31600b57cec5SDimitry Andric Status error; 31610b57cec5SDimitry Andric assert(bp_site != nullptr); 31620b57cec5SDimitry Andric addr_t addr = bp_site->GetLoadAddress(); 31630b57cec5SDimitry Andric user_id_t site_id = bp_site->GetID(); 31640b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); 31659dba64beSDimitry Andric LLDB_LOGF(log, 31669dba64beSDimitry Andric "ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 31670b57cec5SDimitry Andric ") addr = 0x%8.8" PRIx64, 31680b57cec5SDimitry Andric site_id, (uint64_t)addr); 31690b57cec5SDimitry Andric 31700b57cec5SDimitry Andric if (bp_site->IsEnabled()) { 31710b57cec5SDimitry Andric const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); 31720b57cec5SDimitry Andric 31730b57cec5SDimitry Andric BreakpointSite::Type bp_type = bp_site->GetType(); 31740b57cec5SDimitry Andric switch (bp_type) { 31750b57cec5SDimitry Andric case BreakpointSite::eSoftware: 31760b57cec5SDimitry Andric error = DisableSoftwareBreakpoint(bp_site); 31770b57cec5SDimitry Andric break; 31780b57cec5SDimitry Andric 31790b57cec5SDimitry Andric case BreakpointSite::eHardware: 31800b57cec5SDimitry Andric if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, 3181fe6060f1SDimitry Andric addr, bp_op_size, 3182fe6060f1SDimitry Andric GetInterruptTimeout())) 31830b57cec5SDimitry Andric error.SetErrorToGenericError(); 31840b57cec5SDimitry Andric break; 31850b57cec5SDimitry Andric 31860b57cec5SDimitry Andric case BreakpointSite::eExternal: { 3187e8d8bef9SDimitry Andric if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, 3188fe6060f1SDimitry Andric addr, bp_op_size, 3189fe6060f1SDimitry Andric GetInterruptTimeout())) 31900b57cec5SDimitry Andric error.SetErrorToGenericError(); 31910b57cec5SDimitry Andric } break; 31920b57cec5SDimitry Andric } 31930b57cec5SDimitry Andric if (error.Success()) 31940b57cec5SDimitry Andric bp_site->SetEnabled(false); 31950b57cec5SDimitry Andric } else { 31969dba64beSDimitry Andric LLDB_LOGF(log, 31979dba64beSDimitry Andric "ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 31980b57cec5SDimitry Andric ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", 31990b57cec5SDimitry Andric site_id, (uint64_t)addr); 32000b57cec5SDimitry Andric return error; 32010b57cec5SDimitry Andric } 32020b57cec5SDimitry Andric 32030b57cec5SDimitry Andric if (error.Success()) 32040b57cec5SDimitry Andric error.SetErrorToGenericError(); 32050b57cec5SDimitry Andric return error; 32060b57cec5SDimitry Andric } 32070b57cec5SDimitry Andric 32080b57cec5SDimitry Andric // Pre-requisite: wp != NULL. 32090b57cec5SDimitry Andric static GDBStoppointType GetGDBStoppointType(Watchpoint *wp) { 32100b57cec5SDimitry Andric assert(wp); 32110b57cec5SDimitry Andric bool watch_read = wp->WatchpointRead(); 32120b57cec5SDimitry Andric bool watch_write = wp->WatchpointWrite(); 32130b57cec5SDimitry Andric 32140b57cec5SDimitry Andric // watch_read and watch_write cannot both be false. 32150b57cec5SDimitry Andric assert(watch_read || watch_write); 32160b57cec5SDimitry Andric if (watch_read && watch_write) 32170b57cec5SDimitry Andric return eWatchpointReadWrite; 32180b57cec5SDimitry Andric else if (watch_read) 32190b57cec5SDimitry Andric return eWatchpointRead; 32200b57cec5SDimitry Andric else // Must be watch_write, then. 32210b57cec5SDimitry Andric return eWatchpointWrite; 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric 32240b57cec5SDimitry Andric Status ProcessGDBRemote::EnableWatchpoint(Watchpoint *wp, bool notify) { 32250b57cec5SDimitry Andric Status error; 32260b57cec5SDimitry Andric if (wp) { 32270b57cec5SDimitry Andric user_id_t watchID = wp->GetID(); 32280b57cec5SDimitry Andric addr_t addr = wp->GetLoadAddress(); 32290b57cec5SDimitry Andric Log *log( 32300b57cec5SDimitry Andric ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); 32319dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", 32320b57cec5SDimitry Andric watchID); 32330b57cec5SDimitry Andric if (wp->IsEnabled()) { 32349dba64beSDimitry Andric LLDB_LOGF(log, 32359dba64beSDimitry Andric "ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 32360b57cec5SDimitry Andric ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", 32370b57cec5SDimitry Andric watchID, (uint64_t)addr); 32380b57cec5SDimitry Andric return error; 32390b57cec5SDimitry Andric } 32400b57cec5SDimitry Andric 32410b57cec5SDimitry Andric GDBStoppointType type = GetGDBStoppointType(wp); 32420b57cec5SDimitry Andric // Pass down an appropriate z/Z packet... 32430b57cec5SDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(type)) { 32440b57cec5SDimitry Andric if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, 3245fe6060f1SDimitry Andric wp->GetByteSize(), 3246fe6060f1SDimitry Andric GetInterruptTimeout()) == 0) { 32470b57cec5SDimitry Andric wp->SetEnabled(true, notify); 32480b57cec5SDimitry Andric return error; 32490b57cec5SDimitry Andric } else 32500b57cec5SDimitry Andric error.SetErrorString("sending gdb watchpoint packet failed"); 32510b57cec5SDimitry Andric } else 32520b57cec5SDimitry Andric error.SetErrorString("watchpoints not supported"); 32530b57cec5SDimitry Andric } else { 32540b57cec5SDimitry Andric error.SetErrorString("Watchpoint argument was NULL."); 32550b57cec5SDimitry Andric } 32560b57cec5SDimitry Andric if (error.Success()) 32570b57cec5SDimitry Andric error.SetErrorToGenericError(); 32580b57cec5SDimitry Andric return error; 32590b57cec5SDimitry Andric } 32600b57cec5SDimitry Andric 32610b57cec5SDimitry Andric Status ProcessGDBRemote::DisableWatchpoint(Watchpoint *wp, bool notify) { 32620b57cec5SDimitry Andric Status error; 32630b57cec5SDimitry Andric if (wp) { 32640b57cec5SDimitry Andric user_id_t watchID = wp->GetID(); 32650b57cec5SDimitry Andric 32660b57cec5SDimitry Andric Log *log( 32670b57cec5SDimitry Andric ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); 32680b57cec5SDimitry Andric 32690b57cec5SDimitry Andric addr_t addr = wp->GetLoadAddress(); 32700b57cec5SDimitry Andric 32719dba64beSDimitry Andric LLDB_LOGF(log, 32729dba64beSDimitry Andric "ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 32730b57cec5SDimitry Andric ") addr = 0x%8.8" PRIx64, 32740b57cec5SDimitry Andric watchID, (uint64_t)addr); 32750b57cec5SDimitry Andric 32760b57cec5SDimitry Andric if (!wp->IsEnabled()) { 32779dba64beSDimitry Andric LLDB_LOGF(log, 32789dba64beSDimitry Andric "ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 32790b57cec5SDimitry Andric ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", 32800b57cec5SDimitry Andric watchID, (uint64_t)addr); 32810b57cec5SDimitry Andric // See also 'class WatchpointSentry' within StopInfo.cpp. This disabling 32820b57cec5SDimitry Andric // attempt might come from the user-supplied actions, we'll route it in 32830b57cec5SDimitry Andric // order for the watchpoint object to intelligently process this action. 32840b57cec5SDimitry Andric wp->SetEnabled(false, notify); 32850b57cec5SDimitry Andric return error; 32860b57cec5SDimitry Andric } 32870b57cec5SDimitry Andric 32880b57cec5SDimitry Andric if (wp->IsHardware()) { 32890b57cec5SDimitry Andric GDBStoppointType type = GetGDBStoppointType(wp); 32900b57cec5SDimitry Andric // Pass down an appropriate z/Z packet... 32910b57cec5SDimitry Andric if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, 3292fe6060f1SDimitry Andric wp->GetByteSize(), 3293fe6060f1SDimitry Andric GetInterruptTimeout()) == 0) { 32940b57cec5SDimitry Andric wp->SetEnabled(false, notify); 32950b57cec5SDimitry Andric return error; 32960b57cec5SDimitry Andric } else 32970b57cec5SDimitry Andric error.SetErrorString("sending gdb watchpoint packet failed"); 32980b57cec5SDimitry Andric } 32990b57cec5SDimitry Andric // TODO: clear software watchpoints if we implement them 33000b57cec5SDimitry Andric } else { 33010b57cec5SDimitry Andric error.SetErrorString("Watchpoint argument was NULL."); 33020b57cec5SDimitry Andric } 33030b57cec5SDimitry Andric if (error.Success()) 33040b57cec5SDimitry Andric error.SetErrorToGenericError(); 33050b57cec5SDimitry Andric return error; 33060b57cec5SDimitry Andric } 33070b57cec5SDimitry Andric 33080b57cec5SDimitry Andric void ProcessGDBRemote::Clear() { 33090b57cec5SDimitry Andric m_thread_list_real.Clear(); 33100b57cec5SDimitry Andric m_thread_list.Clear(); 33110b57cec5SDimitry Andric } 33120b57cec5SDimitry Andric 33130b57cec5SDimitry Andric Status ProcessGDBRemote::DoSignal(int signo) { 33140b57cec5SDimitry Andric Status error; 33150b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 33169dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::DoSignal (signal = %d)", signo); 33170b57cec5SDimitry Andric 3318fe6060f1SDimitry Andric if (!m_gdb_comm.SendAsyncSignal(signo, GetInterruptTimeout())) 33190b57cec5SDimitry Andric error.SetErrorStringWithFormat("failed to send signal %i", signo); 33200b57cec5SDimitry Andric return error; 33210b57cec5SDimitry Andric } 33220b57cec5SDimitry Andric 33235ffd83dbSDimitry Andric Status ProcessGDBRemote::ConnectToReplayServer() { 33245ffd83dbSDimitry Andric Status status = m_gdb_replay_server.Connect(m_gdb_comm); 33255ffd83dbSDimitry Andric if (status.Fail()) 33265ffd83dbSDimitry Andric return status; 33270b57cec5SDimitry Andric 3328480093f4SDimitry Andric // Enable replay mode. 3329480093f4SDimitry Andric m_replay_mode = true; 3330480093f4SDimitry Andric 33310b57cec5SDimitry Andric // Start server thread. 33320b57cec5SDimitry Andric m_gdb_replay_server.StartAsyncThread(); 33330b57cec5SDimitry Andric 33340b57cec5SDimitry Andric // Start client thread. 33350b57cec5SDimitry Andric StartAsyncThread(); 33360b57cec5SDimitry Andric 33370b57cec5SDimitry Andric // Do the usual setup. 33380b57cec5SDimitry Andric return ConnectToDebugserver(""); 33390b57cec5SDimitry Andric } 33400b57cec5SDimitry Andric 33410b57cec5SDimitry Andric Status 33420b57cec5SDimitry Andric ProcessGDBRemote::EstablishConnectionIfNeeded(const ProcessInfo &process_info) { 33430b57cec5SDimitry Andric // Make sure we aren't already connected? 33440b57cec5SDimitry Andric if (m_gdb_comm.IsConnected()) 33450b57cec5SDimitry Andric return Status(); 33460b57cec5SDimitry Andric 33470b57cec5SDimitry Andric PlatformSP platform_sp(GetTarget().GetPlatform()); 33480b57cec5SDimitry Andric if (platform_sp && !platform_sp->IsHost()) 33490b57cec5SDimitry Andric return Status("Lost debug server connection"); 33500b57cec5SDimitry Andric 33510b57cec5SDimitry Andric auto error = LaunchAndConnectToDebugserver(process_info); 33520b57cec5SDimitry Andric if (error.Fail()) { 33530b57cec5SDimitry Andric const char *error_string = error.AsCString(); 33540b57cec5SDimitry Andric if (error_string == nullptr) 33550b57cec5SDimitry Andric error_string = "unable to launch " DEBUGSERVER_BASENAME; 33560b57cec5SDimitry Andric } 33570b57cec5SDimitry Andric return error; 33580b57cec5SDimitry Andric } 33590b57cec5SDimitry Andric #if !defined(_WIN32) 33600b57cec5SDimitry Andric #define USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 1 33610b57cec5SDimitry Andric #endif 33620b57cec5SDimitry Andric 33630b57cec5SDimitry Andric #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 33640b57cec5SDimitry Andric static bool SetCloexecFlag(int fd) { 33650b57cec5SDimitry Andric #if defined(FD_CLOEXEC) 33660b57cec5SDimitry Andric int flags = ::fcntl(fd, F_GETFD); 33670b57cec5SDimitry Andric if (flags == -1) 33680b57cec5SDimitry Andric return false; 33690b57cec5SDimitry Andric return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0); 33700b57cec5SDimitry Andric #else 33710b57cec5SDimitry Andric return false; 33720b57cec5SDimitry Andric #endif 33730b57cec5SDimitry Andric } 33740b57cec5SDimitry Andric #endif 33750b57cec5SDimitry Andric 33760b57cec5SDimitry Andric Status ProcessGDBRemote::LaunchAndConnectToDebugserver( 33770b57cec5SDimitry Andric const ProcessInfo &process_info) { 33780b57cec5SDimitry Andric using namespace std::placeholders; // For _1, _2, etc. 33790b57cec5SDimitry Andric 33800b57cec5SDimitry Andric Status error; 33810b57cec5SDimitry Andric if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) { 33820b57cec5SDimitry Andric // If we locate debugserver, keep that located version around 33830b57cec5SDimitry Andric static FileSpec g_debugserver_file_spec; 33840b57cec5SDimitry Andric 33850b57cec5SDimitry Andric ProcessLaunchInfo debugserver_launch_info; 33860b57cec5SDimitry Andric // Make debugserver run in its own session so signals generated by special 33870b57cec5SDimitry Andric // terminal key sequences (^C) don't affect debugserver. 33880b57cec5SDimitry Andric debugserver_launch_info.SetLaunchInSeparateProcessGroup(true); 33890b57cec5SDimitry Andric 33900b57cec5SDimitry Andric const std::weak_ptr<ProcessGDBRemote> this_wp = 33910b57cec5SDimitry Andric std::static_pointer_cast<ProcessGDBRemote>(shared_from_this()); 33920b57cec5SDimitry Andric debugserver_launch_info.SetMonitorProcessCallback( 33930b57cec5SDimitry Andric std::bind(MonitorDebugserverProcess, this_wp, _1, _2, _3, _4), false); 33940b57cec5SDimitry Andric debugserver_launch_info.SetUserID(process_info.GetUserID()); 33950b57cec5SDimitry Andric 33965ffd83dbSDimitry Andric #if defined(__APPLE__) 33975ffd83dbSDimitry Andric // On macOS 11, we need to support x86_64 applications translated to 33985ffd83dbSDimitry Andric // arm64. We check whether a binary is translated and spawn the correct 33995ffd83dbSDimitry Andric // debugserver accordingly. 34005ffd83dbSDimitry Andric int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 34015ffd83dbSDimitry Andric static_cast<int>(process_info.GetProcessID()) }; 34025ffd83dbSDimitry Andric struct kinfo_proc processInfo; 34035ffd83dbSDimitry Andric size_t bufsize = sizeof(processInfo); 34045ffd83dbSDimitry Andric if (sysctl(mib, (unsigned)(sizeof(mib)/sizeof(int)), &processInfo, 34055ffd83dbSDimitry Andric &bufsize, NULL, 0) == 0 && bufsize > 0) { 34065ffd83dbSDimitry Andric if (processInfo.kp_proc.p_flag & P_TRANSLATED) { 34075ffd83dbSDimitry Andric FileSpec rosetta_debugserver("/Library/Apple/usr/libexec/oah/debugserver"); 34085ffd83dbSDimitry Andric debugserver_launch_info.SetExecutableFile(rosetta_debugserver, false); 34095ffd83dbSDimitry Andric } 34105ffd83dbSDimitry Andric } 34115ffd83dbSDimitry Andric #endif 34125ffd83dbSDimitry Andric 34130b57cec5SDimitry Andric int communication_fd = -1; 34140b57cec5SDimitry Andric #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 34150b57cec5SDimitry Andric // Use a socketpair on non-Windows systems for security and performance 34160b57cec5SDimitry Andric // reasons. 34170b57cec5SDimitry Andric int sockets[2]; /* the pair of socket descriptors */ 34180b57cec5SDimitry Andric if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) { 34190b57cec5SDimitry Andric error.SetErrorToErrno(); 34200b57cec5SDimitry Andric return error; 34210b57cec5SDimitry Andric } 34220b57cec5SDimitry Andric 34230b57cec5SDimitry Andric int our_socket = sockets[0]; 34240b57cec5SDimitry Andric int gdb_socket = sockets[1]; 34259dba64beSDimitry Andric auto cleanup_our = llvm::make_scope_exit([&]() { close(our_socket); }); 34269dba64beSDimitry Andric auto cleanup_gdb = llvm::make_scope_exit([&]() { close(gdb_socket); }); 34270b57cec5SDimitry Andric 34280b57cec5SDimitry Andric // Don't let any child processes inherit our communication socket 34290b57cec5SDimitry Andric SetCloexecFlag(our_socket); 34300b57cec5SDimitry Andric communication_fd = gdb_socket; 34310b57cec5SDimitry Andric #endif 34320b57cec5SDimitry Andric 34330b57cec5SDimitry Andric error = m_gdb_comm.StartDebugserverProcess( 34340b57cec5SDimitry Andric nullptr, GetTarget().GetPlatform().get(), debugserver_launch_info, 34350b57cec5SDimitry Andric nullptr, nullptr, communication_fd); 34360b57cec5SDimitry Andric 34370b57cec5SDimitry Andric if (error.Success()) 34380b57cec5SDimitry Andric m_debugserver_pid = debugserver_launch_info.GetProcessID(); 34390b57cec5SDimitry Andric else 34400b57cec5SDimitry Andric m_debugserver_pid = LLDB_INVALID_PROCESS_ID; 34410b57cec5SDimitry Andric 34420b57cec5SDimitry Andric if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) { 34430b57cec5SDimitry Andric #ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 34440b57cec5SDimitry Andric // Our process spawned correctly, we can now set our connection to use 34450b57cec5SDimitry Andric // our end of the socket pair 34469dba64beSDimitry Andric cleanup_our.release(); 34475ffd83dbSDimitry Andric m_gdb_comm.SetConnection( 34485ffd83dbSDimitry Andric std::make_unique<ConnectionFileDescriptor>(our_socket, true)); 34490b57cec5SDimitry Andric #endif 34500b57cec5SDimitry Andric StartAsyncThread(); 34510b57cec5SDimitry Andric } 34520b57cec5SDimitry Andric 34530b57cec5SDimitry Andric if (error.Fail()) { 34540b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 34550b57cec5SDimitry Andric 34569dba64beSDimitry Andric LLDB_LOGF(log, "failed to start debugserver process: %s", 34570b57cec5SDimitry Andric error.AsCString()); 34580b57cec5SDimitry Andric return error; 34590b57cec5SDimitry Andric } 34600b57cec5SDimitry Andric 34610b57cec5SDimitry Andric if (m_gdb_comm.IsConnected()) { 34620b57cec5SDimitry Andric // Finish the connection process by doing the handshake without 34630b57cec5SDimitry Andric // connecting (send NULL URL) 34640b57cec5SDimitry Andric error = ConnectToDebugserver(""); 34650b57cec5SDimitry Andric } else { 34660b57cec5SDimitry Andric error.SetErrorString("connection failed"); 34670b57cec5SDimitry Andric } 34680b57cec5SDimitry Andric } 34690b57cec5SDimitry Andric return error; 34700b57cec5SDimitry Andric } 34710b57cec5SDimitry Andric 34720b57cec5SDimitry Andric bool ProcessGDBRemote::MonitorDebugserverProcess( 34730b57cec5SDimitry Andric std::weak_ptr<ProcessGDBRemote> process_wp, lldb::pid_t debugserver_pid, 34740b57cec5SDimitry Andric bool exited, // True if the process did exit 34750b57cec5SDimitry Andric int signo, // Zero for no signal 34760b57cec5SDimitry Andric int exit_status // Exit value of process if signal is zero 34770b57cec5SDimitry Andric ) { 34780b57cec5SDimitry Andric // "debugserver_pid" argument passed in is the process ID for debugserver 34790b57cec5SDimitry Andric // that we are tracking... 34800b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 34810b57cec5SDimitry Andric const bool handled = true; 34820b57cec5SDimitry Andric 34839dba64beSDimitry Andric LLDB_LOGF(log, 34849dba64beSDimitry Andric "ProcessGDBRemote::%s(process_wp, pid=%" PRIu64 34850b57cec5SDimitry Andric ", signo=%i (0x%x), exit_status=%i)", 34860b57cec5SDimitry Andric __FUNCTION__, debugserver_pid, signo, signo, exit_status); 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric std::shared_ptr<ProcessGDBRemote> process_sp = process_wp.lock(); 34899dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s(process = %p)", __FUNCTION__, 34900b57cec5SDimitry Andric static_cast<void *>(process_sp.get())); 34910b57cec5SDimitry Andric if (!process_sp || process_sp->m_debugserver_pid != debugserver_pid) 34920b57cec5SDimitry Andric return handled; 34930b57cec5SDimitry Andric 34940b57cec5SDimitry Andric // Sleep for a half a second to make sure our inferior process has time to 34950b57cec5SDimitry Andric // set its exit status before we set it incorrectly when both the debugserver 34960b57cec5SDimitry Andric // and the inferior process shut down. 34979dba64beSDimitry Andric std::this_thread::sleep_for(std::chrono::milliseconds(500)); 34989dba64beSDimitry Andric 34990b57cec5SDimitry Andric // If our process hasn't yet exited, debugserver might have died. If the 35000b57cec5SDimitry Andric // process did exit, then we are reaping it. 35010b57cec5SDimitry Andric const StateType state = process_sp->GetState(); 35020b57cec5SDimitry Andric 35030b57cec5SDimitry Andric if (state != eStateInvalid && state != eStateUnloaded && 35040b57cec5SDimitry Andric state != eStateExited && state != eStateDetached) { 35050b57cec5SDimitry Andric char error_str[1024]; 35060b57cec5SDimitry Andric if (signo) { 35070b57cec5SDimitry Andric const char *signal_cstr = 35080b57cec5SDimitry Andric process_sp->GetUnixSignals()->GetSignalAsCString(signo); 35090b57cec5SDimitry Andric if (signal_cstr) 35100b57cec5SDimitry Andric ::snprintf(error_str, sizeof(error_str), 35110b57cec5SDimitry Andric DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); 35120b57cec5SDimitry Andric else 35130b57cec5SDimitry Andric ::snprintf(error_str, sizeof(error_str), 35140b57cec5SDimitry Andric DEBUGSERVER_BASENAME " died with signal %i", signo); 35150b57cec5SDimitry Andric } else { 35160b57cec5SDimitry Andric ::snprintf(error_str, sizeof(error_str), 35170b57cec5SDimitry Andric DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", 35180b57cec5SDimitry Andric exit_status); 35190b57cec5SDimitry Andric } 35200b57cec5SDimitry Andric 35210b57cec5SDimitry Andric process_sp->SetExitStatus(-1, error_str); 35220b57cec5SDimitry Andric } 35230b57cec5SDimitry Andric // Debugserver has exited we need to let our ProcessGDBRemote know that it no 35240b57cec5SDimitry Andric // longer has a debugserver instance 35250b57cec5SDimitry Andric process_sp->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; 35260b57cec5SDimitry Andric return handled; 35270b57cec5SDimitry Andric } 35280b57cec5SDimitry Andric 35290b57cec5SDimitry Andric void ProcessGDBRemote::KillDebugserverProcess() { 35300b57cec5SDimitry Andric m_gdb_comm.Disconnect(); 35310b57cec5SDimitry Andric if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) { 35320b57cec5SDimitry Andric Host::Kill(m_debugserver_pid, SIGINT); 35330b57cec5SDimitry Andric m_debugserver_pid = LLDB_INVALID_PROCESS_ID; 35340b57cec5SDimitry Andric } 35350b57cec5SDimitry Andric } 35360b57cec5SDimitry Andric 35370b57cec5SDimitry Andric void ProcessGDBRemote::Initialize() { 35380b57cec5SDimitry Andric static llvm::once_flag g_once_flag; 35390b57cec5SDimitry Andric 35400b57cec5SDimitry Andric llvm::call_once(g_once_flag, []() { 35410b57cec5SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(), 35420b57cec5SDimitry Andric GetPluginDescriptionStatic(), CreateInstance, 35430b57cec5SDimitry Andric DebuggerInitialize); 35440b57cec5SDimitry Andric }); 35450b57cec5SDimitry Andric } 35460b57cec5SDimitry Andric 35470b57cec5SDimitry Andric void ProcessGDBRemote::DebuggerInitialize(Debugger &debugger) { 35480b57cec5SDimitry Andric if (!PluginManager::GetSettingForProcessPlugin( 35490b57cec5SDimitry Andric debugger, PluginProperties::GetSettingName())) { 35500b57cec5SDimitry Andric const bool is_global_setting = true; 35510b57cec5SDimitry Andric PluginManager::CreateSettingForProcessPlugin( 3552349cc55cSDimitry Andric debugger, GetGlobalPluginProperties().GetValueProperties(), 35530b57cec5SDimitry Andric ConstString("Properties for the gdb-remote process plug-in."), 35540b57cec5SDimitry Andric is_global_setting); 35550b57cec5SDimitry Andric } 35560b57cec5SDimitry Andric } 35570b57cec5SDimitry Andric 35580b57cec5SDimitry Andric bool ProcessGDBRemote::StartAsyncThread() { 35590b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 35600b57cec5SDimitry Andric 35619dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s ()", __FUNCTION__); 35620b57cec5SDimitry Andric 35630b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); 35640b57cec5SDimitry Andric if (!m_async_thread.IsJoinable()) { 35650b57cec5SDimitry Andric // Create a thread that watches our internal state and controls which 35660b57cec5SDimitry Andric // events make it to clients (into the DCProcess event queue). 35670b57cec5SDimitry Andric 35680b57cec5SDimitry Andric llvm::Expected<HostThread> async_thread = ThreadLauncher::LaunchThread( 35690b57cec5SDimitry Andric "<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this); 35700b57cec5SDimitry Andric if (!async_thread) { 3571480093f4SDimitry Andric LLDB_LOG_ERROR(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), 3572480093f4SDimitry Andric async_thread.takeError(), 3573480093f4SDimitry Andric "failed to launch host thread: {}"); 35740b57cec5SDimitry Andric return false; 35750b57cec5SDimitry Andric } 35760b57cec5SDimitry Andric m_async_thread = *async_thread; 35779dba64beSDimitry Andric } else 35789dba64beSDimitry Andric LLDB_LOGF(log, 35799dba64beSDimitry Andric "ProcessGDBRemote::%s () - Called when Async thread was " 35800b57cec5SDimitry Andric "already running.", 35810b57cec5SDimitry Andric __FUNCTION__); 35820b57cec5SDimitry Andric 35830b57cec5SDimitry Andric return m_async_thread.IsJoinable(); 35840b57cec5SDimitry Andric } 35850b57cec5SDimitry Andric 35860b57cec5SDimitry Andric void ProcessGDBRemote::StopAsyncThread() { 35870b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 35880b57cec5SDimitry Andric 35899dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s ()", __FUNCTION__); 35900b57cec5SDimitry Andric 35910b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex); 35920b57cec5SDimitry Andric if (m_async_thread.IsJoinable()) { 35930b57cec5SDimitry Andric m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit); 35940b57cec5SDimitry Andric 35950b57cec5SDimitry Andric // This will shut down the async thread. 35960b57cec5SDimitry Andric m_gdb_comm.Disconnect(); // Disconnect from the debug server. 35970b57cec5SDimitry Andric 35980b57cec5SDimitry Andric // Stop the stdio thread 35990b57cec5SDimitry Andric m_async_thread.Join(nullptr); 36000b57cec5SDimitry Andric m_async_thread.Reset(); 36019dba64beSDimitry Andric } else 36029dba64beSDimitry Andric LLDB_LOGF( 36039dba64beSDimitry Andric log, 36040b57cec5SDimitry Andric "ProcessGDBRemote::%s () - Called when Async thread was not running.", 36050b57cec5SDimitry Andric __FUNCTION__); 36060b57cec5SDimitry Andric } 36070b57cec5SDimitry Andric 36080b57cec5SDimitry Andric thread_result_t ProcessGDBRemote::AsyncThread(void *arg) { 36090b57cec5SDimitry Andric ProcessGDBRemote *process = (ProcessGDBRemote *)arg; 36100b57cec5SDimitry Andric 36110b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 36129dba64beSDimitry Andric LLDB_LOGF(log, 36139dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 36140b57cec5SDimitry Andric ") thread starting...", 36150b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID()); 36160b57cec5SDimitry Andric 36170b57cec5SDimitry Andric EventSP event_sp; 3618fe6060f1SDimitry Andric 3619fe6060f1SDimitry Andric // We need to ignore any packets that come in after we have 3620fe6060f1SDimitry Andric // have decided the process has exited. There are some 3621fe6060f1SDimitry Andric // situations, for instance when we try to interrupt a running 3622fe6060f1SDimitry Andric // process and the interrupt fails, where another packet might 3623fe6060f1SDimitry Andric // get delivered after we've decided to give up on the process. 3624fe6060f1SDimitry Andric // But once we've decided we are done with the process we will 3625fe6060f1SDimitry Andric // not be in a state to do anything useful with new packets. 3626fe6060f1SDimitry Andric // So it is safer to simply ignore any remaining packets by 3627fe6060f1SDimitry Andric // explicitly checking for eStateExited before reentering the 3628fe6060f1SDimitry Andric // fetch loop. 3629fe6060f1SDimitry Andric 36300b57cec5SDimitry Andric bool done = false; 3631fe6060f1SDimitry Andric while (!done && process->GetPrivateState() != eStateExited) { 36329dba64beSDimitry Andric LLDB_LOGF(log, 36339dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 36340b57cec5SDimitry Andric ") listener.WaitForEvent (NULL, event_sp)...", 36350b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID()); 3636fe6060f1SDimitry Andric 36370b57cec5SDimitry Andric if (process->m_async_listener_sp->GetEvent(event_sp, llvm::None)) { 36380b57cec5SDimitry Andric const uint32_t event_type = event_sp->GetType(); 36390b57cec5SDimitry Andric if (event_sp->BroadcasterIs(&process->m_async_broadcaster)) { 36409dba64beSDimitry Andric LLDB_LOGF(log, 36419dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 36420b57cec5SDimitry Andric ") Got an event of type: %d...", 36430b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID(), event_type); 36440b57cec5SDimitry Andric 36450b57cec5SDimitry Andric switch (event_type) { 36460b57cec5SDimitry Andric case eBroadcastBitAsyncContinue: { 36470b57cec5SDimitry Andric const EventDataBytes *continue_packet = 36480b57cec5SDimitry Andric EventDataBytes::GetEventDataFromEvent(event_sp.get()); 36490b57cec5SDimitry Andric 36500b57cec5SDimitry Andric if (continue_packet) { 36510b57cec5SDimitry Andric const char *continue_cstr = 36520b57cec5SDimitry Andric (const char *)continue_packet->GetBytes(); 36530b57cec5SDimitry Andric const size_t continue_cstr_len = continue_packet->GetByteSize(); 36549dba64beSDimitry Andric LLDB_LOGF(log, 36559dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 36560b57cec5SDimitry Andric ") got eBroadcastBitAsyncContinue: %s", 36570b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID(), continue_cstr); 36580b57cec5SDimitry Andric 36590b57cec5SDimitry Andric if (::strstr(continue_cstr, "vAttach") == nullptr) 36600b57cec5SDimitry Andric process->SetPrivateState(eStateRunning); 36610b57cec5SDimitry Andric StringExtractorGDBRemote response; 36620b57cec5SDimitry Andric 36630b57cec5SDimitry Andric StateType stop_state = 36640b57cec5SDimitry Andric process->GetGDBRemote().SendContinuePacketAndWaitForResponse( 36650b57cec5SDimitry Andric *process, *process->GetUnixSignals(), 36660b57cec5SDimitry Andric llvm::StringRef(continue_cstr, continue_cstr_len), 3667349cc55cSDimitry Andric process->GetInterruptTimeout(), response); 36680b57cec5SDimitry Andric 36690b57cec5SDimitry Andric // We need to immediately clear the thread ID list so we are sure 36700b57cec5SDimitry Andric // to get a valid list of threads. The thread ID list might be 36710b57cec5SDimitry Andric // contained within the "response", or the stop reply packet that 36720b57cec5SDimitry Andric // caused the stop. So clear it now before we give the stop reply 36730b57cec5SDimitry Andric // packet to the process using the 36740b57cec5SDimitry Andric // process->SetLastStopPacket()... 36750b57cec5SDimitry Andric process->ClearThreadIDList(); 36760b57cec5SDimitry Andric 36770b57cec5SDimitry Andric switch (stop_state) { 36780b57cec5SDimitry Andric case eStateStopped: 36790b57cec5SDimitry Andric case eStateCrashed: 36800b57cec5SDimitry Andric case eStateSuspended: 36810b57cec5SDimitry Andric process->SetLastStopPacket(response); 36820b57cec5SDimitry Andric process->SetPrivateState(stop_state); 36830b57cec5SDimitry Andric break; 36840b57cec5SDimitry Andric 36850b57cec5SDimitry Andric case eStateExited: { 36860b57cec5SDimitry Andric process->SetLastStopPacket(response); 36870b57cec5SDimitry Andric process->ClearThreadIDList(); 36880b57cec5SDimitry Andric response.SetFilePos(1); 36890b57cec5SDimitry Andric 36900b57cec5SDimitry Andric int exit_status = response.GetHexU8(); 36910b57cec5SDimitry Andric std::string desc_string; 3692349cc55cSDimitry Andric if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';') { 36930b57cec5SDimitry Andric llvm::StringRef desc_str; 36940b57cec5SDimitry Andric llvm::StringRef desc_token; 36950b57cec5SDimitry Andric while (response.GetNameColonValue(desc_token, desc_str)) { 36960b57cec5SDimitry Andric if (desc_token != "description") 36970b57cec5SDimitry Andric continue; 36980b57cec5SDimitry Andric StringExtractor extractor(desc_str); 36990b57cec5SDimitry Andric extractor.GetHexByteString(desc_string); 37000b57cec5SDimitry Andric } 37010b57cec5SDimitry Andric } 37020b57cec5SDimitry Andric process->SetExitStatus(exit_status, desc_string.c_str()); 37030b57cec5SDimitry Andric done = true; 37040b57cec5SDimitry Andric break; 37050b57cec5SDimitry Andric } 37060b57cec5SDimitry Andric case eStateInvalid: { 37070b57cec5SDimitry Andric // Check to see if we were trying to attach and if we got back 37080b57cec5SDimitry Andric // the "E87" error code from debugserver -- this indicates that 37090b57cec5SDimitry Andric // the process is not debuggable. Return a slightly more 37100b57cec5SDimitry Andric // helpful error message about why the attach failed. 37110b57cec5SDimitry Andric if (::strstr(continue_cstr, "vAttach") != nullptr && 37120b57cec5SDimitry Andric response.GetError() == 0x87) { 37130b57cec5SDimitry Andric process->SetExitStatus(-1, "cannot attach to process due to " 37140b57cec5SDimitry Andric "System Integrity Protection"); 37150b57cec5SDimitry Andric } else if (::strstr(continue_cstr, "vAttach") != nullptr && 37160b57cec5SDimitry Andric response.GetStatus().Fail()) { 37170b57cec5SDimitry Andric process->SetExitStatus(-1, response.GetStatus().AsCString()); 37180b57cec5SDimitry Andric } else { 37190b57cec5SDimitry Andric process->SetExitStatus(-1, "lost connection"); 37200b57cec5SDimitry Andric } 3721fe6060f1SDimitry Andric done = true; 37220b57cec5SDimitry Andric break; 37230b57cec5SDimitry Andric } 37240b57cec5SDimitry Andric 37250b57cec5SDimitry Andric default: 37260b57cec5SDimitry Andric process->SetPrivateState(stop_state); 37270b57cec5SDimitry Andric break; 37280b57cec5SDimitry Andric } // switch(stop_state) 37290b57cec5SDimitry Andric } // if (continue_packet) 37305ffd83dbSDimitry Andric } // case eBroadcastBitAsyncContinue 37310b57cec5SDimitry Andric break; 37320b57cec5SDimitry Andric 37330b57cec5SDimitry Andric case eBroadcastBitAsyncThreadShouldExit: 37349dba64beSDimitry Andric LLDB_LOGF(log, 37359dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 37360b57cec5SDimitry Andric ") got eBroadcastBitAsyncThreadShouldExit...", 37370b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID()); 37380b57cec5SDimitry Andric done = true; 37390b57cec5SDimitry Andric break; 37400b57cec5SDimitry Andric 37410b57cec5SDimitry Andric default: 37429dba64beSDimitry Andric LLDB_LOGF(log, 37439dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 37440b57cec5SDimitry Andric ") got unknown event 0x%8.8x", 37450b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID(), event_type); 37460b57cec5SDimitry Andric done = true; 37470b57cec5SDimitry Andric break; 37480b57cec5SDimitry Andric } 37490b57cec5SDimitry Andric } else if (event_sp->BroadcasterIs(&process->m_gdb_comm)) { 37500b57cec5SDimitry Andric switch (event_type) { 37510b57cec5SDimitry Andric case Communication::eBroadcastBitReadThreadDidExit: 37520b57cec5SDimitry Andric process->SetExitStatus(-1, "lost connection"); 37530b57cec5SDimitry Andric done = true; 37540b57cec5SDimitry Andric break; 37550b57cec5SDimitry Andric 37560b57cec5SDimitry Andric default: 37579dba64beSDimitry Andric LLDB_LOGF(log, 37589dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 37590b57cec5SDimitry Andric ") got unknown event 0x%8.8x", 37600b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID(), event_type); 37610b57cec5SDimitry Andric done = true; 37620b57cec5SDimitry Andric break; 37630b57cec5SDimitry Andric } 37640b57cec5SDimitry Andric } 37650b57cec5SDimitry Andric } else { 37669dba64beSDimitry Andric LLDB_LOGF(log, 37679dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 37680b57cec5SDimitry Andric ") listener.WaitForEvent (NULL, event_sp) => false", 37690b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID()); 37700b57cec5SDimitry Andric done = true; 37710b57cec5SDimitry Andric } 37720b57cec5SDimitry Andric } 37730b57cec5SDimitry Andric 37749dba64beSDimitry Andric LLDB_LOGF(log, 37759dba64beSDimitry Andric "ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 37760b57cec5SDimitry Andric ") thread exiting...", 37770b57cec5SDimitry Andric __FUNCTION__, arg, process->GetID()); 37780b57cec5SDimitry Andric 37790b57cec5SDimitry Andric return {}; 37800b57cec5SDimitry Andric } 37810b57cec5SDimitry Andric 37820b57cec5SDimitry Andric // uint32_t 37830b57cec5SDimitry Andric // ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList 37840b57cec5SDimitry Andric // &matches, std::vector<lldb::pid_t> &pids) 37850b57cec5SDimitry Andric //{ 37860b57cec5SDimitry Andric // // If we are planning to launch the debugserver remotely, then we need to 37870b57cec5SDimitry Andric // fire up a debugserver 37880b57cec5SDimitry Andric // // process and ask it for the list of processes. But if we are local, we 37890b57cec5SDimitry Andric // can let the Host do it. 37900b57cec5SDimitry Andric // if (m_local_debugserver) 37910b57cec5SDimitry Andric // { 37920b57cec5SDimitry Andric // return Host::ListProcessesMatchingName (name, matches, pids); 37930b57cec5SDimitry Andric // } 37940b57cec5SDimitry Andric // else 37950b57cec5SDimitry Andric // { 37960b57cec5SDimitry Andric // // FIXME: Implement talking to the remote debugserver. 37970b57cec5SDimitry Andric // return 0; 37980b57cec5SDimitry Andric // } 37990b57cec5SDimitry Andric // 38000b57cec5SDimitry Andric //} 38010b57cec5SDimitry Andric // 38020b57cec5SDimitry Andric bool ProcessGDBRemote::NewThreadNotifyBreakpointHit( 38030b57cec5SDimitry Andric void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, 38040b57cec5SDimitry Andric lldb::user_id_t break_loc_id) { 38050b57cec5SDimitry Andric // I don't think I have to do anything here, just make sure I notice the new 38060b57cec5SDimitry Andric // thread when it starts to 38070b57cec5SDimitry Andric // run so I can stop it if that's what I want to do. 38080b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 38099dba64beSDimitry Andric LLDB_LOGF(log, "Hit New Thread Notification breakpoint."); 38100b57cec5SDimitry Andric return false; 38110b57cec5SDimitry Andric } 38120b57cec5SDimitry Andric 38130b57cec5SDimitry Andric Status ProcessGDBRemote::UpdateAutomaticSignalFiltering() { 38140b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 38150b57cec5SDimitry Andric LLDB_LOG(log, "Check if need to update ignored signals"); 38160b57cec5SDimitry Andric 38170b57cec5SDimitry Andric // QPassSignals package is not supported by the server, there is no way we 38180b57cec5SDimitry Andric // can ignore any signals on server side. 38190b57cec5SDimitry Andric if (!m_gdb_comm.GetQPassSignalsSupported()) 38200b57cec5SDimitry Andric return Status(); 38210b57cec5SDimitry Andric 38220b57cec5SDimitry Andric // No signals, nothing to send. 38230b57cec5SDimitry Andric if (m_unix_signals_sp == nullptr) 38240b57cec5SDimitry Andric return Status(); 38250b57cec5SDimitry Andric 38260b57cec5SDimitry Andric // Signals' version hasn't changed, no need to send anything. 38270b57cec5SDimitry Andric uint64_t new_signals_version = m_unix_signals_sp->GetVersion(); 38280b57cec5SDimitry Andric if (new_signals_version == m_last_signals_version) { 38290b57cec5SDimitry Andric LLDB_LOG(log, "Signals' version hasn't changed. version={0}", 38300b57cec5SDimitry Andric m_last_signals_version); 38310b57cec5SDimitry Andric return Status(); 38320b57cec5SDimitry Andric } 38330b57cec5SDimitry Andric 38340b57cec5SDimitry Andric auto signals_to_ignore = 38350b57cec5SDimitry Andric m_unix_signals_sp->GetFilteredSignals(false, false, false); 38360b57cec5SDimitry Andric Status error = m_gdb_comm.SendSignalsToIgnore(signals_to_ignore); 38370b57cec5SDimitry Andric 38380b57cec5SDimitry Andric LLDB_LOG(log, 38390b57cec5SDimitry Andric "Signals' version changed. old version={0}, new version={1}, " 38400b57cec5SDimitry Andric "signals ignored={2}, update result={3}", 38410b57cec5SDimitry Andric m_last_signals_version, new_signals_version, 38420b57cec5SDimitry Andric signals_to_ignore.size(), error); 38430b57cec5SDimitry Andric 38440b57cec5SDimitry Andric if (error.Success()) 38450b57cec5SDimitry Andric m_last_signals_version = new_signals_version; 38460b57cec5SDimitry Andric 38470b57cec5SDimitry Andric return error; 38480b57cec5SDimitry Andric } 38490b57cec5SDimitry Andric 38500b57cec5SDimitry Andric bool ProcessGDBRemote::StartNoticingNewThreads() { 38510b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 38520b57cec5SDimitry Andric if (m_thread_create_bp_sp) { 38530b57cec5SDimitry Andric if (log && log->GetVerbose()) 38549dba64beSDimitry Andric LLDB_LOGF(log, "Enabled noticing new thread breakpoint."); 38550b57cec5SDimitry Andric m_thread_create_bp_sp->SetEnabled(true); 38560b57cec5SDimitry Andric } else { 38570b57cec5SDimitry Andric PlatformSP platform_sp(GetTarget().GetPlatform()); 38580b57cec5SDimitry Andric if (platform_sp) { 38590b57cec5SDimitry Andric m_thread_create_bp_sp = 38600b57cec5SDimitry Andric platform_sp->SetThreadCreationBreakpoint(GetTarget()); 38610b57cec5SDimitry Andric if (m_thread_create_bp_sp) { 38620b57cec5SDimitry Andric if (log && log->GetVerbose()) 38639dba64beSDimitry Andric LLDB_LOGF( 38649dba64beSDimitry Andric log, "Successfully created new thread notification breakpoint %i", 38650b57cec5SDimitry Andric m_thread_create_bp_sp->GetID()); 38660b57cec5SDimitry Andric m_thread_create_bp_sp->SetCallback( 38670b57cec5SDimitry Andric ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); 38680b57cec5SDimitry Andric } else { 38699dba64beSDimitry Andric LLDB_LOGF(log, "Failed to create new thread notification breakpoint."); 38700b57cec5SDimitry Andric } 38710b57cec5SDimitry Andric } 38720b57cec5SDimitry Andric } 38730b57cec5SDimitry Andric return m_thread_create_bp_sp.get() != nullptr; 38740b57cec5SDimitry Andric } 38750b57cec5SDimitry Andric 38760b57cec5SDimitry Andric bool ProcessGDBRemote::StopNoticingNewThreads() { 38770b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 38780b57cec5SDimitry Andric if (log && log->GetVerbose()) 38799dba64beSDimitry Andric LLDB_LOGF(log, "Disabling new thread notification breakpoint."); 38800b57cec5SDimitry Andric 38810b57cec5SDimitry Andric if (m_thread_create_bp_sp) 38820b57cec5SDimitry Andric m_thread_create_bp_sp->SetEnabled(false); 38830b57cec5SDimitry Andric 38840b57cec5SDimitry Andric return true; 38850b57cec5SDimitry Andric } 38860b57cec5SDimitry Andric 38870b57cec5SDimitry Andric DynamicLoader *ProcessGDBRemote::GetDynamicLoader() { 38880b57cec5SDimitry Andric if (m_dyld_up.get() == nullptr) 3889349cc55cSDimitry Andric m_dyld_up.reset(DynamicLoader::FindPlugin(this, "")); 38900b57cec5SDimitry Andric return m_dyld_up.get(); 38910b57cec5SDimitry Andric } 38920b57cec5SDimitry Andric 38930b57cec5SDimitry Andric Status ProcessGDBRemote::SendEventData(const char *data) { 38940b57cec5SDimitry Andric int return_value; 38950b57cec5SDimitry Andric bool was_supported; 38960b57cec5SDimitry Andric 38970b57cec5SDimitry Andric Status error; 38980b57cec5SDimitry Andric 38990b57cec5SDimitry Andric return_value = m_gdb_comm.SendLaunchEventDataPacket(data, &was_supported); 39000b57cec5SDimitry Andric if (return_value != 0) { 39010b57cec5SDimitry Andric if (!was_supported) 39020b57cec5SDimitry Andric error.SetErrorString("Sending events is not supported for this process."); 39030b57cec5SDimitry Andric else 39040b57cec5SDimitry Andric error.SetErrorStringWithFormat("Error sending event data: %d.", 39050b57cec5SDimitry Andric return_value); 39060b57cec5SDimitry Andric } 39070b57cec5SDimitry Andric return error; 39080b57cec5SDimitry Andric } 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric DataExtractor ProcessGDBRemote::GetAuxvData() { 39110b57cec5SDimitry Andric DataBufferSP buf; 39120b57cec5SDimitry Andric if (m_gdb_comm.GetQXferAuxvReadSupported()) { 3913349cc55cSDimitry Andric llvm::Expected<std::string> response = m_gdb_comm.ReadExtFeature("auxv", ""); 3914349cc55cSDimitry Andric if (response) 3915349cc55cSDimitry Andric buf = std::make_shared<DataBufferHeap>(response->c_str(), 3916349cc55cSDimitry Andric response->length()); 3917349cc55cSDimitry Andric else 3918349cc55cSDimitry Andric LLDB_LOG_ERROR( 3919349cc55cSDimitry Andric ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS), 3920349cc55cSDimitry Andric response.takeError(), "{0}"); 39210b57cec5SDimitry Andric } 39220b57cec5SDimitry Andric return DataExtractor(buf, GetByteOrder(), GetAddressByteSize()); 39230b57cec5SDimitry Andric } 39240b57cec5SDimitry Andric 39250b57cec5SDimitry Andric StructuredData::ObjectSP 39260b57cec5SDimitry Andric ProcessGDBRemote::GetExtendedInfoForThread(lldb::tid_t tid) { 39270b57cec5SDimitry Andric StructuredData::ObjectSP object_sp; 39280b57cec5SDimitry Andric 39290b57cec5SDimitry Andric if (m_gdb_comm.GetThreadExtendedInfoSupported()) { 39300b57cec5SDimitry Andric StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); 39310b57cec5SDimitry Andric SystemRuntime *runtime = GetSystemRuntime(); 39320b57cec5SDimitry Andric if (runtime) { 39330b57cec5SDimitry Andric runtime->AddThreadExtendedInfoPacketHints(args_dict); 39340b57cec5SDimitry Andric } 39350b57cec5SDimitry Andric args_dict->GetAsDictionary()->AddIntegerItem("thread", tid); 39360b57cec5SDimitry Andric 39370b57cec5SDimitry Andric StreamString packet; 39380b57cec5SDimitry Andric packet << "jThreadExtendedInfo:"; 39390b57cec5SDimitry Andric args_dict->Dump(packet, false); 39400b57cec5SDimitry Andric 39410b57cec5SDimitry Andric // FIXME the final character of a JSON dictionary, '}', is the escape 39420b57cec5SDimitry Andric // character in gdb-remote binary mode. lldb currently doesn't escape 39430b57cec5SDimitry Andric // these characters in its packet output -- so we add the quoted version of 39440b57cec5SDimitry Andric // the } character here manually in case we talk to a debugserver which un- 39450b57cec5SDimitry Andric // escapes the characters at packet read time. 39460b57cec5SDimitry Andric packet << (char)(0x7d ^ 0x20); 39470b57cec5SDimitry Andric 39480b57cec5SDimitry Andric StringExtractorGDBRemote response; 39490b57cec5SDimitry Andric response.SetResponseValidatorToJSON(); 3950fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) == 39510b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 39520b57cec5SDimitry Andric StringExtractorGDBRemote::ResponseType response_type = 39530b57cec5SDimitry Andric response.GetResponseType(); 39540b57cec5SDimitry Andric if (response_type == StringExtractorGDBRemote::eResponse) { 39550b57cec5SDimitry Andric if (!response.Empty()) { 39565ffd83dbSDimitry Andric object_sp = 39575ffd83dbSDimitry Andric StructuredData::ParseJSON(std::string(response.GetStringRef())); 39580b57cec5SDimitry Andric } 39590b57cec5SDimitry Andric } 39600b57cec5SDimitry Andric } 39610b57cec5SDimitry Andric } 39620b57cec5SDimitry Andric return object_sp; 39630b57cec5SDimitry Andric } 39640b57cec5SDimitry Andric 39650b57cec5SDimitry Andric StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos( 39660b57cec5SDimitry Andric lldb::addr_t image_list_address, lldb::addr_t image_count) { 39670b57cec5SDimitry Andric 39680b57cec5SDimitry Andric StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); 39690b57cec5SDimitry Andric args_dict->GetAsDictionary()->AddIntegerItem("image_list_address", 39700b57cec5SDimitry Andric image_list_address); 39710b57cec5SDimitry Andric args_dict->GetAsDictionary()->AddIntegerItem("image_count", image_count); 39720b57cec5SDimitry Andric 39730b57cec5SDimitry Andric return GetLoadedDynamicLibrariesInfos_sender(args_dict); 39740b57cec5SDimitry Andric } 39750b57cec5SDimitry Andric 39760b57cec5SDimitry Andric StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos() { 39770b57cec5SDimitry Andric StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); 39780b57cec5SDimitry Andric 39790b57cec5SDimitry Andric args_dict->GetAsDictionary()->AddBooleanItem("fetch_all_solibs", true); 39800b57cec5SDimitry Andric 39810b57cec5SDimitry Andric return GetLoadedDynamicLibrariesInfos_sender(args_dict); 39820b57cec5SDimitry Andric } 39830b57cec5SDimitry Andric 39840b57cec5SDimitry Andric StructuredData::ObjectSP ProcessGDBRemote::GetLoadedDynamicLibrariesInfos( 39850b57cec5SDimitry Andric const std::vector<lldb::addr_t> &load_addresses) { 39860b57cec5SDimitry Andric StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); 39870b57cec5SDimitry Andric StructuredData::ArraySP addresses(new StructuredData::Array); 39880b57cec5SDimitry Andric 39890b57cec5SDimitry Andric for (auto addr : load_addresses) { 39900b57cec5SDimitry Andric StructuredData::ObjectSP addr_sp(new StructuredData::Integer(addr)); 39910b57cec5SDimitry Andric addresses->AddItem(addr_sp); 39920b57cec5SDimitry Andric } 39930b57cec5SDimitry Andric 39940b57cec5SDimitry Andric args_dict->GetAsDictionary()->AddItem("solib_addresses", addresses); 39950b57cec5SDimitry Andric 39960b57cec5SDimitry Andric return GetLoadedDynamicLibrariesInfos_sender(args_dict); 39970b57cec5SDimitry Andric } 39980b57cec5SDimitry Andric 39990b57cec5SDimitry Andric StructuredData::ObjectSP 40000b57cec5SDimitry Andric ProcessGDBRemote::GetLoadedDynamicLibrariesInfos_sender( 40010b57cec5SDimitry Andric StructuredData::ObjectSP args_dict) { 40020b57cec5SDimitry Andric StructuredData::ObjectSP object_sp; 40030b57cec5SDimitry Andric 40040b57cec5SDimitry Andric if (m_gdb_comm.GetLoadedDynamicLibrariesInfosSupported()) { 40050b57cec5SDimitry Andric // Scope for the scoped timeout object 40060b57cec5SDimitry Andric GDBRemoteCommunication::ScopedTimeout timeout(m_gdb_comm, 40070b57cec5SDimitry Andric std::chrono::seconds(10)); 40080b57cec5SDimitry Andric 40090b57cec5SDimitry Andric StreamString packet; 40100b57cec5SDimitry Andric packet << "jGetLoadedDynamicLibrariesInfos:"; 40110b57cec5SDimitry Andric args_dict->Dump(packet, false); 40120b57cec5SDimitry Andric 40130b57cec5SDimitry Andric // FIXME the final character of a JSON dictionary, '}', is the escape 40140b57cec5SDimitry Andric // character in gdb-remote binary mode. lldb currently doesn't escape 40150b57cec5SDimitry Andric // these characters in its packet output -- so we add the quoted version of 40160b57cec5SDimitry Andric // the } character here manually in case we talk to a debugserver which un- 40170b57cec5SDimitry Andric // escapes the characters at packet read time. 40180b57cec5SDimitry Andric packet << (char)(0x7d ^ 0x20); 40190b57cec5SDimitry Andric 40200b57cec5SDimitry Andric StringExtractorGDBRemote response; 40210b57cec5SDimitry Andric response.SetResponseValidatorToJSON(); 4022fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) == 40230b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 40240b57cec5SDimitry Andric StringExtractorGDBRemote::ResponseType response_type = 40250b57cec5SDimitry Andric response.GetResponseType(); 40260b57cec5SDimitry Andric if (response_type == StringExtractorGDBRemote::eResponse) { 40270b57cec5SDimitry Andric if (!response.Empty()) { 40285ffd83dbSDimitry Andric object_sp = 40295ffd83dbSDimitry Andric StructuredData::ParseJSON(std::string(response.GetStringRef())); 40300b57cec5SDimitry Andric } 40310b57cec5SDimitry Andric } 40320b57cec5SDimitry Andric } 40330b57cec5SDimitry Andric } 40340b57cec5SDimitry Andric return object_sp; 40350b57cec5SDimitry Andric } 40360b57cec5SDimitry Andric 40370b57cec5SDimitry Andric StructuredData::ObjectSP ProcessGDBRemote::GetSharedCacheInfo() { 40380b57cec5SDimitry Andric StructuredData::ObjectSP object_sp; 40390b57cec5SDimitry Andric StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); 40400b57cec5SDimitry Andric 40410b57cec5SDimitry Andric if (m_gdb_comm.GetSharedCacheInfoSupported()) { 40420b57cec5SDimitry Andric StreamString packet; 40430b57cec5SDimitry Andric packet << "jGetSharedCacheInfo:"; 40440b57cec5SDimitry Andric args_dict->Dump(packet, false); 40450b57cec5SDimitry Andric 40460b57cec5SDimitry Andric // FIXME the final character of a JSON dictionary, '}', is the escape 40470b57cec5SDimitry Andric // character in gdb-remote binary mode. lldb currently doesn't escape 40480b57cec5SDimitry Andric // these characters in its packet output -- so we add the quoted version of 40490b57cec5SDimitry Andric // the } character here manually in case we talk to a debugserver which un- 40500b57cec5SDimitry Andric // escapes the characters at packet read time. 40510b57cec5SDimitry Andric packet << (char)(0x7d ^ 0x20); 40520b57cec5SDimitry Andric 40530b57cec5SDimitry Andric StringExtractorGDBRemote response; 40540b57cec5SDimitry Andric response.SetResponseValidatorToJSON(); 4055fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) == 40560b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 40570b57cec5SDimitry Andric StringExtractorGDBRemote::ResponseType response_type = 40580b57cec5SDimitry Andric response.GetResponseType(); 40590b57cec5SDimitry Andric if (response_type == StringExtractorGDBRemote::eResponse) { 40600b57cec5SDimitry Andric if (!response.Empty()) { 40615ffd83dbSDimitry Andric object_sp = 40625ffd83dbSDimitry Andric StructuredData::ParseJSON(std::string(response.GetStringRef())); 40630b57cec5SDimitry Andric } 40640b57cec5SDimitry Andric } 40650b57cec5SDimitry Andric } 40660b57cec5SDimitry Andric } 40670b57cec5SDimitry Andric return object_sp; 40680b57cec5SDimitry Andric } 40690b57cec5SDimitry Andric 40700b57cec5SDimitry Andric Status ProcessGDBRemote::ConfigureStructuredData( 40710b57cec5SDimitry Andric ConstString type_name, const StructuredData::ObjectSP &config_sp) { 40720b57cec5SDimitry Andric return m_gdb_comm.ConfigureRemoteStructuredData(type_name, config_sp); 40730b57cec5SDimitry Andric } 40740b57cec5SDimitry Andric 40750b57cec5SDimitry Andric // Establish the largest memory read/write payloads we should use. If the 40760b57cec5SDimitry Andric // remote stub has a max packet size, stay under that size. 40770b57cec5SDimitry Andric // 40780b57cec5SDimitry Andric // If the remote stub's max packet size is crazy large, use a reasonable 40790b57cec5SDimitry Andric // largeish default. 40800b57cec5SDimitry Andric // 40810b57cec5SDimitry Andric // If the remote stub doesn't advertise a max packet size, use a conservative 40820b57cec5SDimitry Andric // default. 40830b57cec5SDimitry Andric 40840b57cec5SDimitry Andric void ProcessGDBRemote::GetMaxMemorySize() { 40850b57cec5SDimitry Andric const uint64_t reasonable_largeish_default = 128 * 1024; 40860b57cec5SDimitry Andric const uint64_t conservative_default = 512; 40870b57cec5SDimitry Andric 40880b57cec5SDimitry Andric if (m_max_memory_size == 0) { 40890b57cec5SDimitry Andric uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize(); 40900b57cec5SDimitry Andric if (stub_max_size != UINT64_MAX && stub_max_size != 0) { 40910b57cec5SDimitry Andric // Save the stub's claimed maximum packet size 40920b57cec5SDimitry Andric m_remote_stub_max_memory_size = stub_max_size; 40930b57cec5SDimitry Andric 40940b57cec5SDimitry Andric // Even if the stub says it can support ginormous packets, don't exceed 40950b57cec5SDimitry Andric // our reasonable largeish default packet size. 40960b57cec5SDimitry Andric if (stub_max_size > reasonable_largeish_default) { 40970b57cec5SDimitry Andric stub_max_size = reasonable_largeish_default; 40980b57cec5SDimitry Andric } 40990b57cec5SDimitry Andric 41000b57cec5SDimitry Andric // Memory packet have other overheads too like Maddr,size:#NN Instead of 41010b57cec5SDimitry Andric // calculating the bytes taken by size and addr every time, we take a 41020b57cec5SDimitry Andric // maximum guess here. 41030b57cec5SDimitry Andric if (stub_max_size > 70) 41040b57cec5SDimitry Andric stub_max_size -= 32 + 32 + 6; 41050b57cec5SDimitry Andric else { 41060b57cec5SDimitry Andric // In unlikely scenario that max packet size is less then 70, we will 41070b57cec5SDimitry Andric // hope that data being written is small enough to fit. 41080b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet( 41090b57cec5SDimitry Andric GDBR_LOG_COMM | GDBR_LOG_MEMORY)); 41100b57cec5SDimitry Andric if (log) 41110b57cec5SDimitry Andric log->Warning("Packet size is too small. " 41120b57cec5SDimitry Andric "LLDB may face problems while writing memory"); 41130b57cec5SDimitry Andric } 41140b57cec5SDimitry Andric 41150b57cec5SDimitry Andric m_max_memory_size = stub_max_size; 41160b57cec5SDimitry Andric } else { 41170b57cec5SDimitry Andric m_max_memory_size = conservative_default; 41180b57cec5SDimitry Andric } 41190b57cec5SDimitry Andric } 41200b57cec5SDimitry Andric } 41210b57cec5SDimitry Andric 41220b57cec5SDimitry Andric void ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize( 41230b57cec5SDimitry Andric uint64_t user_specified_max) { 41240b57cec5SDimitry Andric if (user_specified_max != 0) { 41250b57cec5SDimitry Andric GetMaxMemorySize(); 41260b57cec5SDimitry Andric 41270b57cec5SDimitry Andric if (m_remote_stub_max_memory_size != 0) { 41280b57cec5SDimitry Andric if (m_remote_stub_max_memory_size < user_specified_max) { 41290b57cec5SDimitry Andric m_max_memory_size = m_remote_stub_max_memory_size; // user specified a 41300b57cec5SDimitry Andric // packet size too 41310b57cec5SDimitry Andric // big, go as big 41320b57cec5SDimitry Andric // as the remote stub says we can go. 41330b57cec5SDimitry Andric } else { 41340b57cec5SDimitry Andric m_max_memory_size = user_specified_max; // user's packet size is good 41350b57cec5SDimitry Andric } 41360b57cec5SDimitry Andric } else { 41370b57cec5SDimitry Andric m_max_memory_size = 41380b57cec5SDimitry Andric user_specified_max; // user's packet size is probably fine 41390b57cec5SDimitry Andric } 41400b57cec5SDimitry Andric } 41410b57cec5SDimitry Andric } 41420b57cec5SDimitry Andric 41430b57cec5SDimitry Andric bool ProcessGDBRemote::GetModuleSpec(const FileSpec &module_file_spec, 41440b57cec5SDimitry Andric const ArchSpec &arch, 41450b57cec5SDimitry Andric ModuleSpec &module_spec) { 41460b57cec5SDimitry Andric Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 41470b57cec5SDimitry Andric 41480b57cec5SDimitry Andric const ModuleCacheKey key(module_file_spec.GetPath(), 41490b57cec5SDimitry Andric arch.GetTriple().getTriple()); 41500b57cec5SDimitry Andric auto cached = m_cached_module_specs.find(key); 41510b57cec5SDimitry Andric if (cached != m_cached_module_specs.end()) { 41520b57cec5SDimitry Andric module_spec = cached->second; 41530b57cec5SDimitry Andric return bool(module_spec); 41540b57cec5SDimitry Andric } 41550b57cec5SDimitry Andric 41560b57cec5SDimitry Andric if (!m_gdb_comm.GetModuleInfo(module_file_spec, arch, module_spec)) { 41579dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s - failed to get module info for %s:%s", 41580b57cec5SDimitry Andric __FUNCTION__, module_file_spec.GetPath().c_str(), 41590b57cec5SDimitry Andric arch.GetTriple().getTriple().c_str()); 41600b57cec5SDimitry Andric return false; 41610b57cec5SDimitry Andric } 41620b57cec5SDimitry Andric 41630b57cec5SDimitry Andric if (log) { 41640b57cec5SDimitry Andric StreamString stream; 41650b57cec5SDimitry Andric module_spec.Dump(stream); 41669dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s - got module info for (%s:%s) : %s", 41670b57cec5SDimitry Andric __FUNCTION__, module_file_spec.GetPath().c_str(), 41680b57cec5SDimitry Andric arch.GetTriple().getTriple().c_str(), stream.GetData()); 41690b57cec5SDimitry Andric } 41700b57cec5SDimitry Andric 41710b57cec5SDimitry Andric m_cached_module_specs[key] = module_spec; 41720b57cec5SDimitry Andric return true; 41730b57cec5SDimitry Andric } 41740b57cec5SDimitry Andric 41750b57cec5SDimitry Andric void ProcessGDBRemote::PrefetchModuleSpecs( 41760b57cec5SDimitry Andric llvm::ArrayRef<FileSpec> module_file_specs, const llvm::Triple &triple) { 41770b57cec5SDimitry Andric auto module_specs = m_gdb_comm.GetModulesInfo(module_file_specs, triple); 41780b57cec5SDimitry Andric if (module_specs) { 41790b57cec5SDimitry Andric for (const FileSpec &spec : module_file_specs) 41800b57cec5SDimitry Andric m_cached_module_specs[ModuleCacheKey(spec.GetPath(), 41810b57cec5SDimitry Andric triple.getTriple())] = ModuleSpec(); 41820b57cec5SDimitry Andric for (const ModuleSpec &spec : *module_specs) 41830b57cec5SDimitry Andric m_cached_module_specs[ModuleCacheKey(spec.GetFileSpec().GetPath(), 41840b57cec5SDimitry Andric triple.getTriple())] = spec; 41850b57cec5SDimitry Andric } 41860b57cec5SDimitry Andric } 41870b57cec5SDimitry Andric 41880b57cec5SDimitry Andric llvm::VersionTuple ProcessGDBRemote::GetHostOSVersion() { 41890b57cec5SDimitry Andric return m_gdb_comm.GetOSVersion(); 41900b57cec5SDimitry Andric } 41910b57cec5SDimitry Andric 41929dba64beSDimitry Andric llvm::VersionTuple ProcessGDBRemote::GetHostMacCatalystVersion() { 41939dba64beSDimitry Andric return m_gdb_comm.GetMacCatalystVersion(); 41949dba64beSDimitry Andric } 41959dba64beSDimitry Andric 41960b57cec5SDimitry Andric namespace { 41970b57cec5SDimitry Andric 41980b57cec5SDimitry Andric typedef std::vector<std::string> stringVec; 41990b57cec5SDimitry Andric 42000b57cec5SDimitry Andric typedef std::vector<struct GdbServerRegisterInfo> GDBServerRegisterVec; 42010b57cec5SDimitry Andric struct RegisterSetInfo { 42020b57cec5SDimitry Andric ConstString name; 42030b57cec5SDimitry Andric }; 42040b57cec5SDimitry Andric 42050b57cec5SDimitry Andric typedef std::map<uint32_t, RegisterSetInfo> RegisterSetMap; 42060b57cec5SDimitry Andric 42070b57cec5SDimitry Andric struct GdbServerTargetInfo { 42080b57cec5SDimitry Andric std::string arch; 42090b57cec5SDimitry Andric std::string osabi; 42100b57cec5SDimitry Andric stringVec includes; 42110b57cec5SDimitry Andric RegisterSetMap reg_set_map; 42120b57cec5SDimitry Andric }; 42130b57cec5SDimitry Andric 42140b57cec5SDimitry Andric bool ParseRegisters(XMLNode feature_node, GdbServerTargetInfo &target_info, 4215349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> ®isters) { 42160b57cec5SDimitry Andric if (!feature_node) 42170b57cec5SDimitry Andric return false; 42180b57cec5SDimitry Andric 42190b57cec5SDimitry Andric feature_node.ForEachChildElementWithName( 4220349cc55cSDimitry Andric "reg", [&target_info, ®isters](const XMLNode ®_node) -> bool { 42210b57cec5SDimitry Andric std::string gdb_group; 42220b57cec5SDimitry Andric std::string gdb_type; 4223349cc55cSDimitry Andric DynamicRegisterInfo::Register reg_info; 42240b57cec5SDimitry Andric bool encoding_set = false; 42250b57cec5SDimitry Andric bool format_set = false; 42260b57cec5SDimitry Andric 4227349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 42280b57cec5SDimitry Andric reg_node.ForEachAttribute([&target_info, &gdb_group, &gdb_type, 4229349cc55cSDimitry Andric &encoding_set, &format_set, ®_info]( 42300b57cec5SDimitry Andric const llvm::StringRef &name, 42310b57cec5SDimitry Andric const llvm::StringRef &value) -> bool { 42320b57cec5SDimitry Andric if (name == "name") { 4233349cc55cSDimitry Andric reg_info.name.SetString(value); 42340b57cec5SDimitry Andric } else if (name == "bitsize") { 4235349cc55cSDimitry Andric if (llvm::to_integer(value, reg_info.byte_size)) 42360b57cec5SDimitry Andric reg_info.byte_size = 4237349cc55cSDimitry Andric llvm::divideCeil(reg_info.byte_size, CHAR_BIT); 42380b57cec5SDimitry Andric } else if (name == "type") { 42390b57cec5SDimitry Andric gdb_type = value.str(); 42400b57cec5SDimitry Andric } else if (name == "group") { 42410b57cec5SDimitry Andric gdb_group = value.str(); 42420b57cec5SDimitry Andric } else if (name == "regnum") { 4243349cc55cSDimitry Andric llvm::to_integer(value, reg_info.regnum_remote); 42440b57cec5SDimitry Andric } else if (name == "offset") { 4245349cc55cSDimitry Andric llvm::to_integer(value, reg_info.byte_offset); 42460b57cec5SDimitry Andric } else if (name == "altname") { 4247349cc55cSDimitry Andric reg_info.alt_name.SetString(value); 42480b57cec5SDimitry Andric } else if (name == "encoding") { 42490b57cec5SDimitry Andric encoding_set = true; 42500b57cec5SDimitry Andric reg_info.encoding = Args::StringToEncoding(value, eEncodingUint); 42510b57cec5SDimitry Andric } else if (name == "format") { 42520b57cec5SDimitry Andric format_set = true; 4253349cc55cSDimitry Andric if (!OptionArgParser::ToFormat(value.data(), reg_info.format, 4254349cc55cSDimitry Andric nullptr) 42550b57cec5SDimitry Andric .Success()) 4256349cc55cSDimitry Andric reg_info.format = 4257349cc55cSDimitry Andric llvm::StringSwitch<lldb::Format>(value) 4258349cc55cSDimitry Andric .Case("vector-sint8", eFormatVectorOfSInt8) 4259349cc55cSDimitry Andric .Case("vector-uint8", eFormatVectorOfUInt8) 4260349cc55cSDimitry Andric .Case("vector-sint16", eFormatVectorOfSInt16) 4261349cc55cSDimitry Andric .Case("vector-uint16", eFormatVectorOfUInt16) 4262349cc55cSDimitry Andric .Case("vector-sint32", eFormatVectorOfSInt32) 4263349cc55cSDimitry Andric .Case("vector-uint32", eFormatVectorOfUInt32) 4264349cc55cSDimitry Andric .Case("vector-float32", eFormatVectorOfFloat32) 4265349cc55cSDimitry Andric .Case("vector-uint64", eFormatVectorOfUInt64) 4266349cc55cSDimitry Andric .Case("vector-uint128", eFormatVectorOfUInt128) 4267349cc55cSDimitry Andric .Default(eFormatInvalid); 42680b57cec5SDimitry Andric } else if (name == "group_id") { 4269349cc55cSDimitry Andric uint32_t set_id = UINT32_MAX; 4270349cc55cSDimitry Andric llvm::to_integer(value, set_id); 42710b57cec5SDimitry Andric RegisterSetMap::const_iterator pos = 42720b57cec5SDimitry Andric target_info.reg_set_map.find(set_id); 42730b57cec5SDimitry Andric if (pos != target_info.reg_set_map.end()) 4274349cc55cSDimitry Andric reg_info.set_name = pos->second.name; 42750b57cec5SDimitry Andric } else if (name == "gcc_regnum" || name == "ehframe_regnum") { 4276349cc55cSDimitry Andric llvm::to_integer(value, reg_info.regnum_ehframe); 42770b57cec5SDimitry Andric } else if (name == "dwarf_regnum") { 4278349cc55cSDimitry Andric llvm::to_integer(value, reg_info.regnum_dwarf); 42790b57cec5SDimitry Andric } else if (name == "generic") { 4280349cc55cSDimitry Andric reg_info.regnum_generic = Args::StringToGenericRegister(value); 42810b57cec5SDimitry Andric } else if (name == "value_regnums") { 4282349cc55cSDimitry Andric SplitCommaSeparatedRegisterNumberString(value, reg_info.value_regs, 4283349cc55cSDimitry Andric 0); 42840b57cec5SDimitry Andric } else if (name == "invalidate_regnums") { 4285349cc55cSDimitry Andric SplitCommaSeparatedRegisterNumberString( 4286349cc55cSDimitry Andric value, reg_info.invalidate_regs, 0); 42870b57cec5SDimitry Andric } else { 4288349cc55cSDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet( 4289349cc55cSDimitry Andric GDBR_LOG_PROCESS)); 4290349cc55cSDimitry Andric LLDB_LOGF(log, 4291349cc55cSDimitry Andric "ProcessGDBRemote::%s unhandled reg attribute %s = %s", 4292349cc55cSDimitry Andric __FUNCTION__, name.data(), value.data()); 42930b57cec5SDimitry Andric } 42940b57cec5SDimitry Andric return true; // Keep iterating through all attributes 42950b57cec5SDimitry Andric }); 42960b57cec5SDimitry Andric 42970b57cec5SDimitry Andric if (!gdb_type.empty() && !(encoding_set || format_set)) { 42985ffd83dbSDimitry Andric if (llvm::StringRef(gdb_type).startswith("int")) { 42990b57cec5SDimitry Andric reg_info.format = eFormatHex; 43000b57cec5SDimitry Andric reg_info.encoding = eEncodingUint; 43010b57cec5SDimitry Andric } else if (gdb_type == "data_ptr" || gdb_type == "code_ptr") { 43020b57cec5SDimitry Andric reg_info.format = eFormatAddressInfo; 43030b57cec5SDimitry Andric reg_info.encoding = eEncodingUint; 4304349cc55cSDimitry Andric } else if (gdb_type == "float") { 43050b57cec5SDimitry Andric reg_info.format = eFormatFloat; 43060b57cec5SDimitry Andric reg_info.encoding = eEncodingIEEE754; 4307349cc55cSDimitry Andric } else if (gdb_type == "aarch64v" || 4308349cc55cSDimitry Andric llvm::StringRef(gdb_type).startswith("vec") || 4309349cc55cSDimitry Andric gdb_type == "i387_ext" || gdb_type == "uint128") { 4310349cc55cSDimitry Andric // lldb doesn't handle 128-bit uints correctly (for ymm*h), so treat 4311349cc55cSDimitry Andric // them as vector (similarly to xmm/ymm) 4312349cc55cSDimitry Andric reg_info.format = eFormatVectorOfUInt8; 4313349cc55cSDimitry Andric reg_info.encoding = eEncodingVector; 43140b57cec5SDimitry Andric } 43150b57cec5SDimitry Andric } 43160b57cec5SDimitry Andric 43170b57cec5SDimitry Andric // Only update the register set name if we didn't get a "reg_set" 43180b57cec5SDimitry Andric // attribute. "set_name" will be empty if we didn't have a "reg_set" 43190b57cec5SDimitry Andric // attribute. 4320349cc55cSDimitry Andric if (!reg_info.set_name) { 43210b57cec5SDimitry Andric if (!gdb_group.empty()) { 4322349cc55cSDimitry Andric reg_info.set_name.SetCString(gdb_group.c_str()); 43230b57cec5SDimitry Andric } else { 43240b57cec5SDimitry Andric // If no register group name provided anywhere, 43250b57cec5SDimitry Andric // we'll create a 'general' register set 4326349cc55cSDimitry Andric reg_info.set_name.SetCString("general"); 43270b57cec5SDimitry Andric } 43280b57cec5SDimitry Andric } 43290b57cec5SDimitry Andric 4330349cc55cSDimitry Andric if (reg_info.byte_size == 0) { 4331349cc55cSDimitry Andric Log *log( 4332349cc55cSDimitry Andric ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 4333349cc55cSDimitry Andric LLDB_LOGF(log, 4334349cc55cSDimitry Andric "ProcessGDBRemote::%s Skipping zero bitsize register %s", 4335349cc55cSDimitry Andric __FUNCTION__, reg_info.name.AsCString()); 4336349cc55cSDimitry Andric } else 4337349cc55cSDimitry Andric registers.push_back(reg_info); 43380b57cec5SDimitry Andric 43390b57cec5SDimitry Andric return true; // Keep iterating through all "reg" elements 43400b57cec5SDimitry Andric }); 43410b57cec5SDimitry Andric return true; 43420b57cec5SDimitry Andric } 43430b57cec5SDimitry Andric 43440b57cec5SDimitry Andric } // namespace 43450b57cec5SDimitry Andric 43460b57cec5SDimitry Andric // This method fetches a register description feature xml file from 43470b57cec5SDimitry Andric // the remote stub and adds registers/register groupsets/architecture 43480b57cec5SDimitry Andric // information to the current process. It will call itself recursively 43490b57cec5SDimitry Andric // for nested register definition files. It returns true if it was able 43500b57cec5SDimitry Andric // to fetch and parse an xml file. 43519dba64beSDimitry Andric bool ProcessGDBRemote::GetGDBServerRegisterInfoXMLAndProcess( 4352349cc55cSDimitry Andric ArchSpec &arch_to_use, std::string xml_filename, 4353349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> ®isters) { 43540b57cec5SDimitry Andric // request the target xml file 4355349cc55cSDimitry Andric llvm::Expected<std::string> raw = m_gdb_comm.ReadExtFeature("features", xml_filename); 4356349cc55cSDimitry Andric if (errorToBool(raw.takeError())) 43570b57cec5SDimitry Andric return false; 43580b57cec5SDimitry Andric 43590b57cec5SDimitry Andric XMLDocument xml_document; 43600b57cec5SDimitry Andric 4361349cc55cSDimitry Andric if (xml_document.ParseMemory(raw->c_str(), raw->size(), 4362349cc55cSDimitry Andric xml_filename.c_str())) { 43630b57cec5SDimitry Andric GdbServerTargetInfo target_info; 43640b57cec5SDimitry Andric std::vector<XMLNode> feature_nodes; 43650b57cec5SDimitry Andric 43660b57cec5SDimitry Andric // The top level feature XML file will start with a <target> tag. 43670b57cec5SDimitry Andric XMLNode target_node = xml_document.GetRootElement("target"); 43680b57cec5SDimitry Andric if (target_node) { 43690b57cec5SDimitry Andric target_node.ForEachChildElement([&target_info, &feature_nodes]( 43700b57cec5SDimitry Andric const XMLNode &node) -> bool { 43710b57cec5SDimitry Andric llvm::StringRef name = node.GetName(); 43720b57cec5SDimitry Andric if (name == "architecture") { 43730b57cec5SDimitry Andric node.GetElementText(target_info.arch); 43740b57cec5SDimitry Andric } else if (name == "osabi") { 43750b57cec5SDimitry Andric node.GetElementText(target_info.osabi); 43760b57cec5SDimitry Andric } else if (name == "xi:include" || name == "include") { 43770b57cec5SDimitry Andric llvm::StringRef href = node.GetAttributeValue("href"); 43780b57cec5SDimitry Andric if (!href.empty()) 43790b57cec5SDimitry Andric target_info.includes.push_back(href.str()); 43800b57cec5SDimitry Andric } else if (name == "feature") { 43810b57cec5SDimitry Andric feature_nodes.push_back(node); 43820b57cec5SDimitry Andric } else if (name == "groups") { 43830b57cec5SDimitry Andric node.ForEachChildElementWithName( 43840b57cec5SDimitry Andric "group", [&target_info](const XMLNode &node) -> bool { 43850b57cec5SDimitry Andric uint32_t set_id = UINT32_MAX; 43860b57cec5SDimitry Andric RegisterSetInfo set_info; 43870b57cec5SDimitry Andric 43880b57cec5SDimitry Andric node.ForEachAttribute( 43890b57cec5SDimitry Andric [&set_id, &set_info](const llvm::StringRef &name, 43900b57cec5SDimitry Andric const llvm::StringRef &value) -> bool { 4391349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 43920b57cec5SDimitry Andric if (name == "id") 4393349cc55cSDimitry Andric llvm::to_integer(value, set_id); 43940b57cec5SDimitry Andric if (name == "name") 43950b57cec5SDimitry Andric set_info.name = ConstString(value); 43960b57cec5SDimitry Andric return true; // Keep iterating through all attributes 43970b57cec5SDimitry Andric }); 43980b57cec5SDimitry Andric 43990b57cec5SDimitry Andric if (set_id != UINT32_MAX) 44000b57cec5SDimitry Andric target_info.reg_set_map[set_id] = set_info; 44010b57cec5SDimitry Andric return true; // Keep iterating through all "group" elements 44020b57cec5SDimitry Andric }); 44030b57cec5SDimitry Andric } 44040b57cec5SDimitry Andric return true; // Keep iterating through all children of the target_node 44050b57cec5SDimitry Andric }); 44060b57cec5SDimitry Andric } else { 44070b57cec5SDimitry Andric // In an included XML feature file, we're already "inside" the <target> 44080b57cec5SDimitry Andric // tag of the initial XML file; this included file will likely only have 44090b57cec5SDimitry Andric // a <feature> tag. Need to check for any more included files in this 44100b57cec5SDimitry Andric // <feature> element. 44110b57cec5SDimitry Andric XMLNode feature_node = xml_document.GetRootElement("feature"); 44120b57cec5SDimitry Andric if (feature_node) { 44130b57cec5SDimitry Andric feature_nodes.push_back(feature_node); 44140b57cec5SDimitry Andric feature_node.ForEachChildElement([&target_info]( 44150b57cec5SDimitry Andric const XMLNode &node) -> bool { 44160b57cec5SDimitry Andric llvm::StringRef name = node.GetName(); 44170b57cec5SDimitry Andric if (name == "xi:include" || name == "include") { 44180b57cec5SDimitry Andric llvm::StringRef href = node.GetAttributeValue("href"); 44190b57cec5SDimitry Andric if (!href.empty()) 44200b57cec5SDimitry Andric target_info.includes.push_back(href.str()); 44210b57cec5SDimitry Andric } 44220b57cec5SDimitry Andric return true; 44230b57cec5SDimitry Andric }); 44240b57cec5SDimitry Andric } 44250b57cec5SDimitry Andric } 44260b57cec5SDimitry Andric 4427349cc55cSDimitry Andric // gdbserver does not implement the LLDB packets used to determine host 4428349cc55cSDimitry Andric // or process architecture. If that is the case, attempt to use 4429349cc55cSDimitry Andric // the <architecture/> field from target.xml, e.g.: 4430349cc55cSDimitry Andric // 44310b57cec5SDimitry Andric // <architecture>i386:x86-64</architecture> (seen from VMWare ESXi) 4432349cc55cSDimitry Andric // <architecture>arm</architecture> (seen from Segger JLink on unspecified 4433349cc55cSDimitry Andric // arm board) 44340b57cec5SDimitry Andric if (!arch_to_use.IsValid() && !target_info.arch.empty()) { 44350b57cec5SDimitry Andric // We don't have any information about vendor or OS. 4436349cc55cSDimitry Andric arch_to_use.SetTriple(llvm::StringSwitch<std::string>(target_info.arch) 4437349cc55cSDimitry Andric .Case("i386:x86-64", "x86_64") 4438349cc55cSDimitry Andric .Default(target_info.arch) + 4439349cc55cSDimitry Andric "--"); 44400b57cec5SDimitry Andric 4441349cc55cSDimitry Andric if (arch_to_use.IsValid()) 44420b57cec5SDimitry Andric GetTarget().MergeArchitecture(arch_to_use); 44430b57cec5SDimitry Andric } 44440b57cec5SDimitry Andric 44450b57cec5SDimitry Andric if (arch_to_use.IsValid()) { 44460b57cec5SDimitry Andric for (auto &feature_node : feature_nodes) { 4447349cc55cSDimitry Andric ParseRegisters(feature_node, target_info, 4448349cc55cSDimitry Andric registers); 44490b57cec5SDimitry Andric } 44500b57cec5SDimitry Andric 44510b57cec5SDimitry Andric for (const auto &include : target_info.includes) { 4452e8d8bef9SDimitry Andric GetGDBServerRegisterInfoXMLAndProcess(arch_to_use, include, 4453349cc55cSDimitry Andric registers); 44540b57cec5SDimitry Andric } 44550b57cec5SDimitry Andric } 44560b57cec5SDimitry Andric } else { 44570b57cec5SDimitry Andric return false; 44580b57cec5SDimitry Andric } 44590b57cec5SDimitry Andric return true; 44600b57cec5SDimitry Andric } 44610b57cec5SDimitry Andric 4462349cc55cSDimitry Andric void ProcessGDBRemote::AddRemoteRegisters( 4463349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> ®isters, 4464349cc55cSDimitry Andric const ArchSpec &arch_to_use) { 4465349cc55cSDimitry Andric std::map<uint32_t, uint32_t> remote_to_local_map; 4466349cc55cSDimitry Andric uint32_t remote_regnum = 0; 4467349cc55cSDimitry Andric for (auto it : llvm::enumerate(registers)) { 4468349cc55cSDimitry Andric DynamicRegisterInfo::Register &remote_reg_info = it.value(); 4469349cc55cSDimitry Andric 4470349cc55cSDimitry Andric // Assign successive remote regnums if missing. 4471349cc55cSDimitry Andric if (remote_reg_info.regnum_remote == LLDB_INVALID_REGNUM) 4472349cc55cSDimitry Andric remote_reg_info.regnum_remote = remote_regnum; 4473349cc55cSDimitry Andric 4474349cc55cSDimitry Andric // Create a mapping from remote to local regnos. 4475349cc55cSDimitry Andric remote_to_local_map[remote_reg_info.regnum_remote] = it.index(); 4476349cc55cSDimitry Andric 4477349cc55cSDimitry Andric remote_regnum = remote_reg_info.regnum_remote + 1; 4478349cc55cSDimitry Andric } 4479349cc55cSDimitry Andric 4480349cc55cSDimitry Andric for (DynamicRegisterInfo::Register &remote_reg_info : registers) { 4481349cc55cSDimitry Andric auto proc_to_lldb = [&remote_to_local_map](uint32_t process_regnum) { 4482349cc55cSDimitry Andric auto lldb_regit = remote_to_local_map.find(process_regnum); 4483349cc55cSDimitry Andric return lldb_regit != remote_to_local_map.end() ? lldb_regit->second 4484349cc55cSDimitry Andric : LLDB_INVALID_REGNUM; 4485349cc55cSDimitry Andric }; 4486349cc55cSDimitry Andric 4487349cc55cSDimitry Andric llvm::transform(remote_reg_info.value_regs, 4488349cc55cSDimitry Andric remote_reg_info.value_regs.begin(), proc_to_lldb); 4489349cc55cSDimitry Andric llvm::transform(remote_reg_info.invalidate_regs, 4490349cc55cSDimitry Andric remote_reg_info.invalidate_regs.begin(), proc_to_lldb); 4491349cc55cSDimitry Andric } 4492349cc55cSDimitry Andric 4493349cc55cSDimitry Andric // Don't use Process::GetABI, this code gets called from DidAttach, and 4494349cc55cSDimitry Andric // in that context we haven't set the Target's architecture yet, so the 4495349cc55cSDimitry Andric // ABI is also potentially incorrect. 4496349cc55cSDimitry Andric if (ABISP abi_sp = ABI::FindPlugin(shared_from_this(), arch_to_use)) 4497349cc55cSDimitry Andric abi_sp->AugmentRegisterInfo(registers); 4498349cc55cSDimitry Andric 4499349cc55cSDimitry Andric m_register_info_sp->SetRegisterInfo(std::move(registers), arch_to_use); 4500349cc55cSDimitry Andric } 4501349cc55cSDimitry Andric 45020b57cec5SDimitry Andric // query the target of gdb-remote for extended target information returns 45030b57cec5SDimitry Andric // true on success (got register definitions), false on failure (did not). 45040b57cec5SDimitry Andric bool ProcessGDBRemote::GetGDBServerRegisterInfo(ArchSpec &arch_to_use) { 45050b57cec5SDimitry Andric // Make sure LLDB has an XML parser it can use first 45060b57cec5SDimitry Andric if (!XMLDocument::XMLEnabled()) 45070b57cec5SDimitry Andric return false; 45080b57cec5SDimitry Andric 45090b57cec5SDimitry Andric // check that we have extended feature read support 45100b57cec5SDimitry Andric if (!m_gdb_comm.GetQXferFeaturesReadSupported()) 45110b57cec5SDimitry Andric return false; 45120b57cec5SDimitry Andric 4513349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> registers; 4514e8d8bef9SDimitry Andric if (GetGDBServerRegisterInfoXMLAndProcess(arch_to_use, "target.xml", 4515349cc55cSDimitry Andric registers)) 4516349cc55cSDimitry Andric AddRemoteRegisters(registers, arch_to_use); 45170b57cec5SDimitry Andric 4518e8d8bef9SDimitry Andric return m_register_info_sp->GetNumRegisters() > 0; 45190b57cec5SDimitry Andric } 45200b57cec5SDimitry Andric 45219dba64beSDimitry Andric llvm::Expected<LoadedModuleInfoList> ProcessGDBRemote::GetLoadedModuleList() { 45220b57cec5SDimitry Andric // Make sure LLDB has an XML parser it can use first 45230b57cec5SDimitry Andric if (!XMLDocument::XMLEnabled()) 45249dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 45259dba64beSDimitry Andric "XML parsing not available"); 45260b57cec5SDimitry Andric 45270b57cec5SDimitry Andric Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS); 45289dba64beSDimitry Andric LLDB_LOGF(log, "ProcessGDBRemote::%s", __FUNCTION__); 45290b57cec5SDimitry Andric 45309dba64beSDimitry Andric LoadedModuleInfoList list; 45310b57cec5SDimitry Andric GDBRemoteCommunicationClient &comm = m_gdb_comm; 4532349cc55cSDimitry Andric bool can_use_svr4 = GetGlobalPluginProperties().GetUseSVR4(); 45330b57cec5SDimitry Andric 45340b57cec5SDimitry Andric // check that we have extended feature read support 45350b57cec5SDimitry Andric if (can_use_svr4 && comm.GetQXferLibrariesSVR4ReadSupported()) { 45360b57cec5SDimitry Andric // request the loaded library list 4537349cc55cSDimitry Andric llvm::Expected<std::string> raw = comm.ReadExtFeature("libraries-svr4", ""); 4538349cc55cSDimitry Andric if (!raw) 4539349cc55cSDimitry Andric return raw.takeError(); 45400b57cec5SDimitry Andric 45410b57cec5SDimitry Andric // parse the xml file in memory 4542349cc55cSDimitry Andric LLDB_LOGF(log, "parsing: %s", raw->c_str()); 45430b57cec5SDimitry Andric XMLDocument doc; 45440b57cec5SDimitry Andric 4545349cc55cSDimitry Andric if (!doc.ParseMemory(raw->c_str(), raw->size(), "noname.xml")) 45469dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 45479dba64beSDimitry Andric "Error reading noname.xml"); 45480b57cec5SDimitry Andric 45490b57cec5SDimitry Andric XMLNode root_element = doc.GetRootElement("library-list-svr4"); 45500b57cec5SDimitry Andric if (!root_element) 45519dba64beSDimitry Andric return llvm::createStringError( 45529dba64beSDimitry Andric llvm::inconvertibleErrorCode(), 45539dba64beSDimitry Andric "Error finding library-list-svr4 xml element"); 45540b57cec5SDimitry Andric 45550b57cec5SDimitry Andric // main link map structure 45560b57cec5SDimitry Andric llvm::StringRef main_lm = root_element.GetAttributeValue("main-lm"); 4557349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 4558349cc55cSDimitry Andric if (!main_lm.empty()) 4559349cc55cSDimitry Andric llvm::to_integer(main_lm, list.m_link_map); 45600b57cec5SDimitry Andric 45610b57cec5SDimitry Andric root_element.ForEachChildElementWithName( 45620b57cec5SDimitry Andric "library", [log, &list](const XMLNode &library) -> bool { 45630b57cec5SDimitry Andric LoadedModuleInfoList::LoadedModuleInfo module; 45640b57cec5SDimitry Andric 4565349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 45660b57cec5SDimitry Andric library.ForEachAttribute( 45670b57cec5SDimitry Andric [&module](const llvm::StringRef &name, 45680b57cec5SDimitry Andric const llvm::StringRef &value) -> bool { 4569349cc55cSDimitry Andric uint64_t uint_value = LLDB_INVALID_ADDRESS; 45700b57cec5SDimitry Andric if (name == "name") 45710b57cec5SDimitry Andric module.set_name(value.str()); 45720b57cec5SDimitry Andric else if (name == "lm") { 45730b57cec5SDimitry Andric // the address of the link_map struct. 4574349cc55cSDimitry Andric llvm::to_integer(value, uint_value); 4575349cc55cSDimitry Andric module.set_link_map(uint_value); 45760b57cec5SDimitry Andric } else if (name == "l_addr") { 45770b57cec5SDimitry Andric // the displacement as read from the field 'l_addr' of the 45780b57cec5SDimitry Andric // link_map struct. 4579349cc55cSDimitry Andric llvm::to_integer(value, uint_value); 4580349cc55cSDimitry Andric module.set_base(uint_value); 45810b57cec5SDimitry Andric // base address is always a displacement, not an absolute 45820b57cec5SDimitry Andric // value. 45830b57cec5SDimitry Andric module.set_base_is_offset(true); 45840b57cec5SDimitry Andric } else if (name == "l_ld") { 45855ffd83dbSDimitry Andric // the memory address of the libraries PT_DYNAMIC section. 4586349cc55cSDimitry Andric llvm::to_integer(value, uint_value); 4587349cc55cSDimitry Andric module.set_dynamic(uint_value); 45880b57cec5SDimitry Andric } 45890b57cec5SDimitry Andric 45900b57cec5SDimitry Andric return true; // Keep iterating over all properties of "library" 45910b57cec5SDimitry Andric }); 45920b57cec5SDimitry Andric 45930b57cec5SDimitry Andric if (log) { 45940b57cec5SDimitry Andric std::string name; 45950b57cec5SDimitry Andric lldb::addr_t lm = 0, base = 0, ld = 0; 45960b57cec5SDimitry Andric bool base_is_offset; 45970b57cec5SDimitry Andric 45980b57cec5SDimitry Andric module.get_name(name); 45990b57cec5SDimitry Andric module.get_link_map(lm); 46000b57cec5SDimitry Andric module.get_base(base); 46010b57cec5SDimitry Andric module.get_base_is_offset(base_is_offset); 46020b57cec5SDimitry Andric module.get_dynamic(ld); 46030b57cec5SDimitry Andric 46049dba64beSDimitry Andric LLDB_LOGF(log, 46059dba64beSDimitry Andric "found (link_map:0x%08" PRIx64 ", base:0x%08" PRIx64 46060b57cec5SDimitry Andric "[%s], ld:0x%08" PRIx64 ", name:'%s')", 46070b57cec5SDimitry Andric lm, base, (base_is_offset ? "offset" : "absolute"), ld, 46080b57cec5SDimitry Andric name.c_str()); 46090b57cec5SDimitry Andric } 46100b57cec5SDimitry Andric 46110b57cec5SDimitry Andric list.add(module); 46120b57cec5SDimitry Andric return true; // Keep iterating over all "library" elements in the root 46130b57cec5SDimitry Andric // node 46140b57cec5SDimitry Andric }); 46150b57cec5SDimitry Andric 46160b57cec5SDimitry Andric if (log) 46179dba64beSDimitry Andric LLDB_LOGF(log, "found %" PRId32 " modules in total", 46180b57cec5SDimitry Andric (int)list.m_list.size()); 46199dba64beSDimitry Andric return list; 46200b57cec5SDimitry Andric } else if (comm.GetQXferLibrariesReadSupported()) { 46210b57cec5SDimitry Andric // request the loaded library list 4622349cc55cSDimitry Andric llvm::Expected<std::string> raw = comm.ReadExtFeature("libraries", ""); 46230b57cec5SDimitry Andric 4624349cc55cSDimitry Andric if (!raw) 4625349cc55cSDimitry Andric return raw.takeError(); 46260b57cec5SDimitry Andric 4627349cc55cSDimitry Andric LLDB_LOGF(log, "parsing: %s", raw->c_str()); 46280b57cec5SDimitry Andric XMLDocument doc; 46290b57cec5SDimitry Andric 4630349cc55cSDimitry Andric if (!doc.ParseMemory(raw->c_str(), raw->size(), "noname.xml")) 46319dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 46329dba64beSDimitry Andric "Error reading noname.xml"); 46330b57cec5SDimitry Andric 46340b57cec5SDimitry Andric XMLNode root_element = doc.GetRootElement("library-list"); 46350b57cec5SDimitry Andric if (!root_element) 46369dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 46379dba64beSDimitry Andric "Error finding library-list xml element"); 46380b57cec5SDimitry Andric 4639349cc55cSDimitry Andric // FIXME: we're silently ignoring invalid data here 46400b57cec5SDimitry Andric root_element.ForEachChildElementWithName( 46410b57cec5SDimitry Andric "library", [log, &list](const XMLNode &library) -> bool { 46420b57cec5SDimitry Andric LoadedModuleInfoList::LoadedModuleInfo module; 46430b57cec5SDimitry Andric 46440b57cec5SDimitry Andric llvm::StringRef name = library.GetAttributeValue("name"); 46450b57cec5SDimitry Andric module.set_name(name.str()); 46460b57cec5SDimitry Andric 46470b57cec5SDimitry Andric // The base address of a given library will be the address of its 46480b57cec5SDimitry Andric // first section. Most remotes send only one section for Windows 46490b57cec5SDimitry Andric // targets for example. 46500b57cec5SDimitry Andric const XMLNode §ion = 46510b57cec5SDimitry Andric library.FindFirstChildElementWithName("section"); 46520b57cec5SDimitry Andric llvm::StringRef address = section.GetAttributeValue("address"); 4653349cc55cSDimitry Andric uint64_t address_value = LLDB_INVALID_ADDRESS; 4654349cc55cSDimitry Andric llvm::to_integer(address, address_value); 4655349cc55cSDimitry Andric module.set_base(address_value); 46560b57cec5SDimitry Andric // These addresses are absolute values. 46570b57cec5SDimitry Andric module.set_base_is_offset(false); 46580b57cec5SDimitry Andric 46590b57cec5SDimitry Andric if (log) { 46600b57cec5SDimitry Andric std::string name; 46610b57cec5SDimitry Andric lldb::addr_t base = 0; 46620b57cec5SDimitry Andric bool base_is_offset; 46630b57cec5SDimitry Andric module.get_name(name); 46640b57cec5SDimitry Andric module.get_base(base); 46650b57cec5SDimitry Andric module.get_base_is_offset(base_is_offset); 46660b57cec5SDimitry Andric 46679dba64beSDimitry Andric LLDB_LOGF(log, "found (base:0x%08" PRIx64 "[%s], name:'%s')", base, 46680b57cec5SDimitry Andric (base_is_offset ? "offset" : "absolute"), name.c_str()); 46690b57cec5SDimitry Andric } 46700b57cec5SDimitry Andric 46710b57cec5SDimitry Andric list.add(module); 46720b57cec5SDimitry Andric return true; // Keep iterating over all "library" elements in the root 46730b57cec5SDimitry Andric // node 46740b57cec5SDimitry Andric }); 46750b57cec5SDimitry Andric 46760b57cec5SDimitry Andric if (log) 46779dba64beSDimitry Andric LLDB_LOGF(log, "found %" PRId32 " modules in total", 46780b57cec5SDimitry Andric (int)list.m_list.size()); 46799dba64beSDimitry Andric return list; 46800b57cec5SDimitry Andric } else { 46819dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 46829dba64beSDimitry Andric "Remote libraries not supported"); 46830b57cec5SDimitry Andric } 46840b57cec5SDimitry Andric } 46850b57cec5SDimitry Andric 46860b57cec5SDimitry Andric lldb::ModuleSP ProcessGDBRemote::LoadModuleAtAddress(const FileSpec &file, 46870b57cec5SDimitry Andric lldb::addr_t link_map, 46880b57cec5SDimitry Andric lldb::addr_t base_addr, 46890b57cec5SDimitry Andric bool value_is_offset) { 46900b57cec5SDimitry Andric DynamicLoader *loader = GetDynamicLoader(); 46910b57cec5SDimitry Andric if (!loader) 46920b57cec5SDimitry Andric return nullptr; 46930b57cec5SDimitry Andric 46940b57cec5SDimitry Andric return loader->LoadModuleAtAddress(file, link_map, base_addr, 46950b57cec5SDimitry Andric value_is_offset); 46960b57cec5SDimitry Andric } 46970b57cec5SDimitry Andric 46989dba64beSDimitry Andric llvm::Error ProcessGDBRemote::LoadModules() { 46990b57cec5SDimitry Andric using lldb_private::process_gdb_remote::ProcessGDBRemote; 47000b57cec5SDimitry Andric 47010b57cec5SDimitry Andric // request a list of loaded libraries from GDBServer 47029dba64beSDimitry Andric llvm::Expected<LoadedModuleInfoList> module_list = GetLoadedModuleList(); 47039dba64beSDimitry Andric if (!module_list) 47049dba64beSDimitry Andric return module_list.takeError(); 47050b57cec5SDimitry Andric 47060b57cec5SDimitry Andric // get a list of all the modules 47070b57cec5SDimitry Andric ModuleList new_modules; 47080b57cec5SDimitry Andric 47099dba64beSDimitry Andric for (LoadedModuleInfoList::LoadedModuleInfo &modInfo : module_list->m_list) { 47100b57cec5SDimitry Andric std::string mod_name; 47110b57cec5SDimitry Andric lldb::addr_t mod_base; 47120b57cec5SDimitry Andric lldb::addr_t link_map; 47130b57cec5SDimitry Andric bool mod_base_is_offset; 47140b57cec5SDimitry Andric 47150b57cec5SDimitry Andric bool valid = true; 47160b57cec5SDimitry Andric valid &= modInfo.get_name(mod_name); 47170b57cec5SDimitry Andric valid &= modInfo.get_base(mod_base); 47180b57cec5SDimitry Andric valid &= modInfo.get_base_is_offset(mod_base_is_offset); 47190b57cec5SDimitry Andric if (!valid) 47200b57cec5SDimitry Andric continue; 47210b57cec5SDimitry Andric 47220b57cec5SDimitry Andric if (!modInfo.get_link_map(link_map)) 47230b57cec5SDimitry Andric link_map = LLDB_INVALID_ADDRESS; 47240b57cec5SDimitry Andric 47250b57cec5SDimitry Andric FileSpec file(mod_name); 47260b57cec5SDimitry Andric FileSystem::Instance().Resolve(file); 47270b57cec5SDimitry Andric lldb::ModuleSP module_sp = 47280b57cec5SDimitry Andric LoadModuleAtAddress(file, link_map, mod_base, mod_base_is_offset); 47290b57cec5SDimitry Andric 47300b57cec5SDimitry Andric if (module_sp.get()) 47310b57cec5SDimitry Andric new_modules.Append(module_sp); 47320b57cec5SDimitry Andric } 47330b57cec5SDimitry Andric 47340b57cec5SDimitry Andric if (new_modules.GetSize() > 0) { 47350b57cec5SDimitry Andric ModuleList removed_modules; 47360b57cec5SDimitry Andric Target &target = GetTarget(); 47370b57cec5SDimitry Andric ModuleList &loaded_modules = m_process->GetTarget().GetImages(); 47380b57cec5SDimitry Andric 47390b57cec5SDimitry Andric for (size_t i = 0; i < loaded_modules.GetSize(); ++i) { 47400b57cec5SDimitry Andric const lldb::ModuleSP loaded_module = loaded_modules.GetModuleAtIndex(i); 47410b57cec5SDimitry Andric 47420b57cec5SDimitry Andric bool found = false; 47430b57cec5SDimitry Andric for (size_t j = 0; j < new_modules.GetSize(); ++j) { 47440b57cec5SDimitry Andric if (new_modules.GetModuleAtIndex(j).get() == loaded_module.get()) 47450b57cec5SDimitry Andric found = true; 47460b57cec5SDimitry Andric } 47470b57cec5SDimitry Andric 47480b57cec5SDimitry Andric // The main executable will never be included in libraries-svr4, don't 47490b57cec5SDimitry Andric // remove it 47500b57cec5SDimitry Andric if (!found && 47510b57cec5SDimitry Andric loaded_module.get() != target.GetExecutableModulePointer()) { 47520b57cec5SDimitry Andric removed_modules.Append(loaded_module); 47530b57cec5SDimitry Andric } 47540b57cec5SDimitry Andric } 47550b57cec5SDimitry Andric 47560b57cec5SDimitry Andric loaded_modules.Remove(removed_modules); 47570b57cec5SDimitry Andric m_process->GetTarget().ModulesDidUnload(removed_modules, false); 47580b57cec5SDimitry Andric 47590b57cec5SDimitry Andric new_modules.ForEach([&target](const lldb::ModuleSP module_sp) -> bool { 47600b57cec5SDimitry Andric lldb_private::ObjectFile *obj = module_sp->GetObjectFile(); 47610b57cec5SDimitry Andric if (!obj) 47620b57cec5SDimitry Andric return true; 47630b57cec5SDimitry Andric 47640b57cec5SDimitry Andric if (obj->GetType() != ObjectFile::Type::eTypeExecutable) 47650b57cec5SDimitry Andric return true; 47660b57cec5SDimitry Andric 47670b57cec5SDimitry Andric lldb::ModuleSP module_copy_sp = module_sp; 47680b57cec5SDimitry Andric target.SetExecutableModule(module_copy_sp, eLoadDependentsNo); 47690b57cec5SDimitry Andric return false; 47700b57cec5SDimitry Andric }); 47710b57cec5SDimitry Andric 47720b57cec5SDimitry Andric loaded_modules.AppendIfNeeded(new_modules); 47730b57cec5SDimitry Andric m_process->GetTarget().ModulesDidLoad(new_modules); 47740b57cec5SDimitry Andric } 47750b57cec5SDimitry Andric 47769dba64beSDimitry Andric return llvm::ErrorSuccess(); 47770b57cec5SDimitry Andric } 47780b57cec5SDimitry Andric 47790b57cec5SDimitry Andric Status ProcessGDBRemote::GetFileLoadAddress(const FileSpec &file, 47800b57cec5SDimitry Andric bool &is_loaded, 47810b57cec5SDimitry Andric lldb::addr_t &load_addr) { 47820b57cec5SDimitry Andric is_loaded = false; 47830b57cec5SDimitry Andric load_addr = LLDB_INVALID_ADDRESS; 47840b57cec5SDimitry Andric 47850b57cec5SDimitry Andric std::string file_path = file.GetPath(false); 47860b57cec5SDimitry Andric if (file_path.empty()) 47870b57cec5SDimitry Andric return Status("Empty file name specified"); 47880b57cec5SDimitry Andric 47890b57cec5SDimitry Andric StreamString packet; 47900b57cec5SDimitry Andric packet.PutCString("qFileLoadAddress:"); 47910b57cec5SDimitry Andric packet.PutStringAsRawHex8(file_path); 47920b57cec5SDimitry Andric 47930b57cec5SDimitry Andric StringExtractorGDBRemote response; 4794fe6060f1SDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) != 47950b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult::Success) 47960b57cec5SDimitry Andric return Status("Sending qFileLoadAddress packet failed"); 47970b57cec5SDimitry Andric 47980b57cec5SDimitry Andric if (response.IsErrorResponse()) { 47990b57cec5SDimitry Andric if (response.GetError() == 1) { 48000b57cec5SDimitry Andric // The file is not loaded into the inferior 48010b57cec5SDimitry Andric is_loaded = false; 48020b57cec5SDimitry Andric load_addr = LLDB_INVALID_ADDRESS; 48030b57cec5SDimitry Andric return Status(); 48040b57cec5SDimitry Andric } 48050b57cec5SDimitry Andric 48060b57cec5SDimitry Andric return Status( 48070b57cec5SDimitry Andric "Fetching file load address from remote server returned an error"); 48080b57cec5SDimitry Andric } 48090b57cec5SDimitry Andric 48100b57cec5SDimitry Andric if (response.IsNormalResponse()) { 48110b57cec5SDimitry Andric is_loaded = true; 48120b57cec5SDimitry Andric load_addr = response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 48130b57cec5SDimitry Andric return Status(); 48140b57cec5SDimitry Andric } 48150b57cec5SDimitry Andric 48160b57cec5SDimitry Andric return Status( 48170b57cec5SDimitry Andric "Unknown error happened during sending the load address packet"); 48180b57cec5SDimitry Andric } 48190b57cec5SDimitry Andric 48200b57cec5SDimitry Andric void ProcessGDBRemote::ModulesDidLoad(ModuleList &module_list) { 48210b57cec5SDimitry Andric // We must call the lldb_private::Process::ModulesDidLoad () first before we 48220b57cec5SDimitry Andric // do anything 48230b57cec5SDimitry Andric Process::ModulesDidLoad(module_list); 48240b57cec5SDimitry Andric 48250b57cec5SDimitry Andric // After loading shared libraries, we can ask our remote GDB server if it 48260b57cec5SDimitry Andric // needs any symbols. 48270b57cec5SDimitry Andric m_gdb_comm.ServeSymbolLookups(this); 48280b57cec5SDimitry Andric } 48290b57cec5SDimitry Andric 48300b57cec5SDimitry Andric void ProcessGDBRemote::HandleAsyncStdout(llvm::StringRef out) { 48310b57cec5SDimitry Andric AppendSTDOUT(out.data(), out.size()); 48320b57cec5SDimitry Andric } 48330b57cec5SDimitry Andric 48340b57cec5SDimitry Andric static const char *end_delimiter = "--end--;"; 48350b57cec5SDimitry Andric static const int end_delimiter_len = 8; 48360b57cec5SDimitry Andric 48370b57cec5SDimitry Andric void ProcessGDBRemote::HandleAsyncMisc(llvm::StringRef data) { 48380b57cec5SDimitry Andric std::string input = data.str(); // '1' to move beyond 'A' 48390b57cec5SDimitry Andric if (m_partial_profile_data.length() > 0) { 48400b57cec5SDimitry Andric m_partial_profile_data.append(input); 48410b57cec5SDimitry Andric input = m_partial_profile_data; 48420b57cec5SDimitry Andric m_partial_profile_data.clear(); 48430b57cec5SDimitry Andric } 48440b57cec5SDimitry Andric 48450b57cec5SDimitry Andric size_t found, pos = 0, len = input.length(); 48460b57cec5SDimitry Andric while ((found = input.find(end_delimiter, pos)) != std::string::npos) { 48470b57cec5SDimitry Andric StringExtractorGDBRemote profileDataExtractor( 48480b57cec5SDimitry Andric input.substr(pos, found).c_str()); 48490b57cec5SDimitry Andric std::string profile_data = 48500b57cec5SDimitry Andric HarmonizeThreadIdsForProfileData(profileDataExtractor); 48510b57cec5SDimitry Andric BroadcastAsyncProfileData(profile_data); 48520b57cec5SDimitry Andric 48530b57cec5SDimitry Andric pos = found + end_delimiter_len; 48540b57cec5SDimitry Andric } 48550b57cec5SDimitry Andric 48560b57cec5SDimitry Andric if (pos < len) { 48570b57cec5SDimitry Andric // Last incomplete chunk. 48580b57cec5SDimitry Andric m_partial_profile_data = input.substr(pos); 48590b57cec5SDimitry Andric } 48600b57cec5SDimitry Andric } 48610b57cec5SDimitry Andric 48620b57cec5SDimitry Andric std::string ProcessGDBRemote::HarmonizeThreadIdsForProfileData( 48630b57cec5SDimitry Andric StringExtractorGDBRemote &profileDataExtractor) { 48640b57cec5SDimitry Andric std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; 48650b57cec5SDimitry Andric std::string output; 48660b57cec5SDimitry Andric llvm::raw_string_ostream output_stream(output); 48670b57cec5SDimitry Andric llvm::StringRef name, value; 48680b57cec5SDimitry Andric 48690b57cec5SDimitry Andric // Going to assuming thread_used_usec comes first, else bail out. 48700b57cec5SDimitry Andric while (profileDataExtractor.GetNameColonValue(name, value)) { 48710b57cec5SDimitry Andric if (name.compare("thread_used_id") == 0) { 48720b57cec5SDimitry Andric StringExtractor threadIDHexExtractor(value); 48730b57cec5SDimitry Andric uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); 48740b57cec5SDimitry Andric 48750b57cec5SDimitry Andric bool has_used_usec = false; 48760b57cec5SDimitry Andric uint32_t curr_used_usec = 0; 48770b57cec5SDimitry Andric llvm::StringRef usec_name, usec_value; 48780b57cec5SDimitry Andric uint32_t input_file_pos = profileDataExtractor.GetFilePos(); 48790b57cec5SDimitry Andric if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) { 48800b57cec5SDimitry Andric if (usec_name.equals("thread_used_usec")) { 48810b57cec5SDimitry Andric has_used_usec = true; 48820b57cec5SDimitry Andric usec_value.getAsInteger(0, curr_used_usec); 48830b57cec5SDimitry Andric } else { 48840b57cec5SDimitry Andric // We didn't find what we want, it is probably an older version. Bail 48850b57cec5SDimitry Andric // out. 48860b57cec5SDimitry Andric profileDataExtractor.SetFilePos(input_file_pos); 48870b57cec5SDimitry Andric } 48880b57cec5SDimitry Andric } 48890b57cec5SDimitry Andric 48900b57cec5SDimitry Andric if (has_used_usec) { 48910b57cec5SDimitry Andric uint32_t prev_used_usec = 0; 48920b57cec5SDimitry Andric std::map<uint64_t, uint32_t>::iterator iterator = 48930b57cec5SDimitry Andric m_thread_id_to_used_usec_map.find(thread_id); 48940b57cec5SDimitry Andric if (iterator != m_thread_id_to_used_usec_map.end()) { 48950b57cec5SDimitry Andric prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; 48960b57cec5SDimitry Andric } 48970b57cec5SDimitry Andric 48980b57cec5SDimitry Andric uint32_t real_used_usec = curr_used_usec - prev_used_usec; 48990b57cec5SDimitry Andric // A good first time record is one that runs for at least 0.25 sec 49000b57cec5SDimitry Andric bool good_first_time = 49010b57cec5SDimitry Andric (prev_used_usec == 0) && (real_used_usec > 250000); 49020b57cec5SDimitry Andric bool good_subsequent_time = 49030b57cec5SDimitry Andric (prev_used_usec > 0) && 49040b57cec5SDimitry Andric ((real_used_usec > 0) || (HasAssignedIndexIDToThread(thread_id))); 49050b57cec5SDimitry Andric 49060b57cec5SDimitry Andric if (good_first_time || good_subsequent_time) { 49070b57cec5SDimitry Andric // We try to avoid doing too many index id reservation, resulting in 49080b57cec5SDimitry Andric // fast increase of index ids. 49090b57cec5SDimitry Andric 49100b57cec5SDimitry Andric output_stream << name << ":"; 49110b57cec5SDimitry Andric int32_t index_id = AssignIndexIDToThread(thread_id); 49120b57cec5SDimitry Andric output_stream << index_id << ";"; 49130b57cec5SDimitry Andric 49140b57cec5SDimitry Andric output_stream << usec_name << ":" << usec_value << ";"; 49150b57cec5SDimitry Andric } else { 49160b57cec5SDimitry Andric // Skip past 'thread_used_name'. 49170b57cec5SDimitry Andric llvm::StringRef local_name, local_value; 49180b57cec5SDimitry Andric profileDataExtractor.GetNameColonValue(local_name, local_value); 49190b57cec5SDimitry Andric } 49200b57cec5SDimitry Andric 49210b57cec5SDimitry Andric // Store current time as previous time so that they can be compared 49220b57cec5SDimitry Andric // later. 49230b57cec5SDimitry Andric new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; 49240b57cec5SDimitry Andric } else { 49250b57cec5SDimitry Andric // Bail out and use old string. 49260b57cec5SDimitry Andric output_stream << name << ":" << value << ";"; 49270b57cec5SDimitry Andric } 49280b57cec5SDimitry Andric } else { 49290b57cec5SDimitry Andric output_stream << name << ":" << value << ";"; 49300b57cec5SDimitry Andric } 49310b57cec5SDimitry Andric } 49320b57cec5SDimitry Andric output_stream << end_delimiter; 49330b57cec5SDimitry Andric m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; 49340b57cec5SDimitry Andric 49350b57cec5SDimitry Andric return output_stream.str(); 49360b57cec5SDimitry Andric } 49370b57cec5SDimitry Andric 49380b57cec5SDimitry Andric void ProcessGDBRemote::HandleStopReply() { 49390b57cec5SDimitry Andric if (GetStopID() != 0) 49400b57cec5SDimitry Andric return; 49410b57cec5SDimitry Andric 49420b57cec5SDimitry Andric if (GetID() == LLDB_INVALID_PROCESS_ID) { 49430b57cec5SDimitry Andric lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID(); 49440b57cec5SDimitry Andric if (pid != LLDB_INVALID_PROCESS_ID) 49450b57cec5SDimitry Andric SetID(pid); 49460b57cec5SDimitry Andric } 49470b57cec5SDimitry Andric BuildDynamicRegisterInfo(true); 49480b57cec5SDimitry Andric } 49490b57cec5SDimitry Andric 4950349cc55cSDimitry Andric llvm::Expected<bool> ProcessGDBRemote::SaveCore(llvm::StringRef outfile) { 4951349cc55cSDimitry Andric if (!m_gdb_comm.GetSaveCoreSupported()) 4952349cc55cSDimitry Andric return false; 4953349cc55cSDimitry Andric 4954349cc55cSDimitry Andric StreamString packet; 4955349cc55cSDimitry Andric packet.PutCString("qSaveCore;path-hint:"); 4956349cc55cSDimitry Andric packet.PutStringAsRawHex8(outfile); 4957349cc55cSDimitry Andric 4958349cc55cSDimitry Andric StringExtractorGDBRemote response; 4959349cc55cSDimitry Andric if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) == 4960349cc55cSDimitry Andric GDBRemoteCommunication::PacketResult::Success) { 4961349cc55cSDimitry Andric // TODO: grab error message from the packet? StringExtractor seems to 4962349cc55cSDimitry Andric // be missing a method for that 4963349cc55cSDimitry Andric if (response.IsErrorResponse()) 4964349cc55cSDimitry Andric return llvm::createStringError( 4965349cc55cSDimitry Andric llvm::inconvertibleErrorCode(), 4966349cc55cSDimitry Andric llvm::formatv("qSaveCore returned an error")); 4967349cc55cSDimitry Andric 4968349cc55cSDimitry Andric std::string path; 4969349cc55cSDimitry Andric 4970349cc55cSDimitry Andric // process the response 4971349cc55cSDimitry Andric for (auto x : llvm::split(response.GetStringRef(), ';')) { 4972349cc55cSDimitry Andric if (x.consume_front("core-path:")) 4973349cc55cSDimitry Andric StringExtractor(x).GetHexByteString(path); 4974349cc55cSDimitry Andric } 4975349cc55cSDimitry Andric 4976349cc55cSDimitry Andric // verify that we've gotten what we need 4977349cc55cSDimitry Andric if (path.empty()) 4978349cc55cSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 4979349cc55cSDimitry Andric "qSaveCore returned no core path"); 4980349cc55cSDimitry Andric 4981349cc55cSDimitry Andric // now transfer the core file 4982349cc55cSDimitry Andric FileSpec remote_core{llvm::StringRef(path)}; 4983349cc55cSDimitry Andric Platform &platform = *GetTarget().GetPlatform(); 4984349cc55cSDimitry Andric Status error = platform.GetFile(remote_core, FileSpec(outfile)); 4985349cc55cSDimitry Andric 4986349cc55cSDimitry Andric if (platform.IsRemote()) { 4987349cc55cSDimitry Andric // NB: we unlink the file on error too 4988349cc55cSDimitry Andric platform.Unlink(remote_core); 4989349cc55cSDimitry Andric if (error.Fail()) 4990349cc55cSDimitry Andric return error.ToError(); 4991349cc55cSDimitry Andric } 4992349cc55cSDimitry Andric 4993349cc55cSDimitry Andric return true; 4994349cc55cSDimitry Andric } 4995349cc55cSDimitry Andric 4996349cc55cSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 4997349cc55cSDimitry Andric "Unable to send qSaveCore"); 4998349cc55cSDimitry Andric } 4999349cc55cSDimitry Andric 50000b57cec5SDimitry Andric static const char *const s_async_json_packet_prefix = "JSON-async:"; 50010b57cec5SDimitry Andric 50020b57cec5SDimitry Andric static StructuredData::ObjectSP 50030b57cec5SDimitry Andric ParseStructuredDataPacket(llvm::StringRef packet) { 50040b57cec5SDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 50050b57cec5SDimitry Andric 50060b57cec5SDimitry Andric if (!packet.consume_front(s_async_json_packet_prefix)) { 50070b57cec5SDimitry Andric if (log) { 50089dba64beSDimitry Andric LLDB_LOGF( 50099dba64beSDimitry Andric log, 50100b57cec5SDimitry Andric "GDBRemoteCommunicationClientBase::%s() received $J packet " 50110b57cec5SDimitry Andric "but was not a StructuredData packet: packet starts with " 50120b57cec5SDimitry Andric "%s", 50130b57cec5SDimitry Andric __FUNCTION__, 50140b57cec5SDimitry Andric packet.slice(0, strlen(s_async_json_packet_prefix)).str().c_str()); 50150b57cec5SDimitry Andric } 50160b57cec5SDimitry Andric return StructuredData::ObjectSP(); 50170b57cec5SDimitry Andric } 50180b57cec5SDimitry Andric 50190b57cec5SDimitry Andric // This is an asynchronous JSON packet, destined for a StructuredDataPlugin. 50205ffd83dbSDimitry Andric StructuredData::ObjectSP json_sp = 50215ffd83dbSDimitry Andric StructuredData::ParseJSON(std::string(packet)); 50220b57cec5SDimitry Andric if (log) { 50230b57cec5SDimitry Andric if (json_sp) { 50240b57cec5SDimitry Andric StreamString json_str; 50259dba64beSDimitry Andric json_sp->Dump(json_str, true); 50260b57cec5SDimitry Andric json_str.Flush(); 50279dba64beSDimitry Andric LLDB_LOGF(log, 50289dba64beSDimitry Andric "ProcessGDBRemote::%s() " 50290b57cec5SDimitry Andric "received Async StructuredData packet: %s", 50300b57cec5SDimitry Andric __FUNCTION__, json_str.GetData()); 50310b57cec5SDimitry Andric } else { 50329dba64beSDimitry Andric LLDB_LOGF(log, 50339dba64beSDimitry Andric "ProcessGDBRemote::%s" 50340b57cec5SDimitry Andric "() received StructuredData packet:" 50350b57cec5SDimitry Andric " parse failure", 50360b57cec5SDimitry Andric __FUNCTION__); 50370b57cec5SDimitry Andric } 50380b57cec5SDimitry Andric } 50390b57cec5SDimitry Andric return json_sp; 50400b57cec5SDimitry Andric } 50410b57cec5SDimitry Andric 50420b57cec5SDimitry Andric void ProcessGDBRemote::HandleAsyncStructuredDataPacket(llvm::StringRef data) { 50430b57cec5SDimitry Andric auto structured_data_sp = ParseStructuredDataPacket(data); 50440b57cec5SDimitry Andric if (structured_data_sp) 50450b57cec5SDimitry Andric RouteAsyncStructuredData(structured_data_sp); 50460b57cec5SDimitry Andric } 50470b57cec5SDimitry Andric 50480b57cec5SDimitry Andric class CommandObjectProcessGDBRemoteSpeedTest : public CommandObjectParsed { 50490b57cec5SDimitry Andric public: 50500b57cec5SDimitry Andric CommandObjectProcessGDBRemoteSpeedTest(CommandInterpreter &interpreter) 50510b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process plugin packet speed-test", 50520b57cec5SDimitry Andric "Tests packet speeds of various sizes to determine " 50530b57cec5SDimitry Andric "the performance characteristics of the GDB remote " 50540b57cec5SDimitry Andric "connection. ", 50550b57cec5SDimitry Andric nullptr), 50560b57cec5SDimitry Andric m_option_group(), 50570b57cec5SDimitry Andric m_num_packets(LLDB_OPT_SET_1, false, "count", 'c', 0, eArgTypeCount, 50580b57cec5SDimitry Andric "The number of packets to send of each varying size " 50590b57cec5SDimitry Andric "(default is 1000).", 50600b57cec5SDimitry Andric 1000), 50610b57cec5SDimitry Andric m_max_send(LLDB_OPT_SET_1, false, "max-send", 's', 0, eArgTypeCount, 50620b57cec5SDimitry Andric "The maximum number of bytes to send in a packet. Sizes " 50630b57cec5SDimitry Andric "increase in powers of 2 while the size is less than or " 50640b57cec5SDimitry Andric "equal to this option value. (default 1024).", 50650b57cec5SDimitry Andric 1024), 50660b57cec5SDimitry Andric m_max_recv(LLDB_OPT_SET_1, false, "max-receive", 'r', 0, eArgTypeCount, 50670b57cec5SDimitry Andric "The maximum number of bytes to receive in a packet. Sizes " 50680b57cec5SDimitry Andric "increase in powers of 2 while the size is less than or " 50690b57cec5SDimitry Andric "equal to this option value. (default 1024).", 50700b57cec5SDimitry Andric 1024), 50710b57cec5SDimitry Andric m_json(LLDB_OPT_SET_1, false, "json", 'j', 50720b57cec5SDimitry Andric "Print the output as JSON data for easy parsing.", false, true) { 50730b57cec5SDimitry Andric m_option_group.Append(&m_num_packets, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 50740b57cec5SDimitry Andric m_option_group.Append(&m_max_send, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 50750b57cec5SDimitry Andric m_option_group.Append(&m_max_recv, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 50760b57cec5SDimitry Andric m_option_group.Append(&m_json, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 50770b57cec5SDimitry Andric m_option_group.Finalize(); 50780b57cec5SDimitry Andric } 50790b57cec5SDimitry Andric 5080fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemoteSpeedTest() override = default; 50810b57cec5SDimitry Andric 50820b57cec5SDimitry Andric Options *GetOptions() override { return &m_option_group; } 50830b57cec5SDimitry Andric 50840b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 50850b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 50860b57cec5SDimitry Andric if (argc == 0) { 50870b57cec5SDimitry Andric ProcessGDBRemote *process = 50880b57cec5SDimitry Andric (ProcessGDBRemote *)m_interpreter.GetExecutionContext() 50890b57cec5SDimitry Andric .GetProcessPtr(); 50900b57cec5SDimitry Andric if (process) { 50910b57cec5SDimitry Andric StreamSP output_stream_sp( 50920b57cec5SDimitry Andric m_interpreter.GetDebugger().GetAsyncOutputStream()); 50930b57cec5SDimitry Andric result.SetImmediateOutputStream(output_stream_sp); 50940b57cec5SDimitry Andric 50950b57cec5SDimitry Andric const uint32_t num_packets = 50960b57cec5SDimitry Andric (uint32_t)m_num_packets.GetOptionValue().GetCurrentValue(); 50970b57cec5SDimitry Andric const uint64_t max_send = m_max_send.GetOptionValue().GetCurrentValue(); 50980b57cec5SDimitry Andric const uint64_t max_recv = m_max_recv.GetOptionValue().GetCurrentValue(); 50990b57cec5SDimitry Andric const bool json = m_json.GetOptionValue().GetCurrentValue(); 51000b57cec5SDimitry Andric const uint64_t k_recv_amount = 51010b57cec5SDimitry Andric 4 * 1024 * 1024; // Receive amount in bytes 51020b57cec5SDimitry Andric process->GetGDBRemote().TestPacketSpeed( 51030b57cec5SDimitry Andric num_packets, max_send, max_recv, k_recv_amount, json, 51040b57cec5SDimitry Andric output_stream_sp ? *output_stream_sp : result.GetOutputStream()); 51050b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 51060b57cec5SDimitry Andric return true; 51070b57cec5SDimitry Andric } 51080b57cec5SDimitry Andric } else { 51090b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes no arguments", 51100b57cec5SDimitry Andric m_cmd_name.c_str()); 51110b57cec5SDimitry Andric } 51120b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 51130b57cec5SDimitry Andric return false; 51140b57cec5SDimitry Andric } 51150b57cec5SDimitry Andric 51160b57cec5SDimitry Andric protected: 51170b57cec5SDimitry Andric OptionGroupOptions m_option_group; 51180b57cec5SDimitry Andric OptionGroupUInt64 m_num_packets; 51190b57cec5SDimitry Andric OptionGroupUInt64 m_max_send; 51200b57cec5SDimitry Andric OptionGroupUInt64 m_max_recv; 51210b57cec5SDimitry Andric OptionGroupBoolean m_json; 51220b57cec5SDimitry Andric }; 51230b57cec5SDimitry Andric 51240b57cec5SDimitry Andric class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed { 51250b57cec5SDimitry Andric private: 51260b57cec5SDimitry Andric public: 51270b57cec5SDimitry Andric CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) 51280b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process plugin packet history", 51290b57cec5SDimitry Andric "Dumps the packet history buffer. ", nullptr) {} 51300b57cec5SDimitry Andric 5131fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemotePacketHistory() override = default; 51320b57cec5SDimitry Andric 51330b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 51340b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 51350b57cec5SDimitry Andric if (argc == 0) { 51360b57cec5SDimitry Andric ProcessGDBRemote *process = 51370b57cec5SDimitry Andric (ProcessGDBRemote *)m_interpreter.GetExecutionContext() 51380b57cec5SDimitry Andric .GetProcessPtr(); 51390b57cec5SDimitry Andric if (process) { 51400b57cec5SDimitry Andric process->GetGDBRemote().DumpHistory(result.GetOutputStream()); 51410b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 51420b57cec5SDimitry Andric return true; 51430b57cec5SDimitry Andric } 51440b57cec5SDimitry Andric } else { 51450b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes no arguments", 51460b57cec5SDimitry Andric m_cmd_name.c_str()); 51470b57cec5SDimitry Andric } 51480b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 51490b57cec5SDimitry Andric return false; 51500b57cec5SDimitry Andric } 51510b57cec5SDimitry Andric }; 51520b57cec5SDimitry Andric 51530b57cec5SDimitry Andric class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed { 51540b57cec5SDimitry Andric private: 51550b57cec5SDimitry Andric public: 51560b57cec5SDimitry Andric CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) 51570b57cec5SDimitry Andric : CommandObjectParsed( 51580b57cec5SDimitry Andric interpreter, "process plugin packet xfer-size", 51590b57cec5SDimitry Andric "Maximum size that lldb will try to read/write one one chunk.", 51600b57cec5SDimitry Andric nullptr) {} 51610b57cec5SDimitry Andric 5162fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemotePacketXferSize() override = default; 51630b57cec5SDimitry Andric 51640b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 51650b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 51660b57cec5SDimitry Andric if (argc == 0) { 51670b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes an argument to specify the max " 51680b57cec5SDimitry Andric "amount to be transferred when " 51690b57cec5SDimitry Andric "reading/writing", 51700b57cec5SDimitry Andric m_cmd_name.c_str()); 51710b57cec5SDimitry Andric return false; 51720b57cec5SDimitry Andric } 51730b57cec5SDimitry Andric 51740b57cec5SDimitry Andric ProcessGDBRemote *process = 51750b57cec5SDimitry Andric (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); 51760b57cec5SDimitry Andric if (process) { 51770b57cec5SDimitry Andric const char *packet_size = command.GetArgumentAtIndex(0); 51780b57cec5SDimitry Andric errno = 0; 51790b57cec5SDimitry Andric uint64_t user_specified_max = strtoul(packet_size, nullptr, 10); 51800b57cec5SDimitry Andric if (errno == 0 && user_specified_max != 0) { 51810b57cec5SDimitry Andric process->SetUserSpecifiedMaxMemoryTransferSize(user_specified_max); 51820b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 51830b57cec5SDimitry Andric return true; 51840b57cec5SDimitry Andric } 51850b57cec5SDimitry Andric } 51860b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 51870b57cec5SDimitry Andric return false; 51880b57cec5SDimitry Andric } 51890b57cec5SDimitry Andric }; 51900b57cec5SDimitry Andric 51910b57cec5SDimitry Andric class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed { 51920b57cec5SDimitry Andric private: 51930b57cec5SDimitry Andric public: 51940b57cec5SDimitry Andric CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) 51950b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process plugin packet send", 51960b57cec5SDimitry Andric "Send a custom packet through the GDB remote " 51970b57cec5SDimitry Andric "protocol and print the answer. " 51980b57cec5SDimitry Andric "The packet header and footer will automatically " 51990b57cec5SDimitry Andric "be added to the packet prior to sending and " 52000b57cec5SDimitry Andric "stripped from the result.", 52010b57cec5SDimitry Andric nullptr) {} 52020b57cec5SDimitry Andric 5203fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemotePacketSend() override = default; 52040b57cec5SDimitry Andric 52050b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 52060b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 52070b57cec5SDimitry Andric if (argc == 0) { 52080b57cec5SDimitry Andric result.AppendErrorWithFormat( 52090b57cec5SDimitry Andric "'%s' takes a one or more packet content arguments", 52100b57cec5SDimitry Andric m_cmd_name.c_str()); 52110b57cec5SDimitry Andric return false; 52120b57cec5SDimitry Andric } 52130b57cec5SDimitry Andric 52140b57cec5SDimitry Andric ProcessGDBRemote *process = 52150b57cec5SDimitry Andric (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); 52160b57cec5SDimitry Andric if (process) { 52170b57cec5SDimitry Andric for (size_t i = 0; i < argc; ++i) { 52180b57cec5SDimitry Andric const char *packet_cstr = command.GetArgumentAtIndex(0); 52190b57cec5SDimitry Andric StringExtractorGDBRemote response; 52200b57cec5SDimitry Andric process->GetGDBRemote().SendPacketAndWaitForResponse( 5221fe6060f1SDimitry Andric packet_cstr, response, process->GetInterruptTimeout()); 52220b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 52230b57cec5SDimitry Andric Stream &output_strm = result.GetOutputStream(); 52240b57cec5SDimitry Andric output_strm.Printf(" packet: %s\n", packet_cstr); 52255ffd83dbSDimitry Andric std::string response_str = std::string(response.GetStringRef()); 52260b57cec5SDimitry Andric 52270b57cec5SDimitry Andric if (strstr(packet_cstr, "qGetProfileData") != nullptr) { 52280b57cec5SDimitry Andric response_str = process->HarmonizeThreadIdsForProfileData(response); 52290b57cec5SDimitry Andric } 52300b57cec5SDimitry Andric 52310b57cec5SDimitry Andric if (response_str.empty()) 52320b57cec5SDimitry Andric output_strm.PutCString("response: \nerror: UNIMPLEMENTED\n"); 52330b57cec5SDimitry Andric else 52349dba64beSDimitry Andric output_strm.Printf("response: %s\n", response.GetStringRef().data()); 52350b57cec5SDimitry Andric } 52360b57cec5SDimitry Andric } 52370b57cec5SDimitry Andric return true; 52380b57cec5SDimitry Andric } 52390b57cec5SDimitry Andric }; 52400b57cec5SDimitry Andric 52410b57cec5SDimitry Andric class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw { 52420b57cec5SDimitry Andric private: 52430b57cec5SDimitry Andric public: 52440b57cec5SDimitry Andric CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) 52450b57cec5SDimitry Andric : CommandObjectRaw(interpreter, "process plugin packet monitor", 52460b57cec5SDimitry Andric "Send a qRcmd packet through the GDB remote protocol " 52470b57cec5SDimitry Andric "and print the response." 52480b57cec5SDimitry Andric "The argument passed to this command will be hex " 52490b57cec5SDimitry Andric "encoded into a valid 'qRcmd' packet, sent and the " 52500b57cec5SDimitry Andric "response will be printed.") {} 52510b57cec5SDimitry Andric 5252fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemotePacketMonitor() override = default; 52530b57cec5SDimitry Andric 52540b57cec5SDimitry Andric bool DoExecute(llvm::StringRef command, 52550b57cec5SDimitry Andric CommandReturnObject &result) override { 52560b57cec5SDimitry Andric if (command.empty()) { 52570b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes a command string argument", 52580b57cec5SDimitry Andric m_cmd_name.c_str()); 52590b57cec5SDimitry Andric return false; 52600b57cec5SDimitry Andric } 52610b57cec5SDimitry Andric 52620b57cec5SDimitry Andric ProcessGDBRemote *process = 52630b57cec5SDimitry Andric (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); 52640b57cec5SDimitry Andric if (process) { 52650b57cec5SDimitry Andric StreamString packet; 52660b57cec5SDimitry Andric packet.PutCString("qRcmd,"); 52670b57cec5SDimitry Andric packet.PutBytesAsRawHex8(command.data(), command.size()); 52680b57cec5SDimitry Andric 52690b57cec5SDimitry Andric StringExtractorGDBRemote response; 52700b57cec5SDimitry Andric Stream &output_strm = result.GetOutputStream(); 52710b57cec5SDimitry Andric process->GetGDBRemote().SendPacketAndReceiveResponseWithOutputSupport( 5272fe6060f1SDimitry Andric packet.GetString(), response, process->GetInterruptTimeout(), 52730b57cec5SDimitry Andric [&output_strm](llvm::StringRef output) { output_strm << output; }); 52740b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 52750b57cec5SDimitry Andric output_strm.Printf(" packet: %s\n", packet.GetData()); 52765ffd83dbSDimitry Andric const std::string &response_str = std::string(response.GetStringRef()); 52770b57cec5SDimitry Andric 52780b57cec5SDimitry Andric if (response_str.empty()) 52790b57cec5SDimitry Andric output_strm.PutCString("response: \nerror: UNIMPLEMENTED\n"); 52800b57cec5SDimitry Andric else 52819dba64beSDimitry Andric output_strm.Printf("response: %s\n", response.GetStringRef().data()); 52820b57cec5SDimitry Andric } 52830b57cec5SDimitry Andric return true; 52840b57cec5SDimitry Andric } 52850b57cec5SDimitry Andric }; 52860b57cec5SDimitry Andric 52870b57cec5SDimitry Andric class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword { 52880b57cec5SDimitry Andric private: 52890b57cec5SDimitry Andric public: 52900b57cec5SDimitry Andric CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) 52910b57cec5SDimitry Andric : CommandObjectMultiword(interpreter, "process plugin packet", 52920b57cec5SDimitry Andric "Commands that deal with GDB remote packets.", 52930b57cec5SDimitry Andric nullptr) { 52940b57cec5SDimitry Andric LoadSubCommand( 52950b57cec5SDimitry Andric "history", 52960b57cec5SDimitry Andric CommandObjectSP( 52970b57cec5SDimitry Andric new CommandObjectProcessGDBRemotePacketHistory(interpreter))); 52980b57cec5SDimitry Andric LoadSubCommand( 52990b57cec5SDimitry Andric "send", CommandObjectSP( 53000b57cec5SDimitry Andric new CommandObjectProcessGDBRemotePacketSend(interpreter))); 53010b57cec5SDimitry Andric LoadSubCommand( 53020b57cec5SDimitry Andric "monitor", 53030b57cec5SDimitry Andric CommandObjectSP( 53040b57cec5SDimitry Andric new CommandObjectProcessGDBRemotePacketMonitor(interpreter))); 53050b57cec5SDimitry Andric LoadSubCommand( 53060b57cec5SDimitry Andric "xfer-size", 53070b57cec5SDimitry Andric CommandObjectSP( 53080b57cec5SDimitry Andric new CommandObjectProcessGDBRemotePacketXferSize(interpreter))); 53090b57cec5SDimitry Andric LoadSubCommand("speed-test", 53100b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessGDBRemoteSpeedTest( 53110b57cec5SDimitry Andric interpreter))); 53120b57cec5SDimitry Andric } 53130b57cec5SDimitry Andric 5314fe6060f1SDimitry Andric ~CommandObjectProcessGDBRemotePacket() override = default; 53150b57cec5SDimitry Andric }; 53160b57cec5SDimitry Andric 53170b57cec5SDimitry Andric class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword { 53180b57cec5SDimitry Andric public: 53190b57cec5SDimitry Andric CommandObjectMultiwordProcessGDBRemote(CommandInterpreter &interpreter) 53200b57cec5SDimitry Andric : CommandObjectMultiword( 53210b57cec5SDimitry Andric interpreter, "process plugin", 53220b57cec5SDimitry Andric "Commands for operating on a ProcessGDBRemote process.", 53230b57cec5SDimitry Andric "process plugin <subcommand> [<subcommand-options>]") { 53240b57cec5SDimitry Andric LoadSubCommand( 53250b57cec5SDimitry Andric "packet", 53260b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessGDBRemotePacket(interpreter))); 53270b57cec5SDimitry Andric } 53280b57cec5SDimitry Andric 5329fe6060f1SDimitry Andric ~CommandObjectMultiwordProcessGDBRemote() override = default; 53300b57cec5SDimitry Andric }; 53310b57cec5SDimitry Andric 53320b57cec5SDimitry Andric CommandObject *ProcessGDBRemote::GetPluginCommandObject() { 53330b57cec5SDimitry Andric if (!m_command_sp) 53340b57cec5SDimitry Andric m_command_sp = std::make_shared<CommandObjectMultiwordProcessGDBRemote>( 53350b57cec5SDimitry Andric GetTarget().GetDebugger().GetCommandInterpreter()); 53360b57cec5SDimitry Andric return m_command_sp.get(); 53370b57cec5SDimitry Andric } 5338349cc55cSDimitry Andric 5339349cc55cSDimitry Andric void ProcessGDBRemote::DidForkSwitchSoftwareBreakpoints(bool enable) { 5340349cc55cSDimitry Andric GetBreakpointSiteList().ForEach([this, enable](BreakpointSite *bp_site) { 5341349cc55cSDimitry Andric if (bp_site->IsEnabled() && 5342349cc55cSDimitry Andric (bp_site->GetType() == BreakpointSite::eSoftware || 5343349cc55cSDimitry Andric bp_site->GetType() == BreakpointSite::eExternal)) { 5344349cc55cSDimitry Andric m_gdb_comm.SendGDBStoppointTypePacket( 5345349cc55cSDimitry Andric eBreakpointSoftware, enable, bp_site->GetLoadAddress(), 5346349cc55cSDimitry Andric GetSoftwareBreakpointTrapOpcode(bp_site), GetInterruptTimeout()); 5347349cc55cSDimitry Andric } 5348349cc55cSDimitry Andric }); 5349349cc55cSDimitry Andric } 5350349cc55cSDimitry Andric 5351349cc55cSDimitry Andric void ProcessGDBRemote::DidForkSwitchHardwareTraps(bool enable) { 5352349cc55cSDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) { 5353349cc55cSDimitry Andric GetBreakpointSiteList().ForEach([this, enable](BreakpointSite *bp_site) { 5354349cc55cSDimitry Andric if (bp_site->IsEnabled() && 5355349cc55cSDimitry Andric bp_site->GetType() == BreakpointSite::eHardware) { 5356349cc55cSDimitry Andric m_gdb_comm.SendGDBStoppointTypePacket( 5357349cc55cSDimitry Andric eBreakpointHardware, enable, bp_site->GetLoadAddress(), 5358349cc55cSDimitry Andric GetSoftwareBreakpointTrapOpcode(bp_site), GetInterruptTimeout()); 5359349cc55cSDimitry Andric } 5360349cc55cSDimitry Andric }); 5361349cc55cSDimitry Andric } 5362349cc55cSDimitry Andric 5363349cc55cSDimitry Andric WatchpointList &wps = GetTarget().GetWatchpointList(); 5364349cc55cSDimitry Andric size_t wp_count = wps.GetSize(); 5365349cc55cSDimitry Andric for (size_t i = 0; i < wp_count; ++i) { 5366349cc55cSDimitry Andric WatchpointSP wp = wps.GetByIndex(i); 5367349cc55cSDimitry Andric if (wp->IsEnabled()) { 5368349cc55cSDimitry Andric GDBStoppointType type = GetGDBStoppointType(wp.get()); 5369349cc55cSDimitry Andric m_gdb_comm.SendGDBStoppointTypePacket(type, enable, wp->GetLoadAddress(), 5370349cc55cSDimitry Andric wp->GetByteSize(), 5371349cc55cSDimitry Andric GetInterruptTimeout()); 5372349cc55cSDimitry Andric } 5373349cc55cSDimitry Andric } 5374349cc55cSDimitry Andric } 5375349cc55cSDimitry Andric 5376349cc55cSDimitry Andric void ProcessGDBRemote::DidFork(lldb::pid_t child_pid, lldb::tid_t child_tid) { 5377349cc55cSDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 5378349cc55cSDimitry Andric 5379349cc55cSDimitry Andric lldb::pid_t parent_pid = m_gdb_comm.GetCurrentProcessID(); 5380349cc55cSDimitry Andric // Any valid TID will suffice, thread-relevant actions will set a proper TID 5381349cc55cSDimitry Andric // anyway. 5382349cc55cSDimitry Andric lldb::tid_t parent_tid = m_thread_ids.front(); 5383349cc55cSDimitry Andric 5384349cc55cSDimitry Andric lldb::pid_t follow_pid, detach_pid; 5385349cc55cSDimitry Andric lldb::tid_t follow_tid, detach_tid; 5386349cc55cSDimitry Andric 5387349cc55cSDimitry Andric switch (GetFollowForkMode()) { 5388349cc55cSDimitry Andric case eFollowParent: 5389349cc55cSDimitry Andric follow_pid = parent_pid; 5390349cc55cSDimitry Andric follow_tid = parent_tid; 5391349cc55cSDimitry Andric detach_pid = child_pid; 5392349cc55cSDimitry Andric detach_tid = child_tid; 5393349cc55cSDimitry Andric break; 5394349cc55cSDimitry Andric case eFollowChild: 5395349cc55cSDimitry Andric follow_pid = child_pid; 5396349cc55cSDimitry Andric follow_tid = child_tid; 5397349cc55cSDimitry Andric detach_pid = parent_pid; 5398349cc55cSDimitry Andric detach_tid = parent_tid; 5399349cc55cSDimitry Andric break; 5400349cc55cSDimitry Andric } 5401349cc55cSDimitry Andric 5402349cc55cSDimitry Andric // Switch to the process that is going to be detached. 5403349cc55cSDimitry Andric if (!m_gdb_comm.SetCurrentThread(detach_tid, detach_pid)) { 5404349cc55cSDimitry Andric LLDB_LOG(log, "ProcessGDBRemote::DidFork() unable to set pid/tid"); 5405349cc55cSDimitry Andric return; 5406349cc55cSDimitry Andric } 5407349cc55cSDimitry Andric 5408349cc55cSDimitry Andric // Disable all software breakpoints in the forked process. 5409349cc55cSDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) 5410349cc55cSDimitry Andric DidForkSwitchSoftwareBreakpoints(false); 5411349cc55cSDimitry Andric 5412349cc55cSDimitry Andric // Remove hardware breakpoints / watchpoints from parent process if we're 5413349cc55cSDimitry Andric // following child. 5414349cc55cSDimitry Andric if (GetFollowForkMode() == eFollowChild) 5415349cc55cSDimitry Andric DidForkSwitchHardwareTraps(false); 5416349cc55cSDimitry Andric 5417349cc55cSDimitry Andric // Switch to the process that is going to be followed 5418349cc55cSDimitry Andric if (!m_gdb_comm.SetCurrentThread(follow_tid, follow_pid) || 5419349cc55cSDimitry Andric !m_gdb_comm.SetCurrentThreadForRun(follow_tid, follow_pid)) { 5420349cc55cSDimitry Andric LLDB_LOG(log, "ProcessGDBRemote::DidFork() unable to reset pid/tid"); 5421349cc55cSDimitry Andric return; 5422349cc55cSDimitry Andric } 5423349cc55cSDimitry Andric 5424349cc55cSDimitry Andric LLDB_LOG(log, "Detaching process {0}", detach_pid); 5425349cc55cSDimitry Andric Status error = m_gdb_comm.Detach(false, detach_pid); 5426349cc55cSDimitry Andric if (error.Fail()) { 5427349cc55cSDimitry Andric LLDB_LOG(log, "ProcessGDBRemote::DidFork() detach packet send failed: {0}", 5428349cc55cSDimitry Andric error.AsCString() ? error.AsCString() : "<unknown error>"); 5429349cc55cSDimitry Andric return; 5430349cc55cSDimitry Andric } 5431349cc55cSDimitry Andric 5432349cc55cSDimitry Andric // Hardware breakpoints/watchpoints are not inherited implicitly, 5433349cc55cSDimitry Andric // so we need to readd them if we're following child. 5434349cc55cSDimitry Andric if (GetFollowForkMode() == eFollowChild) 5435349cc55cSDimitry Andric DidForkSwitchHardwareTraps(true); 5436349cc55cSDimitry Andric } 5437349cc55cSDimitry Andric 5438349cc55cSDimitry Andric void ProcessGDBRemote::DidVFork(lldb::pid_t child_pid, lldb::tid_t child_tid) { 5439349cc55cSDimitry Andric Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); 5440349cc55cSDimitry Andric 5441349cc55cSDimitry Andric assert(!m_vfork_in_progress); 5442349cc55cSDimitry Andric m_vfork_in_progress = true; 5443349cc55cSDimitry Andric 5444349cc55cSDimitry Andric // Disable all software breakpoints for the duration of vfork. 5445349cc55cSDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) 5446349cc55cSDimitry Andric DidForkSwitchSoftwareBreakpoints(false); 5447349cc55cSDimitry Andric 5448349cc55cSDimitry Andric lldb::pid_t detach_pid; 5449349cc55cSDimitry Andric lldb::tid_t detach_tid; 5450349cc55cSDimitry Andric 5451349cc55cSDimitry Andric switch (GetFollowForkMode()) { 5452349cc55cSDimitry Andric case eFollowParent: 5453349cc55cSDimitry Andric detach_pid = child_pid; 5454349cc55cSDimitry Andric detach_tid = child_tid; 5455349cc55cSDimitry Andric break; 5456349cc55cSDimitry Andric case eFollowChild: 5457349cc55cSDimitry Andric detach_pid = m_gdb_comm.GetCurrentProcessID(); 5458349cc55cSDimitry Andric // Any valid TID will suffice, thread-relevant actions will set a proper TID 5459349cc55cSDimitry Andric // anyway. 5460349cc55cSDimitry Andric detach_tid = m_thread_ids.front(); 5461349cc55cSDimitry Andric 5462349cc55cSDimitry Andric // Switch to the parent process before detaching it. 5463349cc55cSDimitry Andric if (!m_gdb_comm.SetCurrentThread(detach_tid, detach_pid)) { 5464349cc55cSDimitry Andric LLDB_LOG(log, "ProcessGDBRemote::DidFork() unable to set pid/tid"); 5465349cc55cSDimitry Andric return; 5466349cc55cSDimitry Andric } 5467349cc55cSDimitry Andric 5468349cc55cSDimitry Andric // Remove hardware breakpoints / watchpoints from the parent process. 5469349cc55cSDimitry Andric DidForkSwitchHardwareTraps(false); 5470349cc55cSDimitry Andric 5471349cc55cSDimitry Andric // Switch to the child process. 5472349cc55cSDimitry Andric if (!m_gdb_comm.SetCurrentThread(child_tid, child_pid) || 5473349cc55cSDimitry Andric !m_gdb_comm.SetCurrentThreadForRun(child_tid, child_pid)) { 5474349cc55cSDimitry Andric LLDB_LOG(log, "ProcessGDBRemote::DidFork() unable to reset pid/tid"); 5475349cc55cSDimitry Andric return; 5476349cc55cSDimitry Andric } 5477349cc55cSDimitry Andric break; 5478349cc55cSDimitry Andric } 5479349cc55cSDimitry Andric 5480349cc55cSDimitry Andric LLDB_LOG(log, "Detaching process {0}", detach_pid); 5481349cc55cSDimitry Andric Status error = m_gdb_comm.Detach(false, detach_pid); 5482349cc55cSDimitry Andric if (error.Fail()) { 5483349cc55cSDimitry Andric LLDB_LOG(log, 5484349cc55cSDimitry Andric "ProcessGDBRemote::DidFork() detach packet send failed: {0}", 5485349cc55cSDimitry Andric error.AsCString() ? error.AsCString() : "<unknown error>"); 5486349cc55cSDimitry Andric return; 5487349cc55cSDimitry Andric } 5488349cc55cSDimitry Andric } 5489349cc55cSDimitry Andric 5490349cc55cSDimitry Andric void ProcessGDBRemote::DidVForkDone() { 5491349cc55cSDimitry Andric assert(m_vfork_in_progress); 5492349cc55cSDimitry Andric m_vfork_in_progress = false; 5493349cc55cSDimitry Andric 5494349cc55cSDimitry Andric // Reenable all software breakpoints that were enabled before vfork. 5495349cc55cSDimitry Andric if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) 5496349cc55cSDimitry Andric DidForkSwitchSoftwareBreakpoints(true); 5497349cc55cSDimitry Andric } 5498349cc55cSDimitry Andric 5499349cc55cSDimitry Andric void ProcessGDBRemote::DidExec() { 5500349cc55cSDimitry Andric // If we are following children, vfork is finished by exec (rather than 5501349cc55cSDimitry Andric // vforkdone that is submitted for parent). 5502349cc55cSDimitry Andric if (GetFollowForkMode() == eFollowChild) 5503349cc55cSDimitry Andric m_vfork_in_progress = false; 5504349cc55cSDimitry Andric Process::DidExec(); 5505349cc55cSDimitry Andric } 5506