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/Log.h"
26 #include "lldb/Utility/State.h"
27 #include "lldb/Utility/Status.h"
29 
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/Support/Host.h"
32 
33 // Define these constants from FreeBSD mman.h for use when targeting remote
34 // FreeBSD systems even when host has different values.
35 #define MAP_PRIVATE 0x0002
36 #define MAP_ANON 0x1000
37 
38 using namespace lldb;
39 using namespace lldb_private;
40 using namespace lldb_private::platform_freebsd;
41 
43 
45 
46 
47 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
49  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
50  arch ? arch->GetArchitectureName() : "<null>",
51  arch ? arch->GetTriple().getTriple() : "<null>");
52 
53  bool create = force;
54  if (!create && arch && arch->IsValid()) {
55  const llvm::Triple &triple = arch->GetTriple();
56  switch (triple.getOS()) {
57  case llvm::Triple::FreeBSD:
58  create = true;
59  break;
60 
61 #if defined(__FreeBSD__)
62  // Only accept "unknown" for the OS if the host is BSD and it "unknown"
63  // wasn't specified (it was just returned because it was NOT specified)
64  case llvm::Triple::OSType::UnknownOS:
65  create = !arch->TripleOSWasSpecified();
66  break;
67 #endif
68  default:
69  break;
70  }
71  }
72  LLDB_LOG(log, "create = {0}", create);
73  if (create) {
74  return PlatformSP(new PlatformFreeBSD(false));
75  }
76  return PlatformSP();
77 }
78 
79 ConstString PlatformFreeBSD::GetPluginNameStatic(bool is_host) {
80  if (is_host) {
81  static ConstString g_host_name(Platform::GetHostPlatformName());
82  return g_host_name;
83  } else {
84  static ConstString g_remote_name("remote-freebsd");
85  return g_remote_name;
86  }
87 }
88 
89 const char *PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
90  if (is_host)
91  return "Local FreeBSD user platform plug-in.";
92  else
93  return "Remote FreeBSD user platform plug-in.";
94 }
95 
96 void PlatformFreeBSD::Initialize() {
97  Platform::Initialize();
98 
99  if (g_initialize_count++ == 0) {
100 #if defined(__FreeBSD__)
101  PlatformSP default_platform_sp(new PlatformFreeBSD(true));
102  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
103  Platform::SetHostPlatform(default_platform_sp);
104 #endif
105  PluginManager::RegisterPlugin(
106  PlatformFreeBSD::GetPluginNameStatic(false),
107  PlatformFreeBSD::GetPluginDescriptionStatic(false),
108  PlatformFreeBSD::CreateInstance, nullptr);
109  }
110 }
111 
112 void PlatformFreeBSD::Terminate() {
113  if (g_initialize_count > 0) {
114  if (--g_initialize_count == 0) {
115  PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
116  }
117  }
118 
120 }
121 
122 /// Default Constructor
123 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
124  : PlatformPOSIX(is_host) // This is the local host platform
125 {}
126 
128  ArchSpec &arch) {
129  if (IsHost()) {
130  ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
131  if (hostArch.GetTriple().isOSFreeBSD()) {
132  if (idx == 0) {
133  arch = hostArch;
134  return arch.IsValid();
135  } else if (idx == 1) {
136  // If the default host architecture is 64-bit, look for a 32-bit
137  // variant
138  if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
139  arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
140  return arch.IsValid();
141  }
142  }
143  }
144  } else {
146  return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
147 
148  llvm::Triple triple;
149  // Set the OS to FreeBSD
150  triple.setOS(llvm::Triple::FreeBSD);
151  // Set the architecture
152  switch (idx) {
153  case 0:
154  triple.setArchName("x86_64");
155  break;
156  case 1:
157  triple.setArchName("i386");
158  break;
159  case 2:
160  triple.setArchName("aarch64");
161  break;
162  case 3:
163  triple.setArchName("arm");
164  break;
165  case 4:
166  triple.setArchName("mips64");
167  break;
168  case 5:
169  triple.setArchName("mips");
170  break;
171  case 6:
172  triple.setArchName("ppc64");
173  break;
174  case 7:
175  triple.setArchName("ppc");
176  break;
177  default:
178  return false;
179  }
180  // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
181  // vendor by calling triple.SetVendorName("unknown") so that it is a
182  // "unspecified unknown". This means when someone calls
183  // triple.GetVendorName() it will return an empty string which indicates
184  // that the vendor can be set when two architectures are merged
185 
186  // Now set the triple into "arch" and return true
187  arch.SetTriple(triple);
188  return true;
189  }
190  return false;
191 }
192 
194  Platform::GetStatus(strm);
195 
196 #if LLDB_ENABLE_POSIX
197  // Display local kernel information only when we are running in host mode.
198  // Otherwise, we would end up printing non-FreeBSD information (when running
199  // on Mac OS for example).
200  if (IsHost()) {
201  struct utsname un;
202 
203  if (uname(&un))
204  return;
205 
206  strm.Printf(" Kernel: %s\n", un.sysname);
207  strm.Printf(" Release: %s\n", un.release);
208  strm.Printf(" Version: %s\n", un.version);
209  }
210 #endif
211 }
212 
214  if (IsHost()) {
215  return true;
216  } else {
217  // If we're connected, we can debug.
218  return IsConnected();
219  }
220 }
221 
223  m_trap_handlers.push_back(ConstString("_sigtramp"));
224 }
225 
227  addr_t addr, addr_t length,
228  unsigned prot, unsigned flags,
229  addr_t fd, addr_t offset) {
230  uint64_t flags_platform = 0;
231 
232  if (flags & eMmapFlagsPrivate)
233  flags_platform |= MAP_PRIVATE;
234  if (flags & eMmapFlagsAnon)
235  flags_platform |= MAP_ANON;
236 
237  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
238  if (arch.GetTriple().getArch() == llvm::Triple::x86)
239  args.push_back(0);
240  return args;
241 }
lldb_private::RemoteAwarePlatform::IsConnected
bool IsConnected() const override
Definition: RemoteAwarePlatform.cpp:380
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:17
lldb_private::platform_freebsd::PlatformFreeBSD::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformFreeBSD.cpp:193
lldb_private::platform_freebsd::PlatformFreeBSD::GetSupportedArchitectureAtIndex
bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformFreeBSD.cpp:127
BreakpointLocation.h
PlatformFreeBSD.h
LIBLLDB_LOG_PLATFORM
#define LIBLLDB_LOG_PLATFORM
Definition: Logging.h:39
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:900
MAP_ANON
#define MAP_ANON
Definition: PlatformFreeBSD.cpp:36
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:222
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
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:213
Debugger.h
Process.h
Target.h
PlatformPOSIX
Definition: PlatformPOSIX.h:18
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformFreeBSD.cpp:35
Log.h
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:739
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:149
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
HostInfo.h
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
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:226
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:242
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
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
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformFreeBSD.cpp:44
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:400
BreakpointSite.h
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:437
lldb_private::platform_freebsd
Definition: PlatformFreeBSD.h:15
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb
Definition: SBAddress.h:15