LLDB mainline
PlatformFreeBSD.cpp
Go to the documentation of this file.
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
19#include "lldb/Core/Debugger.h"
21#include "lldb/Host/HostInfo.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/State.h"
28#include "lldb/Utility/Status.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
44
45static uint32_t g_initialize_count = 0;
46
47
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
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
99 }
100}
101
111
112/// Default Constructor
114 : PlatformPOSIX(is_host) // This is the local host platform
115{
116 if (is_host) {
117 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
118 m_supported_architectures.push_back(hostArch);
119 if (hostArch.GetTriple().isArch64Bit()) {
121 HostInfo::GetArchitecture(HostInfo::eArchKind32));
122 }
123 } else {
125 {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::aarch64,
126 llvm::Triple::arm, llvm::Triple::ppc64, llvm::Triple::ppc},
127 llvm::Triple::FreeBSD);
128 }
129}
130
131std::vector<ArchSpec>
134 return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
136}
137
140
141#if LLDB_ENABLE_POSIX
142 // Display local kernel information only when we are running in host mode.
143 // Otherwise, we would end up printing non-FreeBSD information (when running
144 // on Mac OS for example).
145 if (IsHost()) {
146 struct utsname un;
147
148 if (uname(&un))
149 return;
150
151 strm.Printf(" Kernel: %s\n", un.sysname);
152 strm.Printf(" Release: %s\n", un.release);
153 strm.Printf(" Version: %s\n", un.version);
154 }
155#endif
156}
157
159 if (IsHost()) {
160 return true;
161 } else {
162 // If we're connected, we can debug.
163 return IsConnected();
164 }
165}
166
170
172 addr_t addr, addr_t length,
173 unsigned prot, unsigned flags,
174 addr_t fd, addr_t offset) {
175 uint64_t flags_platform = 0;
176
177 if (flags & eMmapFlagsPrivate)
178 flags_platform |= MAP_PRIVATE;
179 if (flags & eMmapFlagsAnon)
180 flags_platform |= MAP_ANON;
181
182 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
183 if (arch.GetTriple().getArch() == llvm::Triple::x86)
184 args.push_back(0);
185 return args;
186}
187
189 {
190 std::lock_guard<std::mutex> guard(m_mutex);
191 if (!m_type_system)
192 m_type_system = std::make_shared<TypeSystemClang>("siginfo", triple);
193 }
194 TypeSystemClang *ast = m_type_system.get();
195
196 // generic types
197 CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
199 CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
201
202 // platform-specific types
203 CompilerType &pid_type = int_type;
204 CompilerType &uid_type = uint_type;
205
206 CompilerType sigval_type = ast->CreateRecordType(
207 nullptr, OptionalClangModuleID(), "__lldb_sigval_t",
208 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
209 ast->StartTagDeclarationDefinition(sigval_type);
210 ast->AddFieldToRecordType(sigval_type, "sival_int", int_type, 0);
211 ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type, 0);
212 ast->CompleteTagDeclarationDefinition(sigval_type);
213
214 // siginfo_t
215 CompilerType siginfo_type = ast->CreateRecordType(
216 nullptr, OptionalClangModuleID(), "__lldb_siginfo_t",
217 llvm::to_underlying(clang::TagTypeKind::Struct), lldb::eLanguageTypeC);
218 ast->StartTagDeclarationDefinition(siginfo_type);
219 ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type, 0);
220 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type, 0);
221 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type, 0);
222 ast->AddFieldToRecordType(siginfo_type, "si_pid", pid_type, 0);
223 ast->AddFieldToRecordType(siginfo_type, "si_uid", uid_type, 0);
224 ast->AddFieldToRecordType(siginfo_type, "si_status", int_type, 0);
225 ast->AddFieldToRecordType(siginfo_type, "si_addr", voidp_type, 0);
226 ast->AddFieldToRecordType(siginfo_type, "si_value", sigval_type, 0);
227
228 // union used to hold the signal data
229 CompilerType union_type = ast->CreateRecordType(
230 nullptr, OptionalClangModuleID(), "",
231 llvm::to_underlying(clang::TagTypeKind::Union), lldb::eLanguageTypeC);
232 ast->StartTagDeclarationDefinition(union_type);
233
235 union_type, "_fault",
236 ast->CreateStructForIdentifier(llvm::StringRef(),
237 {
238 {"_trapno", int_type},
239 }),
240 0);
241
242 ast->AddFieldToRecordType(
243 union_type, "_timer",
244 ast->CreateStructForIdentifier(llvm::StringRef(),
245 {
246 {"_timerid", int_type},
247 {"_overrun", int_type},
248 }),
249 0);
250
251 ast->AddFieldToRecordType(
252 union_type, "_mesgq",
253 ast->CreateStructForIdentifier(llvm::StringRef(),
254 {
255 {"_mqd", int_type},
256 }),
257 0);
258
259 ast->AddFieldToRecordType(
260 union_type, "_poll",
261 ast->CreateStructForIdentifier(llvm::StringRef(),
262 {
263 {"_band", long_type},
264 }),
265 0);
266
267 ast->CompleteTagDeclarationDefinition(union_type);
268 ast->AddFieldToRecordType(siginfo_type, "_reason", union_type, 0);
269
270 ast->CompleteTagDeclarationDefinition(siginfo_type);
271 return siginfo_type;
272}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
static uint32_t g_initialize_count
#define MAP_ANON
#define MAP_PRIVATE
#define LLDB_PLUGIN_DEFINE(PluginName)
PlatformPOSIX(bool is_host)
Default Constructor.
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
Generic representation of a type in a programming language.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition ConstString.h:40
std::vector< ConstString > m_trap_handlers
Definition Platform.h:1032
static void Terminate()
Definition Platform.cpp:138
static void SetHostPlatform(const lldb::PlatformSP &platform_sp)
Definition Platform.cpp:145
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition Platform.cpp:247
static void Initialize()
Definition Platform.cpp:136
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.
bool IsHost() const
Definition Platform.h:505
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
A TypeSystem implementation based on Clang.
CompilerType GetBasicType(lldb::BasicType type)
CompilerType CreateStructForIdentifier(llvm::StringRef type_name, const std::initializer_list< std::pair< const char *, CompilerType > > &type_fields, bool packed=false)
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, llvm::StringRef name, int kind, lldb::LanguageType language, std::optional< ClangASTMetadata > metadata=std::nullopt, bool exports_symbols=false)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, uint32_t bitfield_bit_size)
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
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
PlatformFreeBSD(bool is_host)
Default Constructor.
static llvm::StringRef GetPluginNameStatic(bool is_host)
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch)
static llvm::StringRef GetPluginDescriptionStatic(bool is_host)
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.
CompilerType GetSiginfoType(const llvm::Triple &triple) override
std::shared_ptr< TypeSystemClang > m_type_system
void GetStatus(Stream &strm) override
Report the current status for this platform.
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition Platform.h:65
@ eMmapFlagsPrivate
Definition Platform.h:47
@ eBasicTypeUnsignedInt
std::shared_ptr< lldb_private::Platform > PlatformSP
@ eLanguageTypeC
Non-standardized C, such as K&R.
uint64_t addr_t
Definition lldb-types.h:80