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 <stdio.h>
13 #if LLDB_ENABLE_POSIX
14 #include <sys/utsname.h>
15 #endif
16 
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"
29 
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
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 using namespace lldb_private::platform_freebsd;
38 
39 LLDB_PLUGIN_DEFINE(PlatformFreeBSD)
40 
41 static uint32_t g_initialize_count = 0;
42 
43 
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>");
49 
50   bool create = force;
51   if (!create && arch && arch->IsValid()) {
52     const llvm::Triple &triple = arch->GetTriple();
53     switch (triple.getOS()) {
54     case llvm::Triple::FreeBSD:
55       create = true;
56       break;
57 
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();
63       break;
64 #endif
65     default:
66       break;
67     }
68   }
69   LLDB_LOG(log, "create = {0}", create);
70   if (create) {
71     return PlatformSP(new PlatformFreeBSD(false));
72   }
73   return PlatformSP();
74 }
75 
76 ConstString PlatformFreeBSD::GetPluginNameStatic(bool is_host) {
77   if (is_host) {
78     static ConstString g_host_name(Platform::GetHostPlatformName());
79     return g_host_name;
80   } else {
81     static ConstString g_remote_name("remote-freebsd");
82     return g_remote_name;
83   }
84 }
85 
86 const char *PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
87   if (is_host)
88     return "Local FreeBSD user platform plug-in.";
89   else
90     return "Remote FreeBSD user platform plug-in.";
91 }
92 
93 ConstString PlatformFreeBSD::GetPluginName() {
94   return GetPluginNameStatic(IsHost());
95 }
96 
97 void PlatformFreeBSD::Initialize() {
98   Platform::Initialize();
99 
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);
105 #endif
106     PluginManager::RegisterPlugin(
107         PlatformFreeBSD::GetPluginNameStatic(false),
108         PlatformFreeBSD::GetPluginDescriptionStatic(false),
109         PlatformFreeBSD::CreateInstance, nullptr);
110   }
111 }
112 
113 void PlatformFreeBSD::Terminate() {
114   if (g_initialize_count > 0) {
115     if (--g_initialize_count == 0) {
116       PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
117     }
118   }
119 
120   PlatformPOSIX::Terminate();
121 }
122 
123 /// Default Constructor
124 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
125     : PlatformPOSIX(is_host) // This is the local host platform
126 {}
127 
128 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
129                                                       ArchSpec &arch) {
130   if (IsHost()) {
131     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
132     if (hostArch.GetTriple().isOSFreeBSD()) {
133       if (idx == 0) {
134         arch = hostArch;
135         return arch.IsValid();
136       } else if (idx == 1) {
137         // If the default host architecture is 64-bit, look for a 32-bit
138         // variant
139         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
140           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
141           return arch.IsValid();
142         }
143       }
144     }
145   } else {
146     if (m_remote_platform_sp)
147       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
148 
149     llvm::Triple triple;
150     // Set the OS to FreeBSD
151     triple.setOS(llvm::Triple::FreeBSD);
152     // Set the architecture
153     switch (idx) {
154     case 0:
155       triple.setArchName("x86_64");
156       break;
157     case 1:
158       triple.setArchName("i386");
159       break;
160     case 2:
161       triple.setArchName("aarch64");
162       break;
163     case 3:
164       triple.setArchName("arm");
165       break;
166     case 4:
167       triple.setArchName("mips64");
168       break;
169     case 5:
170       triple.setArchName("mips");
171       break;
172     case 6:
173       triple.setArchName("ppc64");
174       break;
175     case 7:
176       triple.setArchName("ppc");
177       break;
178     default:
179       return false;
180     }
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
186 
187     // Now set the triple into "arch" and return true
188     arch.SetTriple(triple);
189     return true;
190   }
191   return false;
192 }
193 
194 void PlatformFreeBSD::GetStatus(Stream &strm) {
195   Platform::GetStatus(strm);
196 
197 #if LLDB_ENABLE_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).
201   if (IsHost()) {
202     struct utsname un;
203 
204     if (uname(&un))
205       return;
206 
207     strm.Printf("    Kernel: %s\n", un.sysname);
208     strm.Printf("   Release: %s\n", un.release);
209     strm.Printf("   Version: %s\n", un.version);
210   }
211 #endif
212 }
213 
214 size_t
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;
221 
222     if (bp_loc_sp) {
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;
227     }
228 
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
232       return 0;
233     }
234 
235     static const uint8_t g_arm_breakpoint_opcode[] = {0xFE, 0xDE, 0xFF, 0xE7};
236     size_t trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
237     assert(bp_site);
238     if (bp_site->SetTrapOpcode(g_arm_breakpoint_opcode, trap_opcode_size))
239       return trap_opcode_size;
240   }
241     LLVM_FALLTHROUGH;
242   default:
243     return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
244   }
245 }
246 
247 bool PlatformFreeBSD::CanDebugProcess() {
248   if (getenv("FREEBSD_REMOTE_PLUGIN")) {
249     if (IsHost()) {
250       return true;
251     } else {
252       // If we're connected, we can debug.
253       return IsConnected();
254     }
255   }
256   return false;
257 }
258 
259 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
260   m_trap_handlers.push_back(ConstString("_sigtramp"));
261 }
262 
263 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
264                                                  addr_t addr, addr_t length,
265                                                  unsigned prot, unsigned flags,
266                                                  addr_t fd, addr_t offset) {
267   uint64_t flags_platform = 0;
268 
269   if (flags & eMmapFlagsPrivate)
270     flags_platform |= MAP_PRIVATE;
271   if (flags & eMmapFlagsAnon)
272     flags_platform |= MAP_ANON;
273 
274   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
275   if (arch.GetTriple().getArch() == llvm::Triple::x86)
276     args.push_back(0);
277   return args;
278 }
279