1 //===-- PlatformQemuUser.cpp ----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Plugins/Platform/QemuUser/PlatformQemuUser.h" 10 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h" 11 #include "lldb/Core/PluginManager.h" 12 #include "lldb/Host/FileSystem.h" 13 #include "lldb/Host/ProcessLaunchInfo.h" 14 #include "lldb/Interpreter/OptionValueProperties.h" 15 #include "lldb/Target/Process.h" 16 #include "lldb/Target/Target.h" 17 #include "lldb/Utility/Listener.h" 18 #include "lldb/Utility/Log.h" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 23 LLDB_PLUGIN_DEFINE(PlatformQemuUser) 24 25 #define LLDB_PROPERTIES_platformqemuuser 26 #include "PlatformQemuUserProperties.inc" 27 28 enum { 29 #define LLDB_PROPERTIES_platformqemuuser 30 #include "PlatformQemuUserPropertiesEnum.inc" 31 }; 32 33 class PluginProperties : public Properties { 34 public: 35 PluginProperties() { 36 m_collection_sp = std::make_shared<OptionValueProperties>( 37 ConstString(PlatformQemuUser::GetPluginNameStatic())); 38 m_collection_sp->Initialize(g_platformqemuuser_properties); 39 } 40 41 llvm::StringRef GetArchitecture() { 42 return m_collection_sp->GetPropertyAtIndexAsString( 43 nullptr, ePropertyArchitecture, ""); 44 } 45 46 FileSpec GetEmulatorPath() { 47 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, 48 ePropertyEmulatorPath); 49 } 50 }; 51 52 static PluginProperties &GetGlobalProperties() { 53 static PluginProperties g_settings; 54 return g_settings; 55 } 56 57 llvm::StringRef PlatformQemuUser::GetPluginDescriptionStatic() { 58 return "Platform for debugging binaries under user mode qemu"; 59 } 60 61 void PlatformQemuUser::Initialize() { 62 PluginManager::RegisterPlugin( 63 GetPluginNameStatic(), GetPluginDescriptionStatic(), 64 PlatformQemuUser::CreateInstance, PlatformQemuUser::DebuggerInitialize); 65 } 66 67 void PlatformQemuUser::Terminate() { 68 PluginManager::UnregisterPlugin(PlatformQemuUser::CreateInstance); 69 } 70 71 void PlatformQemuUser::DebuggerInitialize(Debugger &debugger) { 72 if (!PluginManager::GetSettingForPlatformPlugin( 73 debugger, ConstString(GetPluginNameStatic()))) { 74 PluginManager::CreateSettingForPlatformPlugin( 75 debugger, GetGlobalProperties().GetValueProperties(), 76 ConstString("Properties for the qemu-user platform plugin."), 77 /*is_global_property=*/true); 78 } 79 } 80 81 PlatformSP PlatformQemuUser::CreateInstance(bool force, const ArchSpec *arch) { 82 if (force) 83 return PlatformSP(new PlatformQemuUser()); 84 return nullptr; 85 } 86 87 std::vector<ArchSpec> PlatformQemuUser::GetSupportedArchitectures() { 88 llvm::Triple triple = HostInfo::GetArchitecture().GetTriple(); 89 triple.setEnvironment(llvm::Triple::UnknownEnvironment); 90 triple.setArchName(GetGlobalProperties().GetArchitecture()); 91 if (triple.getArch() != llvm::Triple::UnknownArch) 92 return {ArchSpec(triple)}; 93 return {}; 94 } 95 96 static auto get_arg_range(const Args &args) { 97 return llvm::make_range(args.GetArgumentArrayRef().begin(), 98 args.GetArgumentArrayRef().end()); 99 } 100 101 lldb::ProcessSP PlatformQemuUser::DebugProcess(ProcessLaunchInfo &launch_info, 102 Debugger &debugger, 103 Target &target, Status &error) { 104 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 105 106 std::string qemu = GetGlobalProperties().GetEmulatorPath().GetPath(); 107 108 llvm::SmallString<0> socket_model, socket_path; 109 HostInfo::GetProcessTempDir().GetPath(socket_model); 110 llvm::sys::path::append(socket_model, "qemu-%%%%%%%%.socket"); 111 do { 112 llvm::sys::fs::createUniquePath(socket_model, socket_path, false); 113 } while (FileSystem::Instance().Exists(socket_path)); 114 115 Args args( 116 {qemu, "-g", socket_path, launch_info.GetExecutableFile().GetPath()}); 117 for (size_t i = 1; i < launch_info.GetArguments().size(); ++i) 118 args.AppendArgument(launch_info.GetArguments()[i].ref()); 119 120 LLDB_LOG(log, "{0} -> {1}", get_arg_range(launch_info.GetArguments()), 121 get_arg_range(args)); 122 123 launch_info.SetArguments(args, true); 124 launch_info.SetLaunchInSeparateProcessGroup(true); 125 launch_info.GetFlags().Clear(eLaunchFlagDebug); 126 launch_info.SetMonitorProcessCallback(ProcessLaunchInfo::NoOpMonitorCallback, 127 false); 128 129 error = Host::LaunchProcess(launch_info); 130 if (error.Fail()) 131 return nullptr; 132 133 ProcessSP process_sp = target.CreateProcess( 134 launch_info.GetListener(), 135 process_gdb_remote::ProcessGDBRemote::GetPluginNameStatic(), nullptr, 136 true); 137 ListenerSP listener_sp = 138 Listener::MakeListener("lldb.platform_qemu_user.debugprocess"); 139 launch_info.SetHijackListener(listener_sp); 140 Process::ProcessEventHijacker hijacker(*process_sp, listener_sp); 141 142 error = process_sp->ConnectRemote(("unix-connect://" + socket_path).str()); 143 if (error.Fail()) 144 return nullptr; 145 146 process_sp->WaitForProcessToStop(llvm::None, nullptr, false, listener_sp); 147 return process_sp; 148 } 149