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/LLDBLog.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/State.h"
26 #include "lldb/Utility/Status.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 
39 
41 
42 
43 PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) {
44  Log *log = GetLog(LLDBLog::Platform);
45  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
46  arch ? arch->GetArchitectureName() : "<null>",
47  arch ? arch->GetTriple().getTriple() : "<null>");
48 
49  bool create = force;
50  if (!create && arch && arch->IsValid()) {
51  const llvm::Triple &triple = arch->GetTriple();
52  switch (triple.getOS()) {
53  case llvm::Triple::OpenBSD:
54  create = true;
55  break;
56 
57 #if defined(__OpenBSD__)
58  // Only accept "unknown" for the OS if the host is BSD and it "unknown"
59  // wasn't specified (it was just returned because it was NOT specified)
60  case llvm::Triple::OSType::UnknownOS:
61  create = !arch->TripleOSWasSpecified();
62  break;
63 #endif
64  default:
65  break;
66  }
67  }
68  LLDB_LOG(log, "create = {0}", create);
69  if (create) {
70  return PlatformSP(new PlatformOpenBSD(false));
71  }
72  return PlatformSP();
73 }
74 
75 llvm::StringRef PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) {
76  if (is_host)
77  return "Local OpenBSD user platform plug-in.";
78  return "Remote OpenBSD user platform plug-in.";
79 }
80 
81 void PlatformOpenBSD::Initialize() {
82  Platform::Initialize();
83 
84  if (g_initialize_count++ == 0) {
85 #if defined(__OpenBSD__)
86  PlatformSP default_platform_sp(new PlatformOpenBSD(true));
87  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
88  Platform::SetHostPlatform(default_platform_sp);
89 #endif
90  PluginManager::RegisterPlugin(
91  PlatformOpenBSD::GetPluginNameStatic(false),
92  PlatformOpenBSD::GetPluginDescriptionStatic(false),
93  PlatformOpenBSD::CreateInstance, nullptr);
94  }
95 }
96 
97 void PlatformOpenBSD::Terminate() {
98  if (g_initialize_count > 0) {
99  if (--g_initialize_count == 0) {
100  PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance);
101  }
102  }
103 
105 }
106 
107 /// Default Constructor
108 PlatformOpenBSD::PlatformOpenBSD(bool is_host)
109  : PlatformPOSIX(is_host) // This is the local host platform
110 {
111  if (is_host) {
112  m_supported_architectures.push_back(HostInfo::GetArchitecture());
113  } else {
115  CreateArchList({llvm::Triple::x86_64, llvm::Triple::x86,
116  llvm::Triple::aarch64, llvm::Triple::arm},
117  llvm::Triple::OpenBSD);
118  }
119 }
120 
121 std::vector<ArchSpec>
124  return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
126 }
127 
129  Platform::GetStatus(strm);
130 
131 #if LLDB_ENABLE_POSIX
132  // Display local kernel information only when we are running in host mode.
133  // Otherwise, we would end up printing non-OpenBSD information (when running
134  // on Mac OS for example).
135  if (IsHost()) {
136  struct utsname un;
137 
138  if (uname(&un))
139  return;
140 
141  strm.Printf(" Kernel: %s\n", un.sysname);
142  strm.Printf(" Release: %s\n", un.release);
143  strm.Printf(" Version: %s\n", un.version);
144  }
145 #endif
146 }
147 
148 // OpenBSD processes cannot yet be launched by spawning and attaching.
150  return false;
151 }
152 
154  m_trap_handlers.push_back(ConstString("_sigtramp"));
155 }
156 
158  addr_t addr, addr_t length,
159  unsigned prot, unsigned flags,
160  addr_t fd, addr_t offset) {
161  uint64_t flags_platform = 0;
162 
163  if (flags & eMmapFlagsPrivate)
164  flags_platform |= MAP_PRIVATE;
165  if (flags & eMmapFlagsAnon)
166  flags_platform |= MAP_ANON;
167 
168  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
169  return args;
170 }
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformOpenBSD.cpp:40
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:58
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:157
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:153
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:897
lldb_private::Stream
Definition: Stream.h:28
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::platform_openbsd::PlatformOpenBSD::m_supported_architectures
std::vector< ArchSpec > m_supported_architectures
Definition: PlatformOpenBSD.h:57
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:128
MAP_ANON
#define MAP_ANON
Definition: PlatformOpenBSD.cpp:32
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:140
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:149
HostInfo.h
lldb_private::platform_openbsd::PlatformOpenBSD::GetSupportedArchitectures
std::vector< ArchSpec > GetSupportedArchitectures(const ArchSpec &process_host_arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformOpenBSD.cpp:122
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:336
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:31
Status.h
lldb_private::Platform::CreateArchList
static std::vector< ArchSpec > CreateArchList(llvm::ArrayRef< llvm::Triple::ArchType > archs, llvm::Triple::OSType os)
Create a list of ArchSpecs with the given OS and a architectures.
Definition: Platform.cpp:1144
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:285
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:115
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:420
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb
Definition: SBAddress.h:15
LLDBLog.h