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/LLDBLog.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
34using namespace lldb;
35using namespace lldb_private;
36using namespace lldb_private::platform_openbsd;
37
38LLDB_PLUGIN_DEFINE(PlatformOpenBSD)
39
40static uint32_t g_initialize_count = 0;
41
42
43PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) {
44 Log *log = GetLog(mask: 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
75llvm::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
81void 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 name: PlatformOpenBSD::GetPluginNameStatic(is_host: false),
92 description: PlatformOpenBSD::GetPluginDescriptionStatic(is_host: false),
93 create_callback: PlatformOpenBSD::CreateInstance, debugger_init_callback: nullptr);
94 }
95}
96
97void PlatformOpenBSD::Terminate() {
98 if (g_initialize_count > 0) {
99 if (--g_initialize_count == 0) {
100 PluginManager::UnregisterPlugin(create_callback: PlatformOpenBSD::CreateInstance);
101 }
102 }
103
104 PlatformPOSIX::Terminate();
105}
106
107/// Default Constructor
108PlatformOpenBSD::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(x: HostInfo::GetArchitecture());
113 } else {
114 m_supported_architectures =
115 CreateArchList(archs: {llvm::Triple::x86_64, llvm::Triple::x86,
116 llvm::Triple::aarch64, llvm::Triple::arm},
117 os: llvm::Triple::OpenBSD);
118 }
119}
120
121std::vector<ArchSpec>
122PlatformOpenBSD::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
123 if (m_remote_platform_sp)
124 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
125 return m_supported_architectures;
126}
127
128void PlatformOpenBSD::GetStatus(Stream &strm) {
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(name: &un))
139 return;
140
141 strm.Printf(format: " Kernel: %s\n", un.sysname);
142 strm.Printf(format: " Release: %s\n", un.release);
143 strm.Printf(format: " Version: %s\n", un.version);
144 }
145#endif
146}
147
148// OpenBSD processes cannot yet be launched by spawning and attaching.
149bool PlatformOpenBSD::CanDebugProcess() {
150 return false;
151}
152
153void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() {
154 m_trap_handlers.push_back(x: ConstString("_sigtramp"));
155}
156
157MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch,
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}
171

source code of lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp