1 //===-- PlatformOpenBSD.cpp -------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "PlatformOpenBSD.h"
11 #include "lldb/Host/Config.h"
12
13 #include <stdio.h>
14 #ifndef LLDB_DISABLE_POSIX
15 #include <sys/utsname.h>
16 #endif
17
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/State.h"
26 #include "lldb/Utility/Status.h"
27 #include "lldb/Utility/StreamString.h"
28
29 // Define these constants from OpenBSD mman.h for use when targeting remote
30 // openbsd systems even when host has different values.
31 #define MAP_PRIVATE 0x0002
32 #define MAP_ANON 0x1000
33
34 using namespace lldb;
35 using namespace lldb_private;
36 using namespace lldb_private::platform_openbsd;
37
38 static uint32_t g_initialize_count = 0;
39
40 //------------------------------------------------------------------
41
CreateInstance(bool force,const ArchSpec * arch)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
GetPluginNameStatic(bool is_host)74 ConstString PlatformOpenBSD::GetPluginNameStatic(bool is_host) {
75 if (is_host) {
76 static ConstString g_host_name(Platform::GetHostPlatformName());
77 return g_host_name;
78 } else {
79 static ConstString g_remote_name("remote-openbsd");
80 return g_remote_name;
81 }
82 }
83
GetPluginDescriptionStatic(bool is_host)84 const char *PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) {
85 if (is_host)
86 return "Local OpenBSD user platform plug-in.";
87 else
88 return "Remote OpenBSD user platform plug-in.";
89 }
90
GetPluginName()91 ConstString PlatformOpenBSD::GetPluginName() {
92 return GetPluginNameStatic(IsHost());
93 }
94
Initialize()95 void PlatformOpenBSD::Initialize() {
96 Platform::Initialize();
97
98 if (g_initialize_count++ == 0) {
99 #if defined(__OpenBSD__)
100 PlatformSP default_platform_sp(new PlatformOpenBSD(true));
101 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
102 Platform::SetHostPlatform(default_platform_sp);
103 #endif
104 PluginManager::RegisterPlugin(
105 PlatformOpenBSD::GetPluginNameStatic(false),
106 PlatformOpenBSD::GetPluginDescriptionStatic(false),
107 PlatformOpenBSD::CreateInstance, nullptr);
108 }
109 }
110
Terminate()111 void PlatformOpenBSD::Terminate() {
112 if (g_initialize_count > 0) {
113 if (--g_initialize_count == 0) {
114 PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance);
115 }
116 }
117
118 PlatformPOSIX::Terminate();
119 }
120
121 //------------------------------------------------------------------
122 /// Default Constructor
123 //------------------------------------------------------------------
PlatformOpenBSD(bool is_host)124 PlatformOpenBSD::PlatformOpenBSD(bool is_host)
125 : PlatformPOSIX(is_host) // This is the local host platform
126 {}
127
128 PlatformOpenBSD::~PlatformOpenBSD() = default;
129
GetSupportedArchitectureAtIndex(uint32_t idx,ArchSpec & arch)130 bool PlatformOpenBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
131 ArchSpec &arch) {
132 if (IsHost()) {
133 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
134 if (hostArch.GetTriple().isOSOpenBSD()) {
135 if (idx == 0) {
136 arch = hostArch;
137 return arch.IsValid();
138 }
139 }
140 } else {
141 if (m_remote_platform_sp)
142 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
143
144 llvm::Triple triple;
145 // Set the OS to OpenBSD
146 triple.setOS(llvm::Triple::OpenBSD);
147 // Set the architecture
148 switch (idx) {
149 case 0:
150 triple.setArchName("x86_64");
151 break;
152 case 1:
153 triple.setArchName("i386");
154 break;
155 case 2:
156 triple.setArchName("aarch64");
157 break;
158 case 3:
159 triple.setArchName("arm");
160 break;
161 default:
162 return false;
163 }
164 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
165 // vendor by calling triple.SetVendorName("unknown") so that it is a
166 // "unspecified unknown". This means when someone calls
167 // triple.GetVendorName() it will return an empty string which indicates
168 // that the vendor can be set when two architectures are merged
169
170 // Now set the triple into "arch" and return true
171 arch.SetTriple(triple);
172 return true;
173 }
174 return false;
175 }
176
GetStatus(Stream & strm)177 void PlatformOpenBSD::GetStatus(Stream &strm) {
178 Platform::GetStatus(strm);
179
180 #ifndef LLDB_DISABLE_POSIX
181 // Display local kernel information only when we are running in host mode.
182 // Otherwise, we would end up printing non-OpenBSD information (when running
183 // on Mac OS for example).
184 if (IsHost()) {
185 struct utsname un;
186
187 if (uname(&un))
188 return;
189
190 strm.Printf(" Kernel: %s\n", un.sysname);
191 strm.Printf(" Release: %s\n", un.release);
192 strm.Printf(" Version: %s\n", un.version);
193 }
194 #endif
195 }
196
197 // OpenBSD processes cannot yet be launched by spawning and attaching.
CanDebugProcess()198 bool PlatformOpenBSD::CanDebugProcess() {
199 return false;
200 }
201
CalculateTrapHandlerSymbolNames()202 void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() {
203 m_trap_handlers.push_back(ConstString("_sigtramp"));
204 }
205
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)206 MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch,
207 addr_t addr, addr_t length,
208 unsigned prot, unsigned flags,
209 addr_t fd, addr_t offset) {
210 uint64_t flags_platform = 0;
211
212 if (flags & eMmapFlagsPrivate)
213 flags_platform |= MAP_PRIVATE;
214 if (flags & eMmapFlagsAnon)
215 flags_platform |= MAP_ANON;
216
217 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
218 return args;
219 }
220