1//===-- PlatformFreeBSD.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 "PlatformFreeBSD.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/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/BreakpointSite.h"
19#include "lldb/Core/Debugger.h"
20#include "lldb/Core/PluginManager.h"
21#include "lldb/Host/HostInfo.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Utility/FileSpec.h"
25#include "lldb/Utility/LLDBLog.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/State.h"
28#include "lldb/Utility/Status.h"
29#include "lldb/Utility/StreamString.h"
30
31#include "llvm/TargetParser/Host.h"
32#include "llvm/TargetParser/Triple.h"
33
34// Define these constants from FreeBSD mman.h for use when targeting remote
35// FreeBSD systems even when host has different values.
36#define MAP_PRIVATE 0x0002
37#define MAP_ANON 0x1000
38
39using namespace lldb;
40using namespace lldb_private;
41using namespace lldb_private::platform_freebsd;
42
43LLDB_PLUGIN_DEFINE(PlatformFreeBSD)
44
45static uint32_t g_initialize_count = 0;
46
47
48PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
49 Log *log = GetLog(mask: LLDBLog::Platform);
50 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
51 arch ? arch->GetArchitectureName() : "<null>",
52 arch ? arch->GetTriple().getTriple() : "<null>");
53
54 bool create = force;
55 if (!create && arch && arch->IsValid()) {
56 const llvm::Triple &triple = arch->GetTriple();
57 switch (triple.getOS()) {
58 case llvm::Triple::FreeBSD:
59 create = true;
60 break;
61
62#if defined(__FreeBSD__)
63 // Only accept "unknown" for the OS if the host is BSD and it "unknown"
64 // wasn't specified (it was just returned because it was NOT specified)
65 case llvm::Triple::OSType::UnknownOS:
66 create = !arch->TripleOSWasSpecified();
67 break;
68#endif
69 default:
70 break;
71 }
72 }
73 LLDB_LOG(log, "create = {0}", create);
74 if (create) {
75 return PlatformSP(new PlatformFreeBSD(false));
76 }
77 return PlatformSP();
78}
79
80llvm::StringRef PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
81 if (is_host)
82 return "Local FreeBSD user platform plug-in.";
83 return "Remote FreeBSD user platform plug-in.";
84}
85
86void PlatformFreeBSD::Initialize() {
87 Platform::Initialize();
88
89 if (g_initialize_count++ == 0) {
90#if defined(__FreeBSD__)
91 PlatformSP default_platform_sp(new PlatformFreeBSD(true));
92 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
93 Platform::SetHostPlatform(default_platform_sp);
94#endif
95 PluginManager::RegisterPlugin(
96 name: PlatformFreeBSD::GetPluginNameStatic(is_host: false),
97 description: PlatformFreeBSD::GetPluginDescriptionStatic(is_host: false),
98 create_callback: PlatformFreeBSD::CreateInstance, debugger_init_callback: nullptr);
99 }
100}
101
102void PlatformFreeBSD::Terminate() {
103 if (g_initialize_count > 0) {
104 if (--g_initialize_count == 0) {
105 PluginManager::UnregisterPlugin(create_callback: PlatformFreeBSD::CreateInstance);
106 }
107 }
108
109 PlatformPOSIX::Terminate();
110}
111
112/// Default Constructor
113PlatformFreeBSD::PlatformFreeBSD(bool is_host)
114 : PlatformPOSIX(is_host) // This is the local host platform
115{
116 if (is_host) {
117 ArchSpec hostArch = HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKindDefault);
118 m_supported_architectures.push_back(x: hostArch);
119 if (hostArch.GetTriple().isArch64Bit()) {
120 m_supported_architectures.push_back(
121 x: HostInfo::GetArchitecture(arch_kind: HostInfo::eArchKind32));
122 }
123 } else {
124 m_supported_architectures = CreateArchList(
125 archs: {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::aarch64,
126 llvm::Triple::arm, llvm::Triple::mips64, llvm::Triple::ppc64,
127 llvm::Triple::ppc},
128 os: llvm::Triple::FreeBSD);
129 }
130}
131
132std::vector<ArchSpec>
133PlatformFreeBSD::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
134 if (m_remote_platform_sp)
135 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
136 return m_supported_architectures;
137}
138
139void PlatformFreeBSD::GetStatus(Stream &strm) {
140 Platform::GetStatus(strm);
141
142#if LLDB_ENABLE_POSIX
143 // Display local kernel information only when we are running in host mode.
144 // Otherwise, we would end up printing non-FreeBSD information (when running
145 // on Mac OS for example).
146 if (IsHost()) {
147 struct utsname un;
148
149 if (uname(name: &un))
150 return;
151
152 strm.Printf(format: " Kernel: %s\n", un.sysname);
153 strm.Printf(format: " Release: %s\n", un.release);
154 strm.Printf(format: " Version: %s\n", un.version);
155 }
156#endif
157}
158
159bool PlatformFreeBSD::CanDebugProcess() {
160 if (IsHost()) {
161 return true;
162 } else {
163 // If we're connected, we can debug.
164 return IsConnected();
165 }
166}
167
168void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
169 m_trap_handlers.push_back(x: ConstString("_sigtramp"));
170}
171
172MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
173 addr_t addr, addr_t length,
174 unsigned prot, unsigned flags,
175 addr_t fd, addr_t offset) {
176 uint64_t flags_platform = 0;
177
178 if (flags & eMmapFlagsPrivate)
179 flags_platform |= MAP_PRIVATE;
180 if (flags & eMmapFlagsAnon)
181 flags_platform |= MAP_ANON;
182
183 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
184 if (arch.GetTriple().getArch() == llvm::Triple::x86)
185 args.push_back(Elt: 0);
186 return args;
187}
188
189CompilerType PlatformFreeBSD::GetSiginfoType(const llvm::Triple &triple) {
190 {
191 std::lock_guard<std::mutex> guard(m_mutex);
192 if (!m_type_system)
193 m_type_system = std::make_shared<TypeSystemClang>(args: "siginfo", args: triple);
194 }
195 TypeSystemClang *ast = m_type_system.get();
196
197 // generic types
198 CompilerType int_type = ast->GetBasicType(type: eBasicTypeInt);
199 CompilerType uint_type = ast->GetBasicType(type: eBasicTypeUnsignedInt);
200 CompilerType long_type = ast->GetBasicType(type: eBasicTypeLong);
201 CompilerType voidp_type = ast->GetBasicType(type: eBasicTypeVoid).GetPointerType();
202
203 // platform-specific types
204 CompilerType &pid_type = int_type;
205 CompilerType &uid_type = uint_type;
206
207 CompilerType sigval_type = ast->CreateRecordType(
208 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "__lldb_sigval_t",
209 kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeC);
210 ast->StartTagDeclarationDefinition(type: sigval_type);
211 ast->AddFieldToRecordType(type: sigval_type, name: "sival_int", field_type: int_type,
212 access: lldb::eAccessPublic, bitfield_bit_size: 0);
213 ast->AddFieldToRecordType(type: sigval_type, name: "sival_ptr", field_type: voidp_type,
214 access: lldb::eAccessPublic, bitfield_bit_size: 0);
215 ast->CompleteTagDeclarationDefinition(type: sigval_type);
216
217 // siginfo_t
218 CompilerType siginfo_type = ast->CreateRecordType(
219 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "__lldb_siginfo_t",
220 kind: llvm::to_underlying(E: clang::TagTypeKind::Struct), language: lldb::eLanguageTypeC);
221 ast->StartTagDeclarationDefinition(type: siginfo_type);
222 ast->AddFieldToRecordType(type: siginfo_type, name: "si_signo", field_type: int_type,
223 access: lldb::eAccessPublic, bitfield_bit_size: 0);
224 ast->AddFieldToRecordType(type: siginfo_type, name: "si_errno", field_type: int_type,
225 access: lldb::eAccessPublic, bitfield_bit_size: 0);
226 ast->AddFieldToRecordType(type: siginfo_type, name: "si_code", field_type: int_type,
227 access: lldb::eAccessPublic, bitfield_bit_size: 0);
228 ast->AddFieldToRecordType(type: siginfo_type, name: "si_pid", field_type: pid_type,
229 access: lldb::eAccessPublic, bitfield_bit_size: 0);
230 ast->AddFieldToRecordType(type: siginfo_type, name: "si_uid", field_type: uid_type,
231 access: lldb::eAccessPublic, bitfield_bit_size: 0);
232 ast->AddFieldToRecordType(type: siginfo_type, name: "si_status", field_type: int_type,
233 access: lldb::eAccessPublic, bitfield_bit_size: 0);
234 ast->AddFieldToRecordType(type: siginfo_type, name: "si_addr", field_type: voidp_type,
235 access: lldb::eAccessPublic, bitfield_bit_size: 0);
236 ast->AddFieldToRecordType(type: siginfo_type, name: "si_value", field_type: sigval_type,
237 access: lldb::eAccessPublic, bitfield_bit_size: 0);
238
239 // union used to hold the signal data
240 CompilerType union_type = ast->CreateRecordType(
241 decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, name: "",
242 kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeC);
243 ast->StartTagDeclarationDefinition(type: union_type);
244
245 ast->AddFieldToRecordType(
246 type: union_type, name: "_fault",
247 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
248 type_fields: {
249 {"_trapno", int_type},
250 }),
251 access: lldb::eAccessPublic, bitfield_bit_size: 0);
252
253 ast->AddFieldToRecordType(
254 type: union_type, name: "_timer",
255 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
256 type_fields: {
257 {"_timerid", int_type},
258 {"_overrun", int_type},
259 }),
260 access: lldb::eAccessPublic, bitfield_bit_size: 0);
261
262 ast->AddFieldToRecordType(
263 type: union_type, name: "_mesgq",
264 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
265 type_fields: {
266 {"_mqd", int_type},
267 }),
268 access: lldb::eAccessPublic, bitfield_bit_size: 0);
269
270 ast->AddFieldToRecordType(
271 type: union_type, name: "_poll",
272 field_type: ast->CreateStructForIdentifier(type_name: llvm::StringRef(),
273 type_fields: {
274 {"_band", long_type},
275 }),
276 access: lldb::eAccessPublic, bitfield_bit_size: 0);
277
278 ast->CompleteTagDeclarationDefinition(type: union_type);
279 ast->AddFieldToRecordType(type: siginfo_type, name: "_reason", field_type: union_type,
280 access: lldb::eAccessPublic, bitfield_bit_size: 0);
281
282 ast->CompleteTagDeclarationDefinition(type: siginfo_type);
283 return siginfo_type;
284}
285

source code of lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp