1 //===-- PlatformOpenBSD.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 "PlatformOpenBSD.h" 10 #include "lldb/Host/Config.h" 11 12 #include <cstdio> 13 #if LLDB_ENABLE_POSIX 14 #include <sys/utsname.h> 15 #endif 16 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Host/HostInfo.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Utility/FileSpec.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/State.h" 25 #include "lldb/Utility/Status.h" 26 #include "lldb/Utility/StreamString.h" 27 28 // Define these constants from OpenBSD mman.h for use when targeting remote 29 // openbsd systems even when host has different values. 30 #define MAP_PRIVATE 0x0002 31 #define MAP_ANON 0x1000 32 33 using namespace lldb; 34 using namespace lldb_private; 35 using namespace lldb_private::platform_openbsd; 36 37 LLDB_PLUGIN_DEFINE(PlatformOpenBSD) 38 39 static uint32_t g_initialize_count = 0; 40 41 42 PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) { 43 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 44 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 45 arch ? arch->GetArchitectureName() : "<null>", 46 arch ? arch->GetTriple().getTriple() : "<null>"); 47 48 bool create = force; 49 if (!create && arch && arch->IsValid()) { 50 const llvm::Triple &triple = arch->GetTriple(); 51 switch (triple.getOS()) { 52 case llvm::Triple::OpenBSD: 53 create = true; 54 break; 55 56 #if defined(__OpenBSD__) 57 // Only accept "unknown" for the OS if the host is BSD and it "unknown" 58 // wasn't specified (it was just returned because it was NOT specified) 59 case llvm::Triple::OSType::UnknownOS: 60 create = !arch->TripleOSWasSpecified(); 61 break; 62 #endif 63 default: 64 break; 65 } 66 } 67 LLDB_LOG(log, "create = {0}", create); 68 if (create) { 69 return PlatformSP(new PlatformOpenBSD(false)); 70 } 71 return PlatformSP(); 72 } 73 74 llvm::StringRef PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) { 75 if (is_host) 76 return "Local OpenBSD user platform plug-in."; 77 return "Remote OpenBSD user platform plug-in."; 78 } 79 80 void PlatformOpenBSD::Initialize() { 81 Platform::Initialize(); 82 83 if (g_initialize_count++ == 0) { 84 #if defined(__OpenBSD__) 85 PlatformSP default_platform_sp(new PlatformOpenBSD(true)); 86 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 87 Platform::SetHostPlatform(default_platform_sp); 88 #endif 89 PluginManager::RegisterPlugin( 90 PlatformOpenBSD::GetPluginNameStatic(false), 91 PlatformOpenBSD::GetPluginDescriptionStatic(false), 92 PlatformOpenBSD::CreateInstance, nullptr); 93 } 94 } 95 96 void PlatformOpenBSD::Terminate() { 97 if (g_initialize_count > 0) { 98 if (--g_initialize_count == 0) { 99 PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance); 100 } 101 } 102 103 PlatformPOSIX::Terminate(); 104 } 105 106 /// Default Constructor 107 PlatformOpenBSD::PlatformOpenBSD(bool is_host) 108 : PlatformPOSIX(is_host) // This is the local host platform 109 {} 110 111 bool PlatformOpenBSD::GetSupportedArchitectureAtIndex(uint32_t idx, 112 ArchSpec &arch) { 113 if (IsHost()) { 114 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 115 if (hostArch.GetTriple().isOSOpenBSD()) { 116 if (idx == 0) { 117 arch = hostArch; 118 return arch.IsValid(); 119 } 120 } 121 } else { 122 if (m_remote_platform_sp) 123 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 124 125 llvm::Triple triple; 126 // Set the OS to OpenBSD 127 triple.setOS(llvm::Triple::OpenBSD); 128 // Set the architecture 129 switch (idx) { 130 case 0: 131 triple.setArchName("x86_64"); 132 break; 133 case 1: 134 triple.setArchName("i386"); 135 break; 136 case 2: 137 triple.setArchName("aarch64"); 138 break; 139 case 3: 140 triple.setArchName("arm"); 141 break; 142 default: 143 return false; 144 } 145 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 146 // vendor by calling triple.SetVendorName("unknown") so that it is a 147 // "unspecified unknown". This means when someone calls 148 // triple.GetVendorName() it will return an empty string which indicates 149 // that the vendor can be set when two architectures are merged 150 151 // Now set the triple into "arch" and return true 152 arch.SetTriple(triple); 153 return true; 154 } 155 return false; 156 } 157 158 void PlatformOpenBSD::GetStatus(Stream &strm) { 159 Platform::GetStatus(strm); 160 161 #if LLDB_ENABLE_POSIX 162 // Display local kernel information only when we are running in host mode. 163 // Otherwise, we would end up printing non-OpenBSD information (when running 164 // on Mac OS for example). 165 if (IsHost()) { 166 struct utsname un; 167 168 if (uname(&un)) 169 return; 170 171 strm.Printf(" Kernel: %s\n", un.sysname); 172 strm.Printf(" Release: %s\n", un.release); 173 strm.Printf(" Version: %s\n", un.version); 174 } 175 #endif 176 } 177 178 // OpenBSD processes cannot yet be launched by spawning and attaching. 179 bool PlatformOpenBSD::CanDebugProcess() { 180 return false; 181 } 182 183 void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() { 184 m_trap_handlers.push_back(ConstString("_sigtramp")); 185 } 186 187 MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch, 188 addr_t addr, addr_t length, 189 unsigned prot, unsigned flags, 190 addr_t fd, addr_t offset) { 191 uint64_t flags_platform = 0; 192 193 if (flags & eMmapFlagsPrivate) 194 flags_platform |= MAP_PRIVATE; 195 if (flags & eMmapFlagsAnon) 196 flags_platform |= MAP_ANON; 197 198 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 199 return args; 200 } 201