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"
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"
30 
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/Support/Host.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 
39 using namespace lldb;
40 using namespace lldb_private;
41 using namespace lldb_private::platform_freebsd;
42 
44 
46 
47 
48 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
49  Log *log = GetLog(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 
80 llvm::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 
86 void 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  PlatformFreeBSD::GetPluginNameStatic(false),
97  PlatformFreeBSD::GetPluginDescriptionStatic(false),
98  PlatformFreeBSD::CreateInstance, nullptr);
99  }
100 }
101 
102 void PlatformFreeBSD::Terminate() {
103  if (g_initialize_count > 0) {
104  if (--g_initialize_count == 0) {
105  PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
106  }
107  }
108 
110 }
111 
112 /// Default Constructor
113 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
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()) {
120  m_supported_architectures.push_back(
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::mips64, llvm::Triple::ppc64,
127  llvm::Triple::ppc},
128  llvm::Triple::FreeBSD);
129  }
130 }
131 
132 std::vector<ArchSpec>
135  return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
137 }
138 
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(&un))
150  return;
151 
152  strm.Printf(" Kernel: %s\n", un.sysname);
153  strm.Printf(" Release: %s\n", un.release);
154  strm.Printf(" Version: %s\n", un.version);
155  }
156 #endif
157 }
158 
160  if (IsHost()) {
161  return true;
162  } else {
163  // If we're connected, we can debug.
164  return IsConnected();
165  }
166 }
167 
169  m_trap_handlers.push_back(ConstString("_sigtramp"));
170 }
171 
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(0);
186  return args;
187 }
188 
189 CompilerType PlatformFreeBSD::GetSiginfoType(const llvm::Triple &triple) {
190  if (!m_type_system_up)
191  m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
192  TypeSystemClang *ast = m_type_system_up.get();
193 
194  // generic types
195  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
197  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
199 
200  // platform-specific types
201  CompilerType &pid_type = int_type;
202  CompilerType &uid_type = uint_type;
203 
204  CompilerType sigval_type = ast->CreateRecordType(
205  nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
206  clang::TTK_Union, lldb::eLanguageTypeC);
207  ast->StartTagDeclarationDefinition(sigval_type);
208  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
210  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
212  ast->CompleteTagDeclarationDefinition(sigval_type);
213 
214  // siginfo_t
215  CompilerType siginfo_type = ast->CreateRecordType(
216  nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
217  clang::TTK_Struct, lldb::eLanguageTypeC);
218  ast->StartTagDeclarationDefinition(siginfo_type);
219  ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
221  ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
223  ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
225  ast->AddFieldToRecordType(siginfo_type, "si_pid", pid_type,
227  ast->AddFieldToRecordType(siginfo_type, "si_uid", uid_type,
229  ast->AddFieldToRecordType(siginfo_type, "si_status", int_type,
231  ast->AddFieldToRecordType(siginfo_type, "si_addr", voidp_type,
233  ast->AddFieldToRecordType(siginfo_type, "si_value", sigval_type,
235 
236  // union used to hold the signal data
237  CompilerType union_type = ast->CreateRecordType(
239  clang::TTK_Union, lldb::eLanguageTypeC);
240  ast->StartTagDeclarationDefinition(union_type);
241 
243  union_type, "_fault",
245  {
246  {"_trapno", int_type},
247  }),
249 
251  union_type, "_timer",
253  {
254  {"_timerid", int_type},
255  {"_overrun", int_type},
256  }),
258 
260  union_type, "_mesgq",
262  {
263  {"_mqd", int_type},
264  }),
266 
268  union_type, "_poll",
270  {
271  {"_band", long_type},
272  }),
274 
275  ast->CompleteTagDeclarationDefinition(union_type);
276  ast->AddFieldToRecordType(siginfo_type, "_reason", union_type,
278 
279  ast->CompleteTagDeclarationDefinition(siginfo_type);
280  return siginfo_type;
281 }
lldb_private::RemoteAwarePlatform::IsConnected
bool IsConnected() const override
Definition: RemoteAwarePlatform.cpp:344
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:58
lldb_private::platform_freebsd::PlatformFreeBSD
Definition: PlatformFreeBSD.h:18
lldb_private::platform_freebsd::PlatformFreeBSD::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformFreeBSD.cpp:139
BreakpointLocation.h
PlatformFreeBSD.h
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:897
MAP_ANON
#define MAP_ANON
Definition: PlatformFreeBSD.cpp:37
lldb_private::platform_freebsd::PlatformFreeBSD::CalculateTrapHandlerSymbolNames
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
Definition: PlatformFreeBSD.cpp:168
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:455
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::platform_freebsd::PlatformFreeBSD::CanDebugProcess
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
Definition: PlatformFreeBSD.cpp:159
Debugger.h
Process.h
Target.h
lldb_private::TypeSystemClang::CreateStructForIdentifier
CompilerType CreateStructForIdentifier(ConstString type_name, const std::initializer_list< std::pair< const char *, CompilerType >> &type_fields, bool packed=false)
Definition: TypeSystemClang.cpp:2269
PlatformPOSIX
Definition: PlatformPOSIX.h:18
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformFreeBSD.cpp:36
Log.h
lldb_private::TypeSystemClang::AddFieldToRecordType
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
Definition: TypeSystemClang.cpp:7274
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb::eBasicTypeInt
@ eBasicTypeInt
Definition: lldb-enumerations.h:763
lldb_private::platform_freebsd::PlatformFreeBSD::m_type_system_up
std::unique_ptr< TypeSystemClang > m_type_system_up
Definition: PlatformFreeBSD.h:63
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::platform_freebsd::PlatformFreeBSD::m_supported_architectures
std::vector< ArchSpec > m_supported_architectures
Definition: PlatformFreeBSD.h:60
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:140
lldb::eBasicTypeVoid
@ eBasicTypeVoid
Definition: lldb-enumerations.h:751
HostInfo.h
lldb::eBasicTypeLong
@ eBasicTypeLong
Definition: lldb-enumerations.h:765
lldb_private::platform_freebsd::PlatformFreeBSD::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: PlatformFreeBSD.cpp:172
lldb::eBasicTypeUnsignedInt
@ eBasicTypeUnsignedInt
Definition: lldb-enumerations.h:764
lldb::eAccessPublic
@ eAccessPublic
Definition: lldb-enumerations.h:509
lldb_private::CompilerType::GetPointerType
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
Definition: CompilerType.cpp:407
lldb_private::OptionalClangModuleID
A Clang module ID.
Definition: TypeSystemClang.h:56
lldb_private::platform_freebsd::PlatformFreeBSD::GetSiginfoType
CompilerType GetSiginfoType(const llvm::Triple &triple) override
Definition: PlatformFreeBSD.cpp:189
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::TypeSystemClang::CreateRecordType
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, ClangASTMetadata *metadata=nullptr, bool exports_symbols=false)
Definition: TypeSystemClang.cpp:1270
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private::TypeSystemClang::StartTagDeclarationDefinition
static bool StartTagDeclarationDefinition(const CompilerType &type)
Definition: TypeSystemClang.cpp:8221
lldb::eLanguageTypeC
@ eLanguageTypeC
Non-standardized C, such as K&R.
Definition: lldb-enumerations.h:439
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:1142
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
lldb_private::TypeSystemClang::GetBasicType
CompilerType GetBasicType(lldb::BasicType type)
Definition: TypeSystemClang.cpp:930
FileSpec.h
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformFreeBSD.cpp:45
lldb_private::TypeSystemClang
A TypeSystem implementation based on Clang.
Definition: TypeSystemClang.h:105
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:285
BreakpointSite.h
lldb_private::platform_freebsd::PlatformFreeBSD::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: PlatformFreeBSD.cpp:133
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::platform_freebsd
Definition: PlatformFreeBSD.h:16
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_private::TypeSystemClang::CompleteTagDeclarationDefinition
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
Definition: TypeSystemClang.cpp:8246
lldb
Definition: SBAddress.h:15
LLDBLog.h