1 //===-- PlatformFreeBSD.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformFreeBSD.h"
11 #include "lldb/Host/Config.h"
14 #ifndef LLDB_DISABLE_POSIX
15 #include <sys/utsname.h>
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Breakpoint/BreakpointSite.h"
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Host/HostInfo.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/FileSpec.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"
31 // Define these constants from FreeBSD mman.h for use when targeting remote
32 // FreeBSD systems even when host has different values.
33 #define MAP_PRIVATE 0x0002
34 #define MAP_ANON 0x1000
37 using namespace lldb_private;
38 using namespace lldb_private::platform_freebsd;
40 static uint32_t g_initialize_count = 0;
42 //------------------------------------------------------------------
44 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
45 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
46 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
47 arch ? arch->GetArchitectureName() : "<null>",
48 arch ? arch->GetTriple().getTriple() : "<null>");
51 if (!create && arch && arch->IsValid()) {
52 const llvm::Triple &triple = arch->GetTriple();
53 switch (triple.getOS()) {
54 case llvm::Triple::FreeBSD:
58 #if defined(__FreeBSD__)
59 // Only accept "unknown" for the OS if the host is BSD and it "unknown"
60 // wasn't specified (it was just returned because it was NOT specified)
61 case llvm::Triple::OSType::UnknownOS:
62 create = !arch->TripleOSWasSpecified();
69 LLDB_LOG(log, "create = {0}", create);
71 return PlatformSP(new PlatformFreeBSD(false));
76 ConstString PlatformFreeBSD::GetPluginNameStatic(bool is_host) {
78 static ConstString g_host_name(Platform::GetHostPlatformName());
81 static ConstString g_remote_name("remote-freebsd");
86 const char *PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
88 return "Local FreeBSD user platform plug-in.";
90 return "Remote FreeBSD user platform plug-in.";
93 ConstString PlatformFreeBSD::GetPluginName() {
94 return GetPluginNameStatic(IsHost());
97 void PlatformFreeBSD::Initialize() {
98 Platform::Initialize();
100 if (g_initialize_count++ == 0) {
101 #if defined(__FreeBSD__)
102 PlatformSP default_platform_sp(new PlatformFreeBSD(true));
103 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
104 Platform::SetHostPlatform(default_platform_sp);
106 PluginManager::RegisterPlugin(
107 PlatformFreeBSD::GetPluginNameStatic(false),
108 PlatformFreeBSD::GetPluginDescriptionStatic(false),
109 PlatformFreeBSD::CreateInstance, nullptr);
113 void PlatformFreeBSD::Terminate() {
114 if (g_initialize_count > 0) {
115 if (--g_initialize_count == 0) {
116 PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
120 PlatformPOSIX::Terminate();
123 //------------------------------------------------------------------
124 /// Default Constructor
125 //------------------------------------------------------------------
126 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
127 : PlatformPOSIX(is_host) // This is the local host platform
130 PlatformFreeBSD::~PlatformFreeBSD() = default;
132 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
135 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
136 if (hostArch.GetTriple().isOSFreeBSD()) {
139 return arch.IsValid();
140 } else if (idx == 1) {
141 // If the default host architecture is 64-bit, look for a 32-bit
143 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
144 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
145 return arch.IsValid();
150 if (m_remote_platform_sp)
151 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
154 // Set the OS to FreeBSD
155 triple.setOS(llvm::Triple::FreeBSD);
156 // Set the architecture
159 triple.setArchName("x86_64");
162 triple.setArchName("i386");
165 triple.setArchName("aarch64");
168 triple.setArchName("arm");
171 triple.setArchName("mips64");
174 triple.setArchName("mips");
177 triple.setArchName("ppc64");
180 triple.setArchName("ppc");
185 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
186 // vendor by calling triple.SetVendorName("unknown") so that it is a
187 // "unspecified unknown". This means when someone calls
188 // triple.GetVendorName() it will return an empty string which indicates
189 // that the vendor can be set when two architectures are merged
191 // Now set the triple into "arch" and return true
192 arch.SetTriple(triple);
198 void PlatformFreeBSD::GetStatus(Stream &strm) {
199 Platform::GetStatus(strm);
201 #ifndef LLDB_DISABLE_POSIX
202 // Display local kernel information only when we are running in host mode.
203 // Otherwise, we would end up printing non-FreeBSD information (when running
204 // on Mac OS for example).
211 strm.Printf(" Kernel: %s\n", un.sysname);
212 strm.Printf(" Release: %s\n", un.release);
213 strm.Printf(" Version: %s\n", un.version);
219 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode(Target &target,
220 BreakpointSite *bp_site) {
221 switch (target.GetArchitecture().GetMachine()) {
222 case llvm::Triple::arm: {
223 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
224 AddressClass addr_class = AddressClass::eUnknown;
227 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
228 if (addr_class == AddressClass::eUnknown &&
229 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
230 addr_class = AddressClass::eCodeAlternateISA;
233 if (addr_class == AddressClass::eCodeAlternateISA) {
234 // TODO: Enable when FreeBSD supports thumb breakpoints.
235 // FreeBSD kernel as of 10.x, does not support thumb breakpoints
239 static const uint8_t g_arm_breakpoint_opcode[] = {0xFE, 0xDE, 0xFF, 0xE7};
240 size_t trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
242 if (bp_site->SetTrapOpcode(g_arm_breakpoint_opcode, trap_opcode_size))
243 return trap_opcode_size;
247 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
251 Status PlatformFreeBSD::LaunchProcess(ProcessLaunchInfo &launch_info) {
254 error = Platform::LaunchProcess(launch_info);
256 if (m_remote_platform_sp)
257 error = m_remote_platform_sp->LaunchProcess(launch_info);
259 error.SetErrorString("the platform is not currently connected");
264 lldb::ProcessSP PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info,
265 Debugger &debugger, Target *target,
267 lldb::ProcessSP process_sp;
269 if (target == NULL) {
270 TargetSP new_target_sp;
271 ArchSpec emptyArchSpec;
273 error = debugger.GetTargetList().CreateTarget(
274 debugger, "", emptyArchSpec, eLoadDependentsNo, m_remote_platform_sp,
276 target = new_target_sp.get();
280 if (target && error.Success()) {
281 debugger.GetTargetList().SetSelectedTarget(target);
282 // The freebsd always currently uses the GDB remote debugger plug-in so
283 // even when debugging locally we are debugging remotely! Just like the
285 process_sp = target->CreateProcess(
286 attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
289 error = process_sp->Attach(attach_info);
292 if (m_remote_platform_sp)
294 m_remote_platform_sp->Attach(attach_info, debugger, target, error);
296 error.SetErrorString("the platform is not currently connected");
301 // FreeBSD processes cannot yet be launched by spawning and attaching.
302 bool PlatformFreeBSD::CanDebugProcess() {
306 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
307 m_trap_handlers.push_back(ConstString("_sigtramp"));
310 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
311 addr_t addr, addr_t length,
312 unsigned prot, unsigned flags,
313 addr_t fd, addr_t offset) {
314 uint64_t flags_platform = 0;
316 if (flags & eMmapFlagsPrivate)
317 flags_platform |= MAP_PRIVATE;
318 if (flags & eMmapFlagsAnon)
319 flags_platform |= MAP_ANON;
321 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
322 if (arch.GetTriple().getArch() == llvm::Triple::x86)