LLDB  mainline
PlatformOpenBSD.cpp
Go to the documentation of this file.
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"
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"
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 
38 
40 
41 
42 PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) {
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 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 
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 
91 ConstString PlatformOpenBSD::GetPluginName() {
92  return GetPluginNameStatic(IsHost());
93 }
94 
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 
111 void PlatformOpenBSD::Terminate() {
112  if (g_initialize_count > 0) {
113  if (--g_initialize_count == 0) {
114  PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance);
115  }
116  }
117 
119 }
120 
121 /// Default Constructor
122 PlatformOpenBSD::PlatformOpenBSD(bool is_host)
123  : PlatformPOSIX(is_host) // This is the local host platform
124 {}
125 
127  ArchSpec &arch) {
128  if (IsHost()) {
129  ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
130  if (hostArch.GetTriple().isOSOpenBSD()) {
131  if (idx == 0) {
132  arch = hostArch;
133  return arch.IsValid();
134  }
135  }
136  } else {
138  return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
139 
140  llvm::Triple triple;
141  // Set the OS to OpenBSD
142  triple.setOS(llvm::Triple::OpenBSD);
143  // Set the architecture
144  switch (idx) {
145  case 0:
146  triple.setArchName("x86_64");
147  break;
148  case 1:
149  triple.setArchName("i386");
150  break;
151  case 2:
152  triple.setArchName("aarch64");
153  break;
154  case 3:
155  triple.setArchName("arm");
156  break;
157  default:
158  return false;
159  }
160  // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
161  // vendor by calling triple.SetVendorName("unknown") so that it is a
162  // "unspecified unknown". This means when someone calls
163  // triple.GetVendorName() it will return an empty string which indicates
164  // that the vendor can be set when two architectures are merged
165 
166  // Now set the triple into "arch" and return true
167  arch.SetTriple(triple);
168  return true;
169  }
170  return false;
171 }
172 
174  Platform::GetStatus(strm);
175 
176 #if LLDB_ENABLE_POSIX
177  // Display local kernel information only when we are running in host mode.
178  // Otherwise, we would end up printing non-OpenBSD information (when running
179  // on Mac OS for example).
180  if (IsHost()) {
181  struct utsname un;
182 
183  if (uname(&un))
184  return;
185 
186  strm.Printf(" Kernel: %s\n", un.sysname);
187  strm.Printf(" Release: %s\n", un.release);
188  strm.Printf(" Version: %s\n", un.version);
189  }
190 #endif
191 }
192 
193 // OpenBSD processes cannot yet be launched by spawning and attaching.
195  return false;
196 }
197 
199  m_trap_handlers.push_back(ConstString("_sigtramp"));
200 }
201 
203  addr_t addr, addr_t length,
204  unsigned prot, unsigned flags,
205  addr_t fd, addr_t offset) {
206  uint64_t flags_platform = 0;
207 
208  if (flags & eMmapFlagsPrivate)
209  flags_platform |= MAP_PRIVATE;
210  if (flags & eMmapFlagsAnon)
211  flags_platform |= MAP_ANON;
212 
213  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
214  return args;
215 }
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformOpenBSD.cpp:39
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:59
PlatformOpenBSD.h
lldb_private::platform_openbsd::PlatformOpenBSD::GetMmapArgumentList
MmapArgList GetMmapArgumentList(const ArchSpec &arch, lldb::addr_t addr, lldb::addr_t length, unsigned prot, unsigned flags, lldb::addr_t fd, lldb::addr_t offset) override
Definition: PlatformOpenBSD.cpp:202
LIBLLDB_LOG_PLATFORM
#define LIBLLDB_LOG_PLATFORM
Definition: Logging.h:39
lldb_private::platform_openbsd::PlatformOpenBSD
Definition: PlatformOpenBSD.h:17
lldb_private::platform_openbsd::PlatformOpenBSD::CalculateTrapHandlerSymbolNames
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
Definition: PlatformOpenBSD.cpp:198
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:903
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
Debugger.h
Process.h
Target.h
PlatformPOSIX
Definition: PlatformPOSIX.h:18
Log.h
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:739
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb_private::platform_openbsd::PlatformOpenBSD::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformOpenBSD.cpp:173
MAP_ANON
#define MAP_ANON
Definition: PlatformOpenBSD.cpp:31
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:149
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::platform_openbsd::PlatformOpenBSD::CanDebugProcess
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
Definition: PlatformOpenBSD.cpp:194
HostInfo.h
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
lldb_private::platform_openbsd::PlatformOpenBSD::GetSupportedArchitectureAtIndex
bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformOpenBSD.cpp:126
lldb_private::platform_openbsd
Definition: PlatformOpenBSD.h:15
uint32_t
PluginManager.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformOpenBSD.cpp:30
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::eMmapFlagsAnon
@ eMmapFlagsAnon
Definition: Platform.h:40
FileSpec.h
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:407
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:440
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb
Definition: SBAddress.h:15