1 //===-- PlatformFreeBSD.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "PlatformFreeBSD.h"
10 #include "lldb/Host/Config.h"
13 #ifndef LLDB_DISABLE_POSIX
14 #include <sys/utsname.h>
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/Log.h"
26 #include "lldb/Utility/State.h"
27 #include "lldb/Utility/Status.h"
28 #include "lldb/Utility/StreamString.h"
30 // Define these constants from FreeBSD mman.h for use when targeting remote
31 // FreeBSD systems even when host has different values.
32 #define MAP_PRIVATE 0x0002
33 #define MAP_ANON 0x1000
36 using namespace lldb_private;
37 using namespace lldb_private::platform_freebsd;
39 static uint32_t g_initialize_count = 0;
42 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
43 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
44 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
45 arch ? arch->GetArchitectureName() : "<null>",
46 arch ? arch->GetTriple().getTriple() : "<null>");
49 if (!create && arch && arch->IsValid()) {
50 const llvm::Triple &triple = arch->GetTriple();
51 switch (triple.getOS()) {
52 case llvm::Triple::FreeBSD:
56 #if defined(__FreeBSD__)
57 // Only accept "unknown" for the OS if the host is BSD and it "unknown"
58 // wasn't specified (it was just returned because it was NOT specified)
59 case llvm::Triple::OSType::UnknownOS:
60 create = !arch->TripleOSWasSpecified();
67 LLDB_LOG(log, "create = {0}", create);
69 return PlatformSP(new PlatformFreeBSD(false));
74 ConstString PlatformFreeBSD::GetPluginNameStatic(bool is_host) {
76 static ConstString g_host_name(Platform::GetHostPlatformName());
79 static ConstString g_remote_name("remote-freebsd");
84 const char *PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
86 return "Local FreeBSD user platform plug-in.";
88 return "Remote FreeBSD user platform plug-in.";
91 ConstString PlatformFreeBSD::GetPluginName() {
92 return GetPluginNameStatic(IsHost());
95 void PlatformFreeBSD::Initialize() {
96 Platform::Initialize();
98 if (g_initialize_count++ == 0) {
99 #if defined(__FreeBSD__)
100 PlatformSP default_platform_sp(new PlatformFreeBSD(true));
101 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
102 Platform::SetHostPlatform(default_platform_sp);
104 PluginManager::RegisterPlugin(
105 PlatformFreeBSD::GetPluginNameStatic(false),
106 PlatformFreeBSD::GetPluginDescriptionStatic(false),
107 PlatformFreeBSD::CreateInstance, nullptr);
111 void PlatformFreeBSD::Terminate() {
112 if (g_initialize_count > 0) {
113 if (--g_initialize_count == 0) {
114 PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
118 PlatformPOSIX::Terminate();
121 /// Default Constructor
122 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
123 : PlatformPOSIX(is_host) // This is the local host platform
126 PlatformFreeBSD::~PlatformFreeBSD() = default;
128 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
131 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
132 if (hostArch.GetTriple().isOSFreeBSD()) {
135 return arch.IsValid();
136 } else if (idx == 1) {
137 // If the default host architecture is 64-bit, look for a 32-bit
139 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
140 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
141 return arch.IsValid();
146 if (m_remote_platform_sp)
147 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
150 // Set the OS to FreeBSD
151 triple.setOS(llvm::Triple::FreeBSD);
152 // Set the architecture
155 triple.setArchName("x86_64");
158 triple.setArchName("i386");
161 triple.setArchName("aarch64");
164 triple.setArchName("arm");
167 triple.setArchName("mips64");
170 triple.setArchName("mips");
173 triple.setArchName("ppc64");
176 triple.setArchName("ppc");
181 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
182 // vendor by calling triple.SetVendorName("unknown") so that it is a
183 // "unspecified unknown". This means when someone calls
184 // triple.GetVendorName() it will return an empty string which indicates
185 // that the vendor can be set when two architectures are merged
187 // Now set the triple into "arch" and return true
188 arch.SetTriple(triple);
194 void PlatformFreeBSD::GetStatus(Stream &strm) {
195 Platform::GetStatus(strm);
197 #ifndef LLDB_DISABLE_POSIX
198 // Display local kernel information only when we are running in host mode.
199 // Otherwise, we would end up printing non-FreeBSD information (when running
200 // on Mac OS for example).
207 strm.Printf(" Kernel: %s\n", un.sysname);
208 strm.Printf(" Release: %s\n", un.release);
209 strm.Printf(" Version: %s\n", un.version);
215 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode(Target &target,
216 BreakpointSite *bp_site) {
217 switch (target.GetArchitecture().GetMachine()) {
218 case llvm::Triple::arm: {
219 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
220 AddressClass addr_class = AddressClass::eUnknown;
223 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
224 if (addr_class == AddressClass::eUnknown &&
225 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
226 addr_class = AddressClass::eCodeAlternateISA;
229 if (addr_class == AddressClass::eCodeAlternateISA) {
230 // TODO: Enable when FreeBSD supports thumb breakpoints.
231 // FreeBSD kernel as of 10.x, does not support thumb breakpoints
235 static const uint8_t g_arm_breakpoint_opcode[] = {0xFE, 0xDE, 0xFF, 0xE7};
236 size_t trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
238 if (bp_site->SetTrapOpcode(g_arm_breakpoint_opcode, trap_opcode_size))
239 return trap_opcode_size;
243 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
247 Status PlatformFreeBSD::LaunchProcess(ProcessLaunchInfo &launch_info) {
250 error = Platform::LaunchProcess(launch_info);
252 if (m_remote_platform_sp)
253 error = m_remote_platform_sp->LaunchProcess(launch_info);
255 error.SetErrorString("the platform is not currently connected");
260 lldb::ProcessSP PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info,
261 Debugger &debugger, Target *target,
263 lldb::ProcessSP process_sp;
265 if (target == nullptr) {
266 TargetSP new_target_sp;
267 ArchSpec emptyArchSpec;
269 error = debugger.GetTargetList().CreateTarget(
270 debugger, "", emptyArchSpec, eLoadDependentsNo, m_remote_platform_sp,
272 target = new_target_sp.get();
276 if (target && error.Success()) {
277 debugger.GetTargetList().SetSelectedTarget(target);
278 // The freebsd always currently uses the GDB remote debugger plug-in so
279 // even when debugging locally we are debugging remotely! Just like the
281 process_sp = target->CreateProcess(
282 attach_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
285 error = process_sp->Attach(attach_info);
288 if (m_remote_platform_sp)
290 m_remote_platform_sp->Attach(attach_info, debugger, target, error);
292 error.SetErrorString("the platform is not currently connected");
297 // FreeBSD processes cannot yet be launched by spawning and attaching.
298 bool PlatformFreeBSD::CanDebugProcess() {
302 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
303 m_trap_handlers.push_back(ConstString("_sigtramp"));
306 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
307 addr_t addr, addr_t length,
308 unsigned prot, unsigned flags,
309 addr_t fd, addr_t offset) {
310 uint64_t flags_platform = 0;
312 if (flags & eMmapFlagsPrivate)
313 flags_platform |= MAP_PRIVATE;
314 if (flags & eMmapFlagsAnon)
315 flags_platform |= MAP_ANON;
317 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
318 if (arch.GetTriple().getArch() == llvm::Triple::x86)