1 //===-- PlatformOpenBSD.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformOpenBSD.h"
11 #include "lldb/Host/Config.h"
15 #ifndef LLDB_DISABLE_POSIX
16 #include <sys/utsname.h>
20 // Other libraries and framework includes
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/State.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/StreamString.h"
33 // Define these constants from OpenBSD mman.h for use when targeting remote
34 // openbsd systems even when host has different values.
35 #define MAP_PRIVATE 0x0002
36 #define MAP_ANON 0x1000
39 using namespace lldb_private;
40 using namespace lldb_private::platform_openbsd;
42 static uint32_t g_initialize_count = 0;
44 //------------------------------------------------------------------
46 PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) {
47 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
48 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
49 arch ? arch->GetArchitectureName() : "<null>",
50 arch ? arch->GetTriple().getTriple() : "<null>");
53 if (create == false && arch && arch->IsValid()) {
54 const llvm::Triple &triple = arch->GetTriple();
55 switch (triple.getOS()) {
56 case llvm::Triple::OpenBSD:
60 #if defined(__OpenBSD__)
61 // Only accept "unknown" for the OS if the host is BSD and it "unknown"
62 // wasn't specified (it was just returned because it was NOT specified)
63 case llvm::Triple::OSType::UnknownOS:
64 create = !arch->TripleOSWasSpecified();
71 LLDB_LOG(log, "create = {0}", create);
73 return PlatformSP(new PlatformOpenBSD(false));
78 ConstString PlatformOpenBSD::GetPluginNameStatic(bool is_host) {
80 static ConstString g_host_name(Platform::GetHostPlatformName());
83 static ConstString g_remote_name("remote-openbsd");
88 const char *PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) {
90 return "Local OpenBSD user platform plug-in.";
92 return "Remote OpenBSD user platform plug-in.";
95 ConstString PlatformOpenBSD::GetPluginName() {
96 return GetPluginNameStatic(IsHost());
99 void PlatformOpenBSD::Initialize() {
100 Platform::Initialize();
102 if (g_initialize_count++ == 0) {
103 #if defined(__OpenBSD__)
104 PlatformSP default_platform_sp(new PlatformOpenBSD(true));
105 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
106 Platform::SetHostPlatform(default_platform_sp);
108 PluginManager::RegisterPlugin(
109 PlatformOpenBSD::GetPluginNameStatic(false),
110 PlatformOpenBSD::GetPluginDescriptionStatic(false),
111 PlatformOpenBSD::CreateInstance, nullptr);
115 void PlatformOpenBSD::Terminate() {
116 if (g_initialize_count > 0) {
117 if (--g_initialize_count == 0) {
118 PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance);
122 PlatformPOSIX::Terminate();
125 //------------------------------------------------------------------
126 /// Default Constructor
127 //------------------------------------------------------------------
128 PlatformOpenBSD::PlatformOpenBSD(bool is_host)
129 : PlatformPOSIX(is_host) // This is the local host platform
132 PlatformOpenBSD::~PlatformOpenBSD() = default;
134 bool PlatformOpenBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
137 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
138 if (hostArch.GetTriple().isOSOpenBSD()) {
141 return arch.IsValid();
145 if (m_remote_platform_sp)
146 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
149 // Set the OS to OpenBSD
150 triple.setOS(llvm::Triple::OpenBSD);
151 // Set the architecture
154 triple.setArchName("x86_64");
157 triple.setArchName("i386");
160 triple.setArchName("aarch64");
163 triple.setArchName("arm");
168 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
169 // vendor by calling triple.SetVendorName("unknown") so that it is a
170 // "unspecified unknown". This means when someone calls
171 // triple.GetVendorName() it will return an empty string which indicates
172 // that the vendor can be set when two architectures are merged
174 // Now set the triple into "arch" and return true
175 arch.SetTriple(triple);
181 void PlatformOpenBSD::GetStatus(Stream &strm) {
182 Platform::GetStatus(strm);
184 #ifndef LLDB_DISABLE_POSIX
185 // Display local kernel information only when we are running in host mode.
186 // Otherwise, we would end up printing non-OpenBSD information (when running
187 // on Mac OS for example).
194 strm.Printf(" Kernel: %s\n", un.sysname);
195 strm.Printf(" Release: %s\n", un.release);
196 strm.Printf(" Version: %s\n", un.version);
201 // OpenBSD processes cannot yet be launched by spawning and attaching.
202 bool PlatformOpenBSD::CanDebugProcess() {
206 void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() {
207 m_trap_handlers.push_back(ConstString("_sigtramp"));
210 MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch,
211 addr_t addr, addr_t length,
212 unsigned prot, unsigned flags,
213 addr_t fd, addr_t offset) {
214 uint64_t flags_platform = 0;
216 if (flags & eMmapFlagsPrivate)
217 flags_platform |= MAP_PRIVATE;
218 if (flags & eMmapFlagsAnon)
219 flags_platform |= MAP_ANON;
221 MmapArgList args({addr, length, prot, flags_platform, fd, offset});