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 #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 
42 LLDB_PLUGIN_DEFINE(PlatformFreeBSD)
43 
44 static uint32_t g_initialize_count = 0;
45 
46 
47 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
48   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
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 ConstString PlatformFreeBSD::GetPluginName() {
97   return GetPluginNameStatic(IsHost());
98 }
99 
100 void PlatformFreeBSD::Initialize() {
101   Platform::Initialize();
102 
103   if (g_initialize_count++ == 0) {
104 #if defined(__FreeBSD__)
105     PlatformSP default_platform_sp(new PlatformFreeBSD(true));
106     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
107     Platform::SetHostPlatform(default_platform_sp);
108 #endif
109     PluginManager::RegisterPlugin(
110         PlatformFreeBSD::GetPluginNameStatic(false),
111         PlatformFreeBSD::GetPluginDescriptionStatic(false),
112         PlatformFreeBSD::CreateInstance, nullptr);
113   }
114 }
115 
116 void PlatformFreeBSD::Terminate() {
117   if (g_initialize_count > 0) {
118     if (--g_initialize_count == 0) {
119       PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
120     }
121   }
122 
123   PlatformPOSIX::Terminate();
124 }
125 
126 /// Default Constructor
127 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
128     : PlatformPOSIX(is_host) // This is the local host platform
129 {}
130 
131 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
132                                                       ArchSpec &arch) {
133   if (IsHost()) {
134     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
135     if (hostArch.GetTriple().isOSFreeBSD()) {
136       if (idx == 0) {
137         arch = hostArch;
138         return arch.IsValid();
139       } else if (idx == 1) {
140         // If the default host architecture is 64-bit, look for a 32-bit
141         // variant
142         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
143           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
144           return arch.IsValid();
145         }
146       }
147     }
148   } else {
149     if (m_remote_platform_sp)
150       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
151 
152     llvm::Triple triple;
153     // Set the OS to FreeBSD
154     triple.setOS(llvm::Triple::FreeBSD);
155     // Set the architecture
156     switch (idx) {
157     case 0:
158       triple.setArchName("x86_64");
159       break;
160     case 1:
161       triple.setArchName("i386");
162       break;
163     case 2:
164       triple.setArchName("aarch64");
165       break;
166     case 3:
167       triple.setArchName("arm");
168       break;
169     case 4:
170       triple.setArchName("mips64");
171       break;
172     case 5:
173       triple.setArchName("mips");
174       break;
175     case 6:
176       triple.setArchName("ppc64");
177       break;
178     case 7:
179       triple.setArchName("ppc");
180       break;
181     default:
182       return false;
183     }
184     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
185     // vendor by calling triple.SetVendorName("unknown") so that it is a
186     // "unspecified unknown". This means when someone calls
187     // triple.GetVendorName() it will return an empty string which indicates
188     // that the vendor can be set when two architectures are merged
189 
190     // Now set the triple into "arch" and return true
191     arch.SetTriple(triple);
192     return true;
193   }
194   return false;
195 }
196 
197 void PlatformFreeBSD::GetStatus(Stream &strm) {
198   Platform::GetStatus(strm);
199 
200 #if LLDB_ENABLE_POSIX
201   // Display local kernel information only when we are running in host mode.
202   // Otherwise, we would end up printing non-FreeBSD information (when running
203   // on Mac OS for example).
204   if (IsHost()) {
205     struct utsname un;
206 
207     if (uname(&un))
208       return;
209 
210     strm.Printf("    Kernel: %s\n", un.sysname);
211     strm.Printf("   Release: %s\n", un.release);
212     strm.Printf("   Version: %s\n", un.version);
213   }
214 #endif
215 }
216 
217 bool PlatformFreeBSD::CanDebugProcess() {
218   if (IsHost()) {
219     return true;
220   } else {
221     // If we're connected, we can debug.
222     return IsConnected();
223   }
224 }
225 
226 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
227   m_trap_handlers.push_back(ConstString("_sigtramp"));
228 }
229 
230 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
231                                                  addr_t addr, addr_t length,
232                                                  unsigned prot, unsigned flags,
233                                                  addr_t fd, addr_t offset) {
234   uint64_t flags_platform = 0;
235 
236   if (flags & eMmapFlagsPrivate)
237     flags_platform |= MAP_PRIVATE;
238   if (flags & eMmapFlagsAnon)
239     flags_platform |= MAP_ANON;
240 
241   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
242   if (arch.GetTriple().getArch() == llvm::Triple::x86)
243     args.push_back(0);
244   return args;
245 }
246